"""Test for split_rules module.""" from datetime import date from unittest.mock import Mock import pytest from flows.cable_calculation import splits from flows.cable_calculation import stores @pytest.fixture def fixture_stores(): """Sample store list.""" return [ splits.Store(1, 'foo', [11, 22]), splits.Store(2, 'bar', [33, 44]) ] @pytest.fixture def fixture_rules(): """Sample rule list.""" return [ splits.SplitRule( 'bar', 611, date(2010, 9, 2), lambda x: x), splits.SplitRule( 'foo', 611, date(2016, 10, 2), lambda x: x), splits.SplitRule( 'far', 554, date(2015, 11, 15), lambda x: x), splits.SplitRule( 'roo', 554, date(2015, 3, 25), lambda x: x), splits.SplitRule( 'boo', 611, date(2015, 3, 25), lambda x: x) ] @pytest.fixture def fixture_store_sorted_rules(): """Sorted rule list.""" return [ splits.SplitRule( 'foo', 611, date(2016, 10, 2), lambda x: x), splits.SplitRule( 'boo', 611, date(2015, 3, 25), lambda x: x), splits.SplitRule( 'bar', 611, date(2010, 9, 2), lambda x: x) ] def test_get_store_by_provider(monkeypatch, fixture_stores): """Test store lookup with existing providers.""" monkeypatch.setattr(stores, 'Stores', fixture_stores) for store in fixture_stores: for provider_id in store.provider_ids: assert stores.get_store_by_provider(provider_id) is store def test_get_store_by_provider_unexisting(monkeypatch, fixture_stores): """Test store lookup with unexisting providers.""" monkeypatch.setattr(stores, 'Stores', fixture_stores) assert stores.get_store_by_provider(100000) is None def test_get_sorted_store_rules( monkeypatch, fixture_rules, fixture_store_sorted_rules): """Test that we get rules for a given store sorted by most recent.""" monkeypatch.setattr(stores, 'Rules', fixture_rules) expected = fixture_store_sorted_rules actuals = stores.get_sorted_store_rules(611) # Assertions for i in range(0, len(expected)): # We only care about store_id and start date assert actuals[i].store_id == expected[i].store_id assert actuals[i].start == expected[i].start def test_get_split_rule(monkeypatch, fixture_store_sorted_rules): """Test that we get the rule for a given store and a given date.""" store_id = 611 # Mock get_sorted_store_rules_mock = Mock(return_value=fixture_store_sorted_rules) monkeypatch.setattr( stores, 'get_sorted_store_rules', get_sorted_store_rules_mock) actual_rule = stores.get_split_rule(date(2015, 6, 2), store_id) # Assertions get_sorted_store_rules_mock.assert_called_with(store_id) # We only care about store_id and start date assert actual_rule.store_id == store_id assert actual_rule.start == date(2015, 3, 25) def test_get_split_rule_undefined_date( monkeypatch, fixture_store_sorted_rules): """Test that we get None if date does not match rule date ranges.""" store_id = 611 # Mock get_sorted_store_rules_mock = Mock(return_value=fixture_store_sorted_rules) monkeypatch.setattr( stores, 'get_sorted_store_rules', get_sorted_store_rules_mock) actual_rule = stores.get_split_rule(date(1999, 1, 1), store_id) # Assertions get_sorted_store_rules_mock.assert_called_with(store_id) assert actual_rule is None def test_get_split_rule_undefined_store(monkeypatch): """Test that we get None if there are no rules defined for the store.""" store_id = 10000 # Mock get_sorted_store_rules_mock = Mock(return_value=[]) monkeypatch.setattr( stores, 'get_sorted_store_rules', get_sorted_store_rules_mock) actual_rule = stores.get_split_rule(date(2015, 3, 25), store_id) # Assertions get_sorted_store_rules_mock.assert_called_with(store_id) assert actual_rule is None