class BooleanMatcherTests(TestCase, MockUtilsMixin):
    def setUp(self):
        self._split_parser = SplitParser(object())
        matcher = {
            'matcherType': 'EQUAL_TO_BOOLEAN',
            'booleanMatcherData': True
        }
        split = {'conditions': [{'matcher': matcher}]}
        self._matcher = (self._split_parser._parse_matcher(split, matcher)
                         ._matcher.delegate)

    def test_matcher_construction(self):
        '''
        Tests that the correct matcher matcher is constructed.
        '''
        self.assertIsInstance(self._matcher, BooleanMatcher)

    def test_different_keys(self):
        '''
        Test how different types get parsed
        '''
        self.assertTrue(self._matcher.match(True))
        self.assertTrue(self._matcher.match('tRue'))
        self.assertFalse(self._matcher.match(False))
        self.assertFalse(self._matcher.match('False'))
        self.assertFalse(self._matcher.match(''))
        self.assertFalse(self._matcher.match({}))
class RegexMatcherTests(TestCase, MockUtilsMixin):
    def setUp(self):
        self._split_parser = SplitParser(object())
        matcher = {
            'matcherType': 'MATCHES_STRING',
            'stringMatcherData': '[a-z]'
        }
        split = {'conditions': [{'matcher': matcher}]}
        self._matcher = (self._split_parser._parse_matcher(split, matcher)
                         ._matcher.delegate)

    def test_matcher_construction(self):
        '''
        Tests that the correct matcher matcher is constructed.
        '''
        self.assertIsInstance(self._matcher, RegexMatcher)

    def test_regexes(self):
        '''
        Test different regexes lodeded from regex.txt
        '''
        current_path = os.path.dirname(__file__)
        with open(os.path.join(current_path, 'regex.txt')) as flo:
            lines = [line for line in flo]
        lines.pop()  # Remove empy last line
        for line in lines:
            regex, text, res = line.split('#')
            matcher = RegexMatcher(regex)
            print(regex, text, res)
            self.assertEquals(matcher.match(text), json.loads(res))
class DependencyMatcherTests(TestCase, MockUtilsMixin):
    def setUp(self):
        self._split_parser = SplitParser(object())
        matcher = {
            'matcherType': 'IN_SPLIT_TREATMENT',
            'dependencyMatcherData': {
                'split': 'someSplit',
                'treatments': ['on']
            }
        }
        split = {'conditions': [{'matcher': matcher}]}
        self._matcher = (self._split_parser._parse_matcher(split, matcher)
                         ._matcher.delegate)
        self._mock = self.patch('splitio.clients.MatcherClient')

    def test_matcher_construction(self):
        '''
        Tests that the correct matcher matcher is constructed.
        '''
        self.assertIsInstance(self._matcher, DependencyMatcher)

    def test_matcher_client_is_created_and_get_treatment_called(self):
        self._matcher.match('abc', None, self._mock)
        self._mock.get_treatment.assert_called_once_with('abc', 'someSplit', None)
        self.assertTrue(True)
class ContainsStringMatcherTests(TestCase, MockUtilsMixin):
    def setUp(self):
        self._split_parser = SplitParser(object())
        matcher = {
            'matcherType': 'CONTAINS_STRING',
            'whitelistMatcherData': {'whitelist': ['ABC', 'DEF', 'GHI']}
        }
        split = {'conditions': [{'matcher': matcher}]}
        self._matcher = (self._split_parser._parse_matcher(split, matcher)
                         ._matcher.delegate)

    def test_matcher_construction(self):
        '''
        Tests that the correct matcher matcher is constructed.
        '''
        self.assertIsInstance(self._matcher, ContainsStringMatcher)

    def test_keys_with_string(self):
        '''
        Test that keys starting with one of the prefixes in the condition match
        '''
        self.assertTrue(self._matcher.match('testABC'))
        self.assertTrue(self._matcher.match('testDEFabc'))
        self.assertTrue(self._matcher.match('GHI3214'))

    def test_keys_without_string_dont_match(self):
        '''
        Test that keys that dont start with one of the prefixes don't match.
        '''
        self.assertFalse(self._matcher.match('testJKL'))
        self.assertFalse(self._matcher.match('test123'))
        self.assertFalse(self._matcher.match('testdl_'))

    def test_empty_string_doesnt_match(self):
        '''
        Tests that the empty string doesn't match.
        '''
        self.assertFalse(self._matcher.match(''))

    def test_none_doesnt_match(self):
        '''
        Tests that None doesn't match.
        '''
        self.assertFalse(self._matcher.match(None))
