From 42ee2f5e86da925a03e7557ea3074451dfa50d5a Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Thu, 19 Mar 2020 15:32:24 +0000 Subject: [PATCH] tests: Fix test coverage on recent commit. I guess `test_classes` has 100% line coverage enforcement, which is a bit tricky for error handling. This fixes that, as well as making the name snake_case and improving the format of the errors. --- zerver/lib/test_classes.py | 36 ++++++++++++++++++++---------------- zerver/tests/test_users.py | 2 +- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/zerver/lib/test_classes.py b/zerver/lib/test_classes.py index 56b1b05412..ea0b713152 100644 --- a/zerver/lib/test_classes.py +++ b/zerver/lib/test_classes.py @@ -821,10 +821,10 @@ class ZulipTestCase(TestCase): r for r in data if r['id'] == db_id][0] - def findOne(self, - lst: List[Any], - predicate: Callable[[Any], bool], - member_name: str) -> Any: + def find_one(self, + lst: List[Any], + predicate: Callable[[Any], bool], + member_name: str) -> Any: matches = [ item for item in lst if predicate(item) @@ -834,23 +834,27 @@ class ZulipTestCase(TestCase): # Happy path! return matches[0] - print('\nERROR: findOne fails on this list:\n') + if True: # nocoverage + print('\nERROR: findOne fails on this list:\n') + print('[') - for item in lst: - print(item) + for item in lst: + print(' ', item, ',') + + print(']') + + if len(matches) == 0: + raise ValueError( + 'No matches: {}'.format(member_name) + ) - if len(matches) == 0: raise ValueError( - 'No matches: {}'.format(member_name) + 'Too many matches ({}): {}'.format( + len(matches), + member_name + ) ) - raise ValueError( - 'Too many matches ({}): {}'.format( - len(matches), - member_name - ) - ) - def init_default_ldap_database(self) -> None: """ Takes care of the mock_ldap setup, loads diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.py index f591b6d21a..fe8bd801c5 100644 --- a/zerver/tests/test_users.py +++ b/zerver/tests/test_users.py @@ -1387,7 +1387,7 @@ class GetProfileTest(ZulipTestCase): result = self.api_get(hamlet, "/api/v1/users") self.assert_json_success(result) - my_user = self.findOne( + my_user = self.find_one( result.json()['members'], lambda user: user['email'] == hamlet.email, 'member for Hamlet'