def setUp(self): super(TestLocalRepositoryStore, self).setUp() self.store = LocalRepositoryStore(self.mkdir('repo'))
class TestLocalRepositoryStore(TmpDirTestCase): def setUp(self): super(TestLocalRepositoryStore, self).setUp() self.store = LocalRepositoryStore(self.mkdir('repo')) def test_init_nonexistent(self): self.assertRaises(Exception, LocalRepositoryStore, self.tmppath('missing')) def test_get(self): with open(self.tmppath('repo', 'foo'), 'w') as f: f.write('bar') with self.store.get('foo') as f: self.assertEqual(f.read().decode(), 'bar') def test_get_metadata(self): with open(self.tmppath('repo', 'foo'), 'w') as f: f.write('bar') with open(self.tmppath('repo', 'foo.meta'), 'w') as f: f.write('{"baz": "quux"}') meta = self.store.get_metadata('foo') self.assertEqual(meta, {'baz': 'quux'}) def test_get_w_meta(self): with open(self.tmppath('repo', 'foo'), 'w') as f: f.write('bar') with open(self.tmppath('repo', 'foo.meta'), 'w') as f: f.write('{"baz": "quux"}') f, meta = self.store.get('foo', True) try: self.assertEqual(f.read().decode(), 'bar') self.assertEqual(meta, {'baz': 'quux'}) finally: f.close() def test_get_missing_meta(self): with open(self.tmppath('repo', 'foo'), 'w') as f: f.write('bar') f, meta = self.store.get('foo', True) try: self.assertEqual(f.read().decode(), 'bar') self.assertEqual(meta, {}) finally: f.close() def test_put(self): self.store.put('foo', 'bar') self.assertTMPPContents('bar', 'repo', 'foo') self.assertNotTMPPExists('repo', 'foo.meta') def test_can_put_in_memory_byte_stream(self): self.store.put('foo', BytesIO(b'bar')) self.assertTMPPContents('bar', 'repo', 'foo') def test_can_put_in_memory_unicode_stream(self): self.store.put('foo', StringIO(u'bar')) self.assertTMPPContents('bar', 'repo', 'foo') def test_can_put_unicode_string(self): self.store.put('foo', u'unicode bar') self.assertTMPPContents('unicode bar', 'repo', 'foo') def test_can_put_byte_string(self): self.store.put('foo', b'bytes bar') self.assertTMPPContents('bytes bar', 'repo', 'foo') def test_can_put_unicode_mode_fp(self): with open(self.tmppath('test'), 'w+') as f: f.write(u'some unicode string') f.seek(0) self.store.put('foo', f) self.assertTMPPContents('some unicode string', 'repo', 'foo') def test_can_put_binary_mode_fp(self): with open(self.tmppath('test'), 'w+b') as f: f.write(b'some byte data') f.seek(0) self.store.put('foo', f) self.assertTMPPContents('some byte data', 'repo', 'foo') def test_put_meta(self): self.store.put('foo', 'bar', {'baz': 'quux'}) self.assertTMPPContents('bar', 'repo', 'foo') self.assertTMPPContents('{"baz": "quux"}', 'repo', 'foo.meta') def test_delete(self): self.store.put('foo', 'bar') self.store.delete('foo') def test_delete_missing(self): self.assertRaises(KeyError, self.store.delete, 'foo') def test_delete_meta(self): self.store.put('foo', 'bar', {'baz': 'quux'}) self.store.delete('foo', metadata=True) def test_delete_missing_meta(self): self.store.put('foo', 'bar') # Error is ignored self.store.delete('foo', metadata=True) def test_list(self): with open(self.tmppath('repo', 'foo'), 'w') as f: f.write('quux') with open(self.tmppath('repo', 'bar'), 'w') as f: f.write('quux') self.mkdir('repo', 'baz') self.assertEqual(sorted(self.store.list()), ['baz']) self.assertEqual(sorted(self.store.list(files=True)), ['bar', 'baz', 'foo']) self.assertEqual(sorted(self.store.list(files=True, dirs=False)), ['bar', 'foo'])