"""Test for trackdown_media_access model.""" from datetime import datetime from unittest.mock import patch from freezegun import freeze_time import pytest from sqlalchemy.exc import SQLAlchemyError from assets.constants import error from assets.constants import field_const from assets.models.legacy import trackdown_media_access from tests.models.legacy import ar_operations @pytest.fixture def trackdown_media_access_data(): """Get data to seed trackdown_media_access table.""" return [ { 'id': 1, 'trackdown_user_id': 1, 'user_type': field_const.USER_TYPE_ALW, 'token': '78ea5e4a3f45d8f419b42848af9c19', 'token_type': field_const.TOKEN_TYPE_STREAM, 'last_accessed': datetime.strptime( '2017-01-10T11:42:12', '%Y-%m-%dT%H:%M:%S'), }, { 'id': 2, 'trackdown_user_id': 3, 'user_type': field_const.USER_TYPE_OA, 'token': '58301a5b91d9bc900f76abaf76a70e', 'token_type': field_const.TOKEN_TYPE_STREAM, 'last_accessed': datetime.strptime( '2017-01-10T11:41:12', '%Y-%m-%dT%H:%M:%S'), } ] @pytest.fixture def db_fixture(): """Set up the trackdown media access table.""" ar_operations.create_tables() @pytest.fixture def db_with_data_fixture(trackdown_media_access_data): """Set up the trackdown media access table with test entries.""" ar_operations.create_tables() ar_operations.seed_trackdown_media_access(trackdown_media_access_data) @freeze_time('2017-04-23 12:00:01') @pytest.mark.parametrize('user_type', [ field_const.USER_TYPE_ALW, field_const.USER_TYPE_OA]) def test_create_trackdown_media_access(db_fixture, user_type): """Test trackdown media access record creation.""" expected_user_id = 2 expected_token = '' expected_token_type = field_const.TOKEN_TYPE_STREAM expected_last_accessed = datetime(2017, 4, 23, 12, 0, 1) response = trackdown_media_access.create_track_media_access( user_id=expected_user_id, user_type=user_type, token=expected_token, token_type=expected_token_type) assert response assert response.message['id'] == 1 assert response.message['trackdown_user_id'] == expected_user_id assert response.message['user_type'] == user_type assert response.message['token'] == expected_token assert response.message['token_type'] == expected_token_type assert response.message['last_accessed'] == expected_last_accessed @patch('assets.connectors.mysql.ar_db_session', side_effect=SQLAlchemyError()) def test_create_import_asset_error(monkeypatch): """Test trackdown media access creation with SQLAlchemyError failure.""" response = trackdown_media_access.create_track_media_access( user_id=1, user_type=field_const.USER_TYPE_OA, token='78ea5e4a3f45d8f419b42848af9c19', token_type=field_const.TOKEN_TYPE_STREAM) assert response.status == 500 @pytest.mark.parametrize('user_type, token_type', [ (field_const.USER_TYPE_ALW, 'unknown_token_type'), ('unknown_user_type', field_const.TOKEN_TYPE_STREAM)]) def test_create_import_asset_with_wrong_type(user_type, token_type): """Test trackdown media access creation with wrong types.""" response = trackdown_media_access.create_track_media_access( user_id=1, user_type=user_type, token='78ea5e4a3f45d8f419b42848af9c19', token_type=token_type) assert response.status == 400 def test_get_by_id_success(db_with_data_fixture): """Test creating of getting query by trackdown_media_access_id.""" existing_id = 1 get_by_id_query = trackdown_media_access._get_by_id( trackdown_media_access_id=existing_id, session=ar_operations.session) query_result = get_by_id_query.first() assert query_result def test_get_trackdown_media_access_success(db_with_data_fixture): """Test successful get of existing trackdown media access entry.""" existing_user_id = 1 existing_user_type = field_const.USER_TYPE_ALW existing_token_type = field_const.TOKEN_TYPE_STREAM response = trackdown_media_access.get_trackdown_media_access( user_id=existing_user_id, user_type=existing_user_type, token_type=existing_token_type) assert response assert response.message['trackdown_user_id'] == existing_user_id assert response.message['user_type'] == existing_user_type def test_get_trackdown_media_access_failures(db_with_data_fixture): """Test get_trackdown_media_access fail if entry not found.""" not_existing_user_id = 5 existing_user_type = field_const.USER_TYPE_ALW existing_token_type = field_const.TOKEN_TYPE_STREAM response = trackdown_media_access.get_trackdown_media_access( user_id=not_existing_user_id, user_type=existing_user_type, token_type=existing_token_type) assert response.status == 404 assert response.errors['code'] == error.ERROR_CODE_NOT_FOUND @patch('assets.connectors.mysql.ar_db_session', side_effect=SQLAlchemyError()) def test_get_trackdown_media_access_error(db_with_data_fixture): """Test get_trackdown_media_access with with SQLAlchemyError failure.""" existing_user_id = 1 existing_user_type = field_const.USER_TYPE_ALW existing_token_type = field_const.TOKEN_TYPE_STREAM response = trackdown_media_access.get_trackdown_media_access( user_id=existing_user_id, user_type=existing_user_type, token_type=existing_token_type) assert response.status == 500 def test_update_last_accessed_datetime_success(db_with_data_fixture): """Test successful update of existing trackdown media access.""" existing_id = 1 new_last_accessed = datetime.strptime( '2017-04-10T12:40:00', '%Y-%m-%dT%H:%M:%S') response = trackdown_media_access.update_last_accessed_datetime( trackdown_media_access_id=existing_id, last_accessed=new_last_accessed) assert response assert response.message['last_accessed'] == new_last_accessed assert ar_operations.session.query( trackdown_media_access.TrackdownMediaAccess).filter( (trackdown_media_access.TrackdownMediaAccess .trackdown_media_access_id == existing_id), (trackdown_media_access.TrackdownMediaAccess .last_accessed == new_last_accessed) ).first() is not None def test_update_last_accessed_datetime_failure(db_with_data_fixture): """Test failure of update_last_accessed_datetime with not existing id.""" not_existing_id = 100 new_last_accessed = datetime.strptime( '2017-04-10T12:40:00', '%Y-%m-%dT%H:%M:%S') response = trackdown_media_access.update_last_accessed_datetime( trackdown_media_access_id=not_existing_id, last_accessed=new_last_accessed) assert response.status == 500 @patch('assets.connectors.mysql.ar_db_session', side_effect=SQLAlchemyError()) def test_update_last_accessed_datetime_error( monkeypatch, db_with_data_fixture): """Test update_last_accessed_datetime with SQLAlchemyError failure.""" existing_id = 1 new_last_accessed = datetime.strptime( '2017-04-10T12:40:00', '%Y-%m-%dT%H:%M:%S') response = trackdown_media_access.update_last_accessed_datetime( trackdown_media_access_id=existing_id, last_accessed=new_last_accessed) assert response.status == 500