From 7e1d016211df43bb73bfc8268f823b2a59fd8af6 Mon Sep 17 00:00:00 2001 From: mugdhadhole1 Date: Thu, 17 Oct 2024 19:36:40 +0530 Subject: [PATCH] Inherited ItemsTests class and accessed properties and methods using self Renamed Setup to ItemsTests and inherited it and accessed properties and methods using self Removed extra spaces, unnecessary blank lines and used blank lines where necessary --- test-unit/test_items.py | 268 ++++++++++++++++++++-------------------- 1 file changed, 137 insertions(+), 131 deletions(-) diff --git a/test-unit/test_items.py b/test-unit/test_items.py index e074ba1a..d1673963 100644 --- a/test-unit/test_items.py +++ b/test-unit/test_items.py @@ -4,21 +4,22 @@ from hashlib import sha1 from lobster.location import Location -class SetUp(unittest.TestCase): - mock_namespace = "mock_namespace" - mock_tag = "mock_tag" - mock_framework = "mock_framework" - mock_kind = "mock_kind" - mock_name = "mock_name" - mock_text = "mock_text" - mock_status = "active" - mock_language = "mock_language" - mock_location = create_autospec(Location, instance=True) - tracing_tag = Tracing_Tag(mock_namespace, mock_tag) - item = Item(tracing_tag, mock_location) - requirement = Requirement(tracing_tag, mock_location, mock_framework, mock_kind, mock_name, mock_text, mock_status) - implementation = Implementation(tracing_tag, mock_location, mock_language, mock_kind, mock_name) - activity = Activity(tracing_tag, mock_location, mock_framework, mock_kind) +class ItemsTests(unittest.TestCase): + def setUp(self): + self.mock_namespace = "mock_namespace" + self.mock_tag = "mock_tag" + self.mock_framework = "mock_framework" + self.mock_kind = "mock_kind" + self.mock_name = "mock_name" + self.mock_text = "mock_text" + self.mock_status = "active" + self.mock_language = "mock_language" + self.mock_location = create_autospec(Location, instance=True) + self.tracing_tag = Tracing_Tag(self.mock_namespace, self.mock_tag) + self.item = Item(self.tracing_tag, self.mock_location) + self.requirement = Requirement(self.tracing_tag, self.mock_location, self.mock_framework,self. mock_kind, self.mock_name, self.mock_text, self.mock_status) + self.implementation = Implementation(self.tracing_tag, self.mock_location, self.mock_language, self.mock_kind, self.mock_name) + self.activity = Activity(self.tracing_tag, self.mock_location, self.mock_framework, self.mock_kind) def set_location_data(self, location_type): if location_type == "file": @@ -49,15 +50,20 @@ def set_location_data(self, location_type): } return location_data -class TestTracingTag(unittest.TestCase): +class TestTracingTag(ItemsTests): + def setUp(self): + super().setUp() + def test_key(self): expected_key = "mock_namespace mock_tag" - actual_key = SetUp.tracing_tag.key() + actual_key = self.tracing_tag.key() + self.assertEqual(expected_key, actual_key) def test_to_json(self): expected_json = "mock_namespace mock_tag" - actual_json = SetUp.tracing_tag.to_json() + actual_json = self.tracing_tag.to_json() + self.assertEqual(expected_json, actual_json) @patch('lobster.items.Tracing_Tag.from_text') @@ -65,10 +71,9 @@ def test_from_json(self, mock_from_text): json_input = "namespace string" expected_namespace = "namespace" expected_rest_value = "string" - expected_result = SetUp.tracing_tag + expected_result = self.tracing_tag mock_from_text.return_value = expected_result - - result = SetUp.tracing_tag.from_json(json_input) + result = self.tracing_tag.from_json(json_input) mock_from_text.assert_called_once_with(expected_namespace, expected_rest_value) self.assertEqual(result, expected_result) @@ -78,8 +83,7 @@ def test_from_text_with_version(self): expected_namespace = "mock_namespace" expected_tag = "mock_tag" expected_version = "version" - - result = SetUp.tracing_tag.from_text(SetUp.tracing_tag.namespace, text) + result = self.tracing_tag.from_text(self.tracing_tag.namespace, text) self.assertEqual(result.namespace, expected_namespace) self.assertEqual(result.tag, expected_tag) @@ -90,8 +94,7 @@ def test_from_text_without_version(self): text = "tag" expected_tag = "tag" expected_version = None - - result = SetUp.tracing_tag.from_text(namespace, text) + result = self.tracing_tag.from_text(namespace, text) self.assertEqual(result.namespace, namespace) self.assertEqual(result.tag, expected_tag) @@ -99,76 +102,76 @@ def test_from_text_without_version(self): def test_from_text_invalid_namespace(self): with self.assertRaises(AssertionError): - SetUp.tracing_tag.from_text(123, "tag@version") + self.tracing_tag.from_text(123, "tag@version") def test_from_text_invalid_text(self): with self.assertRaises(AssertionError): - SetUp.tracing_tag.from_text("namespace", 123) + self.tracing_tag.from_text("namespace", 123) def test_hash(self): - hash_val = SetUp.tracing_tag.hash() + hash_val = self.tracing_tag.hash() hfunc = sha1() - hfunc.update(SetUp.tracing_tag.key().encode("UTF-8")) + hfunc.update(self.tracing_tag.key().encode("UTF-8")) expected_hash = hfunc.hexdigest() + self.assertEqual(hash_val, expected_hash) -class TestItem(unittest.TestCase): +class TestItem(ItemsTests): + def setUp(self): + super().setUp() + def test_set_level_valid_string(self): mock_level = "mock_level" + self.item.set_level(mock_level) - SetUp.item.set_level(mock_level) - - self.assertEqual(SetUp.item.level, mock_level) + self.assertEqual(self.item.level, mock_level) def test_set_level_invalid_string(self): invalid_level = 10 with self.assertRaises(AssertionError): - result = SetUp.item.set_level(invalid_level) + result = self.item.set_level(invalid_level) def test_error(self): mock_message = "mock_message" + self.item.error(mock_message) - SetUp.item.error(mock_message) - - self.assertIn(mock_message, SetUp.item.messages) - self.assertTrue(SetUp.item.messages) + self.assertIn(mock_message, self.item.messages) + self.assertTrue(self.item.messages) @patch("lobster.items.Tracing_Tag.key") def test_add_tracing_target(self, mock_key): - mock_target = SetUp.tracing_tag + mock_target = self.tracing_tag expected_result = "mock_namespace mock_tag" - mock_key.return_value = expected_result + self.item.add_tracing_target(mock_target) - SetUp.item.add_tracing_target(mock_target) - self.assertIn(mock_target, SetUp.item.unresolved_references) - self.assertIn(expected_result, SetUp.item.unresolved_references_cache) + self.assertIn(mock_target, self.item.unresolved_references) + self.assertIn(expected_result, self.item.unresolved_references_cache) def test_perform_source_checks(self): mock_valid_source_info = {"key": "value"} - try: - SetUp.item.perform_source_checks(mock_valid_source_info) + self.item.perform_source_checks(mock_valid_source_info) except AssertionError: self.fail("perform_source_checks() raised AssertionError unexpectedly!") def test_perform_source_checks_with_invalid_type(self): - mock_invalid_source_info = ["not", "a", "dict"] + mock_invalid_source_info = ["not", "a", "dictionary"] with self.assertRaises(AssertionError): - SetUp.item.perform_source_checks(mock_invalid_source_info) + self.item.perform_source_checks(mock_invalid_source_info) @patch("lobster.items.Tracing_Tag.key") def test_determine_status_ok(self, mock_key): - SetUp.item.ref_up = [] - SetUp.item.ref_down = [SetUp.tracing_tag] - SetUp.item.just_up = [] - SetUp.item.just_down = [] - SetUp.item.just_global = [] - SetUp.item.messages = [] - SetUp.item.has_error = False - SetUp.item.level = "level1" + self.item.ref_up = [] + self.item.ref_down = [self.tracing_tag] + self.item.just_up = [] + self.item.just_down = [] + self.item.just_global = [] + self.item.messages = [] + self.item.has_error = False + self.item.level = "level1" expected_result = "mock_namespace mock_tag" mock_key.return_value = expected_result config = { @@ -180,19 +183,19 @@ def test_determine_status_ok(self, mock_key): } } stab = { - mock_key() : SetUp.item + mock_key() : self.item } + self.item.determine_status(config, stab) - SetUp.item.determine_status(config, stab) - self.assertEqual(SetUp.item.tracing_status, Tracing_Status.PARTIAL) + self.assertEqual(self.item.tracing_status, Tracing_Status.PARTIAL) def test_determine_status_missing_up_reference(self): mock_namespace = "mock_namespace" mock_tag = "mock_tag" - SetUp.item.level = "level1" - SetUp.item.just_up = [] - SetUp.item.just_global = [] - SetUp.item.ref_up = [] + self.item.level = "level1" + self.item.just_up = [] + self.item.just_global = [] + self.item.ref_up = [] config = { "level1": { "needs_tracing_up": True, @@ -202,20 +205,20 @@ def test_determine_status_missing_up_reference(self): } } stab = { - Tracing_Tag(mock_namespace, mock_tag).key() : SetUp.item + Tracing_Tag(mock_namespace, mock_tag).key() : self.item } + self.item.determine_status(config, stab) - SetUp.item.determine_status(config, stab) - self.assertEqual(SetUp.item.tracing_status, Tracing_Status.MISSING) - self.assertIn("missing up reference", SetUp.item.messages) + self.assertEqual(self.item.tracing_status, Tracing_Status.MISSING) + self.assertIn("missing up reference", self.item.messages) def test_determine_status_missing_down_reference(self): mock_namespace = "mock_namespace" mock_tag = "mock_tag" - SetUp.item.level = "level1" - SetUp.item.just_down = [] - SetUp.item.just_global = [] - SetUp.item.ref_down = [] + self.item.level = "level1" + self.item.just_down = [] + self.item.just_global = [] + self.item.ref_down = [] config = { "level1": { "needs_tracing_up": False, @@ -225,12 +228,12 @@ def test_determine_status_missing_down_reference(self): } } stab = { - Tracing_Tag(mock_namespace, mock_tag).key() : SetUp.item + Tracing_Tag(mock_namespace, mock_tag).key() : self.item } + self.item.determine_status(config, stab) - SetUp.item.determine_status(config, stab) - self.assertEqual(SetUp.item.tracing_status, Tracing_Status.MISSING) - self.assertIn("missing reference to level1", SetUp.item.messages) + self.assertEqual(self.item.tracing_status, Tracing_Status.MISSING) + self.assertIn("missing reference to level1", self.item.messages) @patch("lobster.items.Item.set_level") def test_additional_data_from_json_valid_data(self, mock_set_level): @@ -247,17 +250,16 @@ def test_additional_data_from_json_valid_data(self, mock_set_level): "tracing_status": "OK" } schema_version = 3 - - SetUp.item.additional_data_from_json(mock_level, mock_data, schema_version) + self.item.additional_data_from_json(mock_level, mock_data, schema_version) self.assertEqual(mock_set_level(), mock_level) - self.assertEqual([tag.namespace + " " + tag.tag for tag in SetUp.item.ref_up], ["mock refup"]) - self.assertEqual([tag.namespace + " " + tag.tag for tag in SetUp.item.ref_down], ["mock refdown"]) - self.assertEqual(SetUp.item.messages, ["message1", "message2"]) - self.assertEqual(SetUp.item.just_up, ["up1"]) - self.assertEqual(SetUp.item.just_down, ["down1"]) - self.assertEqual(SetUp.item.just_global, ["global1"]) - self.assertEqual(SetUp.item.tracing_status, Tracing_Status.OK) + self.assertEqual([tag.namespace + " " + tag.tag for tag in self.item.ref_up], ["mock refup"]) + self.assertEqual([tag.namespace + " " + tag.tag for tag in self.item.ref_down], ["mock refdown"]) + self.assertEqual(self.item.messages, ["message1", "message2"]) + self.assertEqual(self.item.just_up, ["up1"]) + self.assertEqual(self.item.just_down, ["down1"]) + self.assertEqual(self.item.just_global, ["global1"]) + self.assertEqual(self.item.tracing_status, Tracing_Status.OK) def test_additional_data_from_json_invalid_level(self): level = 123 @@ -265,7 +267,7 @@ def test_additional_data_from_json_invalid_level(self): schema_version = 3 with self.assertRaises(AssertionError): - SetUp.item.additional_data_from_json(level, data, schema_version) + self.item.additional_data_from_json(level, data, schema_version) def test_additional_data_from_json_invalid_data(self): level = "info" @@ -273,7 +275,7 @@ def test_additional_data_from_json_invalid_data(self): schema_version = 3 with self.assertRaises(AssertionError): - SetUp.item.additional_data_from_json(level, data, schema_version) + self.item.additional_data_from_json(level, data, schema_version) def test_additional_data_from_json_invalid_schema_version(self): level = "info" @@ -281,24 +283,24 @@ def test_additional_data_from_json_invalid_schema_version(self): schema_version = 2 with self.assertRaises(AssertionError): - SetUp.item.additional_data_from_json(level, data, schema_version) + self.item.additional_data_from_json(level, data, schema_version) @patch("lobster.items.Tracing_Tag.to_json") def test_to_json(self, mock_to_json): mock_to_json.return_value = "mock_value" - SetUp.item.name = "mock_name" - SetUp.item.messages = ["message1", "message2"] - SetUp.item.just_up = True - SetUp.item.just_down = False - SetUp.item.just_global = True - SetUp.item.unresolved_references = [SetUp.tracing_tag] - SetUp.item.ref_up = [SetUp.tracing_tag] - SetUp.item.ref_down = [SetUp.tracing_tag] - SetUp.item.tracing_status = MagicMock() - SetUp.item.tracing_status.name = "mock_status" + self.item.name = "mock_name" + self.item.messages = ["message1", "message2"] + self.item.just_up = True + self.item.just_down = False + self.item.just_global = True + self.item.unresolved_references = [self.tracing_tag] + self.item.ref_up = [self.tracing_tag] + self.item.ref_down = [self.tracing_tag] + self.item.tracing_status = MagicMock() + self.item.tracing_status.name = "mock_status" expected_json = { "tag": "mock_value", - "location": SetUp.mock_location.to_json(), + "location": self.mock_location.to_json(), "name": "mock_name", "messages": ["message1", "message2"], "just_up": True, @@ -309,12 +311,14 @@ def test_to_json(self, mock_to_json): "ref_down": ["mock_value"], "tracing_status": "mock_status" } - - result = SetUp.item.to_json() + result = self.item.to_json() self.assertEqual(result, expected_json) -class TestRequirement(unittest.TestCase): +class TestRequirement(ItemsTests): + def setUp(self): + super().setUp() + @patch("lobster.items.Item.to_json") def test_to_json(self, mock_super_to_json): mock_super_to_json.return_value = { @@ -327,7 +331,7 @@ def test_to_json(self, mock_super_to_json): "text": "mock_text", "status": "active" } - result = SetUp.requirement.to_json() + result = self.requirement.to_json() mock_super_to_json.assert_called_once_with() self.assertEqual(result, expected_result) @@ -339,8 +343,7 @@ def test_perform_source_checks_valid_status(self, mock_super_error): source_info = { "valid_status": ["active", "inactive"] } - - SetUp.requirement.perform_source_checks(source_info) + self.requirement.perform_source_checks(source_info) self.assertIsNone(mock_super_error()) @@ -351,29 +354,27 @@ def test_perform_source_checks_invalid_status(self, mock_super_error): source_info = { "valid_status": ["inactive", "closed"] } - - SetUp.requirement.perform_source_checks(source_info) - + self.requirement.perform_source_checks(source_info) expected_error_message = "status is active, expected closed or inactive" + self.assertEqual(mock_super_error(), expected_error_message) def test_perform_source_checks_invalid_source_info(self): invalid_source_info = ["invalid", "list"] with self.assertRaises(AssertionError): - SetUp.requirement.perform_source_checks(invalid_source_info) + self.requirement.perform_source_checks(invalid_source_info) @patch("lobster.items.Tracing_Tag.from_json") def test_from_json(self, mock_from_json): mock_level = "mock_level" mock_schema_version = 3 - setup = SetUp() - mock_from_json.return_value = SetUp.tracing_tag + mock_from_json.return_value = self.tracing_tag for location_type in ["file", "github", "codebeamer", "void"]: with self.subTest(location_type): - location_data = setup.set_location_data(location_type) + location_data = self.set_location_data(location_type) mock_data = { - "tag": SetUp.tracing_tag, + "tag": self.tracing_tag, "location": location_data, "framework": "framework_data", "kind": "kind_data", @@ -381,8 +382,9 @@ def test_from_json(self, mock_from_json): "text": "text_data", "status": "status_data" } - result = setup.requirement.from_json(mock_level, mock_data, mock_schema_version) - self.assertEqual(result.tag, SetUp.tracing_tag) + result = self.requirement.from_json(mock_level, mock_data, mock_schema_version) + + self.assertEqual(result.tag, self.tracing_tag) self.assertEqual(result.framework, "framework_data") self.assertEqual(result.kind, "kind_data") self.assertEqual(result.name, "name_data") @@ -402,7 +404,10 @@ def test_from_json(self, mock_from_json): self.assertEqual(result.location.version, location_data["version"]) self.assertEqual(result.location.name, location_data["name"]) -class TestImplementation(unittest.TestCase): +class TestImplementation(ItemsTests): + def setUp(self): + super().setUp() + @patch("lobster.items.Item.to_json") def test_to_json(self, mock_super_to_json): mock_super_to_json.return_value = { @@ -413,8 +418,7 @@ def test_to_json(self, mock_super_to_json): "language": "mock_language", "kind": "mock_kind" } - - result = SetUp.implementation.to_json() + result = self.implementation.to_json() mock_super_to_json.assert_called_once_with() self.assertEqual(result, expected_result) @@ -423,20 +427,20 @@ def test_to_json(self, mock_super_to_json): def test_from_json(self, mock_from_json): mock_level = "mock_level" mock_schema_version = 3 - setup = SetUp() - mock_from_json.return_value = SetUp.tracing_tag + mock_from_json.return_value = self.tracing_tag for location_type in ["file", "github", "codebeamer", "void"]: with self.subTest(location_type): - location_data = setup.set_location_data(location_type) + location_data = self.set_location_data(location_type) mock_data = { - "tag": SetUp.tracing_tag, + "tag": self.tracing_tag, "location": location_data, "language": "Python", "kind": "kind_data", "name": "name_data", } - result = setup.implementation.from_json(mock_level, mock_data, mock_schema_version) - self.assertEqual(result.tag, SetUp.tracing_tag) + result = self.implementation.from_json(mock_level, mock_data, mock_schema_version) + + self.assertEqual(result.tag, self.tracing_tag) self.assertEqual(result.language, "Python") self.assertEqual(result.kind, "kind_data") self.assertEqual(result.name, "name_data") @@ -454,7 +458,10 @@ def test_from_json(self, mock_from_json): self.assertEqual(result.location.version, location_data["version"]) self.assertEqual(result.location.name, location_data["name"]) -class TestActivity(unittest.TestCase): +class TestActivity(ItemsTests): + def setUp(self): + super().setUp() + @patch("lobster.items.Item.to_json") def test_to_json(self, mock_super_to_json): mock_super_to_json.return_value = { @@ -466,8 +473,7 @@ def test_to_json(self, mock_super_to_json): "kind": "mock_kind", "status": None } - - result = SetUp.activity.to_json() + result = self.activity.to_json() mock_super_to_json.assert_called_once_with() self.assertEqual(result, expected_result) @@ -476,20 +482,20 @@ def test_to_json(self, mock_super_to_json): def test_from_json(self, mock_from_json): mock_level = "mock_level" mock_schema_version = 3 - setup = SetUp() - mock_from_json.return_value = setup.tracing_tag + mock_from_json.return_value = self.tracing_tag for location_type in ["file", "github", "codebeamer", "void"]: with self.subTest(location_type): - location_data = setup.set_location_data(location_type) + location_data = self.set_location_data(location_type) mock_data = { - "tag": setup.tracing_tag, + "tag": self.tracing_tag, "location": location_data, "framework": "framework_data", "kind": "kind_data", "status": None } - result = setup.activity.from_json(mock_level, mock_data, mock_schema_version) - self.assertEqual(result.tag, setup.tracing_tag) + result = self.activity.from_json(mock_level, mock_data, mock_schema_version) + + self.assertEqual(result.tag, self.tracing_tag) self.assertEqual(result.framework, "framework_data") self.assertEqual(result.kind, "kind_data") self.assertEqual(result.status, None)