Skip to content
This repository was archived by the owner on Mar 18, 2021. It is now read-only.

Commit f649249

Browse files
committed
Merge pull request #25 from stablekernel/jc/testing
Allow lists to be included in input JSON, but ignore when trying to i…
2 parents cb4cd36 + 2e90f84 commit f649249

File tree

6 files changed

+102
-22
lines changed

6 files changed

+102
-22
lines changed

lib/db/model.dart

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,14 +249,27 @@ class Model {
249249
if (value is! Map) {
250250
throw new QueryException(
251251
400,
252-
"Expecting a map for ${MirrorSystem.getName(typeMirror.simpleName)} in the $key field, got $value instead.",
252+
"Expecting a Map for ${MirrorSystem.getName(typeMirror.simpleName)} in the $key field, got $value instead.",
253253
-1);
254-
255254
}
256255
Model instance =
257256
(typeMirror as ClassMirror).newInstance(new Symbol(""), []).reflectee;
258257
instance.readMap(value);
259258
return instance;
259+
} else if (typeMirror.isSubtypeOf(reflectType(List))) {
260+
if (value is! List) {
261+
throw new QueryException(
262+
400,
263+
"Expecting a List for ${MirrorSystem.getName(typeMirror.simpleName)} in the $key field, got $value instead.",
264+
-1);
265+
}
266+
267+
var listTypeMirror = typeMirror.typeArguments.first;
268+
return (value as List).map((v) {
269+
Model instance = (listTypeMirror as ClassMirror).newInstance(new Symbol(""), []).reflectee;
270+
instance.readMap(v);
271+
return instance;
272+
}).toList();
260273
}
261274

262275
if (typeMirror.isSubtypeOf(reflectType(DateTime))) {

lib/db/postgresql/postgresl_query.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,13 @@ class _PostgresqlQuery {
9595

9696
valueObject.dynamicBacking.forEach((modelKey, value) {
9797
var column = columns[modelKey];
98-
if (column.relationship != null) {
98+
var relationship = column.relationship;
99+
100+
if (relationship != null) {
101+
if (relationship.type == RelationshipType.hasMany) {
102+
return;
103+
}
104+
99105
var relatedValue = (value as Model).dynamicBacking[column.relationship.destinationModelKey];
100106

101107
if (relatedValue == null) {

test/base/client_test_test.dart

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,42 @@ import 'dart:convert';
55
import 'dart:async';
66

77
Future main() async {
8+
9+
TestClient client = new TestClient(8080);
10+
11+
var server = null;
12+
13+
setUpAll(() async {
14+
HttpServer
15+
.bind("localhost", 8080,
16+
v6Only: false, shared: false)
17+
.then((s)
18+
{
19+
server = s;
20+
21+
server.listen((req) {
22+
var resReq = new ResourceRequest(req);
23+
var controller = new TestController();
24+
controller.deliver(resReq);
25+
});
26+
});
27+
});
28+
29+
tearDownAll(() async {
30+
await server.close();
31+
});
32+
33+
34+
test("Client can expect array of JSON", () async {
35+
var resp = await client.request("/na").get();
36+
expect(resp, hasResponse(200, [], matchesJSON([{
37+
"id" : greaterThan(0)
38+
}])));
39+
});
40+
}
41+
42+
class TestController extends HttpController {
43+
@httpGet get() async {
44+
return new Response.ok([{"id" : 1}, {"id" : 2}]);
45+
}
846
}

test/db/model_tests.dart

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,9 @@ main() {
2929
}
3030
});
3131

32-
test("Accessing model object without field throws exception", () {
32+
test("Accessing model object without field should return null", () {
3333
var user = new User();
34-
try {
35-
var x = user.name;
36-
fail("This should throw an exception.");
37-
print("$x");
38-
} catch (e) {
39-
expect(e.message,
40-
"Accessing property name on User, but is currently undefined for this instance.");
41-
}
34+
expect(user.name, isNull);
4235
});
4336

4437
test("Getting/setting property that is undeclared throws exception", () {
@@ -182,7 +175,7 @@ main() {
182175
fail("Should throw");
183176
} catch (e) {
184177
expect(e.message,
185-
"Expecting a map for User in the owner field, got 12 instead.");
178+
"Expecting a Map for User in the owner field, got 12 instead.");
186179
}
187180
});
188181

@@ -280,7 +273,20 @@ main() {
280273
expect(e is QueryException, true);
281274
expect(e.message, "Key outputInt does not exist for TransientTest");
282275
}
276+
});
283277

278+
test("Reading hasMany relationship from JSON succeeds", () {
279+
var u = new User();
280+
u.readMap({
281+
"name" : "Bob",
282+
"id" : 1,
283+
"posts" : [
284+
{"text" : "Hi", "id" : 1}
285+
]
286+
});
287+
expect(u.posts.length, 1);
288+
expect(u.posts[0].id, 1);
289+
expect(u.posts[0].text, "Hi");
284290
});
285291

286292
}

test/db/postgresql/fetch_test.dart

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,7 @@ void main() {
5151

5252
expect(item.name, "Joe");
5353
expect(item.id, id);
54-
55-
try {
56-
var email = item.email;
57-
fail("This should throw exception");
58-
print("$email");
59-
} catch (e) {
60-
expect(e.message,
61-
"Accessing property email on TestModel, but is currently undefined for this instance.");
62-
}
54+
expect(item.email, isNull);
6355
});
6456

6557
test("Ascending sort descriptors work", () async {

test/db/postgresql/insert_test.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,31 @@ void main() {
186186
var result = await q.insert(adapter);
187187
expect(result.transientValue, null);
188188
});
189+
190+
test("JSON -> Insert with List", () async {
191+
await generateTemporarySchemaFromModels(adapter, [GenUser, GenPost]);
192+
193+
var json = {
194+
"name" : "Bob",
195+
"posts" : [
196+
{"text" : "Post"}
197+
]
198+
};
199+
200+
var u = new GenUser()
201+
..readMap(json);
202+
203+
var q = new Query<GenUser>()
204+
..valueObject = u;
205+
206+
var result = await q.insert(adapter);
207+
expect(result.id, greaterThan(0));
208+
expect(result.name, "Bob");
209+
expect(result.posts, isNull);
210+
211+
q = new Query<GenPost>();
212+
expect(await q.fetch(adapter), hasLength(0));
213+
});
189214
}
190215

191216
@ModelBacking(TestModelBacking)

0 commit comments

Comments
 (0)