"""Integration Tests for ows-sales-goals.""" import pytest from sales_goals.connectors import mysql from sales_goals.models.store import Store def add_store(store_id, name): """Add a digital store to the DB.""" with mysql.sales_goals_session_scope() as session: store = Store(store_id=store_id, name=name) session.add(store) def create_sales_goal(client, product_id): """Create a sales goal.""" return client.create_sales_goals(product_id, { 'notes': 'ya', 'hmv_notes': 'hmv', }) def create_target_market_goal(client, product_id, country_id, first_week=True): """Create target market goal.""" add_store(4, 'Other') data = { 'goals': [ { 'store': {'store_id': 4, 'name': 'Other'}, 'label_goal_value': 1, 'distribution_goal_value': 0 } ], } if first_week: data['first_week_estimate'] = 1212 return client.create_target_market_goal(product_id, country_id, data) def update_target_market_goal( client, product_id, country_id, target_market_goal_id): """Update target market.""" return client.update_target_market_goal( product_id, { 'target_market_goals': [{ 'country_id': country_id, 'first_week_estimate': 123, 'goals': [ { 'store': {'store_id': 4, 'name': 'Other'}, 'label_goal_value': 10, 'distribution_goal_value': 100, 'target_market_goal_id': target_market_goal_id, } ] }] }) def assert_country_found(territories, country_name, category, country_id): """Assert country is found in response.""" try: territory = [territory for territory in territories if territory['name'] == country_name][0] except KeyError: pytest.fail( 'Expected to find {} in list of territories'.format(country_name)) assert territory['category'] == category assert territory['country_id'] == country_id def test_get_territories_workstation(workstation_api_client, mocker, mock_territories): """Test territories endpoint.""" status_code, json = workstation_api_client.get_territories() assert status_code == 200, \ 'Result of GET was {}, expected 200.'.format(status_code) territories = json['items'] assert_country_found(territories, 'United States', 'Top Market', 1) assert_country_found(territories, 'Brazil', 'Country', 13) def test_get_territories_oa(oa_api_client, mocker, mock_territories): """Test territories endpoint.""" status_code, json = oa_api_client.get_territories() assert status_code == 200, \ 'Result of GET was {}, expected 200.'.format(status_code) territories = json['items'] assert_country_found(territories, 'United States', 'Top Market', 1) assert_country_found(territories, 'Brazil', 'Country', 13) def test_create_sales_goal( oa_api_client, test_database, mocker, mock_territories, random_product_id, ): """Test create endpoint.""" status_code, json = create_sales_goal(oa_api_client, random_product_id) assert status_code == 200, \ 'Result of GET was {}, expected 200.'.format(json) assert json['notes'] == 'ya' assert json['hmv_notes'] == 'hmv' assert json['product_id'] == random_product_id def test_get_sales_goal( oa_api_client, test_database, mocker, mock_territories, random_product_id ): """Test fetch endpoint.""" create_sales_goal(oa_api_client, random_product_id) country_id = 2 create_target_market_goal(oa_api_client, random_product_id, country_id) status_code, json = oa_api_client.get_sales_goals(random_product_id) assert status_code == 200, \ 'Result of GET was {}, expected 200.'.format(json) assert json['sales_goals']['notes'] == 'ya' assert json['sales_goals']['hmv_notes'] == 'hmv' assert json['sales_goals']['product_id'] == random_product_id target_market_goal = json['target_market_goals'][0] assert target_market_goal['country'] == {'country_id': 2, 'name': 'Canada'} assert target_market_goal['first_week_estimate'] == 1212 first_item = target_market_goal['goals']['items'][0] assert first_item['distribution_goal_value'] == 0 assert first_item['label_goal_value'] == 1 assert first_item['store'] == { 'name': 'Other', 'store_id': 4 } assert target_market_goal['goals']['total'] == { 'distribution_total': 0, 'label_total': 1 } def test_get_sales_goal_no_first_week( oa_api_client, test_database, mocker, mock_territories, random_product_id ): """Test fetch with no first week estimate returns a result.""" create_sales_goal(oa_api_client, random_product_id) country_id = 2 create_target_market_goal( oa_api_client, random_product_id, country_id, False) status_code, json = oa_api_client.get_sales_goals(random_product_id) assert status_code == 200, \ 'Result of GET was {}, expected 200.'.format(json) target_market_goal = json['target_market_goals'][0] assert target_market_goal['first_week_estimate'] is None def test_create_target_market_goal( oa_api_client, test_database, mocker, mock_territories, random_product_id ): """Test create endpoint.""" create_sales_goal(oa_api_client, random_product_id) country_id = 2 status_code, json = create_target_market_goal( oa_api_client, random_product_id, country_id) assert status_code == 200, \ 'Result of GET was {}, expected 200.'.format(json) assert json['goals']['first_week_estimate'] == 1212 assert json['goals']['total'] == { 'country_id': 2, 'distribution_total': 0, 'label_total': 1 } assert json['goals']['items'][0]['distribution_goal_value'] == 0 assert json['goals']['items'][0]['label_goal_value'] == 1 assert json['goals']['items'][0]['store'] == { 'name': 'Other', 'store_id': 4 } def test_delete_target_market_goal( oa_api_client, test_database, mocker, mock_territories, random_product_id ): """Test create endpoint.""" create_sales_goal(oa_api_client, random_product_id) country_id = 2 create_target_market_goal( oa_api_client, random_product_id, country_id) status_code, json = oa_api_client.delete_target_market_goal( random_product_id, country_id) assert status_code == 200, \ 'Result of GET was {}, expected 200.'.format(json) status_code, json = oa_api_client.get_sales_goals(random_product_id) assert len(json['target_market_goals']) == 0 def test_update_target_market_goal( oa_api_client, test_database, mocker, mock_territories, random_product_id): """Test update endpoint.""" country_id = 2 create_sales_goal(oa_api_client, random_product_id) status_code, json = create_target_market_goal( oa_api_client, random_product_id, country_id) target_market_goal_id = json['goals']['items'][0]['target_market_goal_id'] status_code, json = update_target_market_goal( oa_api_client, random_product_id, country_id, target_market_goal_id) assert status_code == 200, \ 'Result of GET was {}, expected 200.'.format(json) goal = json['target_market_goals'][0] assert goal['first_week_estimate'] == 123 assert goal['goals']['total'] == { 'distribution_total': 100, 'label_total': 10 } first_item = goal['goals']['items'][0] assert first_item['distribution_goal_value'] == 100 assert first_item['label_goal_value'] == 10 assert first_item['store'] == { 'name': 'Other', 'store_id': 4 }