def test_decode_success(self): # Test that encoded project tree node paths decode to the expected values. path = TreeNamePath.from_str('/abc/def') decoded_path = decode_project_tree_node_path(str(path).encode('utf-8')) self.assertEqual(path, decoded_path) path = TreeNamePath.from_str('/') decoded_path = decode_project_tree_node_path(str(path).encode('utf-8')) self.assertEqual(path, decoded_path)
def test_resolve_path_relative_success(self): path = TreeNamePath(['grandchildjkl'], False) self.assertIs(self._tree_4node_child0.resolve_path(path), self._tree_4node_grandchild) # Add another node so we can test multi-level relative paths. great_grandchild = NamedTreeNode('greatgrandchildmno', parent=self._tree_4node_grandchild) path = TreeNamePath(['grandchildjkl', 'greatgrandchildmno'], False) self.assertIs(self._tree_4node_child0.resolve_path(path), great_grandchild)
def test_path_names(self): # Test the path_names property. path = TreeNamePath([]) self.assertSequenceEqual(path.path_names, []) path_list = ['abc', 'def', 'ghi'] path = TreeNamePath(path_list) self.assertSequenceEqual(path.path_names, path_list) path = TreeNamePath(path_list, False) self.assertSequenceEqual(path.path_names, path_list)
def test_str(self): # Test conversion from TreeNamePath to string. path = TreeNamePath([]) self.assertEqual(str(path), '/') path = TreeNamePath([], False) self.assertEqual(str(path), '') path_list = ['123', '456', '7890'] path = TreeNamePath(path_list) self.assertEqual(str(path), '/123/456/7890') path = TreeNamePath(path_list, False) self.assertEqual(str(path), '123/456/7890')
def test_encode_success(self): # Test that project tree node paths encode to the expected values. data = encode_project_tree_node_path(TreeNamePath.from_str('/')) self.assertEqual(data.decode('utf-8'), '/') data = encode_project_tree_node_path( TreeNamePath.from_str('/childDir0')) self.assertEqual(data.decode('utf-8'), '/childDir0') data = encode_project_tree_node_path( TreeNamePath.from_str('/childDir1')) self.assertEqual(data.decode('utf-8'), '/childDir1') data = encode_project_tree_node_path( TreeNamePath.from_str('/childDir0/grandchildSeq')) self.assertEqual(data.decode('utf-8'), '/childDir0/grandchildSeq')
def test_from_str_success(self): # Test the from_str method in cases where it should succeed. path = TreeNamePath.from_str('/') self.assertSequenceEqual(path.path_names, []) self.assertTrue(path.is_absolute) path = TreeNamePath.from_str('') self.assertSequenceEqual(path.path_names, []) self.assertFalse(path.is_absolute) path = TreeNamePath.from_str('/abc/def/ghi') self.assertSequenceEqual(path.path_names, ['abc', 'def', 'ghi']) self.assertTrue(path.is_absolute) path = TreeNamePath.from_str('abc/def/ghi') self.assertSequenceEqual(path.path_names, ['abc', 'def', 'ghi']) self.assertFalse(path.is_absolute)
def test_immutable(self): # Test immutability of sequence returned by path_names property. path = TreeNamePath(['first', 'second']) path_names = path.path_names def mutate(): path_names[0] = 'ABCD' self.assertRaises(BaseException, mutate)
def test_resolve_path_relative_fail(self): path = TreeNamePath(['grandchildjkl'], False) def resolve(node): node.resolve_path(path) self.assertRaises(ValueError, resolve, self._tree_1node) self.assertRaises(ValueError, resolve, self._tree_4node_root) self.assertRaises(ValueError, resolve, self._tree_4node_child1) self.assertRaises(ValueError, resolve, self._tree_4node_grandchild)
def test_resolve_path_absolute_fail(self): path = TreeNamePath(['child1ghi', 'grandchildjkl']) def resolve(node): node.resolve_path(path) self.assertRaises(ValueError, resolve, self._tree_1node) self.assertRaises(ValueError, resolve, self._tree_4node_root) self.assertRaises(ValueError, resolve, self._tree_4node_child0) self.assertRaises(ValueError, resolve, self._tree_4node_child1) self.assertRaises(ValueError, resolve, self._tree_4node_grandchild)
def decode_project_tree_node_path(data): """Decodes a path-based reference to a project tree node. Suitable for use with the PROJECT_TREE_NODE_PATH_MEDIA_TYPE media type. Returns the decoded path as a TreeNamePath object. """ path = TreeNamePath.from_str(bytes(data).decode(MEDIA_STR_ENCODING)) if not path.is_absolute: raise ValueError( QCoreApplication.translate( 'MIMEData', 'Expected an absolute path, got a relative path.')) return path
def decode_seq_component_tree_node_path(data): """Decodes a path-based reference to a sequence component tree node. Suitable for use with the SEQUENCE_COMPONENT_TREE_NODE_PATH_MEDIA_TYPE media type. Returns the decoded paths as a 2-tuple containing the project tree node's absolute path and the sequence component node's absolute path. """ project_tree_path_bytes, seq_component_tree_path_bytes = pickle.loads( bytes(data)) project_tree_path = TreeNamePath.from_str( project_tree_path_bytes.decode(MEDIA_STR_ENCODING)) seq_component_tree_path = TreeNamePath.from_str( seq_component_tree_path_bytes.decode(MEDIA_STR_ENCODING)) if not project_tree_path.is_absolute: raise ValueError( QCoreApplication.translate( 'MIMEData', 'Expected an absolute path, got a relative path.')) if not seq_component_tree_path.is_absolute: raise ValueError( QCoreApplication.translate( 'MIMEData', 'Expected an absolute path, got a relative path.')) return (project_tree_path, seq_component_tree_path)
def test_hash(self): # Test that equal TreeNamePaths have equal hash values. path0 = TreeNamePath.from_str('/abc/def') path1 = TreeNamePath.from_str('/abc/def') self.assertEqual(hash(path0), hash(path1)) path0 = TreeNamePath.from_str('abc/def') path1 = TreeNamePath.from_str('abc/def') self.assertEqual(hash(path0), hash(path1)) path0 = TreeNamePath.from_str('/') path1 = TreeNamePath.from_str('/') self.assertEqual(hash(path0), hash(path1)) path0 = TreeNamePath.from_str('') path1 = TreeNamePath.from_str('') self.assertEqual(hash(path0), hash(path1))
def test_equality(self): # Test equality checking for TreeNamePaths. path0 = TreeNamePath.from_str('/abc/def') path1 = TreeNamePath.from_str('/abc/def') self.assertEqual(path0, path1) path0 = TreeNamePath.from_str('abc/def') path1 = TreeNamePath.from_str('/abc/def') self.assertNotEqual(path0, path1) path0 = TreeNamePath.from_str('abc/def') path1 = TreeNamePath.from_str('abc/ghi') self.assertNotEqual(path0, path1)
def test_is_absolute(self): # Test the is_absolute property using empty paths. path = TreeNamePath([]) self.assertTrue(path.is_absolute) path = TreeNamePath([], True) self.assertTrue(path.is_absolute) path = TreeNamePath([], False) self.assertFalse(path.is_absolute) # Test the is_absolute property using nonempty paths. path_list = ['name0', 'name1', 'name2'] path = TreeNamePath(path_list) self.assertTrue(path.is_absolute) path = TreeNamePath(path_list, True) self.assertTrue(path.is_absolute) path = TreeNamePath(path_list, False) self.assertFalse(path.is_absolute)
def test_encode_fail(self): # Test that project tree node path encoding fails when expected. self.assertRaises(ValueError, encode_project_tree_node_path, TreeNamePath.from_str('abc/def')) self.assertRaises(ValueError, encode_project_tree_node_path, TreeNamePath.from_str(''))