Skip to content

Commit e29d518

Browse files
committed
[#28] fixed getRelatedModelsByID
1 parent 14e9b30 commit e29d518

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

odm2api/ODM2/services/readService.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ def getSimulationByActionID(self, actionID):
939939

940940
def getRelatedModelsByID(self, modelid):
941941
try:
942-
return self._session.query(Relatedmodel).filter_by(RelatedModelID=modelid).all()
942+
return self._session.query(RelatedModels).filter_by(ModelID=modelid).all()
943943
except:
944944
return None
945945

tests/test_odm2/data/populated.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15945,8 +15945,9 @@ CREATE TABLE Models (
1594515945
ModelLink VARCHAR (255) NULL
1594615946
);
1594715947
INSERT INTO "Models" VALUES(1,'swat','soil and water assessment tool','The Soil and Water Assessment Tool (SWAT) is a public domain Model jointly developed by USDA Agricultural Research Service (USDA-ARS) and Texas A&M AgriLife Research, part of The Texas A&M University System. SWAT is a small watershed to river basin-scale Model to simulate the quality and quantity of surface and ground water and predict the environmental impact of land use, land management practices, and climate change.',NULL,NULL);
15948+
INSERT INTO "Models" VALUES(2,'swmm','storm water management model','SWMM is a dynamic hydrology-hydraulic water quality simulation model. It is used for single event or long-term (continuous) simulation of runoff quantity and quality from primarily urban areas. The runoff component operates on a collection of sub catchment areas that receive precipitation and generate runoff and pollutant loads. The routing portion transports this runoff through a system of pipes, channels, storage/treatment devices, pumps, and regulators.','5.1.010','http://www.epa.gov/water-research/storm-water-management-model-swmm');
1594815949
CREATE TABLE RelatedModels (
15949-
RelatedID INTEGER NOT NULL PRIMARY KEY,
15950+
RelationID INTEGER NOT NULL PRIMARY KEY,
1595015951
ModelID INTEGER NOT NULL,
1595115952
RelationshipTypeCV VARCHAR (255) NOT NULL,
1595215953
RelatedModelID INTEGER NOT NULL,

tests/test_odm2/test_readservice.py

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,35 @@
44
from odm2api.ODM2 import models
55
from odm2api.ODMconnection import dbconnection
66
from odm2api.ODM2.services.readService import ReadODM2
7+
from sqlalchemy.orm import class_mapper
8+
import sqlalchemy
79

810
# run this test from the root directory using:
911
# python -m pytest tests/test_odm2/test_readservice.py
1012

13+
globals = {}
1114

1215

16+
def rawSql2Alchemy(rawsqlresult, sqlalchemyClass):
17+
"""
18+
converts the results of a raw sql select query into SqlAlchemy Model Object
19+
:param rawsqlresult: array of values, sql select results
20+
:param sqlalchemyModelObj: model object to convert into
21+
:return: populated model object
22+
"""
23+
24+
map = {}
25+
class_attributes = [prop.key for prop in class_mapper(sqlalchemyClass).iterate_properties
26+
if isinstance(prop, sqlalchemy.orm.ColumnProperty)]
27+
28+
for i in range(len(class_attributes)):
29+
map[class_attributes[i]] = rawsqlresult[i]
30+
31+
modelObj = sqlalchemyClass()
32+
modelObj.__dict__ = map
33+
return modelObj
34+
1335

14-
globals = {}
1536

1637
class TestReadService:
1738

@@ -81,8 +102,6 @@ def test_getAllModels(self):
81102

82103
assert len(res) == len(resapi)
83104

84-
85-
86105
def test_getModelByCode(self):
87106

88107
# get a model from the database
@@ -114,14 +133,22 @@ def test_getModelByCode(self):
114133

115134
def test_getRelatedModelsByID(self):
116135

117-
# get one model from the database
118-
res = self.engine.execute('SELECT * FROM Models').fetchone()
119-
model_id = res[0]
136+
# get all models from the database
137+
res = self.engine.execute('SELECT * FROM Models').fetchall()
138+
m1 = rawSql2Alchemy(res[0], models.Models)
139+
m2 = rawSql2Alchemy(res[1], models.Models)
140+
141+
142+
# create a relationship between them
143+
self.engine.execute('INSERT INTO RelatedModels Values(1, ?, "TestRelationship", ?)', (m1.ModelID), m2.ModelID)
144+
145+
120146

121147
# get related models by id using the api
122-
resapi = self.reader.getRelatedModelsByID(model_id)
148+
resapi = self.reader.getRelatedModelsByID(m1.ModelID)
123149

124-
assert False
150+
assert resapi is not None
151+
assert resapi[0].RelatedModelObj.ModelCode == 'swmm'
125152

126153
def test_getRelatedModelsByCode(self):
127154
pass

0 commit comments

Comments
 (0)