def test_related(self): coll = dut.TraceableCollection() coll.add_relation_pair(self.fwd_relation, self.rev_relation) self.assertFalse( coll.are_related(self.identification_src, [], self.identification_tgt)) item1 = item.TraceableItem(self.identification_src) coll.add_item(item1) self.assertFalse( coll.are_related(self.identification_src, [], self.identification_tgt)) coll.add_relation(self.identification_src, self.fwd_relation, self.identification_tgt) # placeholder should be excluded self.assertFalse( coll.are_related(self.identification_src, [], self.identification_tgt)) item2 = item.TraceableItem(self.identification_tgt) # placeholder is replaced by actual item coll.add_item(item2) self.assertTrue( coll.are_related(self.identification_src, [], self.identification_tgt)) self.assertTrue( coll.are_related(self.identification_src, [self.fwd_relation], self.identification_tgt)) self.assertTrue( coll.are_related(self.identification_src, [self.fwd_relation, 'another-relation'], self.identification_tgt))
def test_add_item(self): coll = dut.TraceableCollection() # Initially no items self.assertFalse(coll.has_item(self.identification_src)) self.assertIsNone(coll.get_item(self.identification_src)) item_iterator = coll.iter_items() self.assertNotIn(self.identification_src, item_iterator) self.assertNotIn(self.identification_tgt, item_iterator) # Add an item item1 = item.TraceableItem(self.identification_src) item1.set_document(self.docname) coll.add_item(item1) self.assertTrue(coll.has_item(self.identification_src)) self.assertEqual(item1, coll.get_item(self.identification_src)) # Add same item: should give warning with self.assertRaises(exception.TraceabilityException): coll.add_item(item1) self.assertTrue(coll.has_item(self.identification_src)) self.assertEqual(1, len(coll.items)) self.assertEqual(item1, coll.get_item(self.identification_src)) # Add a second item, make sure first one is still there self.assertFalse(coll.has_item(self.identification_tgt)) item2 = item.TraceableItem(self.identification_tgt) item2.set_document(self.docname) coll.add_item(item2) self.assertTrue(coll.has_item(self.identification_tgt)) self.assertEqual(item2, coll.get_item(self.identification_tgt)) self.assertEqual(item1, coll.get_item(self.identification_src)) # Verify iterator item_iterator = coll.iter_items() self.assertIn(self.identification_src, item_iterator) self.assertIn(self.identification_tgt, item_iterator) # Self test should pass coll.add_relation_pair(self.fwd_relation, self.rev_relation) coll.self_test()
def test_selftest(self): coll = dut.TraceableCollection() coll.add_relation_pair(self.fwd_relation, self.rev_relation) # Self test should pass coll.self_test() # Create first item item1 = item.TraceableItem(self.identification_src) item1.set_document(self.docname) # Improper use: add target on item level (no sanity check and no automatic reverse link) item1.add_target(self.fwd_relation, self.identification_tgt) # Improper use is not detected at level of item-level item1.self_test() # Add item to collection coll.add_item(item1) # Self test should fail as target item is not in collection with self.assertRaises(dut.MultipleTraceabilityExceptions): coll.self_test() # Self test one limited scope (no matching document), should pass coll.self_test('document-does-not-exist.rst') # Creating and adding second item, self test should still fail as no automatic reverse relation item2 = item.TraceableItem(self.identification_tgt) item2.set_document(self.docname) coll.add_item(item2) with self.assertRaises(dut.MultipleTraceabilityExceptions): coll.self_test() # Mimicing the automatic reverse relation, self test should pass item2.add_target(self.rev_relation, self.identification_src) coll.self_test()
def test_add_target_implicit_self(self): item = dut.TraceableItem(self.identification) item.set_document(self.docname) with self.assertRaises(exception.TraceabilityException): item.add_target(self.fwd_relation, self.identification, implicit=True)
def test_bind_node(self): item = dut.TraceableItem(self.identification) item.set_document(self.docname) node = object() item.bind_node(node) self.assertEqual(node, item.get_node()) item.self_test()
def test_set_content(self): txt = 'some description, with\n newlines and other stuff' item = dut.TraceableItem(self.identification) item.set_document(self.docname) item.set_content(txt) self.assertEqual(txt, item.get_content()) item.self_test()
def test_get_items(self): coll = dut.TraceableCollection() coll.add_relation_pair(self.fwd_relation, self.rev_relation) self.assertEqual(0, len(coll.get_items('\w*'))) item1 = item.TraceableItem(self.identification_src) coll.add_item(item1) coll.add_relation(self.identification_src, self.fwd_relation, self.identification_tgt) # placeholder should be excluded self.assertEqual(1, len(coll.get_items('\w*'))) item2 = item.TraceableItem(self.identification_tgt) # placeholder is replaced by actual item coll.add_item(item2) self.assertEqual(2, len(coll.get_items('\w*'))) # Empty filter should match all items self.assertEqual(2, len(coll.get_items('')))
def test_related(self): item = dut.TraceableItem(self.identification) self.assertEqual(self.identification, item.get_id()) self.assertFalse(item.is_related([self.fwd_relation], self.identification_tgt)) item.add_target(self.fwd_relation, self.identification_tgt) self.assertTrue(item.is_related([self.fwd_relation], self.identification_tgt)) self.assertFalse(item.is_related(['some-other-relation'], self.identification_tgt))
def test_remove_target_implicit(self): item = dut.TraceableItem(self.identification) item.set_document(self.docname) # Add an implicit target item.add_target(self.fwd_relation, self.identification_tgt, implicit=True) targets = item.iter_targets(self.fwd_relation) self.assertEqual(1, len(targets)) self.assertEqual(targets[0], self.identification_tgt) # Remove target to self and explicit targets, no effect item.remove_targets(self.identification) item.remove_targets(self.identification_tgt, explicit=True, implicit=False) targets = item.iter_targets(self.fwd_relation) self.assertEqual(1, len(targets)) self.assertEqual(targets[0], self.identification_tgt) # Remove implicit target to tgt, should be removed item.remove_targets(self.identification_tgt, explicit=False, implicit=True) targets = item.iter_targets(self.fwd_relation) self.assertEqual(0, len(targets)) # Self test should pass item.self_test()
def test_attributes_match(self): item = dut.TraceableItem(self.identification) item.add_attribute(self.attribute_key, self.attribute_value1) self.assertFalse( item.attributes_match({self.attribute_key: self.attribute_value2})) self.assertTrue( item.attributes_match({self.attribute_key: self.attribute_value1}))
def test_match(self): item = dut.TraceableItem(self.identification) self.assertEqual(self.identification, item.get_id()) self.assertFalse(item.is_match('some-name-that(will-definitely)not-match')) self.assertTrue(item.is_match('some')) self.assertTrue(item.is_match('some-random')) self.assertTrue(item.is_match('\w+')) self.assertTrue(item.is_match('[\w-]+andom'))
def test_remove_attribute(self): item = dut.TraceableItem(self.identification) item.set_document(self.docname) item.add_attribute(self.attribute_key1, self.attribute_value1) self.assertEqual(self.attribute_value1, item.get_attribute(self.attribute_key1)) item.remove_attribute(self.attribute_key1) self.assertFalse(item.get_attribute(self.attribute_key1)) item.self_test()
def test_set_document(self): item = dut.TraceableItem(self.identification) with self.assertRaises(exception.TraceabilityException): item.self_test() item.set_document('some-file.rst', 888) self.assertEqual('some-file.rst', item.get_document()) self.assertEqual(888, item.get_line_number()) item.self_test()
def test_add_get_target_implicit(self): item = dut.TraceableItem(self.identification) item.set_document(self.docname) # Initially no targets (explicit+implicit) targets = item.iter_targets(self.fwd_relation) self.assertEqual(0, len(targets)) targets = item.iter_targets(self.fwd_relation, explicit=False) self.assertEqual(0, len(targets)) targets = item.iter_targets(self.fwd_relation, implicit=False) self.assertEqual(0, len(targets)) relations = item.iter_relations() self.assertNotIn(self.fwd_relation, relations) # Add an implicit target item.add_target(self.fwd_relation, self.identification_tgt, implicit=True) targets = item.iter_targets(self.fwd_relation) self.assertEqual(1, len(targets)) self.assertEqual(targets[0], self.identification_tgt) targets = item.iter_targets(self.fwd_relation, explicit=False) self.assertEqual(1, len(targets)) self.assertEqual(targets[0], self.identification_tgt) targets = item.iter_targets(self.fwd_relation, implicit=False) self.assertEqual(0, len(targets)) # Add the same implicit target, should not change (no duplicates) item.add_target(self.fwd_relation, self.identification_tgt, implicit=True) targets = item.iter_targets(self.fwd_relation) self.assertEqual(1, len(targets)) self.assertEqual(targets[0], self.identification_tgt) targets = item.iter_targets(self.fwd_relation, explicit=False) self.assertEqual(1, len(targets)) self.assertEqual(targets[0], self.identification_tgt) targets = item.iter_targets(self.fwd_relation, implicit=False) self.assertEqual(0, len(targets)) # Add the same explicit target, should move the target to be explicit item.add_target(self.fwd_relation, self.identification_tgt) targets = item.iter_targets(self.fwd_relation) self.assertEqual(1, len(targets)) self.assertEqual(targets[0], self.identification_tgt) targets = item.iter_targets(self.fwd_relation, explicit=False) self.assertEqual(0, len(targets)) targets = item.iter_targets(self.fwd_relation, implicit=False) self.assertEqual(1, len(targets)) self.assertEqual(targets[0], self.identification_tgt) # Verify relations iterator relations = item.iter_relations() self.assertIn(self.fwd_relation, relations) # Verify dict data = item.to_dict() self.assertEqual(self.identification, data['id']) self.assertEqual([self.identification_tgt], data['targets'][self.fwd_relation]) self.assertEqual(self.docname, data['document']) self.assertEqual(0, data['line']) # Self test should pass item.self_test()
def test_remove_invalid_attribute(self): item = dut.TraceableItem(self.identification) item.set_document(self.docname) self.assertFalse(item.get_attribute(self.attribute_key)) with self.assertRaises(exception.TraceabilityException): item.remove_attribute(None) with self.assertRaises(exception.TraceabilityException): item.remove_attribute('') item.self_test()
def test_export_single_item(self, json_mock): json_mock_object = MagicMock(spec=dut.json) json_mock.return_value = json_mock_object open_mock = mock_open() coll = dut.TraceableCollection() item1 = item.TraceableItem(self.identification_src) coll.add_item(item1) with patch('mlx.traceable_collection.open', open_mock, create=True): coll.export(self.mock_export_file) open_mock.assert_called_once_with(self.mock_export_file, 'w')
def test_init_placeholder(self): item = dut.TraceableItem(self.identification, placeholder=True) item.set_document(self.docname) with self.assertRaises(exception.TraceabilityException) as err: item.self_test() self.assertEqual(err.exception.get_document(), self.docname) self.assertEqual(self.identification, item.get_id()) self.assertTrue(item.is_placeholder()) # Verify dict self.assertEqual({}, item.to_dict())
def test_init(self): item = dut.TraceableItem(self.identification) item.set_document(self.docname) item.self_test() self.assertEqual(self.identification, item.get_id()) self.assertFalse(item.is_placeholder()) self.assertIsNotNone(item.get_document()) self.assertEqual(0, item.get_line_number()) self.assertIsNone(item.get_node()) self.assertIsNone(item.get_caption()) self.assertIsNone(item.get_content())
def test_add_relation_unknown_source(self): # with unknown source item, exception is expected coll = dut.TraceableCollection() item2 = item.TraceableItem(self.identification_tgt) item2.set_document(self.docname) coll.add_item(item2) coll.add_relation_pair(self.fwd_relation, self.rev_relation) with self.assertRaises(ValueError): coll.add_relation(self.identification_src, self.fwd_relation, self.identification_tgt) # Self test should pass coll.self_test()
def test_get_items_attribute(self): coll = dut.TraceableCollection() item1 = item.TraceableItem(self.identification_src) coll.add_item(item1) item2 = item.TraceableItem(self.identification_tgt) coll.add_item(item2) self.assertEqual(2, len(coll.get_items(''))) self.assertEqual( 0, len( coll.get_items( '', {self.attribute_key: self.attribute_value_src}))) self.assertEqual( 0, len( coll.get_items( '', {self.attribute_key: self.attribute_value_tgt}))) item1.add_attribute(self.attribute_key, self.attribute_value_src) self.assertEqual( 1, len( coll.get_items( '', {self.attribute_key: self.attribute_value_src}))) self.assertEqual( 0, len( coll.get_items( '', {self.attribute_key: self.attribute_value_tgt}))) item2.add_attribute(self.attribute_key, self.attribute_value_tgt) self.assertEqual( 1, len( coll.get_items( '', {self.attribute_key: self.attribute_value_src}))) self.assertEqual( 1, len( coll.get_items( '', {self.attribute_key: self.attribute_value_tgt})))
def test_add_attribute_no_overwrite(self): item = dut.TraceableItem(self.identification) item.set_document(self.docname) self.assertFalse(item.get_attribute(self.attribute_key)) item.add_attribute(self.attribute_key, self.attribute_value1) self.assertEqual(self.attribute_value1, item.get_attribute(self.attribute_key)) item.add_attribute(self.attribute_key, self.attribute_value2, overwrite=False) self.assertEqual(self.attribute_value1, item.get_attribute(self.attribute_key)) item.self_test()
def test_add_relation_happy(self): # Normal addition of relation, everything is there coll = dut.TraceableCollection() item1 = item.TraceableItem(self.identification_src) item1.set_document(self.docname) item2 = item.TraceableItem(self.identification_tgt) item2.set_document(self.docname) coll.add_item(item1) coll.add_item(item2) coll.add_relation_pair(self.fwd_relation, self.rev_relation) coll.add_relation(self.identification_src, self.fwd_relation, self.identification_tgt) # Assert explicit forward relation is created relations = item1.iter_targets(self.fwd_relation, explicit=True, implicit=False) self.assertEqual(1, len(relations)) self.assertEqual(relations[0], self.identification_tgt) relations = item1.iter_targets(self.fwd_relation, explicit=False, implicit=True) self.assertEqual(0, len(relations)) # Assert item2 is not a placeholder item item2_read = coll.get_item(self.identification_tgt) self.assertFalse(item2.is_placeholder()) self.assertEqual(item2, item2_read) # Assert implicit reverse relation is created relations = item2.iter_targets(self.rev_relation, explicit=False, implicit=True) self.assertEqual(1, len(relations)) self.assertEqual(relations[0], self.identification_src) relations = item2.iter_targets(self.fwd_relation, explicit=True, implicit=False) self.assertEqual(0, len(relations)) # Self test should pass coll.self_test()
def test_stringify(self): item = dut.TraceableItem(self.identification) item.set_document(self.docname) item.add_attribute(self.attribute_key1, self.attribute_value1) item.add_target(self.fwd_relation, self.identification_tgt, implicit=False) item.add_target(self.rev_relation, 'one more item', implicit=True) itemstr = str(item) self.assertIn(self.identification, itemstr) self.assertIn(self.attribute_key1, itemstr) self.assertIn(self.attribute_value1, itemstr) self.assertIn(self.fwd_relation, itemstr) self.assertIn(self.identification_tgt, itemstr) self.assertIn(self.rev_relation, itemstr) self.assertIn('one more item', itemstr)
def test_add_relation_unknown_relation(self): # with unknown relation, warning is expected coll = dut.TraceableCollection() item1 = item.TraceableItem(self.identification_src) item1.set_document(self.docname) item2 = item.TraceableItem(self.identification_tgt) item2.set_document(self.docname) coll.add_item(item1) coll.add_item(item2) with self.assertRaises(exception.TraceabilityException): coll.add_relation(self.identification_src, self.fwd_relation, self.identification_tgt) relations = item1.iter_targets(self.fwd_relation, explicit=True, implicit=True) self.assertEqual(0, len(relations)) relations = item2.iter_targets(self.fwd_relation, explicit=True, implicit=True) self.assertEqual(0, len(relations)) # Self test should pass coll.add_relation_pair(self.fwd_relation, self.rev_relation) coll.self_test()
def test_set_caption(self): txt = 'some short description' item = dut.TraceableItem(self.identification) item.set_document(self.docname) item.set_caption(txt) self.assertEqual(txt, item.get_caption()) # Verify dict data = item.to_dict() self.assertEqual(self.identification, data['id']) self.assertEqual(txt, data['caption']) self.assertEqual(self.docname, data['document']) self.assertEqual(0, data['line']) self.assertEqual({}, data['targets']) item.self_test()
def test_get_attributes(self): item = dut.TraceableItem(self.identification) item.set_document(self.docname) self.assertEqual([''], item.get_attributes([self.attribute_key1])) item.add_attribute(self.attribute_key1, self.attribute_value1) self.assertEqual([self.attribute_value1], item.get_attributes([self.attribute_key1])) item.add_attribute(self.attribute_key2, self.attribute_value2) self.assertEqual([self.attribute_value1], item.get_attributes([self.attribute_key1])) self.assertEqual([self.attribute_value2], item.get_attributes([self.attribute_key2])) self.assertEqual([self.attribute_value1, self.attribute_value2], item.get_attributes([self.attribute_key1, self.attribute_key2])) self.assertEqual([self.attribute_value2, self.attribute_value1], item.get_attributes([self.attribute_key2, self.attribute_key1])) item.self_test()
def test_self_test_duplicate_relation(self): item = dut.TraceableItem(self.identification) item.set_document(self.docname) # Add a target item.add_target(self.fwd_relation, self.identification_tgt) # Self test should pass item.self_test() # Add same target (fails as it is not allowed) with self.assertRaises(exception.TraceabilityException): item.add_target(self.fwd_relation, self.identification_tgt) # Hack into class and add same relation anyway item.explicit_relations[self.fwd_relation].append(self.identification_tgt) # Self test should fail with self.assertRaises(exception.TraceabilityException): item.self_test()
def test_add_invalid_attribute(self): item = dut.TraceableItem(self.identification) item.set_document(self.docname) self.assertFalse(item.get_attribute(self.attribute_key1)) with self.assertRaises(exception.TraceabilityException): item.add_attribute(None, self.attribute_value1) with self.assertRaises(exception.TraceabilityException): item.add_attribute('', self.attribute_value1) with self.assertRaises(exception.TraceabilityException): item.add_attribute(self.attribute_key1, None) with self.assertRaises(exception.TraceabilityException): item.add_attribute(self.attribute_key1, '') with self.assertRaises(exception.TraceabilityException): item.add_attribute(self.attribute_key1, self.attribute_value_invalid) item.self_test()
def test_add_item_overwrite(self): coll = dut.TraceableCollection() item1 = item.TraceableItem(self.identification_src) item1.set_document(self.docname) coll.add_item(item1) coll.add_relation_pair(self.fwd_relation, self.rev_relation) coll.add_relation(self.identification_src, self.fwd_relation, self.identification_tgt) # Add target item: should update existing one (keeping relations) item2 = item.TraceableItem(self.identification_tgt) item2.set_document(self.docname) coll.add_item(item2) # Assert old relations are still there item1_out = coll.get_item(self.identification_src) item2_out = coll.get_item(self.identification_tgt) relations = item1_out.iter_targets(self.fwd_relation) self.assertEqual(1, len(relations)) self.assertEqual(relations[0], self.identification_tgt) relations = item2_out.iter_targets(self.rev_relation) self.assertEqual(1, len(relations)) self.assertEqual(relations[0], self.identification_src) # Assert item are not placeholders self.assertFalse(item1_out.is_placeholder()) self.assertFalse(item2_out.is_placeholder())
def test_self_test_invalid_attribute(self): item = dut.TraceableItem(self.identification) item.set_document(self.docname) # Add a valid attribute item.add_attribute(self.attribute_key, self.attribute_value1) # Self test should pass item.self_test() # Add invalid attribute (fails as it is not allowed) with self.assertRaises(exception.TraceabilityException): item.add_attribute(self.attribute_key, None) # Hack into class and add invalid attribute anyway item.attributes[self.attribute_key] = None # Self test should fail with self.assertRaises(exception.TraceabilityException): item.self_test()