def test_cd(self): delve1 = JsonDelver(self.file1.name) delve2 = JsonDelver(self.file2.name) linked = ChainedDelver(delve1, delve2) subset = linked.cd('/dict') self.assertEqual(subset.get('/a'), 'A') self.assertEqual(subset.get('/x'), 'X')
def test_default_merge(self): delve1 = JsonDelver(self.file1.name) delve2 = JsonDelver(self.file2.name) linked = ChainedDelver(delve1, delve2) sentinel = object() self.assertIs(linked.get('/nonexistent', sentinel, strategy=FindStrategy.MERGE), sentinel)
def test_get_first(self): delve1 = JsonDelver(self.file1.name) delve2 = JsonDelver(self.file2.name) linked = ChainedDelver(delve1, delve2) self.assertEqual(linked.get('/string', strategy=FindStrategy.FIRST), 'other') self.assertEqual(linked.get('/list/0', strategy=FindStrategy.FIRST), 'some') self.assertEqual(linked.get('/nonexistent'), None)
def test_symlink(self): alternate = Path('/tmp/symlink') try: alternate.symlink_to(self.file.name) soft = JsonDelver(alternate) hard = JsonDelver(self.file.name) self.assertIs(hard, soft) finally: alternate.unlink()
def test_eq(self): delve1 = JsonDelver(self.file1.name) delve2 = JsonDelver(self.file2.name) delve3 = DataDelver(delve1.get('').copy()) linked = ChainedDelver(delve1, delve2) notequal = ChainedDelver(delve2, delve1) equal = ChainedDelver(delve3, delve2) self.assertNotEqual(linked, notequal) self.assertEqual(linked, equal)
def test_symlink(self): link = Path('/tmp/symlink') try: link.symlink_to(self.file1.name) delve1 = JsonDelver(self.file1.name) delve2 = JsonDelver(link) with self.assertRaises(DuplicateInChainError): ChainedDelver(delve1, delve2) finally: link.unlink()
def test_get_merge(self): delve1 = JsonDelver(self.file1.name) delve2 = JsonDelver(self.file2.name) linked = ChainedDelver(delve1, delve2) self.assertEqual(linked.get('/list', strategy=FindStrategy.MERGE), ['string', 1, None, True, 'some', 'more']) self.assertEqual(linked.get('/dict', strategy=FindStrategy.MERGE), {'a': 'A', 'b': 'B', 'x': 'X', 'y': 'Y'}) self.assertEqual(linked.get('/sometimes', strategy=FindStrategy.MERGE), ['here']) with self.assertRaises(MergeError): linked.get('/string', strategy=FindStrategy.MERGE)
def test_get_collect(self): delve1 = JsonDelver(self.file1.name) delve2 = JsonDelver(self.file2.name) linked = ChainedDelver(delve1, delve2) self.assertEqual(linked.get('/string', strategy=FindStrategy.COLLECT), ['other', 'value']) self.assertEqual(linked.get('/list', strategy=FindStrategy.COLLECT), [['some', 'more'], ['string', 1, None, True]]) self.assertEqual(linked.get('/dict', strategy=FindStrategy.COLLECT), [{'x': 'X', 'y': 'Y'}, {'a': 'A', 'b': 'B'}]) self.assertEqual(linked.get('/sometimes', strategy=FindStrategy.COLLECT), [['here']])
def test_default(self): delve1 = JsonDelver(self.file1.name) delve2 = JsonDelver(self.file2.name) linked = ChainedDelver(delve1, delve2) sentinel = object() self.assertIs(linked.get('/nonexistent', sentinel), sentinel)
def test_delete_nonexistent(self): delve1 = JsonDelver(self.file1.name) delve2 = JsonDelver(self.file2.name) linked = ChainedDelver(delve1, delve2) linked.delete('/nonexistent')
def test_flyweight_redundant_init(self): first = JsonDelver(self.file.name) first.set('/new', 'something') second = JsonDelver(self.file.name) self.assertEqual(second.get('/new'), 'something') self.assertIs(first, second)
def test_delete(self): delve1 = JsonDelver(self.file1.name) delve2 = JsonDelver(self.file2.name) linked = ChainedDelver(delve1, delve2) linked.delete('/list') self.assertIsNone(linked.get('/list'))
def test_delete_readonly(self): delve1 = JsonDelver(self.file1.name) delve2 = JsonDelver(self.file2.name, readonly=True) linked = ChainedDelver(delve1, delve2) with self.assertRaises(ReadonlyError): linked.delete('/list')
def test_nonexistent_file(self): with self.assertRaises(UnreadableFileError): JsonDelver('./nonexistent')
def test_set(self): delve1 = JsonDelver(self.file1.name) delve2 = JsonDelver(self.file2.name) linked = ChainedDelver(delve1, delve2) linked.set('/new', 'foo') self.assertEqual(linked.get('/new'), 'foo')
def test_flyweight(self): first = JsonDelver(self.file.name) second = JsonDelver(self.file.name) self.assertIs(first, second)
def test_str(self): delve = JsonDelver(self.file.name) self.assertEqual(str(delve), Path(self.file.name).name)
def test_write(self): delve = JsonDelver(self.file.name) delve.set('/newkey', 'something') delve.write() written = json.load(open(self.file.name, 'r')) self.assertEqual(written, delve.data)
def test_default(self): delve = JsonDelver(self.file.name) sentinel = object() self.assertIs(delve.get('/nonexistent', sentinel), sentinel)
def test_flyweight_readonly_disagreement(self): first = JsonDelver(self.file.name, readonly=True) with self.assertRaises(InitializationConflict): second = JsonDelver(self.file.name, readonly=False)
def test_nonjson_file(self): otherfile = tempfile.NamedTemporaryFile('w+') otherfile.write('this is not valid json') with self.assertRaises(InvalidFileError): JsonDelver(otherfile.name)
def test_unreadable_file(self): Path(self.file.name).chmod(0o000) with self.assertRaises(UnreadableFileError): JsonDelver(self.file.name)
def test_init(self): delve = JsonDelver(self.file.name) self.assertEqual(delve.get(''), self.data)
def test_readonly(self): delve = JsonDelver(self.file.name, readonly=True) with self.assertRaises(ReadonlyError): delve.write()