Esempio n. 1
0
 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')
Esempio n. 2
0
 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)
Esempio n. 3
0
 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)
Esempio n. 4
0
 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()
Esempio n. 5
0
    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)
Esempio n. 6
0
 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()
Esempio n. 7
0
 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)
Esempio n. 8
0
 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']])
Esempio n. 9
0
 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)
Esempio n. 10
0
 def test_delete_nonexistent(self):
     delve1 = JsonDelver(self.file1.name)
     delve2 = JsonDelver(self.file2.name)
     linked = ChainedDelver(delve1, delve2)
     linked.delete('/nonexistent')
Esempio n. 11
0
 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)
Esempio n. 12
0
 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'))
Esempio n. 13
0
 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')
Esempio n. 14
0
 def test_nonexistent_file(self):
     with self.assertRaises(UnreadableFileError):
         JsonDelver('./nonexistent')
Esempio n. 15
0
 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')
Esempio n. 16
0
 def test_flyweight(self):
     first = JsonDelver(self.file.name)
     second = JsonDelver(self.file.name)
     self.assertIs(first, second)
Esempio n. 17
0
 def test_str(self):
     delve = JsonDelver(self.file.name)
     self.assertEqual(str(delve), Path(self.file.name).name)
Esempio n. 18
0
 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)
Esempio n. 19
0
 def test_default(self):
     delve = JsonDelver(self.file.name)
     sentinel = object()
     self.assertIs(delve.get('/nonexistent', sentinel), sentinel)
Esempio n. 20
0
 def test_flyweight_readonly_disagreement(self):
     first = JsonDelver(self.file.name, readonly=True)
     with self.assertRaises(InitializationConflict):
         second = JsonDelver(self.file.name, readonly=False)
Esempio n. 21
0
 def test_nonjson_file(self):
     otherfile = tempfile.NamedTemporaryFile('w+')
     otherfile.write('this is not valid json')
     with self.assertRaises(InvalidFileError):
         JsonDelver(otherfile.name)
Esempio n. 22
0
 def test_unreadable_file(self):
     Path(self.file.name).chmod(0o000)
     with self.assertRaises(UnreadableFileError):
         JsonDelver(self.file.name)
Esempio n. 23
0
 def test_init(self):
     delve = JsonDelver(self.file.name)
     self.assertEqual(delve.get(''), self.data)
Esempio n. 24
0
 def test_readonly(self):
     delve = JsonDelver(self.file.name, readonly=True)
     with self.assertRaises(ReadonlyError):
         delve.write()