""" conftest.py =========== This file gets picked up when running py.test tests: http://pytest.org/latest/writing_plugins.html#conftest """ from datetime import datetime from decimal import Decimal import application from flexmock import flexmock from owsfeatures import features import pytest from ows_accounting import features as accounitng_features from ows_accounting import handlers from ows_accounting import response from ows_accounting.constants import header from tests.utils import db_operations from tests.utils import payment_holds_db_operations @pytest.fixture def app_context(): """App context fixture.""" with application.app.app_context(): yield @pytest.fixture def db_fixture(request): """DB fixture.""" db_operations.create_tables() db_operations.seed_currencies_table() db_operations.seed_accounting_periods_table() db_operations.seed_currency_exchange_rates_table() @pytest.fixture def mock_handler(): """Mock handler """ def inner_mock_handler(): return 'Not decorated.' return inner_mock_handler @pytest.fixture def test_header(): return { header.GRASS_ACCOUNT_ID: 1234, header.GRASS_ACCOUNT_TYPE: 'vendor' } @pytest.fixture def test_header_subaccount(): return { header.GRASS_ACCOUNT_ID: 1234, header.GRASS_ACCOUNT_TYPE: header.GRASS_ACCOUNT_TYPE_SUBACCOUNT } @pytest.fixture def mock_periods(): return [ {'file_type': 'AVRO', 'period_ids': '202'}, {'file_type': 'AVRO', 'period_ids': '203'} ] @pytest.fixture def mock_attachments_month(): return [ {'file_name': 'attachment_doc.pdf', 'file_size': Decimal('1234'), 'period_id': '217', 'file_key': '/L10303_217_attachment_doc.pdf'}, {'file_name': 'attachment_second_doc.pdf', 'file_size': Decimal('1234'), 'period_id': '217', 'file_key': '/L10303_217_attachment_second_doc.pdf'}] @pytest.fixture def mock_attachments_quarter(): return [ {'file_name': 'attachment_doc.pdf', 'file_size': Decimal('1234'), 'period_id': '217', 'file_key': '/L10303_223_attachment_doc.pdf'}, {'file_name': 'attachment_second_doc.pdf', 'file_size': Decimal('1234'), 'period_id': '217', 'file_key': '/L10303_224_attachment_second_doc.pdf'}, {'file_name': 'attachment_second_doc_2.pdf', 'file_size': Decimal('1234'), 'period_id': '217', 'file_key': '/L10303_225_attachment_second_doc.pdf'}] @pytest.fixture def valid_get_report_request(): return { 'periods': '202,203,204', 'account_id': '123321', 'account_type': 'label', 'page_offset': 0, 'page_limit': 50, 'transaction_types': 'AEA,AEV,AL,AS,AV,CE', 'file_type': 'txt', 'number_format': 'en_US', 'request_context': { 'contact_id': 123, 'first_name': 'Mary Had', 'last_name': 'A Little Lamb', 'contact_email': 'baaa@lamb.com' } } @pytest.fixture def invalid_get_report_request(): """Returns a dict representing JSON sent in body for GET /report. Returns: dict: invalid json body for GET /report """ # number_format is wrong return { 'periods': '202,203,204', 'account_id': '123321', 'account_type': 'label', 'transaction_types': 'AEA,AEV,AL,AS,AV,CE', 'file_type': 'xls', 'number_format': 'nothing', 'request_context': { 'contact_id': 123, 'first_name': 'Mary Had', 'last_name': 'A Little Lamb', 'contact_email': 'baaa@lamb.com' } } @pytest.fixture def invalid_get_report_request_context(): """Returns invalid data due to empty, non-integer contact_id. Returns: dict: invalid json body for GET /report """ return { 'periods': '202,203,204', 'account_id': '123321', 'account_type': 'label', 'transaction_types': 'AEA,AEV,AL,AS,AV,CE', 'file_type': 'txt', 'number_format': 'en_US', 'request_context': { 'contact_id': '' } } @pytest.fixture def invalid_get_report_trailing_spaces(): """Returns a dict representing JSON sent in body for GET /report. Returns: dict: invalid json body for GET /report """ # trailing spaces after periods, invalid period return { 'periods': '202,203,204 ', 'account_id': '123321', 'account_type': 'label', 'transaction_types': 'AEA,AEV,AL,AS,AV,CE', 'file_type': 'xls', 'number_format': 'en_US', 'request_context': { 'contact_id': 123, 'first_name': 'Mary Had', 'last_name': 'A Little Lamb', 'contact_email': 'baaa@lamb.com' } } @pytest.fixture def invalid_get_transaction_types(): """Returns invalid data due to invalid transaction_types param. Returns: dict: invalid JSON body used in GET /report """ return { 'periods': '202,203,204', 'transaction_types': 'DT' } @pytest.fixture def example_data_to_validate_success(): """Returns some data to validate against the example validation schema. Returns: data (dict): a dict containing example data that validates against the example schema """ data = { 'apple': 123, 'fruit': 'abc,', 'request_context': { 'first_name': 'DOO bee DOO waahh'} } return data @pytest.fixture def example_data_to_validate_failure(): """Returns some data to validate against the example validation schema. Returns: data (dict): a dict containing example data that validates against the example schema """ data = { 'apple': 123, 'fruit': 'abc,', 'request_context': { 'last_name': 'DOO bee DOO waahh'} } return data @pytest.fixture def example_validation_schema(): """Returns example JSON Draft4 Schema for testing the validator. Returns: schema (dict): JSON Draft4 Validation Schema """ schema = { '$schema': 'http://json-schema.org/draft-04/schema#', 'definitions': { 'user': { 'type': 'object', 'properties': { 'first_name': { 'type': 'string', 'pattern': '^[^ ].+[^ ]$' }, 'last_name': { 'type': 'string', 'pattern': '^[^ ].+[^ ]$' } }, 'required': ['first_name'] } }, 'type': 'object', 'properties': { 'apple': { 'type': 'integer' }, 'fruit': { 'type': 'string', 'pattern': '^[^ ].+[^ ]$' }, 'request_context': {'$ref': '#/definitions/user'} }, 'required': ['apple', 'request_context', 'fruit'] } return schema @pytest.fixture def add_report_data(): """A fixture with data for logic.reports.add_report function. """ data = ( 'test_correlation_id', '18805', 'vendor', ['202', '203', '204'], ['DA', 'DT'], 'txt', 'en_US', 'test@example.com', 1111, 'first_name', 'last_name', '2016-01-01 12:00:12') return data @pytest.fixture def feature_state(): """A fixture to configure is_feature_enabled fixture. Usage: @pytest.mark.parametrize('feature_state', [True]) """ return 'define with @pytest.mark.parametrize' @pytest.fixture def flag_test_func_name(): """A fixture to configure feature_flag_mock fixture. Usage: @pytest.mark.parametrize('flag_test_func_name', ['is_myflag_enabled']) """ return 'define with @pytest.mark.parametrize' @pytest.fixture def is_feature_enabled(feature_state): """Mock owsfeatures.is_feature_enabled function. Usage: @pytest.mark.parametrize('feature_state', [True]) def test_smth(is_feature_enabled): pass Args: feature_state (bool): Fixture with feature state. Returns: feature_state (bool): state of the feature flag. """ (flexmock(features) .should_receive('is_feature_enabled') .and_return(feature_state)) return feature_state @pytest.fixture def feature_flag_mock(flag_test_func_name, feature_state): """Mock feature flag check. Usage: @pytest.mark.parametrize('flag_test_func_name,feature_state', ['is_my_flag_enabled', True]) Args: flag_test_func_name (str): fixture with the features module function name that tests if the flag is enabled. feature_state (bool): return value of the test function. Returns: feature_state (bool): state of the feature flag. """ (flexmock(accounitng_features) .should_receive(flag_test_func_name) .and_return(feature_state)) return feature_state @pytest.fixture def ows_features_mock(): """Mock owsfeatures call. Note: this mock doesn't set any feature flags, so you'll need to mock function responsible for check of particular flag. """ (flexmock(handlers.python_owsfeatures.FeatureEngine) .should_receive('load') .and_return(True)) @pytest.fixture def attachments_fixture(): """Attachments active fixture""" return [ { "file_key ": "L10303_216_some_file.txt", "label_type_id": "L10303", "label_type_id_period_id": "L10303_216", "bucket_name": "dev-statement-attachements", "file_name": "some_other_file.txt" }, { "file_key ": "L10303_216_some_file.txt", "label_type_id": "L10303", "label_type_id_period_id": "L10303_216", "bucket_name": "dev-statement-attachements", "file_name": "some_file.txt" } ] @pytest.fixture def items_active_fixture(): """An active object.""" return [ { 'id': 1, 'creator_id': 'oa:1234', 'status': 'active', 'description': '7123 is on hold', 'vendor_id': 7123 }, { 'id': 2, 'creator_id': 'oa:1234', 'status': 'active', 'description': '7124 is on hold', 'vendor_id': 7124 }, { 'id': 3, 'creator_id': 'oa:1235', 'status': 'active', 'description': '7125 is on hold', 'vendor_id': 7125 }, { 'id': 4, 'creator_id': 'oa:1236', 'status': 'active', 'description': '7126 is on hold', 'vendor_id': 7126 }, { 'id': 5, 'creator_id': 'oa:1234', 'status': 'active', 'description': '7127 is on hold', 'vendor_id': 7127 } ] @pytest.fixture def hold_active_fixture(): """An active hold object.""" return { 'id': 1, 'creator_id': 'oa:1234', 'status': 'active', 'description': '7123 is on hold', 'vendor_id': 7123 } @pytest.fixture def hold_active_fixture2(): """An active hold object.""" return { 'id': 3, 'creator_id': 'oa:542', 'status': 'active', 'description': '24905 is on hold', 'vendor_id': 24905 } @pytest.fixture def hold_inactive_fixture(): """An inactive hold object.""" return { 'id': 2, 'creator_id': 'oa:4567', 'status': 'inactive', 'description': '25824 is not on hold.', 'vendor_id': 25824 } @pytest.fixture def hold_logs_list_fixture(): """A list of hold log data.""" return [ { 'timestamp': datetime.strptime('21-05-2017', '%d-%m-%Y'), 'vendor_payment_hold_id': 1, 'action': 'create', 'user_id': 'oa:1234', 'description': '7123 is on hold', 'status': 'active' }, { 'timestamp': datetime.strptime('22-05-2017', '%d-%m-%Y'), 'vendor_payment_hold_id': 1, 'action': 'update', 'user_id': 'oa:1234', 'description': '7123 is on hold new', 'status': None } ] @pytest.fixture def payment_holds_db_fixture( hold_active_fixture, hold_inactive_fixture, hold_logs_list_fixture, hold_active_fixture2): """DB fixture for payment_holds.""" payment_holds_db_operations.create_tables() payment_holds_db_operations.seed_vendor_payment_hold( [hold_active_fixture, hold_inactive_fixture, hold_active_fixture2]) payment_holds_db_operations.seed_vendor_payment_hold_log( hold_logs_list_fixture) @pytest.fixture def fixture_accounting_intervals(): """Fixture for accounting intervals.""" intervals = [ { 'type': 'month', 'number': 9, 'year': 2016, 'currency_id': 1, 'periods': [222] }, { 'type': 'quarter', 'number': 2, 'year': 2016, 'currency_id': 2, 'periods': [210, 209, 208] }, { 'type': 'month', 'number': 3, 'year': 2016, 'currency_id': 1, 'periods': [207] } ] return response.Response(intervals) @pytest.fixture def fixture_monthly_accounting_periods_information(): """Fixture for monthly accounting period information""" return [ { 'first_period_id': 239, 'last_period_id': 239, 'month': 11, 'period_type': 'month', 'quarter': 4, 'year': '2018' }, { 'first_period_id': 240, 'last_period_id': 240, 'month': 12, 'period_type': 'month', 'quarter': 4, 'year': '2018' }, { 'first_period_id': 241, 'last_period_id': 241, 'month': 1, 'period_type': 'month', 'quarter': 1, 'year': '2019' }, { 'first_period_id': 242, 'last_period_id': 242, 'month': 2, 'period_type': 'month', 'quarter': 1, 'year': '2019' }, { 'first_period_id': 243, 'last_period_id': 243, 'month': 3, 'period_type': 'month', 'quarter': 1, 'year': '2019' }, { 'first_period_id': 244, 'last_period_id': 244, 'month': 4, 'period_type': 'month', 'quarter': 2, 'year': '2019' }, { 'first_period_id': 245, 'last_period_id': 245, 'month': 5, 'period_type': 'month', 'quarter': 2, 'year': '2019' } ] @pytest.fixture def fixture_quarterly_accounting_periods_information(): """Fixture for quarterly accounting period information""" return [ { 'first_period_id': 229, 'last_period_id': 231, 'month': None, 'period_type': 'quarter', 'quarter': 1, 'year': '2018' }, { 'first_period_id': 232, 'last_period_id': 234, 'month': None, 'period_type': 'quarter', 'quarter': 2, 'year': '2018' }, { 'first_period_id': 235, 'last_period_id': 237, 'month': None, 'period_type': 'quarter', 'quarter': 3, 'year': '2018' }, { 'first_period_id': 238, 'last_period_id': 240, 'month': None, 'period_type': 'quarter', 'quarter': 4, 'year': '2018' }, { 'first_period_id': 241, 'last_period_id': 243, 'month': None, 'period_type': 'quarter', 'quarter': 1, 'year': '2019' } ]