def test_update_category(db: Session) -> None: name = random_string(length=randint(3, 30)) itemtype = mock.MagicMock() category = _create_category(db, name=name, itemtype_id=itemtype.id) name2 = random_string(length=randint(3, 30)) category_update = CategoryUpdate(name=name2) category2 = crud.category.update(db=db, db_obj=category, obj_in=category_update) _compare_entries(category, category2)
def test_update_itemtype(db: Session) -> None: name = random_string(length=randint(3, 30)) itemtype = _create_itemtype(db, name=name) name2 = random_string(length=randint(3, 30)) itemtype_update = ItemTypeUpdate(name=name2) itemtype2 = crud.itemtype.update(db=db, db_obj=itemtype, obj_in=itemtype_update) _compare_entries(itemtype, itemtype2)
def test_update_payment(db: Session) -> None: name = random_string(length=randint(3, 30)) payment = _create_payment(db, name=name) name2 = random_string(length=randint(3, 30)) payment_update = PaymentUpdate(name=name2) payment2 = crud.payment.update(db=db, db_obj=payment, obj_in=payment_update) _compare_entries(payment, payment2)
def test_update_user(db: Session) -> None: password = random_string() email = random_email() user_in = UserCreate(email=email, password=password, is_superuser=True) user = crud.user.create(db, obj_in=user_in) new_password = random_string() user_in_update = UserUpdate(password=new_password, is_superuser=True) crud.user.update(db, db_obj=user, obj_in=user_in_update) user_2 = crud.user.get(db, id=user.id) assert user_2 assert user.email == user_2.email assert verify_password(new_password, user_2.hashed_password)
def test_create_note(db: Session) -> None: db.query(Note).delete() title = random_string(100) description = random_string(255) tags = random_string(100) note_in = NoteCreate(title=title, description=description, tags=tags) note = crud_note.create(db=db, obj_in=note_in) assert note.title == title assert note.description == description assert note.tags == tags
def test_check_if_user_is_active_inactive(db: Session) -> None: email = random_email() password = random_string() user_in = UserCreate(email=email, password=password, disabled=True) user = crud.user.create(db, obj_in=user_in) is_active = crud.user.is_active(user) assert is_active
def test_check_if_user_is_superuser_normal_user(db: Session) -> None: username = random_email() password = random_string() user_in = UserCreate(email=username, password=password) user = crud.user.create(db, obj_in=user_in) is_superuser = crud.user.is_superuser(user) assert is_superuser is False
def test_get_itemtype(db: Session) -> None: name = random_string(length=randint(3, 30)) itemtype = _create_itemtype(db, name=name) stored_itemtype = crud.itemtype.get(db=db, id=itemtype.id) assert stored_itemtype _compare_entries(itemtype, stored_itemtype)
def test_get_payment(db: Session) -> None: name = random_string(length=randint(3, 30)) payment = _create_payment(db, name=name) stored_payment = crud.payment.get(db=db, id=payment.id) assert stored_payment _compare_entries(payment, stored_payment)
def test_create_user(db: Session) -> None: email = random_email() password = random_string() user_in = UserCreate(email=email, password=password) user = crud.user.create(db, obj_in=user_in) assert user.email == email assert hasattr(user, "hashed_password")
def test_check_if_user_is_superuser(db: Session) -> None: email = random_email() password = random_string() user_in = UserCreate(email=email, password=password, is_superuser=True) user = crud.user.create(db, obj_in=user_in) is_superuser = crud.user.is_superuser(user) assert is_superuser is True
def test_remove_itemtype(db: Session, test_itemtype, test_payment) -> None: name = random_string(length=randint(3, 30)) itemtype = _create_itemtype(db, name=name) itemtype2 = crud.itemtype.remove(db=db, id=itemtype.id) itemtype3 = crud.itemtype.get(db=db, id=itemtype.id) assert itemtype3 is None _compare_entries(itemtype, itemtype2)
def test_read_notes(client: TestClient, db: Session) -> None: db.query(Note).delete() for _ in range(10): title = random_string(100) description = random_string(255) tags = random_string(100) note_in = NoteCreate(title=title, description=description, tags=tags) crud_note.create(db=db, obj_in=note_in) response = client.get(f"{get_settings().API_HOST}/notes/", ) content = response.json() assert response.status_code == 200 assert len(content) == 10
def test_remove_payment(db: Session) -> None: name = random_string(length=randint(3, 30)) payment = _create_payment(db, name=name) payment2 = crud.payment.remove(db=db, id=payment.id) payment3 = crud.payment.get(db=db, id=payment.id) assert payment3 is None _compare_entries(payment, payment2)
def test_get_category(db: Session) -> None: name = random_string(length=randint(3, 30)) itemtype = mock.MagicMock() category = _create_category(db, name=name, itemtype_id=itemtype.id) stored_category = crud.category.get(db=db, id=category.id) assert stored_category _compare_entries(category, stored_category)
def test_read_note(client: TestClient, db: Session) -> None: title = random_string(100) description = random_string(255) tags = random_string(100) note_in = NoteCreate(title=title, description=description, tags=tags) note = crud_note.create(db=db, obj_in=note_in) response = client.get(f"{get_settings().API_HOST}/notes/{note.id}", ) content = response.json() assert response.status_code == 200 assert content["id"] == note.id assert content["title"] == note.title assert content["description"] == note.description assert content["tags"] == note.tags
def test_remove_category(db: Session, test_category, test_payment) -> None: name = random_string(length=randint(3, 30)) itemtype = mock.MagicMock() category = _create_category(db, name=name, itemtype_id=itemtype.id) category2 = crud.category.remove(db=db, id=category.id) category3 = crud.category.get(db=db, id=category.id) assert category3 is None _compare_entries(category, category2)
def test_get_user(db: Session) -> None: password = random_string() username = random_email() user_in = UserCreate(email=username, password=password, is_superuser=True) user = crud.user.create(db, obj_in=user_in) user_2 = crud.user.get(db, id=user.id) assert user_2 assert user.email == user_2.email assert jsonable_encoder(user) == jsonable_encoder(user_2)
def test_create_item_description_with_invalid_contraints( db: Session, test_category, test_payment, invalid_length): description = random_string(length=invalid_length) with pytest.raises(ValidationError): ItemCreate(description=description, amount=4.22, date="01.01.2020", category_id=test_category.id, payment_id=test_payment.id)
def test_create_note(client: TestClient, db: Session) -> None: data = { "title": random_string(100), "description": random_string(255), "tags": random_string(100) } response = client.post( f"{get_settings().API_HOST}/notes/", json=data, ) content = response.json() assert response.status_code == 200 assert content["title"] == data["title"] assert content["description"] == data["description"] assert content["tags"] == data["tags"] assert "id" in content
def test_create_category_with_too_long_name_returns_422( client: TestClient, superuser_token_headers: dict, db: Session) -> None: data = {"title_en": random_string(length=31)} response = client.post(BASE_URL, headers=superuser_token_headers, json=data) assert response.status_code == 422 content = response.json() assert "at most 30 characters" in content["detail"][0]["msg"]
def test_authenticate_user(db: Session) -> None: email = random_email() password = random_string() user_in = UserCreate(email=email, password=password) user = crud.user.create(db, obj_in=user_in) authenticated_user = crud.user.authenticate(db, email=email, password=password) assert authenticated_user assert user.email == authenticated_user.email
def test_update_item(db: Session, test_itemtype, test_category, test_payment) -> None: user = create_random_user(db) item = _create_item(db, test_itemtype.id, test_category.id, test_payment.id, user.id) description2 = random_string() item_update = ItemUpdate(description=description2) item2 = crud.item.update(db=db, db_obj=item, obj_in=item_update) _compare_items(item, item2)
def test_retrieve_users(client: TestClient, superuser_token_headers: dict, db: Session) -> None: username = random_email() password = random_string() user_in = UserCreate(email=username, password=password) crud.user.create(db, obj_in=user_in) username2 = random_email() password2 = random_string() user_in2 = UserCreate(email=username2, password=password2) crud.user.create(db, obj_in=user_in2) r = client.get(f"{settings.API_V1_STR}/users/", headers=superuser_token_headers) all_users = r.json() assert len(all_users) > 1 for item in all_users: assert "email" in item
def test_update_category(client: TestClient, test_itemtype, superuser_token_headers: dict, db: Session) -> None: category = create_random_category(db, test_itemtype) update_data = {"title_en": random_string(length=25)} response = client.put(BASE_URL + f"{category.id}", headers=superuser_token_headers, json=update_data) assert response.status_code == 200 content = response.json() assert content["title_en"] == update_data["title_en"]
def test_create_user_by_normal_user( client: TestClient, normal_user_token_headers: Dict[str, str]) -> None: username = random_email() password = random_string() data = {"email": username, "password": password} r = client.post( f"{settings.API_V1_STR}/users/", headers=normal_user_token_headers, json=data, ) assert r.status_code == 400
def test_update_category_with_too_short_name_returns_422( client: TestClient, test_itemtype, superuser_token_headers: dict, db: Session) -> None: category = create_random_category(db, test_itemtype) update_data = {"title_en": random_string(length=2)} response = client.put(BASE_URL + f"{category.id}", headers=superuser_token_headers, json=update_data) assert response.status_code == 422 content = response.json() assert "at least 3 characters" in content["detail"][0]["msg"]
def test_delete_item(db: Session) -> None: db.query(Note).delete() stored_notes = crud_note.get_multi(db=db) assert len(stored_notes) == 0 title = random_string(100) description = random_string(255) tags = random_string(100) note_obj = NoteCreate(title=title, description=description, tags=tags) note_1 = crud_note.create(db=db, obj_in=note_obj) title = random_string(100) description = random_string(255) tags = random_string(100) note_obj = NoteCreate(title=title, description=description, tags=tags) note_2 = crud_note.create(db=db, obj_in=note_obj) stored_notes = crud_note.get_multi(db=db) assert len(stored_notes) == 2 id_delete = note_1.id crud_note.remove(db=db, id=id_delete) note_1 = crud_note.get(db=db, id=id_delete) assert note_1 is None stored_notes = crud_note.get_multi(db=db) assert len(stored_notes) == 1
def test_get_notes(db: Session) -> None: db.query(Note).delete() stored_notes = crud_note.get_multi(db=db) assert len(stored_notes) == 0 for _ in range(10): title = random_string(100) description = random_string(255) tags = random_string(100) note_in = NoteCreate(title=title, description=description, tags=tags) crud_note.create(db=db, obj_in=note_in) stored_notes = crud_note.get_multi(db=db) assert len(stored_notes) == 10 for _ in range(10): title = random_string(100) description = random_string(255) tags = random_string(100) note_in = NoteCreate(title=title, description=description, tags=tags) crud_note.create(db=db, obj_in=note_in) stored_notes = crud_note.get_multi(db=db) assert len(stored_notes) == 20
def test_update_category_with_invalid_id_returns_404( client: TestClient, test_itemtype, superuser_token_headers: dict, db: Session) -> None: data = { "title_en": random_string(length=25), "itemtype_id": test_itemtype.id } response = client.put(BASE_URL + "-1", headers=superuser_token_headers, json=data) assert response.status_code == 404 content = response.json() assert "Category not found" in content['detail']