Skip to content

Commit 383c975

Browse files
committed
Add tests for the legacy use of passing a mapping in the conditions
argument in class Query. Also verify that a DeprecationWarning is raised.
1 parent 50a1063 commit 383c975

1 file changed

Lines changed: 144 additions & 0 deletions

File tree

tests/test_06_query.py

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,3 +1067,147 @@ def test_query_copy(client, entity, kwargs):
10671067
clone = query.copy()
10681068
assert str(clone) == str(query)
10691069
assert entity in clone.select_clause
1070+
1071+
def test_query_legacy_conditions_simple(client):
1072+
"""A simple query for an investigation by name.
1073+
Same as test_query_simple(), but pass the conditions as a mapping.
1074+
Deprecated, transitionally supported for backward compatibility.
1075+
"""
1076+
# The investigation is reused in other tests.
1077+
global investigation
1078+
name = "10100601-ST"
1079+
with pytest.deprecated_call():
1080+
query = Query(client, "Investigation", conditions={
1081+
"name": "= '%s'" % name
1082+
})
1083+
print(str(query))
1084+
assert "Investigation" in query.select_clause
1085+
assert query.join_clause is None
1086+
assert "name" in query.where_clause
1087+
assert query.order_clause is None
1088+
assert query.include_clause is None
1089+
assert query.limit_clause is None
1090+
res = client.search(query)
1091+
assert len(res) == 1
1092+
investigation = res[0]
1093+
assert investigation.BeanName == "Investigation"
1094+
assert investigation.name == name
1095+
1096+
def test_query_legacy_conditions_datafile(client):
1097+
"""Query a datafile by its name, dataset name, and investigation name.
1098+
Same as test_query_datafile(), but pass the conditions as a
1099+
mapping. Deprecated, transitionally supported for backward
1100+
compatibility.
1101+
"""
1102+
dfdata = {
1103+
'name': "e208945.nxs",
1104+
'dataset': "e208945",
1105+
'investigation': "12100409-ST",
1106+
}
1107+
conditions = {
1108+
"name": "= '%s'" % dfdata['name'],
1109+
"dataset.name": "= '%s'" % dfdata['dataset'],
1110+
"dataset.investigation.name": "= '%s'" % dfdata['investigation'],
1111+
}
1112+
with pytest.deprecated_call():
1113+
query = Query(client, "Datafile", conditions=conditions)
1114+
print(str(query))
1115+
assert "Datafile" in query.select_clause
1116+
assert "investigation" in query.join_clause
1117+
assert dfdata['investigation'] in query.where_clause
1118+
assert query.order_clause is None
1119+
assert query.include_clause is None
1120+
assert query.limit_clause is None
1121+
qstr = str(query)
1122+
res = client.search(query)
1123+
assert len(res) == 1
1124+
df = res[0]
1125+
assert df.BeanName == "Datafile"
1126+
assert df.name == dfdata['name']
1127+
1128+
# Same example, but use placeholders in the query string now.
1129+
conditions = {
1130+
"name": "= '%(name)s'",
1131+
"dataset.name": "= '%(dataset)s'",
1132+
"dataset.investigation.name": "= '%(investigation)s'",
1133+
}
1134+
with pytest.deprecated_call():
1135+
query = Query(client, "Datafile", conditions=conditions)
1136+
print(str(query))
1137+
print(str(query) % dfdata)
1138+
assert str(query) % dfdata == qstr
1139+
res = client.search(str(query) % dfdata)
1140+
assert len(res) == 1
1141+
assert res[0] == df
1142+
1143+
@pytest.mark.dependency(depends=['get_investigation'])
1144+
def test_query_legacy_conditions_instruments(client):
1145+
"""Query the instruments related to a given investigation.
1146+
Same as test_query_instruments(), but pass the conditions as a
1147+
mapping. Deprecated, transitionally supported for backward
1148+
compatibility.
1149+
"""
1150+
with pytest.deprecated_call():
1151+
query = Query(client, "Instrument",
1152+
order=["name"],
1153+
conditions={ "investigationInstruments.investigation.id":
1154+
"= %d" % investigation.id },
1155+
includes={"facility", "instrumentScientists.user"})
1156+
print(str(query))
1157+
assert "Instrument" in query.select_clause
1158+
assert "investigation" in query.join_clause
1159+
assert "id" in query.where_clause
1160+
assert "name" in query.order_clause
1161+
assert "instrumentScientists" in query.include_clause
1162+
res = client.search(query)
1163+
assert len(res) == 1
1164+
instr = res[0]
1165+
assert instr.BeanName == "Instrument"
1166+
assert instr.facility.BeanName == "Facility"
1167+
1168+
def test_query_legacy_conditions_list(client):
1169+
"""We may also add a list of conditions on a single attribute.
1170+
Same as test_query_multiple_conditions(), but pass the conditions
1171+
as a mapping. Deprecated, transitionally supported for backward
1172+
compatibility.
1173+
"""
1174+
condition = {
1175+
"datafileCreateTime": [">= '2012-01-01'", "< '2013-01-01'" ]
1176+
}
1177+
with pytest.deprecated_call():
1178+
query = Query(client, "Datafile", conditions=condition)
1179+
print(str(query))
1180+
assert "Datafile" in query.select_clause
1181+
assert query.join_clause is None
1182+
assert "datafileCreateTime" in query.where_clause
1183+
qstr = str(query)
1184+
res = client.search(query)
1185+
assert len(res) == 3 + have_icat_5
1186+
1187+
# The last example also works by adding the conditions separately.
1188+
query = Query(client, "Datafile")
1189+
with pytest.deprecated_call():
1190+
query.addConditions({"datafileCreateTime": ">= '2012-01-01'"})
1191+
with pytest.deprecated_call():
1192+
query.addConditions({"datafileCreateTime": "< '2013-01-01'"})
1193+
print(str(query))
1194+
assert str(query) == qstr
1195+
res = client.search(query)
1196+
assert len(res) == 3 + have_icat_5
1197+
1198+
def test_query_legacy_conditions_jpql_function_mixed(client):
1199+
"""Mix conditions with and without JPQL function on the same attribute.
1200+
Same as test_query_condition_jpql_function_mixed(), but pass the
1201+
conditions as a mapping. Deprecated, transitionally supported for
1202+
backward compatibility.
1203+
"""
1204+
conditions = { "name": "LIKE 'db/%'",
1205+
"LENGTH(fullName)": "> 11", "fullName": "> 'C'" }
1206+
with pytest.deprecated_call():
1207+
query = Query(client, "User", conditions=conditions)
1208+
print(str(query))
1209+
assert "User" in query.select_clause
1210+
assert query.join_clause is None
1211+
assert "LENGTH" in query.where_clause
1212+
res = client.search(query)
1213+
assert len(res) == 3

0 commit comments

Comments
 (0)