blob: c09823674b73c787b59695449057c5f2f7f71ad8 [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001# Copyright 2016 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style
3# license that can be found in the LICENSE file or at
4# https://developers.google.com/open-source/licenses/bsd
5
6"""Tests for the fake module."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
11import inspect
12import 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)
47 assert inspect.ismethod(real_attr)
48 real_spec = inspect.getargspec(real_attr)
49 fake_spec = inspect.getargspec(getattr(fake_cls, name))
50 # check same number of args and kwargs
51 real_kw_len = len(real_spec[3]) if real_spec[3] else 0
52 fake_kw_len = len(fake_spec[3]) if fake_spec[3] else 0
53
54 self.assertEqual(
55 len(real_spec[0]) - real_kw_len,
56 len(fake_spec[0]) - fake_kw_len,
57 'Unequal number of args on %s.%s' % (fake_cls.__name__, name))
58 self.assertEqual(
59 real_kw_len, fake_kw_len,
60 'Unequal number of kwargs on %s.%s' % (fake_cls.__name__, name))
61 if real_kw_len:
62 self.assertEqual(
63 real_spec[0][-real_kw_len:], fake_spec[0][-fake_kw_len:],
64 'Mismatched kwargs on %s.%s' % (fake_cls.__name__, name))
65 self.assertEqual(
66 real_spec[3], fake_spec[3],
67 'Mismatched kwarg defaults on %s.%s' % (fake_cls.__name__, name))