def test_init(self, tmpdir): # Test default instantiation s = JsonStorage(tmpdir.strpath) assert s.path == os.path.join(tmpdir.strpath, 'results.json') assert s.backups == 1 # Test with supplied filename s = JsonStorage(os.path.join(tmpdir.strpath, 'foo.json'), keep_previous=5) assert s.path == os.path.join(tmpdir.strpath, 'foo.json') assert s.backups == 5 # Test with nonexistent path with pytest.raises(OSError): JsonStorage('/foo/bar') with pytest.raises(OSError): JsonStorage('/foo/bar/baz.json')
def test_load(self, tmpdir, setup_dummy_models): s = JsonStorage(tmpdir.strpath) models = setup_dummy_models # convert models to json json_models = [] for model in models: if isinstance(model, Model): m = model.to_json() json_models.append(m) # save models to file with open(os.path.join(tmpdir.strpath, 'results.json'), 'w') as json_file: json.dump(json_models, json_file) loaded = s.load() assert loaded == models
def backend_factory(path, *args, **kwargs): """Create a database backend interface. Parameters ---------- path : str The URL of the database. For DBMS systems, supply the full URL with protocol (e.g. "mongodb://<user>:<password>@<ip>:<port>"). For local JSON file storage, supply the path to the file or directory to save data to. Other Parameters ---------------- *args Arguments to the backend storage object. **kwargs Keyword arguments to the backend storage object. Returns ------- backend : {`pyrameter.db.JsonStorage`,`pyrameter.db.MongoStorage`} The requested backend storage object. Raises ------ InvalidDBPathError Raised when the provided path does not lead to valid db. See Also -------- `pyrameter.db.local.LocalStorage` `pyrameter.db.mongo.MongoStorage` """ if isinstance(path, BaseStorage): return path try: if isinstance(path, string_types) and path.find('mongodb://') == 0: from pyrameter.db.mongo import MongoStorage return MongoStorage(path, *args, **kwargs) else: from pyrameter.db.local import JsonStorage return JsonStorage(path=path, *args, **kwargs) except (TypeError, AttributeError): raise InvalidDBPathError(path)
def test_save(self, tmpdir, setup_dummy_models): s = JsonStorage(tmpdir.strpath) # Test with no models s.save([]) assert os.path.isfile(os.path.join(tmpdir.strpath, 'results.json')) # Test with single model models = [] models.append(Model()) s.save(models) json_list = [] for model in models: json_list.append(model.to_json()) json_list = json.dumps(json_list) with open(os.path.join(tmpdir.strpath, 'results.json')) as json_file: data = json.load(json_file) data = json.dumps(data) assert data == json_list # Test with multiple models models = setup_dummy_models s.save(models) json_list = [] for model in models: json_list.append(model.to_json()) json_list = json.dumps(json_list) with open(os.path.join(tmpdir.strpath, 'results.json')) as json_file: data = json.load(json_file) data = json.dumps(data) assert data == json_list
def test_save(self, tmpdir, dummy_models): s = JsonStorage(tmpdir.strpath) # Test with no models s.save([]) assert os.path.isfile(os.path.join(tmpdir.strpath, 'results.json')) with open(os.path.join(tmpdir.strpath, 'results.json'), 'r') as f: data = json.load(f) assert data == [] # Test with single model models = [] models.append(Model()) s.save(models) json_list = [] for model in models: json_list.append(json.loads(json.dumps(model.to_json()))) with open(os.path.join(tmpdir.strpath, 'results.json')) as json_file: data = json.load(json_file) assert data == json_list # Test with multiple models models = dummy_models s.save(models) json_list = [] for model in models: json_list.append(json.loads(json.dumps(model.to_json()))) with open(os.path.join(tmpdir.strpath, 'results.json')) as json_file: data = json.load(json_file) for i in range(len(json_list)): assert data[i] == json_list[i]
def test_load(self, tmpdir, dummy_models, save_dummy_models): for path in save_dummy_models: s = JsonStorage(path) loaded = s.load() for l in loaded: assert l in dummy_models