Skip to content

Commit

Permalink
modelnotfound
Browse files Browse the repository at this point in the history
  • Loading branch information
gtfierro committed Jan 15, 2025
1 parent 4fb9c8e commit 0dda8d8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 40 deletions.
32 changes: 19 additions & 13 deletions buildingmotif/database/table_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
from sqlalchemy.engine import Engine
from sqlalchemy.exc import NoResultFound

from buildingmotif.database.errors import LibraryNotFound, TemplateNotFound
from buildingmotif.database.errors import (
LibraryNotFound,
ModelNotFound,
ShapeCollectionNotFound,
TemplateNotFound,
)
from buildingmotif.database.tables import (
DBLibrary,
DBModel,
Expand Down Expand Up @@ -77,7 +82,10 @@ def get_db_model(self, id: int) -> DBModel:
:return: DBModel
:rtype: DBModel
"""
db_model = self.bm.session.query(DBModel).filter(DBModel.id == id).one()
try:
db_model = self.bm.session.query(DBModel).filter(DBModel.id == id).one()
except NoResultFound:
raise ModelNotFound(idnum=id)
return db_model

def get_db_model_by_name(self, name: str) -> DBModel:
Expand Down Expand Up @@ -158,24 +166,22 @@ def get_db_shape_collection(self, id: int) -> DBShapeCollection:
:return: DBShapeCollection
:rtype: DBShapeCollection
"""
return (
self.bm.session.query(DBShapeCollection)
.filter(DBShapeCollection.id == id)
.one()
)
try:
return (
self.bm.session.query(DBShapeCollection)
.filter(DBShapeCollection.id == id)
.one()
)
except NoResultFound:
raise ShapeCollectionNotFound(idnum=id)

def delete_db_shape_collection(self, id: int) -> None:
"""Delete database shape collection.
:param id: id of deleted DBShapeCollection
:type id: int
"""
db_shape_collection = (
self.bm.session.query(DBShapeCollection)
.filter(DBShapeCollection.id == id)
.one()
)

db_shape_collection = self.get_db_shape_collection(id)
self.bm.session.delete(db_shape_collection)

# library functions
Expand Down
20 changes: 12 additions & 8 deletions tests/unit/database/table_connection/test_db_library.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import uuid

import pytest
from sqlalchemy.exc import NoResultFound

from buildingmotif.database.errors import (
LibraryNotFound,
ShapeCollectionNotFound,
TemplateNotFound,
)
from buildingmotif.database.tables import DBLibrary, DBShapeCollection, DBTemplate


Expand Down Expand Up @@ -48,7 +52,7 @@ def test_get_db_library(table_connection):


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


Expand All @@ -65,7 +69,7 @@ def test_get_db_library_by_name(table_connection):


def test_get_db_library_by_name_not_found(table_connection):
with pytest.raises(NoResultFound):
with pytest.raises(LibraryNotFound):
table_connection.get_db_library_by_name("I don't exist")


Expand All @@ -80,7 +84,7 @@ def test_update_db_library_name(table_connection):


def test_update_db_library_name_does_not_exist(table_connection):
with pytest.raises(NoResultFound):
with pytest.raises(LibraryNotFound):
table_connection.update_db_library_name("I don't exist", "new_name")


Expand All @@ -93,16 +97,16 @@ def test_delete_db_library(table_connection):

table_connection.delete_db_library(db_library.id)

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

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

with pytest.raises(NoResultFound):
with pytest.raises(ShapeCollectionNotFound):
table_connection.get_db_shape_collection(db_shape_collection.id)


def tests_delete_db_library_does_does_exist(table_connection):
with pytest.raises(NoResultFound):
with pytest.raises(LibraryNotFound):
table_connection.delete_db_library("does not exist")
16 changes: 8 additions & 8 deletions tests/unit/database/table_connection/test_db_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from unittest import mock

import pytest
from sqlalchemy.exc import NoResultFound

from buildingmotif.database.errors import ModelNotFound, ShapeCollectionNotFound
from buildingmotif.database.tables import DBModel, DBShapeCollection


Expand Down Expand Up @@ -58,7 +58,7 @@ def test_get_db_model(mock_uuid4, table_connection):


def test_get_db_model_does_not_exist(table_connection):
with pytest.raises(NoResultFound):
with pytest.raises(ModelNotFound):
table_connection.get_db_model("I don't exist")


Expand All @@ -73,7 +73,7 @@ def test_update_db_model_name(table_connection):


def test_update_db_model_name_does_not_exist(table_connection):
with pytest.raises(NoResultFound):
with pytest.raises(ModelNotFound):
table_connection.update_db_model_name("I don't exist", "new_name")


Expand All @@ -90,21 +90,21 @@ def test_update_db_model_description(table_connection):


def test_update_db_model_description_does_not_exist(table_connection):
with pytest.raises(NoResultFound):
with pytest.raises(ModelNotFound):
table_connection.update_db_model_description("I don't exist", "new_description")


def test_delete_db_model(table_connection):
db_model = table_connection.create_db_model(name="my_db_model")
table_connection.delete_db_model(db_model.id)

with pytest.raises(NoResultFound):
with pytest.raises(ModelNotFound):
table_connection.get_db_model(db_model.id)

with pytest.raises(NoResultFound):
with pytest.raises(ShapeCollectionNotFound):
table_connection.get_db_shape_collection(db_model.manifest.id)


def tests_delete_db_model_does_does_exist(table_connection):
with pytest.raises(NoResultFound):
def tests_delete_db_model_does_not_exist(table_connection):
with pytest.raises(ModelNotFound):
table_connection.delete_db_model("does not exist")
10 changes: 5 additions & 5 deletions tests/unit/database/table_connection/test_db_shape.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import uuid

import pytest
from sqlalchemy.exc import NoResultFound

from buildingmotif.database.errors import ShapeCollectionNotFound
from buildingmotif.database.tables import DBShapeCollection


Expand Down Expand Up @@ -47,18 +47,18 @@ def mockreturn():


def test_get_db_shape_collection_does_not_exist(table_connection):
with pytest.raises(NoResultFound):
with pytest.raises(ShapeCollectionNotFound):
table_connection.get_db_shape_collection("I don't exist")


def test_delete_db_shape_collection(table_connection):
db_shape_collection = table_connection.create_db_shape_collection()
table_connection.delete_db_shape_collection(db_shape_collection.id)

with pytest.raises(NoResultFound):
with pytest.raises(ShapeCollectionNotFound):
table_connection.get_db_shape_collection(db_shape_collection.id)


def tests_delete_db_shape_collection_does_does_exist(table_connection):
with pytest.raises(NoResultFound):
def tests_delete_db_shape_collection_does_not_exist(table_connection):
with pytest.raises(ShapeCollectionNotFound):
table_connection.delete_db_shape_collection("does not exist")
13 changes: 7 additions & 6 deletions tests/unit/database/table_connection/test_db_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from sqlalchemy.exc import IntegrityError, NoResultFound

from buildingmotif import BuildingMOTIF
from buildingmotif.database.errors import LibraryNotFound, TemplateNotFound
from buildingmotif.database.tables import DBTemplate


Expand Down Expand Up @@ -77,7 +78,7 @@ def mockreturn():

monkeypatch.setattr(uuid, "uuid4", mockreturn)

with pytest.raises(NoResultFound):
with pytest.raises(LibraryNotFound):
bm.table_connection.create_db_template(
name="my_db_template",
library_id=-999, # id does not exist
Expand Down Expand Up @@ -139,7 +140,7 @@ def mockreturn():


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


Expand Down Expand Up @@ -176,7 +177,7 @@ def test_update_db_template_name_bad_name(bm: BuildingMOTIF):


def test_update_db_template_name_does_not_exist(bm: BuildingMOTIF):
with pytest.raises(NoResultFound):
with pytest.raises(TemplateNotFound):
bm.table_connection.update_db_template_name(-999, "new_name")


Expand All @@ -188,12 +189,12 @@ def test_delete_db_template(bm: BuildingMOTIF):

bm.table_connection.delete_db_template(db_template.id)

with pytest.raises(NoResultFound):
bm.table_connection.get_db_model(db_template.id)
with pytest.raises(TemplateNotFound):
bm.table_connection.get_db_template(db_template.id)


def tests_delete_db_template_does_does_exist(bm: BuildingMOTIF):
with pytest.raises(NoResultFound):
with pytest.raises(TemplateNotFound):
bm.table_connection.delete_db_template(-999)


Expand Down

0 comments on commit 0dda8d8

Please sign in to comment.