Skip to content
Snippets Groups Projects
Commit 5cc46db7 authored by Jedrzej Rybicki's avatar Jedrzej Rybicki
Browse files

storage more less tested

parent 4d039833
No related branches found
No related tags found
No related merge requests found
...@@ -8,13 +8,8 @@ from apiserver.config import ApiserverSettings ...@@ -8,13 +8,8 @@ from apiserver.config import ApiserverSettings
from .LocationStorage import (AbstractLocationDataStorageAdapter, LocationData, from .LocationStorage import (AbstractLocationDataStorageAdapter, LocationData,
LocationDataType) LocationDataType)
#from dataclasses import dataclass, asdict
#perhaps even that:
#from dataclasses_json import dataclass_json
from pydantic import BaseModel from pydantic import BaseModel
#@dataclass
class StoredData(BaseModel): class StoredData(BaseModel):
actualData: LocationData actualData: LocationData
users: List[str] users: List[str]
...@@ -50,6 +45,9 @@ class JsonFileStorageAdapter(AbstractLocationDataStorageAdapter): ...@@ -50,6 +45,9 @@ class JsonFileStorageAdapter(AbstractLocationDataStorageAdapter):
raise FileNotFoundError(f"The requested object ({oid}) does not exist.") raise FileNotFoundError(f"The requested object ({oid}) does not exist.")
return fullpath return fullpath
def __load_object(self, path):
return StoredData.parse_file(path)
def __get_unique_id(self, path: str) -> str: def __get_unique_id(self, path: str) -> str:
oid = str(uuid.uuid4()) oid = str(uuid.uuid4())
while (os.path.exists(os.path.join(path, oid))): while (os.path.exists(os.path.join(path, oid))):
...@@ -58,15 +56,12 @@ class JsonFileStorageAdapter(AbstractLocationDataStorageAdapter): ...@@ -58,15 +56,12 @@ class JsonFileStorageAdapter(AbstractLocationDataStorageAdapter):
def get_list(self, n_type: LocationDataType) -> List: def get_list(self, n_type: LocationDataType) -> List:
local_path = self.__setup_path(n_type.value) local_path = self.__setup_path(n_type.value)
allFiles = [f for f in os.listdir(
local_path) if os.path.isfile(os.path.join(local_path, f))]
# now each file has to be checked for its filename (= oid)
# and the LocationData name (which is inside the json)
retList = [] retList = []
for f in allFiles: for f in os.listdir(local_path):
data = StoredData.parse_file(os.path.join(local_path, f)) p = os.path.join(local_path, f)
#with open(os.path.join(local_path, f)) as file: if not os.path.isfile(p):
# data = json.load(file) continue
data = self.__load_object(p)
retList.append({data.actualData.name: f}) retList.append({data.actualData.name: f})
return retList return retList
...@@ -78,27 +73,20 @@ class JsonFileStorageAdapter(AbstractLocationDataStorageAdapter): ...@@ -78,27 +73,20 @@ class JsonFileStorageAdapter(AbstractLocationDataStorageAdapter):
json.dump(toStore.dict(), json_file) json.dump(toStore.dict(), json_file)
return (oid, data) return (oid, data)
def __load_object(self, path):
# move to data class?
with open(path, 'r') as f:
data = json.load(f)
return data
def get_details(self, n_type: LocationDataType, oid: str): def get_details(self, n_type: LocationDataType, oid: str):
full_path = self.__get_object_path(value=n_type.value, oid=oid) full_path = self.__get_object_path(value=n_type.value, oid=oid)
obj = self.__load_object(path=full_path) obj = self.__load_object(path=full_path)
return obj['actualData'] return obj.actualData
def update_details(self, n_type: LocationDataType, oid: str, data: LocationData, usr: str): def update_details(self, n_type: LocationDataType, oid: str, data: LocationData, usr: str):
# TODO: usr is ignored here?
full_path = self.__get_object_path(value=n_type.value, oid=oid) full_path = self.__get_object_path(value=n_type.value, oid=oid)
old_data = self.__load_object(path=full_path) obj = self.__load_object(path=full_path)
old_data['actualData']=data obj.actualData = data
full_path = self.__get_object_path(value=n_type.value, oid=oid) with open(full_path, 'w') as f:
with open(full_path, 'w') as file: json.dump(obj.dict(), f)
json.dump(old_data.json(), file)
return (oid, data) return (oid, data)
......
...@@ -12,7 +12,8 @@ class SomeTests(unittest.TestCase): ...@@ -12,7 +12,8 @@ class SomeTests(unittest.TestCase):
def setUp(self): def setUp(self):
Settings = namedtuple('Settings', ['json_storage_path']) Settings = namedtuple('Settings', ['json_storage_path'])
self.test_config = Settings('/tmp/json_test/') self.test_config = Settings('/tmp/json_test/')
pathlib.Path(self.test_config.json_storage_path).mkdir(parents=True, exist_ok=True) pathlib.Path(self.test_config.json_storage_path).mkdir(
parents=True, exist_ok=True)
self.store = JsonFileStorageAdapter(self.test_config) self.store = JsonFileStorageAdapter(self.test_config)
...@@ -33,25 +34,50 @@ class SomeTests(unittest.TestCase): ...@@ -33,25 +34,50 @@ class SomeTests(unittest.TestCase):
def test_add_new(self): def test_add_new(self):
d = LocationData(name='bla', url='local') d = LocationData(name='bla', url='local')
(oid, data) = self.store.add_new(n_type=LocationDataType.DATASET, data=d, usr='test_user') (oid, data) = self.store.add_new(
n_type=LocationDataType.DATASET, data=d, usr='test_user')
self.assertEquals(d, data, "Data should be equal") self.assertEquals(d, data, "Data should be equal")
self.assertIsNotNone(oid) self.assertIsNotNone(oid)
def test_add_and_read(self): def test_add_and_read(self):
l_data = LocationData(name='test1', url='http://n.go', metadata=[]) l_data = LocationData(name='test1', url='http://n.go', metadata=[])
(oid, data) = self.store.add_new(n_type=LocationDataType.DATASET, data=l_data, usr='test_user') (oid, data) = self.store.add_new(
n_type=LocationDataType.DATASET, data=l_data, usr='test_user')
self.assertEquals(l_data, data, "Data should be equal") self.assertEquals(l_data, data, "Data should be equal")
self.assertIsNotNone(oid) self.assertIsNotNone(oid)
print(data)
lst = self.store.get_list(n_type=LocationDataType.DATASET) lst = self.store.get_list(n_type=LocationDataType.DATASET)
self.assertEqual(len(lst), 1, 'One should be there') self.assertEqual(len(lst), 1, 'One should be there')
m_o = lst[0]
self.assertEquals(list(m_o.keys())[0], l_data.name)
self.assertEquals(list(m_o.values())[0], oid)
def test_get_details(self):
# get_details(self, n_type: LocationDataType, oid: str):
l_data = LocationData(name='test1', url='http://n.go', metadata=[])
(oid, data) = self.store.add_new(
n_type=LocationDataType.DATASET, data=l_data, usr='test_user')
self.assertEquals(l_data, data, "Data should be equal")
self.assertIsNotNone(oid)
details = self.store.get_details(
n_type=LocationDataType.DATASET, oid=oid)
self.assertEquals(l_data, details)
def test_nonexisting_details(self):
self.assertRaises(FileNotFoundError, self.store.get_details,
LocationDataType.DATASET, '42')
def test_update_details(self):
l_data = LocationData(name='test1', url='http://n.go', metadata=[])
new_data = LocationData(
name='test2', url='http://go.n', metadata={'key': 'value'})
(oid, data) = self.store.add_new(
n_type=LocationDataType.DATASET, data=l_data, usr='test_user')
self.assertEquals(l_data, data, "Data should be equal")
self.assertIsNotNone(oid)
(oid2, r) = self.store.update_details(n_type=LocationDataType.DATASET, oid=oid,
data=new_data, usr='tst2')
self.assertEquals(new_data, r)
self.assertEquals(oid, oid2)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment