Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 1 | # Copyright 2016 The Chromium Authors |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 4 | |
| 5 | """Tests for the fake module.""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
| 10 | import inspect |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 11 | import six |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 12 | import unittest |
| 13 | |
| 14 | from services import cachemanager_svc |
| 15 | from services import config_svc |
| 16 | from services import features_svc |
| 17 | from services import issue_svc |
| 18 | from services import project_svc |
| 19 | from services import star_svc |
| 20 | from services import user_svc |
| 21 | from services import usergroup_svc |
| 22 | from testing import fake |
| 23 | |
| 24 | fake_class_map = { |
| 25 | fake.AbstractStarService: star_svc.AbstractStarService, |
| 26 | fake.CacheManager: cachemanager_svc.CacheManager, |
| 27 | fake.ProjectService: project_svc.ProjectService, |
| 28 | fake.ConfigService: config_svc.ConfigService, |
| 29 | fake.IssueService: issue_svc.IssueService, |
| 30 | fake.UserGroupService: usergroup_svc.UserGroupService, |
| 31 | fake.UserService: user_svc.UserService, |
| 32 | fake.FeaturesService: features_svc.FeaturesService, |
| 33 | } |
| 34 | |
| 35 | |
| 36 | class FakeMetaTest(unittest.TestCase): |
| 37 | |
| 38 | def testFunctionsHaveSameSignatures(self): |
| 39 | """Verify that the fake class methods match the real ones.""" |
| 40 | for fake_cls, real_cls in fake_class_map.items(): |
| 41 | fake_attrs = set(dir(fake_cls)) |
| 42 | real_attrs = set(dir(real_cls)) |
| 43 | both_attrs = fake_attrs.intersection(real_attrs) |
| 44 | to_test = [x for x in both_attrs if '__' not in x] |
| 45 | for name in to_test: |
| 46 | real_attr = getattr(real_cls, name) |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 47 | if six.PY2: |
| 48 | assert inspect.ismethod(real_attr) |
| 49 | else: |
| 50 | assert inspect.isfunction(real_attr) |
| 51 | real_spec = inspect.getfullargspec(real_attr) |
| 52 | fake_spec = inspect.getfullargspec(getattr(fake_cls, name)) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 53 | # check same number of args and kwargs |
| 54 | real_kw_len = len(real_spec[3]) if real_spec[3] else 0 |
| 55 | fake_kw_len = len(fake_spec[3]) if fake_spec[3] else 0 |
| 56 | |
| 57 | self.assertEqual( |
| 58 | len(real_spec[0]) - real_kw_len, |
| 59 | len(fake_spec[0]) - fake_kw_len, |
| 60 | 'Unequal number of args on %s.%s' % (fake_cls.__name__, name)) |
| 61 | self.assertEqual( |
| 62 | real_kw_len, fake_kw_len, |
| 63 | 'Unequal number of kwargs on %s.%s' % (fake_cls.__name__, name)) |
| 64 | if real_kw_len: |
| 65 | self.assertEqual( |
| 66 | real_spec[0][-real_kw_len:], fake_spec[0][-fake_kw_len:], |
| 67 | 'Mismatched kwargs on %s.%s' % (fake_cls.__name__, name)) |
| 68 | self.assertEqual( |
| 69 | real_spec[3], fake_spec[3], |
| 70 | 'Mismatched kwarg defaults on %s.%s' % (fake_cls.__name__, name)) |