Skip to content

Commit fae54a7

Browse files
committed
fix return format for mapless,convert sets to lists, update tests
1 parent df2b245 commit fae54a7

File tree

2 files changed

+56
-40
lines changed

2 files changed

+56
-40
lines changed

pharus/component_interface.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,19 @@ def __init__(
158158
)
159159
for s, t in (_.split(".") for _ in component_config["tables"])
160160
]
161-
self.parents = set(
162-
[
163-
p
164-
for t in self.tables
165-
for p in t.parents(as_objects=True)
166-
if p.full_table_name not in (t.full_table_name for t in self.tables)
167-
]
161+
self.parents = sorted(
162+
list(
163+
set(
164+
[
165+
p
166+
for t in self.tables
167+
for p in t.parents(as_objects=True)
168+
if p.full_table_name
169+
not in (t.full_table_name for t in self.tables)
170+
]
171+
)
172+
),
173+
key=lambda p: p.full_table_name,
168174
)
169175

170176
def dj_query_route(self):
@@ -205,11 +211,13 @@ def dj_query_route(self):
205211
return "Insert successful"
206212

207213
def fields_route(self):
208-
parent_attributes = set(sum([p.primary_key for p in self.parents], []))
214+
parent_attributes = sorted(
215+
list(set(sum([p.primary_key for p in self.parents], [])))
216+
)
209217
source_fields = {
210218
**{
211219
(p_name := f"{p.database}.{dj.utils.to_camel_case(p.table_name)}"): {
212-
"values": p.fetch("KEY", as_dict=True),
220+
"values": p.fetch("KEY"),
213221
"type": "table",
214222
"name": p_name,
215223
}
@@ -224,7 +232,7 @@ def fields_route(self):
224232
}
225233

226234
if not self.fields_map:
227-
return dict(fields=source_fields)
235+
return dict(fields=list(source_fields.values()))
228236
return dict(
229237
fields=[
230238
dict(

tests/test_form.py

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
def test_insert_map(token, client, connection, schemas_simple):
1212
REST_response = client.post(
1313
"/insert",
14-
json={"A Id": 1, "B Id": 32, "B Number": 1.23, "C Id": 400, "c_int": 99},
14+
json={
15+
"submissions": [
16+
{"A Id": 1, "B Id": 32, "B Number": 1.23, "C Id": 400, "c_int": 99}
17+
]
18+
},
1519
headers=dict(Authorization=f"Bearer {token}"),
1620
)
1721
assert REST_response.status_code == 200, REST_response.data
@@ -21,7 +25,11 @@ def test_insert_map(token, client, connection, schemas_simple):
2125
def test_insert_no_map(token, client, connection, schemas_simple):
2226
REST_response = client.post(
2327
"/insert3",
24-
json={"a_id": 1, "b_id": 32, "b_number": 1.23, "c_id": 400, "c_int": 99},
28+
json={
29+
"submissions": [
30+
{"a_id": 1, "b_id": 32, "b_number": 1.23, "c_id": 400, "c_int": 99}
31+
]
32+
},
2533
headers=dict(Authorization=f"Bearer {token}"),
2634
)
2735
assert REST_response.status_code == 200
@@ -31,7 +39,7 @@ def test_insert_no_map(token, client, connection, schemas_simple):
3139
def test_insert_fail(token, client, connection, schemas_simple):
3240
REST_response = client.post(
3341
"/insert3",
34-
json={"a_id": 1, "b_id": 32, "b_number": 1.23, "c_id": 400},
42+
json={"submissions": [{"a_id": 1, "b_id": 32, "b_number": 1.23, "c_id": 400}]},
3543
headers=dict(Authorization=f"Bearer {token}"),
3644
)
3745
assert REST_response.status_code == 500
@@ -111,8 +119,6 @@ def test_form_response_no_map_shared_FK_hierarchy(
111119
"type": "table",
112120
"values": [{"a_id": 0}, {"a_id": 1}],
113121
},
114-
{"datatype": "int", "name": "bs_id", "type": "attribute"},
115-
{"datatype": "float", "name": "bs_number", "type": "attribute"},
116122
{
117123
"name": "test_group1_simple.TableB",
118124
"type": "table",
@@ -122,9 +128,11 @@ def test_form_response_no_map_shared_FK_hierarchy(
122128
{"a_id": 1, "b_id": 21},
123129
],
124130
},
131+
{"datatype": "int", "name": "bs_id", "type": "attribute"},
132+
{"datatype": "float", "name": "bs_number", "type": "attribute"},
125133
{"datatype": "int", "name": "c_id", "type": "attribute"},
126134
{"datatype": "int", "name": "c_int", "type": "attribute"},
127-
],
135+
]
128136
}
129137

130138

@@ -157,21 +165,21 @@ def test_form_response_no_map_diff_FK(token, client, connection, schemas_simple)
157165
assert REST_response.status_code == 200, f"Error: {REST_response.data}"
158166
assert REST_response.get_json() == {
159167
"fields": [
168+
{
169+
"name": "test_group3_simple.TableZ",
170+
"type": "table",
171+
"values": [{"z_id": 0}, {"z_id": 1}],
172+
},
160173
{
161174
"name": "test_group4_simple.DiffTableZ",
162175
"type": "table",
163176
"values": [{"zs_id": 0}, {"zs_id": 1}],
164177
},
165178
{"datatype": "int", "name": "y_id", "type": "attribute"},
166179
{"datatype": "float", "name": "y_number", "type": "attribute"},
167-
{
168-
"name": "test_group3_simple.TableZ",
169-
"type": "table",
170-
"values": [{"z_id": 0}, {"z_id": 1}],
171-
},
172180
{"datatype": "int", "name": "ys_id", "type": "attribute"},
173181
{"datatype": "float", "name": "ys_number", "type": "attribute"},
174-
],
182+
]
175183
}
176184

177185

@@ -210,35 +218,35 @@ def test_form_response_no_map_many_tables(token, client, connection, schemas_sim
210218
"type": "table",
211219
"values": [{"a_id": 0}, {"a_id": 1}],
212220
},
213-
{"datatype": "int", "name": "b_id", "type": "attribute"},
214-
{"datatype": "float", "name": "b_number", "type": "attribute"},
215-
{"datatype": "int", "name": "bs_id", "type": "attribute"},
216-
{"datatype": "float", "name": "bs_number", "type": "attribute"},
217-
{"datatype": "int", "name": "c_id", "type": "attribute"},
218-
{"datatype": "int", "name": "c_int", "type": "attribute"},
219221
{
220-
"name": "test_group4_simple.DiffTableZ",
222+
"name": "test_group3_simple.TableX",
221223
"type": "table",
222-
"values": [{"zs_id": 0}, {"zs_id": 1}],
224+
"values": [
225+
{"x_id": 0, "x_int": 10, "x_name": "Carlos"},
226+
{"x_id": 1, "x_int": 20, "x_name": "Oscar"},
227+
],
223228
},
224-
{"datatype": "int", "name": "y_id", "type": "attribute"},
225-
{"datatype": "float", "name": "y_number", "type": "attribute"},
226229
{
227230
"name": "test_group3_simple.TableZ",
228231
"type": "table",
229232
"values": [{"z_id": 0}, {"z_id": 1}],
230233
},
231-
{"datatype": "int", "name": "ys_id", "type": "attribute"},
232-
{"datatype": "float", "name": "ys_number", "type": "attribute"},
233234
{
234-
"name": "test_group3_simple.TableX",
235+
"name": "test_group4_simple.DiffTableZ",
235236
"type": "table",
236-
"values": [
237-
{"x_id": 0, "x_int": 10, "x_name": "Carlos"},
238-
{"x_id": 1, "x_int": 20, "x_name": "Oscar"},
239-
],
237+
"values": [{"zs_id": 0}, {"zs_id": 1}],
240238
},
239+
{"datatype": "int", "name": "b_id", "type": "attribute"},
240+
{"datatype": "float", "name": "b_number", "type": "attribute"},
241+
{"datatype": "int", "name": "bs_id", "type": "attribute"},
242+
{"datatype": "float", "name": "bs_number", "type": "attribute"},
243+
{"datatype": "int", "name": "c_id", "type": "attribute"},
244+
{"datatype": "int", "name": "c_int", "type": "attribute"},
245+
{"datatype": "int", "name": "y_id", "type": "attribute"},
246+
{"datatype": "float", "name": "y_number", "type": "attribute"},
247+
{"datatype": "int", "name": "ys_id", "type": "attribute"},
248+
{"datatype": "float", "name": "ys_number", "type": "attribute"},
241249
{"datatype": "int", "name": "w_id", "type": "attribute"},
242250
{"datatype": "int", "name": "w_int", "type": "attribute"},
243-
],
251+
]
244252
}

0 commit comments

Comments
 (0)