Skip to content
Snippets Groups Projects
Commit 9576c6b4 authored by Nils Weber's avatar Nils Weber
Browse files

Added LocationOccurences

parent e7c62513
No related branches found
Tags 11_wwu.1
No related merge requests found
Pipeline #172734 passed
......@@ -8,7 +8,7 @@ from typing import BinaryIO, Dict, List, Optional, Any
from requests import Response
from sampledbapi import SampleDBObject, getData, locations, postData, putData
from sampledbapi import SampleDBObject, getData, locations, users, postData, putData
from sampledbapi.users import User
__all__ = ["Object", "File", "Comment", "getList", "get",
......@@ -212,7 +212,36 @@ class Object(SampleDBObject):
else:
raise TypeError()
#TODO LocationOccurence
def getLocationOccurences(self) -> List[LocationOccurence]:
"""
Get a list of all object locations assignments for a specific object.
Args:
Returns:
List: `See here. <https://scientific-it-systems.iffgit.fz-juelich.de/SampleDB/developer_guide/api.html#reading-a-list-of-an-object-s-locations>`__
"""
return [LocationOccurence(i) for i in getData(f"objects/{self.object_id}/locations")]
def getLocationOccurence(self, location_id: int) -> LocationOccurence:
"""
Get a specific object location assignment (index) for a specific object.
Args:
location_id (int) : ID of the location
Returns:
LocationOccurence: `See here. <https://scientific-it-systems.iffgit.fz-juelich.de/SampleDB/developer_guide/api.html#reading-an-object-s-location>`__
"""
if isinstance(location_id, int):
return LocationOccurence(getData(
f"objects/{self.object_id}/locations/{location_id}"
))
else:
raise TypeError()
def getFileList(self) -> List:
"""Get a list of all files.
......@@ -431,6 +460,31 @@ class File(SampleDBObject):
def __repr__(self) -> str:
return f"File {self.file_id}, " \
+ f"Name {self.original_file_name}"
class LocationOccurence(SampleDBObject):
object_id: Optional[int] = None
location: Optional[locations.Location] = None
responsible_user: Optional[User] = None
user: Optional[User] = None
description: Optional[str] = None
utc_datetime: Optional[datetime] = None
def __init__(self, d: Dict):
"""Initialize a new instrument from dictionary."""
super().__init__(d)
if "location" in d:
self.location = locations.get(d["location"])
if "responsible_user" in d:
self.responsible_user = users.get(d["responsible_user"])
if "user" in d:
self.user = users.get(d["user"])
if "utc_datetime" in d:
self.utc_datetime = datetime.strptime(d["utc_datetime"], '%Y-%m-%dT%H:%M:%S.%f')
def __repr__(self) -> str:
return f"LocationOccurence of object {self.object_id} " \
+ "(at {self.location.name})"
class Comment(SampleDBObject):
......
......@@ -2,7 +2,7 @@ import pytest
import io
import tempfile
from sampledbapi import objects
from test import test_authentication
from test import test_authentication, test_users, test_locations
def mock_object():
return '''{
......@@ -22,6 +22,19 @@ def mock_permission():
def mock_permissions():
return '{"1": "TestPerm", "2": "TestPerm", "3": "TestPerm"}'
def mock_locationOccurence():
return '''{
"object_id": 1,
"location": 1,
"responsible_user": 1,
"user": 1,
"description": "TestDescription",
"utc_datetime": "2022-11-21T09:39:08.470159"
}'''
def mock_locationOccurences():
return f'[{mock_locationOccurence()},{mock_locationOccurence()},{mock_locationOccurence()}]'
def mock_file():
return '''{
"object_id": 1,
......@@ -66,6 +79,10 @@ class TestObjects():
requests_mock.get("http://128.176.208.107:8000/api/v1/objects/1/permissions/projects", text=mock_permissions())
requests_mock.get("http://128.176.208.107:8000/api/v1/objects/1/permissions/projects/1", text=mock_permission())
requests_mock.put("http://128.176.208.107:8000/api/v1/objects/1/permissions/projects/1")
requests_mock.get("http://128.176.208.107:8000/api/v1/objects/1/locations", text=mock_locationOccurences())
requests_mock.get("http://128.176.208.107:8000/api/v1/objects/1/locations/1", text=mock_locationOccurence())
requests_mock.get("http://128.176.208.107:8000/api/v1/locations/1", text=test_locations.mock_location())
requests_mock.get("http://128.176.208.107:8000/api/v1/users/1", text=test_users.mock_user())
requests_mock.get("http://128.176.208.107:8000/api/v1/objects/1/files", text=mock_files())
requests_mock.get("http://128.176.208.107:8000/api/v1/objects/1/files/1", text=mock_file())
requests_mock.post("http://128.176.208.107:8000/api/v1/objects/1/files/")
......@@ -199,8 +216,27 @@ class TestObjects():
def test_setProjectGroupPermissions_success(self, requests_mock):
objects.get(1).setProjectGroupPermissions(1, 'TestPerm')
#def test_locationOccurence(self, requests_mock):
# assert False
def test_getLocationOccurences(self, requests_mock):
locOccs = objects.get(1).getLocationOccurences()
assert len(locOccs) == 3
def test_getLocationOccurence_fail(self, requests_mock):
with pytest.raises(TypeError):
objects.get(1).getLocationOccurence('Test')
def test_getLocationOccurence_success(self, requests_mock):
locOcc = objects.get(1).getLocationOccurence(1)
assert locOcc is not None
def test_getLocationOccurence_properties(self, requests_mock):
locOcc = objects.get(1).getLocationOccurence(1)
assert locOcc.object_id == 1
assert locOcc.location is not None
assert locOcc.responsible_user is not None
assert locOcc.user is not None
assert locOcc.description == 'TestDescription'
assert locOcc.utc_datetime.strftime('%Y-%m-%dT%H:%M:%S.%f') == '2022-11-21T09:39:08.470159'
assert 'LocationOccurence of object' in repr(locOcc)
def test_getFileList(self, requests_mock):
files = objects.get(1).getFileList()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment