Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Table Connection #291

Merged
merged 3 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions buildingmotif/api/views/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ def get_library(library_id: int) -> flask.Response:
:rtype: flask.Response
"""
try:
db_lib = current_app.building_motif.table_connection.get_db_library_by_id(
library_id
)
db_lib = current_app.building_motif.table_connection.get_db_library(library_id)
except NoResultFound:
return {
"message": f"No library with id {library_id}"
Expand Down
2 changes: 1 addition & 1 deletion buildingmotif/api/views/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def get_template(templates_id: int) -> flask.Response:
include_parameters = request.args.get("parameters", False)

try:
template = current_app.building_motif.table_connection.get_db_template_by_id(
template = current_app.building_motif.table_connection.get_db_template(
templates_id
)
except NoResultFound:
Expand Down
26 changes: 13 additions & 13 deletions buildingmotif/database/table_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def get_all_db_libraries(self) -> List[DBLibrary]:
db_libraries = self.bm.session.query(DBLibrary).all()
return db_libraries

def get_db_library_by_id(self, id: int) -> DBLibrary:
def get_db_library(self, id: int) -> DBLibrary:
"""Get database library by id.

:param id: id of DBLibrary
Expand Down Expand Up @@ -237,7 +237,7 @@ def update_db_library_name(self, id: int, name: str) -> None:
:param name: new name
:type name: str
"""
db_library = self.get_db_library_by_id(id)
db_library = self.get_db_library(id)
self.logger.debug(
f"Updating database library name: '{db_library.name}' -> '{name}'"
)
Expand All @@ -250,7 +250,7 @@ def delete_db_library(self, id: int) -> None:
:type id: int
"""

db_library = self.get_db_library_by_id(id)
db_library = self.get_db_library(id)

self.logger.debug(f"Deleting database library: '{db_library.name}'")
self.bm.session.delete(db_library)
Expand All @@ -268,7 +268,7 @@ def create_db_template(self, name: str, library_id: int) -> DBTemplate:
:rtype: DBTemplate
"""
self.logger.debug(f"Creating database template: '{name}'")
library = self.get_db_library_by_id(library_id)
library = self.get_db_library(library_id)
template = DBTemplate(
name=name,
body_id=str(uuid.uuid4()),
Expand All @@ -290,7 +290,7 @@ def get_all_db_templates(self) -> List[DBTemplate]:
db_templates = self.bm.session.query(DBTemplate).all()
return db_templates

def get_db_template_by_id(self, id: int) -> DBTemplate:
def get_db_template(self, id: int) -> DBTemplate:
"""Get database template by id.

:param id: id of DBTemplate
Expand All @@ -316,7 +316,7 @@ def get_db_template_by_name(self, name: str) -> DBTemplate:
self.bm.session.query(DBTemplate).filter(DBTemplate.name == name).one()
)
except NoResultFound:
raise NoResultFound(f"No tempalte found with name {name}")
raise NoResultFound(f"No template found with name {name}")
return db_template

def get_library_defining_db_template(self, id: int) -> DBLibrary:
Expand All @@ -327,7 +327,7 @@ def get_library_defining_db_template(self, id: int) -> DBLibrary:
:return: DBLibrary
:rtype: DBLibrary
"""
return self.get_db_template_by_id(id).library
return self.get_db_template(id).library

def get_db_template_dependencies(self, id: int) -> Tuple[DepsAssociation, ...]:
"""Get a template's dependencies and its arguments.
Expand Down Expand Up @@ -356,7 +356,7 @@ def update_db_template_name(self, id: int, name: str) -> None:
:param name: new name
:type name: str
"""
db_template = self.get_db_template_by_id(id)
db_template = self.get_db_template(id)
self.logger.debug(
f"Updating database template name: '{db_template.name}' -> '{name}'"
)
Expand Down Expand Up @@ -405,7 +405,7 @@ def add_template_dependency_preliminary(
self.logger.debug(
f"Creating depencency from templates with ids: '{template_id}' and: '{dependency_id}'"
)
templ = self.get_db_template_by_id(template_id)
templ = self.get_db_template(template_id)
if "name" not in args.keys():
raise ValueError(
f"The name parameter is required for the dependency '{templ.name}'."
Expand Down Expand Up @@ -490,8 +490,8 @@ def check_template_dependency_relationship(self, dep: DepsAssociation):
f"'name' was bound to {args['name']} but available params are {params}"
)

def remove_template_dependency(self, template_id: int, dependency_id: int):
"""Remove dependency between two templates.
def delete_template_dependency(self, template_id: int, dependency_id: int):
"""Delete dependency between two templates.

:param template_id: dependant template id
:type template_id: int
Expand Down Expand Up @@ -520,7 +520,7 @@ def update_db_template_library(self, id: int, library_id: int) -> None:
:param library_id: id of the new library
:type library_id: int
"""
db_template = self.get_db_template_by_id(id)
db_template = self.get_db_template(id)
self.logger.debug(
f"Updating database template library: '{db_template.library_id}' -> '{library_id}'" # noqa
)
Expand All @@ -532,7 +532,7 @@ def delete_db_template(self, id: int) -> None:
:param id: id of deleted DBTemplate
:type id: int
"""
db_template = self.get_db_template_by_id(id)
db_template = self.get_db_template(id)
self.logger.debug(f"Deleting template: '{db_template.name}'")

self.bm.session.delete(db_template)
6 changes: 3 additions & 3 deletions buildingmotif/dataclasses/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def _load_from_db(cls, id: int) -> "Library":
:rtype: Library
"""
bm = get_building_motif()
db_library = bm.table_connection.get_db_library_by_id(id)
db_library = bm.table_connection.get_db_library(id)

return cls(_id=db_library.id, _name=db_library.name, _bm=bm)

Expand Down Expand Up @@ -497,7 +497,7 @@ def get_templates(self) -> List[Template]:
:return: list of templates
:rtype: List[Template]
"""
db_library = self._bm.table_connection.get_db_library_by_id(self._id)
db_library = self._bm.table_connection.get_db_library(self._id)
templates: List[DBTemplate] = db_library.templates
return [Template.load(t.id) for t in templates]

Expand All @@ -509,7 +509,7 @@ def get_shape_collection(self) -> ShapeCollection:
"""
# TODO: we should save the libraries shape_collection to a class attr on load/create. That
# way we wont need an additional db query each time we call this function.
db_library = self._bm.table_connection.get_db_library_by_id(self._id)
db_library = self._bm.table_connection.get_db_library(self._id)

return ShapeCollection.load(db_library.shape_collection.id)

Expand Down
4 changes: 2 additions & 2 deletions buildingmotif/dataclasses/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def load(cls, id: int) -> "Template":
:rtype: Template
"""
bm = get_building_motif()
db_template = bm.table_connection.get_db_template_by_id(id)
db_template = bm.table_connection.get_db_template(id)
body = bm.graph_connection.get_graph(db_template.body_id)

return cls(
Expand Down Expand Up @@ -137,7 +137,7 @@ def remove_dependency(self, dependency: "Template") -> None:
:param dependency: dependency to remove
:type dependency: Template
"""
self._bm.table_connection.remove_template_dependency(self.id, dependency.id)
self._bm.table_connection.delete_template_dependency(self.id, dependency.id)

@property
def all_parameters(self) -> Set[str]:
Expand Down
2 changes: 1 addition & 1 deletion notebooks/Session-example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
"\"\"\"\n",
"print(t.body)\n",
"t.body.add((URIRef(\"http://example.org/alex\"), RDF.type, FOAF.Person))\n",
"body_id = building_motif.table_connection.get_db_template_by_id(t.id).body_id\n",
"body_id = building_motif.table_connection.get_db_template(t.id).body_id\n",
"assert isomorphic(building_motif.graph_connection.get_graph(body_id), t.body)\n",
"\n",
"cur.execute(\"SELECT * FROM kb_625d302a74_type_statements\")\n",
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/api/test_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def test_get_library(client, building_motif):
# Assert
assert results.status_code == 200

db_library = building_motif.table_connection.get_db_library_by_id(lib.id)
db_library = building_motif.table_connection.get_db_library(lib.id)
assert results.json == {
"id": db_library.id,
"name": db_library.name,
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/api/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_get_template(client, building_motif):
# Assert
assert results.status_code == 200

db_template = building_motif.table_connection.get_db_template_by_id(template.id)
db_template = building_motif.table_connection.get_db_template(template.id)
assert results.json == {
"id": db_template.id,
"name": db_template.name,
Expand Down
14 changes: 6 additions & 8 deletions tests/unit/database/table_connection/test_db_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_get_db_library(table_connection):
db_library = table_connection.create_db_library(name="my_library")
table_connection.create_db_template("my_db_template", library_id=db_library.id)

db_library = table_connection.get_db_library_by_id(id=db_library.id)
db_library = table_connection.get_db_library(id=db_library.id)
assert db_library.name == "my_library"
assert len(db_library.templates) == 1
assert type(db_library.templates[0]) == DBTemplate
Expand All @@ -49,7 +49,7 @@ def test_get_db_library(table_connection):

def test_get_db_library_does_not_exist(table_connection):
with pytest.raises(NoResultFound):
table_connection.get_db_library_by_id("I don't exist")
table_connection.get_db_library("I don't exist")


def test_get_db_library_by_name(table_connection):
Expand All @@ -72,13 +72,11 @@ def test_get_db_library_by_name_not_found(table_connection):
def test_update_db_library_name(table_connection):
db_library_id = table_connection.create_db_library(name="my_db_library").id

assert table_connection.get_db_library_by_id(db_library_id).name == "my_db_library"
assert table_connection.get_db_library(db_library_id).name == "my_db_library"

table_connection.update_db_library_name(db_library_id, "your_db_library")

assert (
table_connection.get_db_library_by_id(db_library_id).name == "your_db_library"
)
assert table_connection.get_db_library(db_library_id).name == "your_db_library"


def test_update_db_library_name_does_not_exist(table_connection):
Expand All @@ -96,10 +94,10 @@ def test_delete_db_library(table_connection):
table_connection.delete_db_library(db_library.id)

with pytest.raises(NoResultFound):
table_connection.get_db_library_by_id(db_library.id)
table_connection.get_db_library(db_library.id)

with pytest.raises(NoResultFound):
table_connection.get_db_template_by_id(db_template.id)
table_connection.get_db_template(db_template.id)

with pytest.raises(NoResultFound):
table_connection.get_db_shape_collection(db_shape_collection.id)
Expand Down
20 changes: 8 additions & 12 deletions tests/unit/database/table_connection/test_db_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def mockreturn():
library_id=db_library.id,
)

db_template = bm.table_connection.get_db_template_by_id(id=db_template.id)
db_template = bm.table_connection.get_db_template(id=db_template.id)

assert db_template.name == "my_db_template"
assert db_template.body_id == str(mocked_uuid)
Expand All @@ -140,7 +140,7 @@ def mockreturn():

def test_get_db_template_does_not_exist(bm: BuildingMOTIF):
with pytest.raises(NoResultFound):
bm.table_connection.get_db_template_by_id(-999)
bm.table_connection.get_db_template(-999)


def test_update_db_template_name(bm: BuildingMOTIF):
Expand All @@ -149,16 +149,12 @@ def test_update_db_template_name(bm: BuildingMOTIF):
name="my_db_template", library_id=db_library.id
)

assert (
bm.table_connection.get_db_template_by_id(db_template.id).name
== "my_db_template"
)
assert bm.table_connection.get_db_template(db_template.id).name == "my_db_template"

bm.table_connection.update_db_template_name(db_template.id, "your_db_template")

assert (
bm.table_connection.get_db_template_by_id(db_template.id).name
== "your_db_template"
bm.table_connection.get_db_template(db_template.id).name == "your_db_template"
)


Expand Down Expand Up @@ -296,7 +292,7 @@ def test_remove_dependencies(bm: BuildingMOTIF):
assert dep_assoc.dependee_id == dependee_template.id
assert dep_assoc.args == {"name": "ding", "h2": "dong"}

bm.table_connection.remove_template_dependency(
bm.table_connection.delete_template_dependency(
dependant_template.id, dependee_template.id
)

Expand All @@ -311,7 +307,7 @@ def test_remove_dependencies_does_not_exist(bm: BuildingMOTIF):
) = create_dependency_test_fixtures(bm)

with pytest.raises(NoResultFound):
bm.table_connection.remove_template_dependency(
bm.table_connection.delete_template_dependency(
dependant_template.id, dependee_template.id
)

Expand All @@ -325,11 +321,11 @@ def test_update_optional_args(bm: BuildingMOTIF):
library_id=db_library.id,
)

assert bm.table_connection.get_db_template_by_id(db_template.id).optional_args == []
assert bm.table_connection.get_db_template(db_template.id).optional_args == []

bm.table_connection.update_db_template_optional_args(db_template.id, ["a", "b"])

assert bm.table_connection.get_db_template_by_id(db_template.id).optional_args == [
assert bm.table_connection.get_db_template(db_template.id).optional_args == [
"a",
"b",
]