"""Unit tests for social_profile model.""" from oto import status import pytest from social_analytics.connectors import mysql from social_analytics.models import social_profile from tests.utils import db_operations @pytest.fixture def db_fixture(): """Drop and re-create all the SQLite tables and seed them.""" db_operations.create_tables() db_operations.seed_data() def test_get_social_profiles(db_fixture): """Test social profile model returns data when seeded.""" result = social_profile.get_social_profiles() assert result result_social_profiles = result.message assert len(result_social_profiles) == 1 result_social_profile = result_social_profiles['items'][0] assert (result_social_profile['social_profile_id'] == db_operations.social_profile_data[0]['social_profile_id']) assert (result_social_profile['platform_name'] == db_operations.social_profile_data[0]['platform_name']) def test_get_social_profiles_empty(): """Test not found response when there is no social profile data.""" db_operations.create_tables() result = social_profile.get_social_profiles() assert not result @mysql.autosession() def test_create_social_profile(db_fixture): """Test that a social profile can be created.""" data = { 'social_profile_id': 1010, 'platform': 'facebook', 'platform_id': 123, 'platform_name': 'New Platform' } result = social_profile.create_social_profile(data) created_social_profile = result.message assert created_social_profile['social_profile_id'] == 1010 assert created_social_profile['platform_name'] == 'New Platform' def test_create_social_profile_empty_body(): """Test that a social profile requires fields to be created.""" data = {} result = social_profile.create_social_profile(data) assert result.status == status.BAD_REQUEST def test_update_social_profile(db_fixture): """Test that a social profile can be updated.""" data = { 'social_profile_id': 1, 'platform': 'facebook', 'platform_id': 123, 'platform_name': 'Anika Pyle Changed Name' } result = social_profile.update_social_profile(data) updated_social_profile = result.message assert updated_social_profile['platform'] == 'facebook' assert updated_social_profile['platform_id'] == 123 assert ( updated_social_profile['platform_name'] == 'Anika Pyle Changed Name') def test_update_social_profile_empty_body(): """Test that a social profile requires fields to be updated.""" data = {} result = social_profile.update_social_profile(data) assert result.status == status.BAD_REQUEST def test_get_social_profile_by_ids(db_fixture): """Test that a social profile can be queried by social_profile_id.""" result = social_profile.get_social_profiles_by_ids(['1', '2']) assert result result_social_profile = result.message assert result_social_profile assert (result_social_profile['items'][0]['social_profile_id'] == db_operations.social_profile_data[0]['social_profile_id']) assert (result_social_profile['items'][0]['platform_name'] == db_operations.social_profile_data[0]['platform_name']) def test_get_social_profile_by_ids_empty_body(): """Test that a social_profile_id is required to query a social profile.""" social_profile_ids = None result = social_profile.get_social_profiles_by_ids(social_profile_ids) assert result.status == status.BAD_REQUEST def test_get_social_profile_by_ids_not_found(db_fixture): """Test not found response when no social profile given id is found.""" social_profile_ids = ['10'] result = social_profile.get_social_profiles_by_ids(social_profile_ids) assert not result def test_get__social_profile_with_platform_id_and_platform( db_fixture): """Test get social profile with platform_id and platform.""" platform_id = 456 platform = 'facebook' result = social_profile.get_social_profile_with_platform_id_and_platform( platform_id, platform) assert result.message def test_get__social_profile_with_profile_id_and_profile_with_no_params( db_fixture): """Test get social profile with platform_id and platform. This case exams the result when there are empty params """ platform_id = None platform = None result = social_profile.get_social_profile_with_platform_id_and_platform( platform_id, platform) assert not result def test_get_social_profile_by_id(db_fixture): """Test get social profile by social profile id.""" social_profile_id = 1 result = social_profile.get_social_profile_by_id(social_profile_id) assert result assert result.message['platform_name'] == 'Anika Pyle' def test_get_social_profile_by_id_not_found(db_fixture): """Test get social profile by social profile id.""" social_profile_id = None result = social_profile.get_social_profile_by_id(social_profile_id) assert not result def test_get__social_profile_with_platform_id_and_platform_not_found( db_fixture): """Test get social profile with platform_id and platform. This case exams the result when social profile is not found. """ platform_id = 123444444 platform = 'facebook' result = social_profile.get_social_profile_with_platform_id_and_platform( platform_id, platform) assert not result assert result.status == status.NOT_FOUND def test_delete_social_profile(db_fixture): """Test delete social profile.""" social_profile_id = 3 result = social_profile.delete_social_profile(social_profile_id) assert result deleted_profile = result.message assert deleted_profile def test_delete_social_profile_not_found(db_fixture): """Test delete social profile not found.""" social_profile_id = 53 result = social_profile.delete_social_profile(social_profile_id) assert not result assert result.status == status.NOT_FOUND def test_delete_social_profile_no_id(db_fixture): """Test delete social profile not found.""" social_profile_id = None result = social_profile.delete_social_profile(social_profile_id) assert not result assert result.status == status.BAD_REQUEST def test_update_social_profiles_last_collected_date(db_fixture): """Test update social profiles last collected date.""" data = [1, 2] result = social_profile.update_social_profiles_last_collected_date(data) assert result assert len(result.message['items']) == 2 def test_update_social_profiles_last_collected_date_wrong_ids(db_fixture): """Test update social profiles last collected date wrong ids.""" data = [1, 2, 40, 60] result = social_profile.update_social_profiles_last_collected_date(data) assert result assert len(result.message['items']) == 2 def test_update_social_profiles_last_collected_date_bad_params(): """Test update social profiles last collected date bad params.""" data = [] result = social_profile.update_social_profiles_last_collected_date(data) assert not result assert result.status == status.BAD_REQUEST