def test_projectservice_get_projectId(self): """ This unit tests the projectid, if it goes up, although we remove some projects. """ md = ProjectService(**get_opts()) portOwncloud = Port("port-owncloud", fileStorage=True) portInvenio = Port("port-invenio", fileStorage=True, metadata=True) id1 = md.addProject("admin", portIn=[]).researchId id2 = md.addProject("admin", portIn=[portOwncloud]).researchId id3 = md.addProject( "user", portIn=[portOwncloud], portOut=[portInvenio] ).researchId # we remove the first one, so there are only 2 projects left md.removeProject(researchId=id1) # save for later asserts id_old = id1 # now we add one project id1 = md.addProject("admin", portIn=[]).researchId # all id's should be different self.assertNotEqual(id1, id2) self.assertNotEqual(id3, id2) self.assertNotEqual(id3, id1) self.assertNotEqual(id_old, id1)
def __init__(self, user, status=Status.CREATED, portIn=None, portOut=None, researchname=None): if portIn is None: portIn = [] if portOut is None: portOut = [] self.user = user self.status = status self.portIn = [] self.portOut = [] self.researchname = researchname # test, if the port can be converted to port object try: for port in portIn: self.portIn.append(Port.fromDict(port)) for port in portOut: self.portOut.append(Port.fromDict(port)) except: self.portIn = portIn self.portOut = portOut
def test_projectservice_get_project(self): """ Check the getters for the projects """ md = ProjectService(**get_opts()) portOwncloud = Port("port-owncloud", fileStorage=True) portInvenio = Port("port-zenodo", fileStorage=True, metadata=True) md.addProject("admin", portIn=[]) md.addProject("admin", portIn=[portOwncloud]) proj = Project(user="******", portIn=[ portOwncloud], portOut=[portInvenio]) md.addProject(proj) self.assertEqual(md.getProject(researchId=0), Project("admin")) # the following is not equal, because the first project comes with a researchId self.assertNotEqual( md.getProject(researchId=0).getDict(), Project( "admin").getDict() ) self.assertEqual(md.getProject(researchId=2), proj) # check if the id is used as index relative to user, if username is set. self.assertEqual( md.getProject(user="******", researchIndex=1), Project("admin", portIn=[portOwncloud]), ) from lib.Exceptions.ProjectServiceExceptions import NotFoundIDError with self.assertRaises(NotFoundIDError): md.getProject(user="******", researchIndex=2)
def test_port_init(self): portSmall = Port("port-owncloud") portOwncloud = Port("port-owncloud", fileStorage=True) portInvenio = Port("port-invenio", fileStorage=True, metadata=True) expected = { "port": "port-owncloud", "properties": [] } self.assertEqual(portSmall.getDict(), expected) expected = { "port": "port-owncloud", "properties": [ {"portType": "fileStorage", "value": True} ] } self.assertEqual(portOwncloud.getDict(), expected) expected = { "port": "port-invenio", "properties": [ {"portType": "fileStorage", "value": True}, {"portType": "metadata", "value": True}, ] } self.assertEqual(portInvenio.getDict(), expected)
def test_project_changePort(self): project1 = Project("admin") project2 = Project("admin", portIn=[]) project3 = Project("admin", portIn=[], portOut=[]) portOwncloud = Port("port-owncloud") portOwncloud2 = Port("port-owncloud") portOwncloud3 = Port("port-owncloud") projOwncloud1 = Project("admin", portIn=[portOwncloud]) project1.addPortIn(portOwncloud) self.assertEqual(project1, projOwncloud1) custom = [ { "key": "filepath", "value": "/filepath/" } ] projOwncloud2 = Project("admin", portIn=[portOwncloud2]) portOwncloud.setProperty("customProperties", custom) project4 = Project("admin", portIn=[portOwncloud]) projOwncloud1.addPortIn(portOwncloud) self.assertEqual(project4, projOwncloud1) self.assertNotEqual(projOwncloud1, projOwncloud2) portOwncloud2.setProperty("customProperties", custom) self.assertEqual(projOwncloud1, projOwncloud2) projOwncloud2.addPortIn(portOwncloud3) self.assertNotEqual(projOwncloud1, projOwncloud2)
def test_projectservice_port_change(self): """ Check the methods, which changes the values within the object. """ md = ProjectService(**get_opts()) portOwncloud = Port("port-owncloud", fileStorage=True) portInvenio = Port("port-invenio", fileStorage=True, metadata=True) md.addProject("admin", portIn=[]) md.addProject("admin", portIn=[portOwncloud]) md.addProject("user", portIn=[portOwncloud], portOut=[portInvenio]) md.getProject(user="******", researchIndex=1)
def test_highest_index(self): md = ProjectService(**get_opts()) self.assertEqual(md.highest_index, 0) portOwncloud = Port("port-owncloud", fileStorage=True) portInvenio = Port("port-invenio", fileStorage=True, metadata=True) md.addProject("admin", portIn=[]) self.assertEqual(md.highest_index, 1) md.addProject("admin", portIn=[portOwncloud]) md.addProject("user", portIn=[portOwncloud], portOut=[portInvenio]) self.assertEqual(md.highest_index, 3)
def post(user_id, research_id): json = request.json logger.debug(f"got json: {json}") from lib.Port import Port portname = json["port"] fs = False md = False cp = None for prop in json.get("properties", []): if prop.get("portType", "") == "fileStorage": fs = prop["value"] elif prop.get("portType", "") == "metadata": md = prop["value"] elif prop.get("portType", "") == "customProperties": cp = prop["value"] logger.debug(f"parsed data: port: {portname}, fs: {fs}, metadata: {md}, cp: {cp}") p = Port(portname, fileStorage=fs, metadata=md, customProperties=cp) project = Singleton.ProjectService.getProject(user_id, int(research_id)) project.addPortIn(p) Singleton.ProjectService.setProject(user_id, project) return jsonify( Singleton.ProjectService.getProject(user_id, int(research_id)).getPortIn() )
def test_highest_index_redis(self): opts = get_opts(True) md = ProjectService(**opts) portOwncloud = Port("port-owncloud", fileStorage=True) portInvenio = Port("port-invenio", fileStorage=True, metadata=True) id1 = md.addProject("admin", portIn=[]).researchId id2 = md.addProject("admin", portIn=[portOwncloud]).researchId md2 = ProjectService(**opts) self.assertEqual(md.highest_index, 2) self.assertEqual(md2.highest_index, 2) id3 = md.addProject( "user", portIn=[portOwncloud], portOut=[portInvenio] ).researchId self.assertEqual(md.highest_index, md2.highest_index)
def test_projectservice_customProp(self): """ This unit tests the projectid, if it goes up, although we remove some projects. """ md = ProjectService(**get_opts()) custom = {"key": "serviceProjectId", "value": "12345"} portOwncloud = Port( "port-owncloud", fileStorage=True, customProperties=custom ) id1 = md.addProject("admin", portIn=[portOwncloud]).researchId # we remove the first one, so there are only 2 projects left self.assertEqual([portOwncloud], md.getProject( researchId=id1).getPortIn())
def test_project_removePort(self): project1 = Project("admin") project2 = Project("admin", portIn=[]) portOwncloud = Port("port-owncloud") projOwncloud1 = Project("admin", portIn=[portOwncloud]) project1.addPortIn(portOwncloud) self.assertEqual(project1, projOwncloud1) project1.removePortIn(portOwncloud) self.assertEqual(project1, Project("admin", portIn=[])) projOwncloud2 = Project("admin", portOut=[portOwncloud]) project2.addPortOut(portOwncloud) self.assertEqual(project2, projOwncloud2) project2.removePortOut(portOwncloud) self.assertEqual(project1, Project("admin", portOut=[]))
def test_service_ports(self): """ Check the setter for imports and portOuts """ md = ProjectService(**get_opts()) portOwncloud = Port("port-owncloud", fileStorage=True) portInvenio = Port("port-invenio", fileStorage=True, metadata=True) md.addProject("admin", portIn=[]) md.addProject("admin", portIn=[portOwncloud]) md.addProject("user", portIn=[portOwncloud], portOut=[portInvenio]) expected = [ { "userId": "admin", "researchId": 0, "researchIndex": 0, "status": Status.CREATED.value, "portIn": [], "portOut": [], "researchname": None, }, { "userId": "admin", "researchId": 1, "researchIndex": 1, "status": Status.CREATED.value, "portIn": [portOwncloud.getDict()], "portOut": [], "researchname": None, }, { "userId": "user", "researchId": 2, "researchIndex": 0, "status": Status.CREATED.value, "portIn": [portOwncloud.getDict()], "portOut": [portInvenio.getDict()], "researchname": None, }, ] self.assertEqual(md.getDict(), expected)
def test_project_addPort(self): project1 = Project("admin") project2 = Project("admin", portIn=[]) project3 = Project("admin", portIn=[], portOut=[]) portOwncloud = Port("port-owncloud") projOwncloud1 = Project("admin", portIn=[portOwncloud]) project1.addPortIn(portOwncloud) self.assertEqual(project1, projOwncloud1) projOwncloud2 = Project("admin", portOut=[portOwncloud]) project2.addPortOut(portOwncloud) self.assertEqual(project2, projOwncloud2, msg="{},{}".format( project2.getDict(), projOwncloud2.getDict())) projOwncloud3 = Project( "admin", portIn=[portOwncloud], portOut=[portOwncloud]) project3.addPortIn(portOwncloud) project3.addPortOut(portOwncloud) self.assertEqual(project3, projOwncloud3)
def test_projectservice_setProject(self): """ This unit tests the setProject method """ md = ProjectService(**get_opts()) custom = {"key": "serviceProjectId", "value": "12345"} portOwncloud = Port( "port-owncloud", fileStorage=True, customProperties=custom ) expected_proj1 = Project("admin") proj1 = md.addProject("admin", portIn=[]) self.assertEqual(expected_proj1, md.getProject( researchId=proj1.researchId)) expected_proj1.addPortIn(portOwncloud) proj1.addPortIn(portOwncloud) md.setProject(proj1.user, proj1) self.assertEqual(expected_proj1, md.getProject( researchId=proj1.researchId))
def post(user_id, research_id): json = request.json from lib.Port import Port fs = False md = False cp = None for prop in json.get("properties", []): if prop.get("portType", "") == "fileStorage": fs = prop["value"] elif prop.get("portType", "") == "metadata": md = prop["value"] elif prop.get("portType", "") == "customProperties": cp = prop["value"] p = Port(json["port"], fileStorage=fs, metadata=md, customProperties=cp) project = Singleton.ProjectService.getProject(user_id, int(research_id)) project.addPortOut(p) Singleton.ProjectService.setProject(user_id, project) return jsonify( Singleton.ProjectService.getProject(user_id, int(research_id)).getPortOut() )
def test_projectservice_remove_project(self): """ Check the remove method """ md = ProjectService(**get_opts()) portOwncloud = Port("port-owncloud", fileStorage=True) portInvenio = Port("port-invenio", fileStorage=True, metadata=True) id1 = md.addProject("admin", portIn=[]).researchId id2 = md.addProject("admin", portIn=[portOwncloud]).researchId id3 = md.addProject( "user", portIn=[portOwncloud], portOut=[portInvenio] ).researchId md.removeProject("admin", id1) expected = [ { "userId": "admin", "researchId": 0, "researchIndex": 0, "status": Status.DELETED.value, "portIn": [], "portOut": [], "researchname": None, }, { "userId": "admin", "researchId": 1, "researchIndex": 1, "status": Status.CREATED.value, "portIn": [portOwncloud.getDict()], "portOut": [], "researchname": None, }, { "userId": "user", "researchId": 2, "researchIndex": 0, "status": Status.CREATED.value, "portIn": [portOwncloud.getDict()], "portOut": [portInvenio.getDict()], "researchname": None, }, ] self.assertEqual([proj.getDict() for proj in md.getProject()], expected) expected[0]["status"] = Status.DELETED.value md.removeProject("admin", 0) self.assertEqual([proj.getDict() for proj in md.getProject()], expected) expected[2]["status"] = Status.DELETED.value md.removeProject("user") self.assertEqual([proj.getDict() for proj in md.getProject()], expected) from lib.Exceptions.ProjectServiceExceptions import ( NotFoundUserError, NotFoundIDError, ) with self.assertRaises(NotFoundUserError): md.removeProject("user") with self.assertRaises(NotFoundIDError): md.removeProject(researchId=2) with self.assertRaises(NotFoundUserError): md.removeProject("user", researchId=2) with self.assertRaises(NotFoundIDError): md.removeProject("user", researchIndex=2)
def test_port_customProperties(self): custom = [ { "key": "serviceProjectId", "value": "12345" } ] expected = { "port": "port-owncloud", "properties": [ { "portType": "customProperties", "value": custom } ] } portOwncloud = Port("port-owncloud", customProperties=custom) self.assertEqual(portOwncloud.getDict(), expected) expected["port"] = "port-zenodo" portZenodo = Port("port-zenodo") portZenodo.setProperty("customProperties", custom) self.assertEqual(portZenodo.getDict(), expected) self.assertEqual(portZenodo, Port.fromDict(portZenodo.getDict())) self.assertNotEqual(portOwncloud, Port.fromDict(portZenodo.getDict())) self.assertNotEqual(Port("port-zenodo"), Port.fromDict(portZenodo.getDict()))
def test_port_change(self): portOwncloud = Port("port-owncloud") expected = { "port": "port-owncloud", "properties": [] } self.assertEqual(portOwncloud.getDict(), expected) self.assertEqual(portOwncloud, Port("port-owncloud")) with self.assertRaises(ValueError): portOwncloud.setProperty(1, True) portOwncloud.setProperty("fileStorage", 1) self.assertFalse(portOwncloud.setProperty("not-found", True)) self.assertTrue(portOwncloud.setProperty("fileStorage", True)) expected = { "port": "port-owncloud", "properties": [ {"portType": "fileStorage", "value": True} ] } self.assertEqual(portOwncloud.getDict(), expected) self.assertEqual(portOwncloud, Port("port-owncloud", fileStorage=True)) portOwncloud.setProperty("metadata", True) expected = { "port": "port-owncloud", "properties": [ {"portType": "fileStorage", "value": True}, {"portType": "metadata", "value": True} ] } self.assertEqual(portOwncloud.getDict(), expected) self.assertEqual(portOwncloud, Port( "port-owncloud", fileStorage=True, metadata=True))