Skip to content

Commit

Permalink
Replace assert statements with TestCase methods in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick-Hall committed Jan 22, 2025
1 parent f35bd63 commit c23ee1a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 43 deletions.
18 changes: 9 additions & 9 deletions gramps/cli/test/argparser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def triggers_option_error(self, option):

def test_wrong_argument_triggers_option_error(self):
bad, ap = self.triggers_option_error("--I-am-a-wrong-argument")
assert bad, ap.__dict__
self.assertTrue(bad, ap.__dict__)

def test_y_shortopt_sets_auto_accept(self):
bad, ap = self.triggers_option_error("-y")
Expand All @@ -59,26 +59,26 @@ def test_y_shortopt_sets_auto_accept(self):

def test_yes_longopt_sets_auto_accept(self):
bad, ap = self.triggers_option_error("--yes")
assert not bad, ap.errors
assert ap.auto_accept
self.assertFalse(bad, ap.errors)
self.assertTrue(ap.auto_accept)

def test_q_shortopt_sets_quiet(self):
bad, ap = self.triggers_option_error("-q")
assert not bad, ap.errors
assert ap.quiet
self.assertFalse(bad, ap.errors)
self.assertTrue(ap.quiet)

def test_quiet_longopt_sets_quiet(self):
bad, ap = self.triggers_option_error("--quiet")
assert not bad, ap.errors
assert ap.quiet
self.assertFalse(bad, ap.errors)
self.assertTrue(ap.quiet)

def test_quiet_exists_by_default(self):
ap = self.create_parser()
assert hasattr(ap, "quiet")
self.assertTrue(hasattr(ap, "quiet"))

def test_auto_accept_unset_by_default(self):
ap = self.create_parser()
assert not ap.auto_accept
self.assertFalse(ap.auto_accept)

def test_exception(self):
argument_parser = self.create_parser("-O")
Expand Down
51 changes: 32 additions & 19 deletions gramps/cli/test/user_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,29 @@ def setUp(self):
self.user._input = Mock(spec=input)

def test_default_fileout_has_write(self):
assert hasattr(self.real_user._fileout, "write")
self.assertTrue(hasattr(self.real_user._fileout, "write"))

def test_default_input(self):
assert self.real_user._input.__name__.endswith("input")
self.assertTrue(self.real_user._input.__name__.endswith("input"))

def test_prompt_returns_True_if_ACCEPT_entered(self):
self.user._input.configure_mock(return_value=TestUser.ACCEPT)
assert self.user.prompt(
TestUser.TITLE, TestUser.MSG, TestUser.ACCEPT, TestUser.REJECT
), "True expected!"
self.assertTrue(
self.user.prompt(
TestUser.TITLE, TestUser.MSG, TestUser.ACCEPT, TestUser.REJECT
),
"True expected!",
)
self.user._input.assert_called_once_with()

def test_prompt_returns_False_if_REJECT_entered(self):
self.user._input.configure_mock(return_value=TestUser.REJECT)
assert not self.user.prompt(
TestUser.TITLE, TestUser.MSG, TestUser.ACCEPT, TestUser.REJECT
), "False expected!"
self.assertFalse(
self.user.prompt(
TestUser.TITLE, TestUser.MSG, TestUser.ACCEPT, TestUser.REJECT
),
"False expected!",
)
self.user._input.assert_called_once_with()

def assert_prompt_contains_text(
Expand Down Expand Up @@ -104,18 +110,22 @@ def test_prompt_strips_underscore_in_reject(self):
def test_auto_accept_accepts_without_prompting(self):
u = user.User(auto_accept=True)
u._fileout = Mock(spec=sys.stderr)
assert u.prompt(
TestUser.TITLE, TestUser.MSG, TestUser.ACCEPT, TestUser.REJECT
), "True expected!"
assert len(u._fileout.method_calls) == 0, list(u._fileout.method_calls)
self.assertTrue(
u.prompt(TestUser.TITLE, TestUser.MSG, TestUser.ACCEPT, TestUser.REJECT),
"True expected!",
)
self.assertEqual(len(u._fileout.method_calls), 0, list(u._fileout.method_calls))

def test_EOFError_in_prompt_caught_as_False(self):
self.user._input.configure_mock(
side_effect=EOFError, return_value=TestUser.REJECT
)
assert not self.user.prompt(
TestUser.TITLE, TestUser.MSG, TestUser.ACCEPT, TestUser.REJECT
), "False expected!"
self.assertFalse(
self.user.prompt(
TestUser.TITLE, TestUser.MSG, TestUser.ACCEPT, TestUser.REJECT
),
"False expected!",
)
self.user._input.assert_called_once_with()


Expand All @@ -131,8 +141,10 @@ def test_progress_can_begin_step_end(self):
self.user.end_progress()

def tearDown(self):
assert len(self.user._fileout.method_calls) == 0, list(
self.user._fileout.method_calls
self.assertEqual(
len(self.user._fileout.method_calls),
0,
list(self.user._fileout.method_calls),
)


Expand All @@ -146,8 +158,9 @@ def test_can_step_using_with(self):
self._progress_begin_step_end()
self.expected_output = list(self.user._fileout.method_calls)
self.user._fileout.reset_mock()
self.assertTrue(
len(self.user._fileout.method_calls) == 0,
self.assertEqual(
len(self.user._fileout.method_calls),
0,
list(self.user._fileout.method_calls),
)

Expand Down
24 changes: 13 additions & 11 deletions gramps/gen/lib/test/serialize_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,23 @@ def test(self):

def test_data(self):
class_name = obj.__class__.__name__
assert isinstance(data, dict), "Ensure that the data is a dict"
assert data.handle == data["handle"], "Test attribute access"
self.assertIsInstance(data, dict, "Ensure that the data is a dict")
self.assertEqual(data.handle, data["handle"], "Test attribute access")

if class_name == "Person":
if (len(data.parent_family_list)) > 0:
# Get a handle:
assert isinstance(data.parent_family_list[0], str), "Test list access"

assert "_object" not in data.keys(), "Object not created"
assert data.get_handle() == data["handle"], "Test method call"
assert "_object" in data.keys(), "Object created"
assert data["_object"].handle == data["handle"], "Object is correct"
assert (
data["_object"].__class__.__name__ == class_name
), "Object is correct type"
self.assertIsInstance(
data.parent_family_list[0], str, "Test list access"
)

self.assertNotIn("_object", data.keys(), "Object not created")
self.assertEqual(data.get_handle(), data["handle"], "Test method call")
self.assertIn("_object", data.keys(), "Object created")
self.assertEqual(data["_object"].handle, data["handle"], "Object is correct")
self.assertEqual(
data["_object"].__class__.__name__, class_name, "Object is correct type"
)

name = "test_data_%s_%s" % (obj.__class__.__name__, obj.handle)
setattr(DatabaseCheck, name, test_data)
Expand Down
10 changes: 6 additions & 4 deletions gramps/gen/test/constfunc_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ def setUp(self):

@unittest.skipUnless(constfunc.lin(), "Written for Linux only...")
def test_consistent_with_DISPLAY_env(self):
assert (
self.has == self.display_nonempty
), "has_display(): {}, $DISPLAY: {}".format(
self.has, env["DISPLAY"] if ("DISPLAY" in env) else "(unset)"
self.assertEqual(
self.has,
self.display_nonempty,
"has_display(): {}, $DISPLAY: {}".format(
self.has, env["DISPLAY"] if ("DISPLAY" in env) else "(unset)"
),
)


Expand Down

0 comments on commit c23ee1a

Please sign in to comment.