def test_list_all_subcategories(db: Session) -> None: subcategory_count = crud.subcategory.count(db) subcategories = crud.subcategory.filter(db) assert len(subcategories) == subcategory_count created = insert_subcategory(db) subcategories = crud.subcategory.filter(db) assert len(subcategories) == subcategory_count + 1 delete_subcategory(db, created)
def test_GET_existing_subcategory(db: Session) -> None: created = insert_subcategory(db) response = client.get(f'/api/v1/subcategory/{created.id}') subcategory_from_api = response.json() assert response.status_code == 200 assert subcategory_from_api['name'] == created.name delete_subcategory(db, created)
def test_delete_subcategory(db: Session) -> None: created = insert_subcategory(db) subcategory_from_db = crud.subcategory.get_by_id(db, created.id) assert subcategory_from_db deleted = crud.subcategory.remove(db, id=created.id) subcategory_from_db = crud.subcategory.get_by_id(db, created.id) assert subcategory_from_db is None assert deleted.id == created.id
def test_update_subcategory(db: Session) -> None: created = insert_subcategory(db) subcategory_from_db = crud.subcategory.get_by_id(db, created.id) subcategory_update = SubcategoryUpdate(name="Changed") updated_subcategory = crud.subcategory.update( db, db_object=subcategory_from_db, object_to_update=subcategory_update) subcategory_from_db = crud.subcategory.get_by_id(db, created.id) assert subcategory_from_db.id == updated_subcategory.id assert subcategory_from_db.name == "Changed" delete_subcategory(db, created)
def test_PUT_existing_subcategory(db: Session) -> None: created = insert_subcategory(db) subcategory_data = {'name': 'Changed'} response = client.put(f'/api/v1/subcategory/{created.id}', json=subcategory_data) subcategory_from_api = response.json() assert response.status_code == 200 assert subcategory_from_api['name'] == 'Changed' delete_subcategory(db, created)
def test_GET_subcategory(db: Session) -> None: subcategory_count = crud.subcategory.count(db) response = client.get('/api/v1/subcategory/') assert response.status_code == 200 assert len(response.json()) == subcategory_count created = insert_subcategory(db) response = client.get('/api/v1/subcategory/') assert response.status_code == 200 assert len(response.json()) == subcategory_count + 1 delete_subcategory(db, created)
def test_POST_existing_subcategory_name(db: Session) -> None: created = insert_subcategory(db) subcategory_data = { 'name': created.name, 'description': random_upper_string(), 'category_id': 1 } response = client.post('/api/v1/subcategory/', json=subcategory_data) created_subcategory = response.json() assert response.status_code == 400 assert "_id" not in created_subcategory delete_subcategory(db, created)
def test_create_subcategory(db: Session) -> None: created = insert_subcategory(db) subcategory_created = crud.subcategory.get_by_id(db, created.id) assert created.id == subcategory_created.id assert created.description == subcategory_created.description delete_subcategory(db, created)