Esempio n. 1
0
    def test_json(self):
        filename = '/tmp/testdict.json'
        remove(filename)
        storage = _Storage(filename, file_format='json')

        storage['name'] = 'jon'
        storage.update({'answer': '42'})
        storage.close()

        storage2 = _Storage(filename, file_format='json')
        self.assertEqual(sorted(storage.items()), sorted(storage2.items()))
        self.assertEqual(2, len(storage2.items()))
        self.assertTrue('name' in storage2.keys())
        self.assertTrue('answer' in storage2.keys())
        self.assertEqual('jon', storage2.pop('name'))
        self.assertEqual('42', storage2['answer'])

        remove(filename)
Esempio n. 2
0
    def test_pickle(self):
        filename = '/tmp/testdict.pickle'
        remove(filename)
        storage = _Storage(filename, file_format='pickle')

        storage['name'] = 'jon'
        storage.update({'answer': 42})
        storage.close()

        storage2 = _Storage(filename, file_format='pickle')
        self.assertEqual(storage, storage2)
        self.assertEqual(2, len(storage2.items()))
        self.assertTrue('name' in storage2.keys())
        self.assertTrue('answer' in storage2.keys())
        self.assertEqual('jon', storage2.pop('name'))
        self.assertEqual(42, storage2['answer'])

        remove(filename)
Esempio n. 3
0
    def test_pickle(self):
        filename = '/tmp/testdict.pickle'
        remove(filename)
        storage = _Storage(filename, file_format='pickle')

        storage['name'] = 'jon'
        storage.update({'answer': 42})
        storage.close()

        storage2 = _Storage(filename, file_format='pickle')
        self.assertEqual(storage, storage2)
        self.assertEqual(2, len(storage2.items()))
        self.assertTrue('name' in storage2.keys())
        self.assertTrue('answer' in storage2.keys())
        self.assertEqual('jon', storage2.pop('name'))
        self.assertEqual(42, storage2['answer'])

        remove(filename)
Esempio n. 4
0
    def test_json(self):
        filename = '/tmp/testdict.json'
        remove(filename)
        storage = _Storage(filename, file_format='json')

        storage['name'] = 'jon'
        storage.update({'answer': '42'})
        storage.close()

        storage2 = _Storage(filename, file_format='json')
        self.assertEqual(sorted(storage.items()), sorted(storage2.items()))
        self.assertEqual(2, len(storage2.items()))
        self.assertTrue('name' in storage2.keys())
        self.assertTrue('answer' in storage2.keys())
        self.assertEqual('jon', storage2.pop('name'))
        self.assertEqual('42', storage2['answer'])

        remove(filename)
    def test_clear(self):
        filename = '/tmp/testclear.json'
        storage = _Storage(filename, file_format='json')
        storage['name'] = 'jon'
        storage.sync()

        # dict with single value is now saved to disk
        with open(filename) as inp:
            self.assertEqual(inp.read(), '{"name":"jon"}')

        # now clear the dict, it should sync to disk.
        storage.clear()
        with open(filename) as inp:
            self.assertEqual(inp.read(), '{}')