blob: a7103283998b13a14d424b9dcdcd9745caf6e766 [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001# 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.
Copybara854996b2021-09-07 19:36:02 +00004
5"""Tests for the fake module."""
6from __future__ import print_function
7from __future__ import division
8from __future__ import absolute_import
9
10import inspect
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010011import six
Copybara854996b2021-09-07 19:36:02 +000012import unittest
13
14from services import cachemanager_svc
15from services import config_svc
16from services import features_svc
17from services import issue_svc
18from services import project_svc
19from services import star_svc
20from services import user_svc
21from services import usergroup_svc
22from testing import fake
23
24fake_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
36class 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ínezf19ea432024-01-23 20:20:52 +010047 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))
Copybara854996b2021-09-07 19:36:02 +000053 # 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))