summaryrefslogtreecommitdiff
path: root/unittests
diff options
context:
space:
mode:
authorDylan Baker <dylan@pnwbakers.com>2016-10-14 15:59:00 -0700
committerDylan Baker <dylan@pnwbakers.com>2016-11-10 10:51:02 -0800
commite92555a647f50b0014198cc91defe0062a23383d (patch)
tree997ca5d4b9acc8630cebcff3f1da2c57b3f01ad1 /unittests
parent3a0192faab0afaab24bafef795fbf6b65dafb36a (diff)
framework: Pull {include,exclude}_filter out of Options
Since these are also just special cases of filters for the standard TestProfile filtering mechanism, and they have a lot of unique classes. This is just a waste, the same can be achieved with a much simpler class structure. Signed-off-by: Dylan Baker <dylanx.c.baker@intel.com>
Diffstat (limited to 'unittests')
-rw-r--r--unittests/framework/test_options.py178
-rw-r--r--unittests/framework/test_profile.py126
2 files changed, 40 insertions, 264 deletions
diff --git a/unittests/framework/test_options.py b/unittests/framework/test_options.py
index 65ff9466e..bf296c10c 100644
--- a/unittests/framework/test_options.py
+++ b/unittests/framework/test_options.py
@@ -23,9 +23,6 @@
from __future__ import (
absolute_import, division, print_function, unicode_literals
)
-import re
-
-import pytest
from framework import options
@@ -33,180 +30,6 @@ from framework import options
# pylint: disable=invalid-name
# pylint: disable=no-self-use
-_RETYPE = type(re.compile(''))
-
-
-def test_ReList_iterable_argument():
- """options._ReList: handles an iterable argument correctly"""
- test = options._ReList(['foo'])
- assert isinstance(test[0], _RETYPE)
-
-
-class TestReList(object):
- """Tests for the ReList class.
-
- These particular tests don't mutate the state of ReList, and thus can be
- run with the same instance over and over, other tests that do mutate the
- state need a per test ReList instance.
-
- """
- @classmethod
- def setup_class(cls):
- cls.test = options._ReList(['foo'])
-
- def test_eq(self):
- """Test options._ReList.__eq__."""
- test1 = ['foo']
- test2 = options._ReList(['foo'])
-
- with pytest.raises(TypeError):
- assert self.test == test1
-
- assert self.test == test2
-
- def test_ne(self):
- """Test hoptions._ReList.__ne__."""
- test1 = ['bar']
- test2 = options._ReList(['bar'])
-
- with pytest.raises(TypeError):
- assert self.test != test1
-
- assert self.test != test2
-
- def test_getitem(self):
- """options._ReList.__getitem__: returns expected value."""
- assert isinstance(self.test[0], _RETYPE)
-
- def test_flags(self):
- """options._ReList.__getitem__: sets flags correctly."""
- assert self.test[0].flags & re.IGNORECASE != 0
-
- def test_len(self):
- """options._ReList.len: returns expected values."""
- assert len(self.test) == 1
-
- def test_to_json(self):
- """options._ReList.to_json: returns expected values."""
- assert self.test.to_json() == ['foo']
-
-
-class TestReListMutate(object):
- """Tests for ReList that mutate state."""
- test = None
-
- def setup(self):
- self.test = options._ReList(['foo'])
-
- def test_relist_insert(self):
- """options._ReList.len: inserts value as expected"""
- obj = re.compile('bar', re.IGNORECASE)
-
- self.test.insert(0, obj)
-
- assert self.test[0] == obj
-
- def test_relist_delitem(self):
- """options._ReList.len: removes value as expected"""
- del self.test[0]
-
- assert len(self.test) == 0
-
- def test_relist_setitem(self):
- """options._ReList.__setitem__: replaces values"""
- sentinel = re.compile('bar')
- self.test[0] = sentinel
-
- # The pattern must be tested because the flags on the re object might
- # require it to be recompiled, thus they might not be the same object,
- # or even be equal according to python (though they are for the
- # purposes of this test)
- assert self.test[0].pattern == sentinel.pattern
-
-
-class TestReListDescriptor(object):
- """Test the ReListDescriptor class.
-
- Since this class is a descriptor it needs to be attached to an object at
- the class level.
-
- """
- test = None
-
- @classmethod
- def setup_class(cls):
- """Create a test object."""
- class _Test(object):
- desc = options._ReListDescriptor('test_desc')
- notexists = options._ReListDescriptor('test_notexists')
-
- def __init__(self):
- self.test_desc = options._ReList()
-
- cls._test = _Test
-
- def setup(self):
- self.test = self._test()
-
- def test_get_exists(self):
- """options._ReListDescriptor.__get__: Returns value if it exists."""
- assert self.test.desc == self.test.test_desc
-
- def test_get_not_exists(self):
- """options._ReListDescriptor.__get__: Returns new _ReList if it doesn't
- exists."""
- assert self.test.notexists == self.test.test_notexists # pylint: disable=no-member
-
- def test_get_not_exists_fail(self, mocker):
- """options._ReListDescriptor.__get__: Raises AttributError if name
- doesn't exist and can't be created."""
- mocker.patch('framework.options.setattr',
- mocker.Mock(side_effect=Exception),
- create=True)
-
- with pytest.raises(AttributeError):
- self.test.notexists # pylint: disable=pointless-statement
-
- def test_set_relist(self):
- """options._ReListDescriptor.__set__: assigns an ReList without
- copying."""
- val = options._ReList(['foo'])
- self.test.desc = val
- assert self.test.desc is val
-
- def test_set_other(self):
- """options._ReListDescriptor.__set__: converts other types to ReList"""
- val = options._ReList(['foo'])
- self.test.desc = ['foo']
- assert self.test.desc == val
-
- def test_delete(self):
- """options._ReListDescriptor.__delete___: raises NotImplementedError"""
- with pytest.raises(NotImplementedError):
- del self.test.desc
-
-
-class TestFilterReList(object):
- """Tests for FilterReList.
-
- provides a unique instance per test, which protects against state mutation.
-
- """
- test = None
-
- def setup(self):
- self.test = options._FilterReList(['foo'])
-
- def test_setitem(self):
- """options._FilterReList.__setitem__: replaces '/' with '.'."""
- self.test[0] = 'foo/bar'
- assert self.test[0].pattern == 'foo.bar'
-
- def test_filterrelist_insert(self):
- """options._FilterReList.insert: replaces '/' with '.'."""
- self.test.insert(0, 'foo/bar')
- assert self.test[0].pattern == 'foo.bar'
-
def test_options_clear():
"""options.Options.clear(): resests options values to init state."""
@@ -215,7 +38,6 @@ def test_options_clear():
test = options._Options()
test.execute = False
test.sync = True
- test.exclude_filter.append('foo')
test.clear()
assert list(iter(baseline)) == list(iter(test))
diff --git a/unittests/framework/test_profile.py b/unittests/framework/test_profile.py
index f2aa5b5f8..ea4ee7063 100644
--- a/unittests/framework/test_profile.py
+++ b/unittests/framework/test_profile.py
@@ -23,11 +23,6 @@
from __future__ import (
absolute_import, division, print_function, unicode_literals
)
-import copy
-try:
- from unittest import mock
-except ImportError:
- import mock
import pytest
import six
@@ -35,7 +30,6 @@ import six
from framework import dmesg
from framework import exceptions
from framework import grouptools
-from framework import options
from framework import profile
from framework.test.gleantest import GleanTest
from . import utils
@@ -101,86 +95,6 @@ class TestTestProfile(object):
profile_.dmesg = False
assert isinstance(profile_.dmesg, dmesg.DummyDmesg)
- class TestPrepareTestList(object):
- """Create tests for TestProfile.prepare_test_list filtering."""
-
- @classmethod
- def setup_class(cls):
- cls.opts = None
- cls.data = None
- cls.__patcher = mock.patch('framework.profile.options.OPTIONS',
- new_callable=options._Options)
-
- def setup(self):
- """Setup each test."""
- self.data = profile.TestDict()
- self.data[grouptools.join('group1', 'test1')] = \
- utils.Test(['thingy'])
- self.data[grouptools.join('group1', 'group3', 'test2')] = \
- utils.Test(['thing'])
- self.data[grouptools.join('group3', 'test5')] = \
- utils.Test(['other'])
- self.data[grouptools.join('group4', 'Test9')] = \
- utils.Test(['is_caps'])
- self.opts = self.__patcher.start()
-
- def teardown(self):
- self.__patcher.stop()
-
- def test_matches_filter_mar_1(self):
- """profile.TestProfile.prepare_test_list: 'not env.filter or
- matches_any_regex()' env.filter is False.
-
- Nothing should be filtered.
- """
- profile_ = profile.TestProfile()
- profile_.test_list = self.data
- profile_.prepare_test_list()
-
- assert dict(profile_.test_list) == dict(self.data)
-
- def test_matches_filter_mar_2(self):
- """profile.TestProfile.prepare_test_list: 'not env.filter or
- matches_any_regex()' mar is False.
- """
- self.opts.include_filter = ['test5']
-
- profile_ = profile.TestProfile()
- profile_.test_list = self.data
- profile_.prepare_test_list()
-
- baseline = {
- grouptools.join('group3', 'test5'): utils.Test(['other'])}
-
- assert dict(profile_.test_list) == baseline
-
- def test_matches_exclude_mar(self):
- """profile.TestProfile.prepare_test_list: 'not
- matches_any_regexp()'.
- """
- self.opts.exclude_filter = ['test5']
-
- baseline = copy.deepcopy(self.data)
- del baseline[grouptools.join('group3', 'test5')]
-
- profile_ = profile.TestProfile()
- profile_.test_list = self.data
- profile_.prepare_test_list()
-
- assert dict(profile_.test_list) == dict(baseline)
-
- def test_matches_include_caps(self):
- """profile.TestProfile.prepare_test_list: matches capitalized
- tests.
- """
- self.opts.exclude_filter = ['test9']
-
- profile_ = profile.TestProfile()
- profile_.test_list = self.data
- profile_.prepare_test_list()
-
- assert grouptools.join('group4', 'Test9') not in profile_.test_list
-
class TestGroupManager(object):
"""Tests for TestProfile.group_manager."""
@@ -386,3 +300,43 @@ class TestTestDict(object):
test['a'] = utils.Test(['bar'])
assert test['a'].command == ['bar']
+
+
+class TestRegexFilter(object):
+ """Tests for the RegexFilter class."""
+
+ class TestNormal(object):
+ """Tests for inverse set to False (default)."""
+
+ def test_empty(self):
+ """Returns True when no filters are provided."""
+ test = profile.RegexFilter([])
+ assert test('foobob', None)
+
+ def test_matches(self):
+ """Returns True when the test matches any regex."""
+ test = profile.RegexFilter([r'foo', r'bar'])
+ assert test('foobob', None)
+
+ def test_not_matches(self):
+ """Returns True when the test matches any regex."""
+ test = profile.RegexFilter([r'fob', r'bar'])
+ assert not test('foobob', None)
+
+ class TestInverse(object):
+ """Tests for inverse set to True."""
+
+ def test_empty(self):
+ """Returns True when no filters are provided."""
+ test = profile.RegexFilter([], inverse=True)
+ assert test('foobob', None)
+
+ def test_matches(self):
+ """Returns False when the test matches any regex."""
+ test = profile.RegexFilter([r'foo', r'bar'], inverse=True)
+ assert not test('foobob', None)
+
+ def test_not_matches(self):
+ """Returns False when the test matches any regex."""
+ test = profile.RegexFilter([r'fob', r'bar'], inverse=True)
+ assert test('foobob', None)