"""AccountPayeeKycNotification model tests.""" from datetime import timedelta from payee.models.payee_kyc_notification import AccountPayeeKycNotification from payee.utils.models import object_as_dict from tests.utils.factories import AccountPayeeKycNotificationFactory def test_get_notification_model(account_fixtures, account_payee_fixtures): """Get AccountPayeeKycNotification by id.""" notification = AccountPayeeKycNotificationFactory.create() account_payee_kyc_notification_id = notification.account_payee_kyc_notification_id result = AccountPayeeKycNotification.get_by_id(account_payee_kyc_notification_id) assert object_as_dict(result) == object_as_dict(notification) def test_delete_notification_model(account_fixtures, account_payee_fixtures): """Delete AccountPayeeKycNotification by id.""" def remove_deleted(data: dict) -> dict: """Remove deleted_* fields.""" del data['deleted_at'] del data['deleted_by'] return data notification = AccountPayeeKycNotificationFactory.create() account_payee_kyc_notification_id = notification.account_payee_kyc_notification_id # soft delete AccountPayeeKycNotification.delete_by_id_or_error( account_payee_kyc_notification_id, soft_delete=True ) result = AccountPayeeKycNotification.get_by_id(account_payee_kyc_notification_id) assert result.deleted_at assert result.deleted_by assert remove_deleted(object_as_dict(result)) == remove_deleted( object_as_dict(notification) ) # completely delete AccountPayeeKycNotification.delete_by_id_or_error( account_payee_kyc_notification_id, soft_delete=False ) result = AccountPayeeKycNotification.get_by_id(account_payee_kyc_notification_id) assert not result def test_get_latest_by_payee_id(account_fixtures, account_payee_fixtures): """Check get_latest_by_payee_id.""" account_payee_id = 1 now = AccountPayeeKycNotification.current_timestamp() AccountPayeeKycNotificationFactory.create( account_payee_id=account_payee_id, created_at=now ) notification = AccountPayeeKycNotificationFactory.create( account_payee_id=account_payee_id, created_at=now + timedelta(seconds=1), ) AccountPayeeKycNotificationFactory.create( account_payee_id=account_payee_id, created_at=now + timedelta(seconds=2), deleted_at=AccountPayeeKycNotification.current_timestamp(), deleted_by='test_user_id', ) result = AccountPayeeKycNotification.get_latest_by_payee_id(account_payee_id) assert object_as_dict(result) == object_as_dict(notification) def test_get_latest_by_payee_id_failure_not_found( account_fixtures, account_payee_fixtures ): """Check get_latest_by_payee_id not found.""" result = AccountPayeeKycNotification.get_latest_by_payee_id(123) assert result is None def test_soft_delete_by_payee_id(account_fixtures, account_payee_fixtures): """Check soft_delete_by_payee_id.""" account_payee_id = 1 count = 4 AccountPayeeKycNotificationFactory.create_batch( count, account_payee_id=account_payee_id ) AccountPayeeKycNotificationFactory.create_batch( 2, account_payee_id=account_payee_id, deleted_at=AccountPayeeKycNotification.current_timestamp(), ) result = AccountPayeeKycNotification.soft_delete_by_payee_id(account_payee_id) assert result == count assert AccountPayeeKycNotification.get_latest_by_payee_id(account_payee_id) is None assert ( not AccountPayeeKycNotification.query.filter( AccountPayeeKycNotification.deleted_at.is_(None) ) .filter(AccountPayeeKycNotification.account_payee_id == account_payee_id) .count() )