def test_read(self): with Collection.open(self._fn_collection, mode='r') as c: self.assertEqual(len(list(c)), 3) with open(self._fn_collection, 'a') as file: file.write("{'a': 0}\n") # ill-formed JSON (single quotes instead of double quotes) with self.assertRaises(JSONParseError): with Collection.open(self._fn_collection, mode='r') as c: pass
def test_read(self): with Collection.open(self._fn_collection, mode="r") as c: assert len(list(c)) == 3 with open(self._fn_collection, "a") as file: file.write( "{'a': 0}\n" ) # ill-formed JSON (single quotes instead of double quotes) with pytest.raises(JSONParseError): with Collection.open(self._fn_collection, mode="r") as c: pass
def test_reopen(self): docs = [dict(_id=str(i)) for i in range(10)] with Collection.open(self._fn_collection) as c: c.update(docs) with Collection.open(self._fn_collection) as c: self.assertEqual(len(c), len(docs)) for doc in self.c: self.assertTrue(doc['_id'] in c)
def setUp(self): self._tmp_dir = TemporaryDirectory(prefix='signac_collection_') self._fn_json = os.path.join(self._tmp_dir.name, 'test.json') self.addCleanup(self._tmp_dir.cleanup) self.c = Collection.open(filename=':memory:') docs = [dict(_id=str(i)) for i in range(10)] self.c.update(docs) self.c.flush()
def test_file_size(self): docs = [dict(_id=str(i)) for i in range(10)] with open(self._fn_collection) as f: self.assertEqual(len(list(f)), 0) with Collection.open(self._fn_collection) as c: c.update(docs) with open(self._fn_collection) as f: self.assertEqual(len(list(f)), len(docs)) with Collection.open(self._fn_collection) as c: self.assertEqual(len(c), len(docs)) for doc in docs: c.replace_one({'_id': doc['_id']}, doc) with Collection.open(self._fn_collection) as c: self.assertEqual(len(c), len(docs)) with open(self._fn_collection) as f: self.assertEqual(len(list(f)), len(docs))
def test_file_size(self): docs = [dict(_id=str(i)) for i in range(10)] with open(self._fn_collection) as f: assert len(list(f)) == 0 with Collection.open(self._fn_collection) as c: c.update(docs) with open(self._fn_collection) as f: assert len(list(f)) == len(docs) with Collection.open(self._fn_collection) as c: assert len(c) == len(docs) for doc in docs: c.replace_one({"_id": doc["_id"]}, doc) with Collection.open(self._fn_collection) as c: assert len(c) == len(docs) with open(self._fn_collection) as f: assert len(list(f)) == len(docs)
def setUp(self, request): self._tmp_dir = TemporaryDirectory(prefix="signac_collection_") request.addfinalizer(self._tmp_dir.cleanup) self._fn_json = os.path.join(self._tmp_dir.name, "test.json") self.c = Collection.open(filename=":memory:") docs = [dict(_id=str(i)) for i in range(10)] self.c.update(docs) self.c.flush()
def test_write_flush_and_reopen(self): docs = [dict(_id=str(i)) for i in range(10)] self.c.update(docs) self.c.flush() assert os.path.getsize(self._fn_collection) > 0 with Collection.open(self._fn_collection) as c: assert len(c) == len(docs) for doc in self.c: assert doc["_id"] in c
def test_write_flush_and_reopen(self): docs = [dict(_id=str(i)) for i in range(10)] self.c.update(docs) self.c.flush() self.assertGreater(os.path.getsize(self._fn_collection), 0) with Collection.open(self._fn_collection) as c: self.assertEqual(len(c), len(docs)) for doc in self.c: self.assertIn(doc['_id'], c)
def test_write_on_readonly(self): c = Collection.open(self._fn_collection, mode="r") assert len(list(c)) == 10 c.insert_one(dict()) assert len(list(c)) == 11 with pytest.raises(io.UnsupportedOperation): c.flush() with pytest.raises(io.UnsupportedOperation): c.close() with pytest.raises(RuntimeError): c.find()
def test_write_on_readonly(self): c = Collection.open(self._fn_collection, mode='r') self.assertEqual(len(list(c)), 10) c.insert_one(dict()) self.assertEqual(len(list(c)), 11) with self.assertRaises(io.UnsupportedOperation): c.flush() with self.assertRaises(io.UnsupportedOperation): c.close() with self.assertRaises(RuntimeError): c.find()
def test_compression_level(self): docs = [dict(_id=str(i)) for i in range(10)] self.c.update(docs) self.c.flush() fn_txt = self._fn_collection + '.txt' with Collection.open(fn_txt) as c_text: c_text.update(self.c) size_txt = os.path.getsize(fn_txt) size_gz = os.path.getsize(self._fn_collection) self.assertGreater(size_txt, 0) self.assertGreater(size_gz, 0) compresslevel = size_txt / size_gz self.assertGreater(compresslevel, 1.0)
def test_compression_level(self): docs = [dict(_id=str(i)) for i in range(10)] self.c.update(docs) self.c.flush() fn_txt = self._fn_collection + ".txt" with Collection.open(fn_txt) as c_text: c_text.update(self.c) size_txt = os.path.getsize(fn_txt) size_gz = os.path.getsize(self._fn_collection) assert size_txt > 0 assert size_gz > 0 compresslevel = size_txt / size_gz assert compresslevel > 1.0
def setUp(self): self._tmp_dir = TemporaryDirectory(prefix='signac_collection_') self._fn_collection = os.path.join(self._tmp_dir.name, 'test.txt') self.addCleanup(self._tmp_dir.cleanup) with Collection.open(self._fn_collection, 'w') as c: c.update([dict(_id=str(i)) for i in range(10)])
def setUp(self): self.c = Collection.open(filename=":memory:", compresslevel=9)
def test_read(self): c = Collection.open(self._fn_collection, mode='r') self.assertEqual(len(list(c)), 10) self.assertEqual(len(list(c)), 10) self.assertEqual(len(c.find()), 10) c.close()
def setUp(self, request): self._tmp_dir = TemporaryDirectory(prefix="signac_collection_") request.addfinalizer(self._tmp_dir.cleanup) self._fn_collection = os.path.join(self._tmp_dir.name, self.filename) self.c = Collection.open(self._fn_collection, mode=self.mode) request.addfinalizer(self.c.close)
def setUp(self): self._tmp_dir = TemporaryDirectory(prefix='signac_collection_') self._fn_collection = os.path.join(self._tmp_dir.name, 'test.txt') self.addCleanup(self._tmp_dir.cleanup) self.c = Collection.open(self._fn_collection, mode=self.mode) self.addCleanup(self.c.close)
def test_read(self): c = Collection.open(self._fn_collection, mode="r") assert len(list(c)) == 10 assert len(list(c)) == 10 assert len(c.find()) == 10 c.close()
def setUp(self, request): self._tmp_dir = TemporaryDirectory(prefix="signac_collection_") request.addfinalizer(self._tmp_dir.cleanup) self._fn_collection = os.path.join(self._tmp_dir.name, "test.txt") with Collection.open(self._fn_collection, "w") as c: c.update([dict(_id=str(i)) for i in range(10)])