class StartsWithMatcherTests(TestCase, MockUtilsMixin):
    def setUp(self):
        self._split_parser = SplitParser(object())
        matcher = {
            'matcherType': 'STARTS_WITH',
            'whitelistMatcherData': {'whitelist': ['ABC', 'DEF', 'GHI']}
        }
        split = {'conditions': [{'matcher': matcher}]}
        self._matcher = (self._split_parser._parse_matcher(split, matcher)
                         ._matcher.delegate)

    def test_matcher_construction(self):
        '''
        Tests that the correct matcher matcher is constructed.
        '''
        self.assertIsInstance(self._matcher, StartsWithMatcher)

    def test_keys_with_prefix_match(self):
        '''
        Test that keys starting with one of the prefixes in the condition match
        '''
        self.assertTrue(self._matcher.match('ABCtest'))
        self.assertTrue(self._matcher.match('DEFtest'))
        self.assertTrue(self._matcher.match('GHItest'))

    def test_keys_without_prefix_dont_match(self):
        '''
        Test that keys that dont start with one of the prefixes don't match.
        '''
        self.assertFalse(self._matcher.match('JKLtest'))
        self.assertFalse(self._matcher.match('123test'))
        self.assertFalse(self._matcher.match('dl_test'))

    def test_empty_string_doesnt_match(self):
        '''
        Tests that the empty string doesn't match.
        '''
        self.assertFalse(self._matcher.match(''))

    def test_none_doesnt_match(self):
        '''
        Tests that None doesn't match.
        '''
        self.assertFalse(self._matcher.match(None))
class PartOfSetMatcherTests(TestCase, MockUtilsMixin):
    def setUp(self):
        self._split_parser = SplitParser(object())
        matcher = {
            'matcherType': 'PART_OF_SET',
            'whitelistMatcherData': {'whitelist': ['ABC', 'DEF', 'GHI']}
        }
        split = {'conditions': [{'matcher': matcher}]}
        self._matcher = (self._split_parser._parse_matcher(split, matcher)
                         ._matcher.delegate)

    def test_matcher_construction(self):
        '''
        Tests that the correct matcher matcher is constructed.
        '''
        self.assertIsInstance(self._matcher, PartOfSetMatcher)

    def test_subset_of_set_matches(self):
        '''
        Test that a subset of the set matches
        '''
        self.assertTrue(self._matcher.match(['ABC', 'DEF', 'GHI']))
        self.assertTrue(self._matcher.match(['ABC']))

    def test_not_subset_of_set_doesnt_match(self):
        '''
        Test that any set with elements that are not in the split's set doesn't
        match
        '''
        self.assertFalse(self._matcher.match(['ABC', 'DEF', 'GHI', 'AWE']))
        self.assertFalse(self._matcher.match(['RFV']))

    def test_empty_set_doesnt_match(self):
        '''
        Tests that an empty set doesn't match
        '''
        self.assertFalse(self._matcher.match([]))

    def test_none_doesnt_match(self):
        '''
        Tests that None doesn't match.
        '''
        self.assertFalse(self._matcher.match(None))
