import pytest from aiohttp import web from copy import deepcopy from tests.constants import SEARCH_ES_DATA_TO_INSERT from tests.dna.category.constants import TRACK_ES_DATA_TO_INSERT from tests.dna.recent_searches.constants import ( RECENT_SEARCH_ARTIST_BODY, RECENT_SEARCH_TRACK_BODY, INVALID_REQUEST_BODY_1, INVALID_REQUEST_BODY_2, INVALID_REQUEST_BODY_3, INVALID_REQUEST_BODY_4, INVALID_REQUEST_BODY_5, EXPECTED_RETURN_DATA_1, EXPECTED_RETURN_DATA_2, EXPECTED_RETURN_DATA_3, EXPECTED_RETURN_DATA_4, EXPECTED_RETURN_DATA_5, RECENT_SEARCH_EXPECTED_RETURN_DATA, ARTIST_RECENT_SEARCH_EXPECTED_RETURN_DATA, TRACK_RECENT_SEARCH_EXPECTED_RETURN_DATA, REQUEST_BODIES, UNIQUE_ENTITIES_EXPECTED_RETURN_DATA, ) class TestRecentSearches: @pytest.mark.parametrize( "request_body, expected_status_code", [ ( RECENT_SEARCH_ARTIST_BODY, web.HTTPCreated.status_code, ), ( RECENT_SEARCH_TRACK_BODY, web.HTTPCreated.status_code, ), ], ) async def test_create_recent_search( self, expected_status_code, request_body, postgres_client, app_client, auth_header_authorized ): response = await app_client.post("/api/dna/recent_searches", headers=auth_header_authorized, json=request_body) assert expected_status_code == response.status response = await app_client.post("/api/dna/recent_searches", headers=auth_header_authorized, json=request_body) assert expected_status_code == response.status @pytest.mark.parametrize( "request_body, expected_return_data, expected_status_code", [ ( INVALID_REQUEST_BODY_1, EXPECTED_RETURN_DATA_1, web.HTTPUnprocessableEntity.status_code, ), ( INVALID_REQUEST_BODY_2, EXPECTED_RETURN_DATA_2, web.HTTPUnprocessableEntity.status_code, ), ( INVALID_REQUEST_BODY_3, EXPECTED_RETURN_DATA_3, web.HTTPUnprocessableEntity.status_code, ), ( INVALID_REQUEST_BODY_4, EXPECTED_RETURN_DATA_4, web.HTTPUnprocessableEntity.status_code, ), ( INVALID_REQUEST_BODY_5, EXPECTED_RETURN_DATA_5, web.HTTPUnprocessableEntity.status_code, ), ], ) async def test_create_recent_search_check_request_body( self, expected_status_code, expected_return_data, request_body, postgres_client, app_client, auth_header_authorized, ): response = await app_client.post("/api/dna/recent_searches", headers=auth_header_authorized, json=request_body) assert expected_status_code == response.status assert expected_return_data == await response.json() @pytest.mark.parametrize( "expected_return_data, request_body_1, request_body_2, expected_status_code, elastic_client", [ ( RECENT_SEARCH_EXPECTED_RETURN_DATA, { "entity_id": "738253", "entity_type": "artist", }, { "entity_id": "13692288", "entity_type": "track", }, web.HTTPCreated.status_code, ( ("artist_search", deepcopy(SEARCH_ES_DATA_TO_INSERT)), ("track_search", deepcopy(TRACK_ES_DATA_TO_INSERT)), ), ), ( [], RECENT_SEARCH_ARTIST_BODY, # non exist artist RECENT_SEARCH_TRACK_BODY, # non exist track web.HTTPCreated.status_code, ( ("artist_search", deepcopy(SEARCH_ES_DATA_TO_INSERT)), ("track_search", deepcopy(TRACK_ES_DATA_TO_INSERT)), ), ), ( ARTIST_RECENT_SEARCH_EXPECTED_RETURN_DATA, { "entity_id": "738253", "entity_type": "artist", }, RECENT_SEARCH_TRACK_BODY, # non exist track web.HTTPCreated.status_code, ( ("artist_search", deepcopy(SEARCH_ES_DATA_TO_INSERT)), ("track_search", deepcopy(TRACK_ES_DATA_TO_INSERT)), ), ), ( TRACK_RECENT_SEARCH_EXPECTED_RETURN_DATA, RECENT_SEARCH_ARTIST_BODY, # non exist artist { "entity_id": "13692288", "entity_type": "track", }, web.HTTPCreated.status_code, ( ("artist_search", deepcopy(SEARCH_ES_DATA_TO_INSERT)), ("track_search", deepcopy(TRACK_ES_DATA_TO_INSERT)), ), ), ], indirect=["elastic_client"], ) async def test_get_recent_searches( self, expected_return_data, request_body_1, request_body_2, expected_status_code, elastic_client, postgres_client, app_client, auth_header_authorized, ): response = await app_client.post( "/api/dna/recent_searches", headers=auth_header_authorized, json=request_body_1 ) assert expected_status_code == response.status response = await app_client.post( "/api/dna/recent_searches", headers=auth_header_authorized, json=request_body_2 ) assert expected_status_code == response.status response = await app_client.get("/api/dna/recent_searches", headers=auth_header_authorized) assert expected_return_data == await response.json() @pytest.mark.parametrize( "expected_return_data, request_bodies, expected_status_code, elastic_client", [ ( UNIQUE_ENTITIES_EXPECTED_RETURN_DATA, REQUEST_BODIES, web.HTTPCreated.status_code, ( ("artist_search", deepcopy(SEARCH_ES_DATA_TO_INSERT)), ("track_search", deepcopy(TRACK_ES_DATA_TO_INSERT)), ), ), ], indirect=["elastic_client"], ) async def test_get_recent_searches_return_unique_entities( self, expected_return_data, request_bodies, expected_status_code, elastic_client, postgres_client, app_client, auth_header_authorized, ): for request_body in request_bodies: response = await app_client.post( "/api/dna/recent_searches", headers=auth_header_authorized, json=request_body ) assert expected_status_code == response.status response = await app_client.get("/api/dna/recent_searches", headers=auth_header_authorized) assert expected_return_data == await response.json()