Skip to content

Commit

Permalink
Add sortPredicate teste
Browse files Browse the repository at this point in the history
  • Loading branch information
victorths committed May 30, 2023
1 parent d48250a commit 413f970
Showing 1 changed file with 46 additions and 58 deletions.
104 changes: 46 additions & 58 deletions packages/postgresql/test/fetch_test.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import 'not_tests/helpers.dart';
import 'not_tests/postgres_test_config.dart';
import 'package:conduit_core/conduit_core.dart';
import 'package:test/test.dart';

import 'not_tests/helpers.dart';
import 'not_tests/postgres_test_config.dart';

void main() {
ManagedContext? context;
tearDown(() async {
Expand All @@ -17,19 +18,16 @@ void main() {
var req = Query<TestModel>(context!)..values = m;
var item = await req.insert();

req = Query<TestModel>(context!)
..predicate = QueryPredicate("id = @id", {"id": item.id});
req = Query<TestModel>(context!)..predicate = QueryPredicate("id = @id", {"id": item.id});
item = (await req.fetchOne())!;

expect(item.name, "Joe");
expect(item.email, "a@a.com");
});

test("Query with dynamic entity and mis-matched context throws exception",
() async {
test("Query with dynamic entity and mis-matched context throws exception", () async {
context = await PostgresTestConfig().contextWithModels([TestModel]);
final someOtherContext =
ManagedContext(ManagedDataModel([]), DefaultPersistentStore());
final someOtherContext = ManagedContext(ManagedDataModel([]), DefaultPersistentStore());
try {
Query.forEntity(
context!.dataModel!.entityForType(TestModel),
Expand Down Expand Up @@ -73,8 +71,7 @@ void main() {
await req.insert();

try {
req = Query<TestModel>(context!)
..returningProperties((t) => [t.id, t["foobar"]]);
req = Query<TestModel>(context!)..returningProperties((t) => [t.id, t["foobar"]]);
fail("unreachable");
} on ArgumentError catch (e) {
expect(
Expand Down Expand Up @@ -106,8 +103,7 @@ void main() {
expect(result[i].email, "asc$i@a.com");
}

req = Query<TestModel>(context!)
..sortBy((t) => t.id, QuerySortOrder.ascending);
req = Query<TestModel>(context!)..sortBy((t) => t.id, QuerySortOrder.ascending);
result = await req.fetch();

int? idIndex = 0;
Expand Down Expand Up @@ -140,11 +136,30 @@ void main() {
}
});

test("Predicate sort descriptors work", () async {
context = await PostgresTestConfig().contextWithModels([TestModel]);

for (int i = 0; i < 10; i++) {
final m = TestModel(name: "Joe$i", email: "desc$i@a.com");

final req = Query<TestModel>(context!)..values = m;

await req.insert();
}

final req = Query<TestModel>(context!);
req.sortPredicate = QuerySortPredicate('random()', QuerySortOrder.ascending);
req.predicate = QueryPredicate("email like @key", {"key": "desc%"});
final result1 = await req.fetch();
final result2 = await req.fetch();

expect(result1, isNot(orderedEquals(result2)));
});

test("Cannot sort by property that doesn't exist", () async {
context = await PostgresTestConfig().contextWithModels([TestModel]);
try {
Query<TestModel>(context!)
.sortBy((u) => u["nonexisting"], QuerySortOrder.ascending);
Query<TestModel>(context!).sortBy((u) => u["nonexisting"], QuerySortOrder.ascending);
expect(true, false);
} on ArgumentError catch (e) {
expect(
Expand Down Expand Up @@ -233,8 +248,7 @@ void main() {
await req.insert();

try {
req = Query<TestModel>(context!)
..returningProperties((t) => [t.id, t["badkey"]]);
req = Query<TestModel>(context!)..returningProperties((t) => [t.id, t["badkey"]]);

fail("unreachable");
} on ArgumentError catch (e) {
Expand Down Expand Up @@ -263,16 +277,11 @@ void main() {
await (Query<GenPost>(context!)..values = p2).insert();
}

final req = Query<GenPost>(context!)
..predicate = QueryPredicate("owner_id = @id", {"id": u1.id});
final req = Query<GenPost>(context!)..predicate = QueryPredicate("owner_id = @id", {"id": u1.id});
var res = await req.fetch();
expect(res.length, 5);
expect(
res
.map((p) => p.text)
.where((text) => num.parse(text!) % 2 == 0)
.toList()
.length,
res.map((p) => p.text).where((text) => num.parse(text!) % 2 == 0).toList().length,
5,
);

Expand All @@ -284,19 +293,14 @@ void main() {
expect(user, isNotNull);
expect(res.length, 5);
expect(
res
.map((p) => p.text)
.where((text) => num.parse(text!) % 2 == 0)
.toList()
.length,
res.map((p) => p.text).where((text) => num.parse(text!) % 2 == 0).toList().length,
5,
);
});

test("Fetch object with null reference", () async {
context = await PostgresTestConfig().contextWithModels([GenUser, GenPost]);
var p1 = await (Query<GenPost>(context!)..values = (GenPost()..text = "1"))
.insert();
var p1 = await (Query<GenPost>(context!)..values = (GenPost()..text = "1")).insert();

final req = Query<GenPost>(context!);
p1 = (await req.fetchOne())!;
Expand All @@ -313,17 +317,14 @@ void main() {
expect(result.id, greaterThan(0));
expect(result.backing.contents!["text"], isNull);

final fq = Query<Omit>(context!)
..predicate = QueryPredicate("id=@id", {"id": result.id});
final fq = Query<Omit>(context!)..predicate = QueryPredicate("id=@id", {"id": result.id});

final fResult = (await fq.fetchOne())!;
expect(fResult.id, result.id);
expect(fResult.backing.contents!["text"], isNull);
});

test(
"Throw exception when fetchOne returns more than one because the fetchLimit can't be applied to joins",
() async {
test("Throw exception when fetchOne returns more than one because the fetchLimit can't be applied to joins", () async {
context = await PostgresTestConfig().contextWithModels([GenUser, GenPost]);

final objects = [GenUser()..name = "Joe", GenUser()..name = "Bob"];
Expand All @@ -347,9 +348,7 @@ void main() {
}
});

test(
"Including RelationshipInverse property can only be done by using name of property",
() async {
test("Including RelationshipInverse property can only be done by using name of property", () async {
context = await PostgresTestConfig().contextWithModels([GenUser, GenPost]);

final u1 = await (Query<GenUser>(context!)..values.name = "Joe").insert();
Expand All @@ -359,16 +358,14 @@ void main() {
..values.owner = u1)
.insert();

var q = Query<GenPost>(context!)
..returningProperties((p) => [p.id, p.owner]);
var q = Query<GenPost>(context!)..returningProperties((p) => [p.id, p.owner]);

final result = (await q.fetchOne())!;
expect(result.owner!.id, 1);
expect(result.owner!.backing.contents!.length, 1);

try {
q = Query<GenPost>(context!)
..returningProperties((p) => [p.id, p["owner_id"]]);
q = Query<GenPost>(context!)..returningProperties((p) => [p.id, p["owner_id"]]);
expect(true, false);
} on ArgumentError catch (e) {
expect(
Expand All @@ -387,9 +384,7 @@ void main() {
expect(result.public, "x");
});

test(
"When fetching valid enum value from db, is available as enum value and in where",
() async {
test("When fetching valid enum value from db, is available as enum value and in where", () async {
context = await PostgresTestConfig().contextWithModels([EnumObject]);

var q = Query<EnumObject>(context!)..values.enumValues = EnumValues.abcd;
Expand All @@ -401,13 +396,11 @@ void main() {
expect(result!.enumValues, EnumValues.abcd);
expect(result.asMap()["enumValues"], "abcd");

q = Query<EnumObject>(context!)
..where((o) => o.enumValues).equalTo(EnumValues.abcd);
q = Query<EnumObject>(context!)..where((o) => o.enumValues).equalTo(EnumValues.abcd);
result = await q.fetchOne();
expect(result, isNotNull);

q = Query<EnumObject>(context!)
..where((o) => o.enumValues).equalTo(EnumValues.efgh);
q = Query<EnumObject>(context!)..where((o) => o.enumValues).equalTo(EnumValues.efgh);
result = await q.fetchOne();
expect(result, isNull);
});
Expand All @@ -427,8 +420,7 @@ void main() {
test("When fetching invalid enum value from db, throws error", () async {
context = await PostgresTestConfig().contextWithModels([EnumObject]);

await context!.persistentStore
.execute("INSERT INTO _enumobject (enumValues) VALUES ('foobar')");
await context!.persistentStore.execute("INSERT INTO _enumobject (enumValues) VALUES ('foobar')");

try {
final q = Query<EnumObject>(context!);
Expand Down Expand Up @@ -469,8 +461,7 @@ void main() {
expect(o!.name, "bob");
});

test("If object does not exist and type is specified, return null",
() async {
test("If object does not exist and type is specified, return null", () async {
final o = await context!.fetchObjectWithID<TestModel>(id! + 1);
expect(o, isNull);
});
Expand All @@ -493,9 +484,7 @@ void main() {
}
});

test(
"If identifier type is not the same type as return type, throw exception with 404",
() async {
test("If identifier type is not the same type as return type, throw exception with 404", () async {
final o = await context!.fetchObjectWithID<TestModel>("not-an-int");
expect(o, isNull);
});
Expand Down Expand Up @@ -569,8 +558,7 @@ class _Omit {
String? text;
}

class PrivateField extends ManagedObject<_PrivateField>
implements _PrivateField {
class PrivateField extends ManagedObject<_PrivateField> implements _PrivateField {
PrivateField() : super() {
_private = "x";
}
Expand Down

0 comments on commit 413f970

Please sign in to comment.