"""Test for ows_accounting interface.""" import flexmock from owsrequest import request import requests from feature_fm.constants.error import ERROR_CODE_OWS_ACCOUNTING_REQUEST from feature_fm.constants.ows_services import CONTENT_TYPE from feature_fm.constants.ows_services import OWS_ACCOUNTING from feature_fm.models import ows_accounting def test_get_average_monthly_net_revenue_success(): """Test getting revenue and the request is successfull.""" account_type = 'vendor' account_id = 1234 revenue = {'average_monthly_net_revenue': 1000} expected_path = ('/{0}/{1}/average-monthly-net-revenue').format( account_type, account_id) ows_accounting_response = flexmock(requests.models.Response()) ows_accounting_response.status_code = 200 (flexmock(ows_accounting_response) .should_receive('json') .and_return(revenue)) (flexmock(request) .should_receive('get') .with_args( OWS_ACCOUNTING, expected_path, headers={'Content-Type': CONTENT_TYPE}) .and_return(ows_accounting_response)) result = ows_accounting.get_average_monthly_net_revenue( account_type, account_id) assert result.status == 200 assert result.message == revenue def test_get_average_monthly_net_revenue_error(): """Test getting revenue and the request fails.""" account_type = 'vendor' account_id = 1234 expected_path = ('/{0}/{1}/average-monthly-net-revenue').format( account_type, account_id) ows_accounting_error = 'ows_accounting_error' ows_accounting_response = flexmock(requests.models.Response()) ows_accounting_response.status_code = 500 (flexmock(ows_accounting_response) .should_receive('text') .and_return(ows_accounting_error)) (flexmock(request) .should_receive('get') .with_args( OWS_ACCOUNTING, expected_path, headers={'Content-Type': CONTENT_TYPE}) .and_return(ows_accounting_response)) result = ows_accounting.get_average_monthly_net_revenue( account_type, account_id) assert result.status == 500 assert result.errors == { 'code': ERROR_CODE_OWS_ACCOUNTING_REQUEST, 'message': ows_accounting_error } def test_get_budget_caps_success(): """Test getting budget caps and the request is successfull.""" budget_caps = [ {'vendor_id': '123', 'budget': '2500'}, {'vendor_id': '456', 'budget': '500'} ] expected_path = '/budget-caps' ows_accounting_response = flexmock(requests.models.Response()) ows_accounting_response.status_code = 200 (flexmock(ows_accounting_response) .should_receive('json') .and_return(budget_caps)) (flexmock(request) .should_receive('get') .with_args( OWS_ACCOUNTING, expected_path, headers={'Content-Type': CONTENT_TYPE}) .and_return(ows_accounting_response)) result = ows_accounting.get_budget_caps() assert result.status == 200 assert result.message == budget_caps def test_get_budget_caps_error(): """Test getting budget caps and the request is successfull.""" expected_path = '/budget-caps' ows_accounting_error = 'ows_accounting_error' ows_accounting_response = flexmock(requests.models.Response()) ows_accounting_response.status_code = 500 (flexmock(ows_accounting_response) .should_receive('json') .and_return(ows_accounting_error)) (flexmock(request) .should_receive('get') .with_args( OWS_ACCOUNTING, expected_path, headers={'Content-Type': CONTENT_TYPE}) .and_return(ows_accounting_response)) result = ows_accounting.get_budget_caps() assert result.status == 500 assert result.errors['code'] == ERROR_CODE_OWS_ACCOUNTING_REQUEST