Skip to content

Commit

Permalink
eng(rest): migrate RestProvider to use query v2 (#498)
Browse files Browse the repository at this point in the history
  • Loading branch information
tshedor authored Dec 16, 2024
1 parent 3b11a47 commit f4240d1
Show file tree
Hide file tree
Showing 29 changed files with 1,158 additions and 226 deletions.
276 changes: 136 additions & 140 deletions packages/brick_core/test/query/query_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,178 +6,174 @@ import 'package:test/test.dart';

void main() {
group('Query', () {
group('properties', () {
test('#action', () {
const q = Query(action: QueryAction.delete);
expect(q.action, QueryAction.delete);
});
test('#action', () {
const q = Query(action: QueryAction.delete);
expect(q.action, QueryAction.delete);
});

group('#providerArgs', () {
test('#providerArgs.page and #providerArgs.sort', () {
const q = Query(providerArgs: {'page': 1, 'sort': 'by_user_asc'});
group('#providerArgs', () {
test('#providerArgs.page and #providerArgs.sort', () {
const q = Query(providerArgs: {'page': 1, 'sort': 'by_user_asc'});

expect(q.providerArgs['page'], 1);
expect(q.providerArgs['sort'], 'by_user_asc');
});
expect(q.providerArgs['page'], 1);
expect(q.providerArgs['sort'], 'by_user_asc');
});
});

test('#providerArgs.limit', () {
const q0 = Query(limit: 0);
expect(q0.limit, 0);
test('#limit', () {
const q0 = Query(limit: 0);
expect(q0.limit, 0);

const q10 = Query(limit: 10);
expect(q10.limit, 10);
const q10 = Query(limit: 10);
expect(q10.limit, 10);

const q18 = Query(limit: 18);
expect(q18.limit, 18);
});
const q18 = Query(limit: 18);
expect(q18.limit, 18);
});

test('#providerArgs.offset', () {
const q0 = Query(limit: 10, offset: 0);
expect(q0.offset, 0);
test('#offset', () {
const q0 = Query(limit: 10, offset: 0);
expect(q0.offset, 0);

const q10 = Query(limit: 10, offset: 10);
expect(q10.offset, 10);
const q10 = Query(limit: 10, offset: 10);
expect(q10.offset, 10);

const q18 = Query(limit: 10, offset: 18);
expect(q18.offset, 18);
});
});
const q18 = Query(limit: 10, offset: 18);
expect(q18.offset, 18);
});

test('#where', () {
const q = Query(
where: [
Where('name', value: 'Thomas'),
],
);
test('#where', () {
const q = Query(
where: [
Where('name', value: 'Thomas'),
],
);

expect(q.where!.first.evaluatedField, 'name');
expect(q.where!.first.value, 'Thomas');
});
expect(q.where!.first.evaluatedField, 'name');
expect(q.where!.first.value, 'Thomas');
});
});

group('==', () {
test('properties are the same', () {
const q1 = Query(
action: QueryAction.delete,
limit: 3,
offset: 3,
);
const q2 = Query(
action: QueryAction.delete,
limit: 3,
offset: 3,
);

expect(q1, q2);
});
group('==', () {
test('properties are the same', () {
const q1 = Query(
action: QueryAction.delete,
limit: 3,
offset: 3,
);
const q2 = Query(
action: QueryAction.delete,
limit: 3,
offset: 3,
);

test('providerArgs are the same', () {
const q1 = Query(providerArgs: {'name': 'Guy'});
const q2 = Query(providerArgs: {'name': 'Guy'});
expect(q1, q2);
});

expect(q1, q2);
});
test('providerArgs are the same', () {
const q1 = Query(providerArgs: {'name': 'Guy'});
const q2 = Query(providerArgs: {'name': 'Guy'});

test('providerArgs have different values', () {
const q1 = Query(providerArgs: {'name': 'Thomas'});
const q2 = Query(providerArgs: {'name': 'Guy'});
expect(q1, q2);
});

expect(q1, isNot(q2));
});
test('providerArgs have different values', () {
const q1 = Query(providerArgs: {'name': 'Thomas'});
const q2 = Query(providerArgs: {'name': 'Guy'});

test('providerArgs have different keys', () {
const q1 = Query(providerArgs: {'email': '[email protected]'});
const q2 = Query(providerArgs: {'name': 'Guy'});
expect(q1, isNot(q2));
});

expect(q1, isNot(q2));
});
test('providerArgs have different keys', () {
const q1 = Query(providerArgs: {'email': '[email protected]'});
const q2 = Query(providerArgs: {'name': 'Guy'});

test('providerArgs are null', () {
const q1 = Query();
const q2 = Query(providerArgs: {'name': 'Guy'});
expect(q1, isNot(q2));
expect(q1, isNot(q2));
});

const q3 = Query();
expect(q1, q3);
});
test('providerArgs are null', () {
const q1 = Query();
const q2 = Query(providerArgs: {'name': 'Guy'});
expect(q1, isNot(q2));

const q3 = Query();
expect(q1, q3);
});
});

group('#copyWith', () {
test('overrides', () {
const q1 = Query(action: QueryAction.insert, limit: 10);
final q2 = q1.copyWith(limit: 20);
expect(q2.action, QueryAction.insert);
expect(q2.limit, 20);
expect(q2.offset, null);

final q3 = q1.copyWith(limit: 50, offset: 20);
expect(q3.action, QueryAction.insert);
expect(q3.limit, 50);
expect(q3.offset, 20);
});
group('#copyWith', () {
test('overrides', () {
const q1 = Query(action: QueryAction.insert, limit: 10);
final q2 = q1.copyWith(limit: 20);
expect(q2.action, QueryAction.insert);
expect(q2.limit, 20);
expect(q2.offset, null);

final q3 = q1.copyWith(limit: 50, offset: 20);
expect(q3.action, QueryAction.insert);
expect(q3.limit, 50);
expect(q3.offset, 20);
});

test('appends', () {
const q1 = Query(action: QueryAction.insert);
final q2 = q1.copyWith(limit: 20);
test('appends', () {
const q1 = Query(action: QueryAction.insert);
final q2 = q1.copyWith(limit: 20);

expect(q1.limit, null);
expect(q2.action, QueryAction.insert);
expect(q2.limit, 20);
});
expect(q1.limit, null);
expect(q2.action, QueryAction.insert);
expect(q2.limit, 20);
});
});

test('#toJson', () {
const source = Query(
action: QueryAction.update,
limit: 3,
offset: 3,
);

expect(
source.toJson(),
{
'action': 2,
'limit': 3,
'offset': 3,
},
);
});

test('#toJson', () {
const source = Query(
test('.fromJson', () {
final json = {
'action': 2,
'limit': 3,
'offset': 3,
};

final result = Query.fromJson(json);
expect(
result,
const Query(
action: QueryAction.update,
limit: 3,
offset: 3,
);
),
);
});

expect(
source.toJson(),
{
'action': 2,
'limit': 3,
'offset': 3,
},
);
group('.where', () {
test('required arguments', () {
const expandedQuery = Query(where: [Where('id', value: 2)]);
final factoried = Query.where('id', 2);
expect(factoried, expandedQuery);
expect(Where.firstByField('id', factoried.where)!.value, 2);
expect(factoried.unlimited, isTrue);
});

group('factories', () {
test('.fromJson', () {
final json = {
'action': 2,
'limit': 3,
'offset': 3,
};

final result = Query.fromJson(json);
expect(
result,
const Query(
action: QueryAction.update,
limit: 3,
offset: 3,
),
);
});

group('.where', () {
test('required arguments', () {
const expandedQuery = Query(where: [Where('id', value: 2)]);
final factoried = Query.where('id', 2);
expect(factoried, expandedQuery);
expect(Where.firstByField('id', factoried.where)!.value, 2);
expect(factoried.unlimited, isTrue);
});

test('limit1:true', () {
const expandedQuery = Query(where: [Where('id', value: 2)], limit: 1);
final factoried = Query.where('id', 2, limit1: true);
expect(factoried, expandedQuery);
expect(factoried.unlimited, isFalse);
});
});
test('limit1:true', () {
const expandedQuery = Query(where: [Where('id', value: 2)], limit: 1);
final factoried = Query.where('id', 2, limit1: true);
expect(factoried, expandedQuery);
expect(factoried.unlimited, isFalse);
});
});
}
Loading

0 comments on commit f4240d1

Please sign in to comment.