class ContainsAnyOfSetMatcherTests(TestCase, MockUtilsMixin):
    def setUp(self):
        self._split_parser = SplitParser(object())
        matcher = {
            'matcherType': 'CONTAINS_ANY_OF_SET',
            'whitelistMatcherData': {'whitelist': ['ABC', 'DEF', 'GHI']}
        }
        split = {'conditions': [{'matcher': matcher}]}
        self._matcher = (self._split_parser._parse_matcher(split, matcher)
                         ._matcher.delegate)

    def test_matcher_construction(self):
        '''
        Tests that the correct matcher matcher is constructed.
        '''
        self.assertIsInstance(self._matcher, ContainsAnyOfSetMatcher)

    def test_set_with_at_least_one_key(self):
        '''
        Test that a set with at least one key matches
        '''
        self.assertTrue(self._matcher.match(['ABC', 'DEF', 'GHI']))
        self.assertTrue(self._matcher.match(['ABC', 'DEF', 'GHI', 'AWE']))
        self.assertTrue(self._matcher.match(['ABC', 'DEF']))

    def test_set_without_any_key_doesnt_match(self):
        '''
        Test that a set without any the keys doesn't match
        '''
        self.assertFalse(self._matcher.match(['AWE']))

    def test_empty_set_doesnt_match(self):
        '''
        Tests that an empty set doesn't match
        '''
        self.assertFalse(self._matcher.match([]))

    def test_none_doesnt_match(self):
        '''
        Tests that None doesn't match.
        '''
        self.assertFalse(self._matcher.match(None))
class EqualToSetMatcherTests(TestCase, MockUtilsMixin):
    def setUp(self):
        self._split_parser = SplitParser(object())
        matcher = {
            'matcherType': 'EQUAL_TO_SET',
            'whitelistMatcherData': {'whitelist': ['ABC', 'DEF', 'GHI']}
        }
        split = {'conditions': [{'matcher': matcher}]}
        self._matcher = (self._split_parser._parse_matcher(split, matcher)
                         ._matcher.delegate)

    def test_matcher_construction(self):
        '''
        Tests that the correct matcher matcher is constructed.
        '''
        self.assertIsInstance(self._matcher, EqualToSetMatcher)

    def test_equal_set_matches(self):
        '''
        Test that the exact same set matches
        '''
        self.assertTrue(self._matcher.match(['ABC', 'DEF', 'GHI']))

    def test_different_set_doesnt_match(self):
        '''
        Test that a different set doesn't match
        '''
        self.assertFalse(self._matcher.match(['ABC', 'DEF', 'GHI', 'AWE']))
        self.assertFalse(self._matcher.match(['ABC']))

    def test_empty_set_doesnt_match(self):
        '''
        Tests that an empty set doesn't match
        '''
        self.assertFalse(self._matcher.match([]))

    def test_none_doesnt_match(self):
        '''
        Tests that None doesn't match.
        '''
        self.assertFalse(self._matcher.match(None))
