Skip to content

Commit

Permalink
Use function to test exception when there is no element
Browse files Browse the repository at this point in the history
  • Loading branch information
dkultasev committed Jul 2, 2020
1 parent e8f0713 commit fe9959d
Showing 1 changed file with 17 additions and 19 deletions.
36 changes: 17 additions & 19 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,37 +30,35 @@ def setUp(self):
}
self.mock_json_load = patch('parse.open_json_file')

def test_user_credentials_object_is_the_same_like_in_file(self):
def delete_cred_element_and_run_get_user_credentials(self, element):
del self.creds[element]
self.mock_json_load.return_value = self.creds
with self.assertRaises(Exception) as ue:
get_user_credentials()
return str(ue)

def test_user_credentials_object_is_same_like_in_file(self):
self.mock_json_load.return_value = self.creds
actual = get_user_credentials()
self.assertEqual(actual, self.creds)

def test_user_credentials_fails_if_there_is_no_user(self):
del self.creds['username']
self.mock_json_load.return_value = self.creds
with self.assertRaises(Exception) as ue:
get_user_credentials()
element = 'username'
self.assertEqual(
'Credentials are in the wrong format (username is missing)',
str(ue.exception))
f'Credentials are in the wrong format ({element} is missing)',
self.test_user_credentials_object_is_same_like_in_file(element))

def test_user_credentials_fails_if_there_is_no_language(self):
del self.creds['language']
self.mock_json_load.return_value = self.creds
with self.assertRaises(Exception) as ue:
get_user_credentials()
element = 'language'
self.assertEqual(
'Credentials are in the wrong format (language is missing)',
str(ue.exception))
f'Credentials are in the wrong format ({element} is missing)',
self.test_user_credentials_object_is_same_like_in_file(element))

def test_user_credentials_fails_if_there_is_no_password(self):
del self.creds['password']
self.mock_json_load.return_value = self.creds
with self.assertRaises(Exception) as ue:
get_user_credentials()
element = 'password'
self.assertEqual(
'Credentials are in the wrong format (password is missing)',
str(ue.exception))
f'Credentials are in the wrong format ({element} is missing)',
self.test_user_credentials_object_is_same_like_in_file(element))

@patch('builtins.open')
@patch('json.load')
Expand Down

0 comments on commit fe9959d

Please sign in to comment.