wrappers.py revision 12882
112882Sspwilson2@wisc.edu# Copyright (c) 2017 Mark D. Hill and David A. Wood
212882Sspwilson2@wisc.edu# All rights reserved.
312882Sspwilson2@wisc.edu#
412882Sspwilson2@wisc.edu# Redistribution and use in source and binary forms, with or without
512882Sspwilson2@wisc.edu# modification, are permitted provided that the following conditions are
612882Sspwilson2@wisc.edu# met: redistributions of source code must retain the above copyright
712882Sspwilson2@wisc.edu# notice, this list of conditions and the following disclaimer;
812882Sspwilson2@wisc.edu# redistributions in binary form must reproduce the above copyright
912882Sspwilson2@wisc.edu# notice, this list of conditions and the following disclaimer in the
1012882Sspwilson2@wisc.edu# documentation and/or other materials provided with the distribution;
1112882Sspwilson2@wisc.edu# neither the name of the copyright holders nor the names of its
1212882Sspwilson2@wisc.edu# contributors may be used to endorse or promote products derived from
1312882Sspwilson2@wisc.edu# this software without specific prior written permission.
1412882Sspwilson2@wisc.edu#
1512882Sspwilson2@wisc.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1612882Sspwilson2@wisc.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1712882Sspwilson2@wisc.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1812882Sspwilson2@wisc.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1912882Sspwilson2@wisc.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2012882Sspwilson2@wisc.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2112882Sspwilson2@wisc.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2212882Sspwilson2@wisc.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2312882Sspwilson2@wisc.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2412882Sspwilson2@wisc.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2512882Sspwilson2@wisc.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2612882Sspwilson2@wisc.edu#
2712882Sspwilson2@wisc.edu# Authors: Sean Wilson
2812882Sspwilson2@wisc.edu
2912882Sspwilson2@wisc.edu'''
3012882Sspwilson2@wisc.eduModule contains wrappers for test items that have been
3112882Sspwilson2@wisc.eduloaded by the testlib :class:`testlib.loader.Loader`.
3212882Sspwilson2@wisc.edu'''
3312882Sspwilson2@wisc.eduimport itertools
3412882Sspwilson2@wisc.edu
3512882Sspwilson2@wisc.eduimport log
3612882Sspwilson2@wisc.eduimport uid
3712882Sspwilson2@wisc.edufrom state import Status, Result
3812882Sspwilson2@wisc.edu
3912882Sspwilson2@wisc.educlass TestCaseMetadata():
4012882Sspwilson2@wisc.edu    def __init__(self, name, uid, path, result, status, suite_uid):
4112882Sspwilson2@wisc.edu        self.name = name
4212882Sspwilson2@wisc.edu        self.uid = uid
4312882Sspwilson2@wisc.edu        self.path = path
4412882Sspwilson2@wisc.edu        self.status = status
4512882Sspwilson2@wisc.edu        self.result = result
4612882Sspwilson2@wisc.edu        self.suite_uid = suite_uid
4712882Sspwilson2@wisc.edu
4812882Sspwilson2@wisc.edu
4912882Sspwilson2@wisc.educlass TestSuiteMetadata():
5012882Sspwilson2@wisc.edu    def __init__(self, name, uid, tags, path, status, result):
5112882Sspwilson2@wisc.edu        self.name = name
5212882Sspwilson2@wisc.edu        self.uid = uid
5312882Sspwilson2@wisc.edu        self.tags = tags
5412882Sspwilson2@wisc.edu        self.path = path
5512882Sspwilson2@wisc.edu        self.status = status
5612882Sspwilson2@wisc.edu        self.result = result
5712882Sspwilson2@wisc.edu
5812882Sspwilson2@wisc.edu
5912882Sspwilson2@wisc.educlass LibraryMetadata():
6012882Sspwilson2@wisc.edu    def __init__(self, name, result, status):
6112882Sspwilson2@wisc.edu        self.name = name
6212882Sspwilson2@wisc.edu        self.result = result
6312882Sspwilson2@wisc.edu        self.status = status
6412882Sspwilson2@wisc.edu
6512882Sspwilson2@wisc.edu
6612882Sspwilson2@wisc.educlass LoadedTestable(object):
6712882Sspwilson2@wisc.edu    '''
6812882Sspwilson2@wisc.edu    Base class for loaded test items.
6912882Sspwilson2@wisc.edu
7012882Sspwilson2@wisc.edu    :property:`result` and :property:`status` setters
7112882Sspwilson2@wisc.edu    notify testlib via the :func:`log_result` and :func:`log_status`
7212882Sspwilson2@wisc.edu    of the updated status.
7312882Sspwilson2@wisc.edu    '''
7412882Sspwilson2@wisc.edu    def __init__(self, obj):
7512882Sspwilson2@wisc.edu        self.obj = obj
7612882Sspwilson2@wisc.edu        self.metadata = self._generate_metadata()
7712882Sspwilson2@wisc.edu
7812882Sspwilson2@wisc.edu    @property
7912882Sspwilson2@wisc.edu    def status(self):
8012882Sspwilson2@wisc.edu        return self.metadata.status
8112882Sspwilson2@wisc.edu
8212882Sspwilson2@wisc.edu    @status.setter
8312882Sspwilson2@wisc.edu    def status(self, status):
8412882Sspwilson2@wisc.edu        self.log_status(status)
8512882Sspwilson2@wisc.edu        self.metadata.status = status
8612882Sspwilson2@wisc.edu
8712882Sspwilson2@wisc.edu    @property
8812882Sspwilson2@wisc.edu    def result(self):
8912882Sspwilson2@wisc.edu        return self.metadata.result
9012882Sspwilson2@wisc.edu
9112882Sspwilson2@wisc.edu    @result.setter
9212882Sspwilson2@wisc.edu    def result(self, result):
9312882Sspwilson2@wisc.edu        self.log_result(result)
9412882Sspwilson2@wisc.edu        self.metadata.result = result
9512882Sspwilson2@wisc.edu
9612882Sspwilson2@wisc.edu    @property
9712882Sspwilson2@wisc.edu    def uid(self):
9812882Sspwilson2@wisc.edu        return self.metadata.uid
9912882Sspwilson2@wisc.edu
10012882Sspwilson2@wisc.edu    @property
10112882Sspwilson2@wisc.edu    def name(self):
10212882Sspwilson2@wisc.edu        return self.metadata.name
10312882Sspwilson2@wisc.edu
10412882Sspwilson2@wisc.edu    @property
10512882Sspwilson2@wisc.edu    def fixtures(self):
10612882Sspwilson2@wisc.edu        return self.obj.fixtures
10712882Sspwilson2@wisc.edu
10812882Sspwilson2@wisc.edu    @fixtures.setter
10912882Sspwilson2@wisc.edu    def fixtures(self, fixtures):
11012882Sspwilson2@wisc.edu        self.obj.fixtures = fixtures
11112882Sspwilson2@wisc.edu
11212882Sspwilson2@wisc.edu    @property
11312882Sspwilson2@wisc.edu    def runner(self):
11412882Sspwilson2@wisc.edu        return self.obj.runner
11512882Sspwilson2@wisc.edu
11612882Sspwilson2@wisc.edu    # TODO Change log to provide status_update, result_update for all types.
11712882Sspwilson2@wisc.edu    def log_status(self, status):
11812882Sspwilson2@wisc.edu        log.test_log.status_update(self, status)
11912882Sspwilson2@wisc.edu
12012882Sspwilson2@wisc.edu    def log_result(self, result):
12112882Sspwilson2@wisc.edu        log.test_log.result_update(self, result)
12212882Sspwilson2@wisc.edu
12312882Sspwilson2@wisc.edu    def __iter__(self):
12412882Sspwilson2@wisc.edu        return iter(())
12512882Sspwilson2@wisc.edu
12612882Sspwilson2@wisc.edu
12712882Sspwilson2@wisc.educlass LoadedTest(LoadedTestable):
12812882Sspwilson2@wisc.edu    def __init__(self, test_obj, loaded_suite, path):
12912882Sspwilson2@wisc.edu        self.parent_suite = loaded_suite
13012882Sspwilson2@wisc.edu        self._path = path
13112882Sspwilson2@wisc.edu        LoadedTestable.__init__(self, test_obj)
13212882Sspwilson2@wisc.edu
13312882Sspwilson2@wisc.edu    def test(self, *args, **kwargs):
13412882Sspwilson2@wisc.edu        self.obj.test(*args, **kwargs)
13512882Sspwilson2@wisc.edu
13612882Sspwilson2@wisc.edu    def _generate_metadata(self):
13712882Sspwilson2@wisc.edu        return TestCaseMetadata( **{
13812882Sspwilson2@wisc.edu            'name':self.obj.name,
13912882Sspwilson2@wisc.edu            'path': self._path,
14012882Sspwilson2@wisc.edu            'uid': uid.TestUID(self._path,
14112882Sspwilson2@wisc.edu                               self.obj.name,
14212882Sspwilson2@wisc.edu                               self.parent_suite.name),
14312882Sspwilson2@wisc.edu            'status': Status.Unscheduled,
14412882Sspwilson2@wisc.edu            'result': Result(Result.NotRun),
14512882Sspwilson2@wisc.edu            'suite_uid': self.parent_suite.metadata.uid
14612882Sspwilson2@wisc.edu        })
14712882Sspwilson2@wisc.edu
14812882Sspwilson2@wisc.edu
14912882Sspwilson2@wisc.educlass LoadedSuite(LoadedTestable):
15012882Sspwilson2@wisc.edu    def __init__(self, suite_obj, path):
15112882Sspwilson2@wisc.edu        self._path = path
15212882Sspwilson2@wisc.edu        LoadedTestable.__init__(self, suite_obj)
15312882Sspwilson2@wisc.edu        self.tests = self._wrap_children(suite_obj)
15412882Sspwilson2@wisc.edu
15512882Sspwilson2@wisc.edu    def _wrap_children(self, suite_obj):
15612882Sspwilson2@wisc.edu        return [LoadedTest(test, self, self.metadata.path)
15712882Sspwilson2@wisc.edu                for test in suite_obj]
15812882Sspwilson2@wisc.edu
15912882Sspwilson2@wisc.edu    def _generate_metadata(self):
16012882Sspwilson2@wisc.edu        return TestSuiteMetadata( **{
16112882Sspwilson2@wisc.edu            'name': self.obj.name,
16212882Sspwilson2@wisc.edu            'tags':self.obj.tags,
16312882Sspwilson2@wisc.edu            'path': self._path,
16412882Sspwilson2@wisc.edu            'uid': uid.SuiteUID(self._path, self.obj.name),
16512882Sspwilson2@wisc.edu            'status': Status.Unscheduled,
16612882Sspwilson2@wisc.edu            'result': Result(Result.NotRun)
16712882Sspwilson2@wisc.edu        })
16812882Sspwilson2@wisc.edu
16912882Sspwilson2@wisc.edu    def __iter__(self):
17012882Sspwilson2@wisc.edu        return iter(self.tests)
17112882Sspwilson2@wisc.edu
17212882Sspwilson2@wisc.edu    @property
17312882Sspwilson2@wisc.edu    def tags(self):
17412882Sspwilson2@wisc.edu        return self.metadata.tags
17512882Sspwilson2@wisc.edu
17612882Sspwilson2@wisc.edu
17712882Sspwilson2@wisc.educlass LoadedLibrary(LoadedTestable):
17812882Sspwilson2@wisc.edu    '''
17912882Sspwilson2@wisc.edu    Wraps a collection of all loaded test suites and
18012882Sspwilson2@wisc.edu    provides utility functions for accessing fixtures.
18112882Sspwilson2@wisc.edu    '''
18212882Sspwilson2@wisc.edu    def __init__(self, suites, global_fixtures):
18312882Sspwilson2@wisc.edu        LoadedTestable.__init__(self, suites)
18412882Sspwilson2@wisc.edu        self.global_fixtures = global_fixtures
18512882Sspwilson2@wisc.edu
18612882Sspwilson2@wisc.edu    def _generate_metadata(self):
18712882Sspwilson2@wisc.edu        return LibraryMetadata( **{
18812882Sspwilson2@wisc.edu            'name': 'Test Library',
18912882Sspwilson2@wisc.edu            'status': Status.Unscheduled,
19012882Sspwilson2@wisc.edu            'result': Result(Result.NotRun)
19112882Sspwilson2@wisc.edu        })
19212882Sspwilson2@wisc.edu
19312882Sspwilson2@wisc.edu    def __iter__(self):
19412882Sspwilson2@wisc.edu        '''
19512882Sspwilson2@wisc.edu        :returns: an iterator over contained :class:`TestSuite` objects.
19612882Sspwilson2@wisc.edu        '''
19712882Sspwilson2@wisc.edu        return iter(self.obj)
19812882Sspwilson2@wisc.edu
19912882Sspwilson2@wisc.edu    def all_fixture_tuples(self):
20012882Sspwilson2@wisc.edu        return itertools.chain(
20112882Sspwilson2@wisc.edu                self.global_fixtures,
20212882Sspwilson2@wisc.edu                *(suite.fixtures for suite in self.obj))
20312882Sspwilson2@wisc.edu
20412882Sspwilson2@wisc.edu    def all_fixtures(self):
20512882Sspwilson2@wisc.edu        '''
20612882Sspwilson2@wisc.edu        :returns: an interator overall all global, suite,
20712882Sspwilson2@wisc.edu          and test fixtures
20812882Sspwilson2@wisc.edu        '''
20912882Sspwilson2@wisc.edu        return itertools.chain(itertools.chain(
21012882Sspwilson2@wisc.edu                self.global_fixtures,
21112882Sspwilson2@wisc.edu                *(suite.fixtures for suite in self.obj)),
21212882Sspwilson2@wisc.edu            *(self.test_fixtures(suite) for suite in self.obj)
21312882Sspwilson2@wisc.edu        )
21412882Sspwilson2@wisc.edu
21512882Sspwilson2@wisc.edu    def test_fixtures(self, suite):
21612882Sspwilson2@wisc.edu        '''
21712882Sspwilson2@wisc.edu        :returns: an interator over all fixtures of each
21812882Sspwilson2@wisc.edu          test contained in the given suite
21912882Sspwilson2@wisc.edu        '''
22012882Sspwilson2@wisc.edu        return itertools.chain(*(test.fixtures for test in suite))
22112882Sspwilson2@wisc.edu
22212882Sspwilson2@wisc.edu    @property
22312882Sspwilson2@wisc.edu    def fixtures(self):
22412882Sspwilson2@wisc.edu        return self.global_fixtures
22512882Sspwilson2@wisc.edu
22612882Sspwilson2@wisc.edu    @property
22712882Sspwilson2@wisc.edu    def uid(self):
22812882Sspwilson2@wisc.edu        return self.name
22912882Sspwilson2@wisc.edu
23012882Sspwilson2@wisc.edu    @property
23112882Sspwilson2@wisc.edu    def suites(self):
23212882Sspwilson2@wisc.edu        return self.obj
23312882Sspwilson2@wisc.edu
23412882Sspwilson2@wisc.edu    @suites.setter
23512882Sspwilson2@wisc.edu    def suites(self, suites):
23612882Sspwilson2@wisc.edu        self.obj = suites
237