Skip to content
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
6 changes: 6 additions & 0 deletions app/migrated_routes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@ def put(self, data, id):
db.session.commit()
except IntegrityError as ie:
db.session.rollback()
if (
isinstance(ie.orig, UniqueViolation)
and ie.orig.diag.constraint_name
== CategoryCollection._NAME_UNIQUE_CONSTRAINT.name
):
abort(409, message="Category with this name already exists")
if (
isinstance(ie.orig, UniqueViolation)
and ie.orig.diag.constraint_name
Expand Down
6 changes: 6 additions & 0 deletions app/migrated_routes/subcategory.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@ def put(self, data, id):
db.session.commit()
except IntegrityError as ie:
db.session.rollback()
if (
isinstance(ie.orig, UniqueViolation)
and ie.orig.diag.constraint_name
== SubcategoryCollection._NAME_UNIQUE_CONSTRAINT.name
):
abort(409, message="Subcategory with this name already exists")
if (
isinstance(ie.orig, UniqueViolation)
and ie.orig.diag.constraint_name
Expand Down
36 changes: 30 additions & 6 deletions tests/test_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,14 @@ def test_get_all_categories(self, create_category):
assert "B" in names

def test_update_category(self, create_authenticated_headers, create_category):
headers = create_authenticated_headers()
response = create_category("OldName", headers=headers)
response = create_category("OldName")
data = response.get_json()
cat_id = data["id"]

update_resp = self.client.put(
f"/categories/{cat_id}", json={"name": "NewName"}, headers=headers
f"/categories/{cat_id}",
json={"name": "NewName"},
headers=create_authenticated_headers(),
)

assert update_resp.status_code == 200
Expand All @@ -91,12 +93,34 @@ def test_update_category(self, create_authenticated_headers, create_category):
self._verify_category_in_db("NewName")
self._verify_category_in_db("OldName", should_exist=False)

def test_update_category_duplicate_name(
self, create_authenticated_headers, create_category
):
create_category("OldName")
response = create_category("NewName")
data = response.get_json()
cat_id = data["id"]

with pytest.raises(IntegrityError) as ie:
self.client.put(
f"/categories/{cat_id}",
json={"name": "OldName"},
headers=create_authenticated_headers(),
)

assert isinstance(ie.value.orig, sqlite3.IntegrityError)
assert "UNIQUE constraint failed" in str(ie.value.orig)
self._verify_category_in_db("OldName")
self._verify_category_in_db("NewName")

def test_delete_category(self, create_authenticated_headers, create_category):
headers = create_authenticated_headers()
response = create_category("ToDelete", headers=headers)
response = create_category("ToDelete")
data = response.get_json()
cat_id = data["id"]
delete_resp = self.client.delete(f"/categories/{cat_id}", headers=headers)

delete_resp = self.client.delete(
f"/categories/{cat_id}", headers=create_authenticated_headers()
)

assert delete_resp.status_code == 204
get_resp = self.client.get(f"/categories/{cat_id}")
Expand Down
16 changes: 10 additions & 6 deletions tests/test_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,14 @@ def test_get_all_products(self, create_product):
assert "B" in names

def test_update_product(self, create_authenticated_headers, create_product):
headers = create_authenticated_headers()
response = create_product("OldProduct", "OldDesc", headers=headers)
response = create_product("OldProduct", "OldDesc")
data = response.get_json()
p_id = data["id"]

update_resp = self.client.put(
f"/product/{p_id}/update", json={"name": "NewProduct", "description": "NewDesc"}, headers=headers
f"/product/{p_id}/update",
json={"name": "NewProduct", "description": "NewDesc"},
headers=create_authenticated_headers(),
)

assert update_resp.status_code == 201
Expand All @@ -88,11 +90,13 @@ def test_update_product(self, create_authenticated_headers, create_product):
self._verify_product_in_db("OldProduct", should_exist=False)

def test_delete_product(self, create_authenticated_headers, create_product):
headers = create_authenticated_headers()
response = create_product("ToDelete", "desc", headers=headers)
response = create_product("ToDelete", "desc")
data = response.get_json()
p_id = data["id"]
delete_resp = self.client.delete(f"/product/{p_id}", headers=headers)

delete_resp = self.client.delete(
f"/product/{p_id}", headers=create_authenticated_headers()
)

assert delete_resp.status_code == 200
get_resp = self.client.get(f"/product/{p_id}")
Expand Down
36 changes: 30 additions & 6 deletions tests/test_subcategory.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,14 @@ def test_get_all_subcategories(self, create_subcategory):
assert "B" in names

def test_update_subcategory(self, create_authenticated_headers, create_subcategory):
headers = create_authenticated_headers()
response = create_subcategory("OldSubcat", headers=headers)
response = create_subcategory("OldSubcat")
data = response.get_json()
sc_id = data["id"]

update_resp = self.client.put(
f"/subcategories/{sc_id}", json={"name": "NewSubcat"}, headers=headers
f"/subcategories/{sc_id}",
json={"name": "NewSubcat"},
headers=create_authenticated_headers(),
)

assert update_resp.status_code == 200
Expand All @@ -90,12 +92,34 @@ def test_update_subcategory(self, create_authenticated_headers, create_subcatego
self._verify_subcategory_in_db("NewSubcat")
self._verify_subcategory_in_db("OldSubcat", should_exist=False)

def test_update_subcategory_duplicate_name(
self, create_authenticated_headers, create_subcategory
):
create_subcategory("OldSubcat")
response = create_subcategory("NewSubcat")
data = response.get_json()
sc_id = data["id"]

with pytest.raises(IntegrityError) as ie:
self.client.put(
f"/subcategories/{sc_id}",
json={"name": "OldSubcat"},
headers=create_authenticated_headers(),
)

assert isinstance(ie.value.orig, sqlite3.IntegrityError)
assert "UNIQUE constraint failed" in str(ie.value.orig)
self._verify_subcategory_in_db("OldSubcat")
self._verify_subcategory_in_db("NewSubcat")

def test_delete_subcategory(self, create_authenticated_headers, create_subcategory):
headers = create_authenticated_headers()
response = create_subcategory("ToDelete", headers=headers)
response = create_subcategory("ToDelete")
data = response.get_json()
sc_id = data["id"]
delete_resp = self.client.delete(f"/subcategories/{sc_id}", headers=headers)

delete_resp = self.client.delete(
f"/subcategories/{sc_id}", headers=create_authenticated_headers()
)

assert delete_resp.status_code == 204
get_resp = self.client.get(f"/subcategories/{sc_id}")
Expand Down