diff --git a/migrations/versions/2b86076bf074_add_is_deleted_field_to_unionmember_.py b/migrations/versions/2b86076bf074_add_is_deleted_field_to_unionmember_.py index 0c7f5a5..c0ac071 100644 --- a/migrations/versions/2b86076bf074_add_is_deleted_field_to_unionmember_.py +++ b/migrations/versions/2b86076bf074_add_is_deleted_field_to_unionmember_.py @@ -18,9 +18,10 @@ def upgrade(): - # ### commands auto generated by Alembic - please adjust! ### op.alter_column('file', 'source', existing_type=sa.VARCHAR(), nullable=False) op.add_column('union_member', sa.Column('is_deleted', sa.Boolean(), nullable=True)) + op.execute(f'UPDATE "union_member" SET is_deleted = False') + op.alter_column('union_member', 'is_deleted', nullable=False) # ### end Alembic commands ### diff --git a/tests/test_routes/test_user.py b/tests/test_routes/test_user.py index 1b69579..5dfe97a 100644 --- a/tests/test_routes/test_user.py +++ b/tests/test_routes/test_user.py @@ -5,6 +5,7 @@ from print_service.models import UnionMember from print_service.settings import get_settings +from sqlalchemy import and_, func url: str = '/is_union_member' @@ -46,14 +47,14 @@ def test_post_success(client, dbsession): 'union_number': '1966', 'student_number': '1967', } - ], + ] } res = client.post(url, data=json.dumps(body)) assert res.status_code == status.HTTP_200_OK - UnionMember.query(session=dbsession).filter( - UnionMember.surname == body['users'][0]['username'], - UnionMember.union_number == body['users'][0]['union_number'], - UnionMember.student_number == body['users'][0]['student_number'], + UnionMember.query(session=dbsession).filter(and_( + UnionMember.surname == func.upper(body['users'][0]['username']), + UnionMember.union_number == func.upper(body['users'][0]['union_number']), + UnionMember.student_number == func.upper(body['users'][0]['student_number'])) ).delete() dbsession.commit() @@ -66,12 +67,56 @@ def test_post_is_deleted(client, union_member_user, add_is_deleted_flag): 'union_number': '6666667', 'student_number': '13033224', } - ], + ] } res = client.post(url, data=json.dumps(body)) assert res.status_code == status.HTTP_404_NOT_FOUND +def test_restore_is_deleted(client, dbsession): + user = UnionMember(id=5, + surname='test_user', + union_number='123', + student_number='56', + is_deleted=False) + dbsession.add(user) + dbsession.commit() + + body = { + 'users': [ + { + 'username': 'test_user', + 'union_number': '123', + 'student_number': '56', + 'is_deleted': True + } + ] + } + _ = client.post(url, data=json.dumps(body)) + res = UnionMember.query(session=dbsession, with_deleted=True).filter(UnionMember.id == 5).one_or_none() + assert res.is_deleted is False + user.is_deleted = True + dbsession.commit() + body = { + 'users': [ + { + 'username': 'test_user', + 'union_number': '123', + 'student_number': '56', + 'is_deleted': False + } + ] + } + res = client.post(url, data=json.dumps(body)) + assert res.status_code == status.HTTP_404_NOT_FOUND + UnionMember.query(session=dbsession, with_deleted=True).filter(and_( + UnionMember.surname == func.upper(body['users'][0]['username']), + UnionMember.union_number == func.upper(body['users'][0]['union_number']), + UnionMember.student_number == func.upper(body['users'][0]['student_number']) + )).delete() + dbsession.commit() + + @pytest.mark.parametrize( 'users', [