示例#9
0
class SplitParserParseMatcherTests(TestCase, MockUtilsMixin):
    def setUp(self):
        self.some_partial_split = mock.MagicMock()
        self.some_segment_fetcher = mock.MagicMock()
        self.some_matcher = mock.MagicMock()

        self.parser = SplitParser(self.some_segment_fetcher)

        self.parse_matcher_all_keys_mock = self.patch_object(
            self.parser, '_parse_matcher_all_keys')
        self.parse_matcher_in_segment_mock = self.patch_object(
            self.parser, '_parse_matcher_in_segment')
        self.parse_matcher_whitelist_mock = self.patch_object(
            self.parser, '_parse_matcher_whitelist')
        self.parse_matcher_equal_to_mock = self.patch_object(
            self.parser, '_parse_matcher_equal_to')
        self.parse_matcher_greater_than_or_equal_to_mock = self.patch_object(
            self.parser, '_parse_matcher_greater_than_or_equal_to')
        self.parse_matcher_less_than_or_equal_to_mock = self.patch_object(
            self.parser, '_parse_matcher_less_than_or_equal_to')
        self.parse_matcher_between_mock = self.patch_object(
            self.parser, '_parse_matcher_between')

        self.parser._parse_matcher_fake = mock.MagicMock()

    def _get_matcher(self, matcher_type):
        return {
            'matcherType': matcher_type,
            'negate': mock.MagicMock(),
            'keySelector': {
                'attribute': mock.MagicMock()
            }
        }

    def test_calls_parse_matcher_all_keys(self):
        """Test that _parse_matcher calls _parse_matcher_all_keys on ALL_KEYS matcher"""
        matcher = self._get_matcher('ALL_KEYS')
        self.parser._parse_matcher(self.some_partial_split, matcher)
        self.parse_matcher_all_keys_mock.assert_called_once_with(
            self.some_partial_split, matcher, block_until_ready=False)

    def test_calls_parse_matcher_in_segment(self):
        """Test that _parse_matcher calls _parse_matcher_in_segment on IN_SEGMENT matcher"""
        matcher = self._get_matcher('IN_SEGMENT')
        self.parser._parse_matcher(self.some_partial_split, matcher)
        self.parse_matcher_in_segment_mock.assert_called_once_with(
            self.some_partial_split, matcher, block_until_ready=False)

    def test_calls_parse_matcher_whitelist(self):
        """Test that _parse_matcher calls _parse_matcher_in_segment on WHITELIST matcher"""
        matcher = self._get_matcher('WHITELIST')
        self.parser._parse_matcher(self.some_partial_split, matcher)
        self.parse_matcher_whitelist_mock.assert_called_once_with(
            self.some_partial_split, matcher, block_until_ready=False)

    def test_calls_parse_matcher_equal_to(self):
        """Test that _parse_matcher calls _parse_matcher_equal_to on EQUAL_TO matcher"""
        matcher = self._get_matcher('EQUAL_TO')
        self.parser._parse_matcher(self.some_partial_split, matcher)
        self.parse_matcher_equal_to_mock.assert_called_once_with(
            self.some_partial_split, matcher, block_until_ready=False)

    def test_calls_parse_matcher_greater_than_or_equal_to(self):
        """
        Test that _parse_matcher calls _parse_matcher_greater_than_or_equal_to on
        GREATER_THAN_OR_EQUAL_TO matcher
        """
        matcher = self._get_matcher('GREATER_THAN_OR_EQUAL_TO')
        self.parser._parse_matcher(self.some_partial_split, matcher)
        self.parse_matcher_greater_than_or_equal_to_mock.assert_called_once_with(
            self.some_partial_split, matcher, block_until_ready=False)

    def test_calls_parse_matcher_less_than_or_equal_to(self):
        """
        Test that _parse_matcher calls _parse_matcher_less_than_or_equal_to on
        LESS_THAN_OR_EQUAL_TO matcher
        """
        matcher = self._get_matcher('LESS_THAN_OR_EQUAL_TO')
        self.parser._parse_matcher(self.some_partial_split, matcher)
        self.parse_matcher_less_than_or_equal_to_mock.assert_called_once_with(
            self.some_partial_split, matcher, block_until_ready=False)

    def test_calls_parse_matcher_between(self):
        """Test that _parse_matcher calls _parse_between on BETWEEN matcher"""
        matcher = self._get_matcher('BETWEEN')
        self.parser._parse_matcher(self.some_partial_split, matcher)
        self.parse_matcher_between_mock.assert_called_once_with(
            self.some_partial_split, matcher, block_until_ready=False)

    def test_raises_exception_if_parse_method_returns_none(self):
        """
        Tests that _parse_matcher raises an exception if the specific parse method returns None
        """
        self.parser._parse_matcher_fake.return_value = None
        with self.assertRaises(ValueError):
            self.parser._parse_matcher(self.some_partial_split,
                                       self._get_matcher('FAKE'))

    def test_returns_attribute_matcher(self):
        """Tests that _parse_matcher returns an AttributeMatcher"""
        self.assertIsInstance(
            self.parser._parse_matcher(self.some_partial_split,
                                       self._get_matcher('FAKE')),
            AttributeMatcher)