Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update doc strings, refine prop key / nullable relationship
Browse files Browse the repository at this point in the history
a-s-g93 committed Dec 6, 2024
1 parent 235e70b commit 6fcbc2a
Showing 6 changed files with 50 additions and 4 deletions.
2 changes: 1 addition & 1 deletion examples/end_to_end/multi_file/countries/countries.ipynb
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {},
"outputs": [
{
4 changes: 3 additions & 1 deletion neo4j_runway/utils/data/data_dictionary/column.py
Original file line number Diff line number Diff line change
@@ -60,9 +60,11 @@ def validate_aliases(

@model_validator(mode="after")
def validate_model(self) -> "Column":
if self.primary_key or self.foreign_key:
if self.primary_key:
self.nullable = False
self.key = True
elif self.foreign_key:
self.key = True
if self.primary_key and self.foreign_key:
raise ValueError("`primary_key` and `foreign_key` may not both be True.")
return self
11 changes: 10 additions & 1 deletion neo4j_runway/utils/data/data_dictionary/data_dictionary.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Dict, List, Optional
from typing import Any, Dict, List

from pydantic import BaseModel

@@ -7,6 +7,15 @@


class DataDictionary(BaseModel):
"""
The data dictionary describing all tables in the data.
Attributes
----------
table_schemas : List[TableSchema]
A list of TableSchema objects.
"""

table_schemas: List[TableSchema]

@property
17 changes: 17 additions & 0 deletions neo4j_runway/utils/data/data_dictionary/table_schema.py
Original file line number Diff line number Diff line change
@@ -99,6 +99,23 @@ def compact_dict(self) -> Dict[str, Any]:
[compact_info.update(c.compact_dict) for c in self.columns]
return {self.name: compact_info}

def get_column(self, column_name: str) -> Optional[Column]:
"""
Retrieve a `Column` object by name.
Parameters
----------
column_name : str
The name of the column.
Returns
-------
Optional[Column]
A `Column` object if it exists, or None
"""

return self.columns_dict.get(column_name)

def get_description(self, column_name: str) -> str:
"""
Retrieve the description for a column. If there is no description, then return an empty string.
2 changes: 1 addition & 1 deletion tests/unit/utils/data/data_dictionary/test_column.py
Original file line number Diff line number Diff line change
@@ -33,7 +33,7 @@ def test_init_nullable_and_primary_key() -> None:
def test_init_nullable_and_foreign_key() -> None:
c = Column(name="col_a", foreign_key=True, nullable=True)

assert not c.nullable
assert c.nullable
assert c.foreign_key


18 changes: 18 additions & 0 deletions tests/unit/utils/data/data_dictionary/test_table_schema.py
Original file line number Diff line number Diff line change
@@ -97,3 +97,21 @@ def test_compact_dict() -> None:
assert len(ts.compact_dict.get(ts.name)) == 2
assert ts.name in ts.compact_dict.keys()
assert len(ts.compact_dict.keys()) == 1


def test_get_column_exists() -> None:
ts = TableSchema(
name="table_a",
columns=[Column(name="col_a", primary_key=True), Column(name="col_b")],
)

assert ts.get_column("col_a") is not None


def test_get_column_not_exists() -> None:
ts = TableSchema(
name="table_a",
columns=[Column(name="col_a", primary_key=True), Column(name="col_b")],
)

assert ts.get_column("error") is None

0 comments on commit 6fcbc2a

Please sign in to comment.