2024-10-13 01:45:27 +00:00
|
|
|
import 'package:protevus_container/mirrors.dart';
|
|
|
|
import 'package:protevus_framework/protevus_framework.dart';
|
|
|
|
import 'package:protevus_framework/http.dart';
|
|
|
|
import 'package:protevus_mongo/protevus_mongo.dart';
|
2016-05-09 22:51:07 +00:00
|
|
|
import 'package:http/http.dart' as http;
|
2021-09-25 06:32:32 +00:00
|
|
|
import 'package:belatuk_json_serializer/belatuk_json_serializer.dart' as god;
|
2016-05-09 22:51:07 +00:00
|
|
|
import 'package:mongo_dart/mongo_dart.dart';
|
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
|
|
|
final headers = {
|
2018-10-18 22:58:03 +00:00
|
|
|
'accept': 'application/json',
|
|
|
|
'content-type': 'application/json'
|
2016-05-09 22:51:07 +00:00
|
|
|
};
|
|
|
|
|
2016-06-22 18:34:28 +00:00
|
|
|
final Map testGreeting = {'to': 'world'};
|
2016-05-09 22:51:07 +00:00
|
|
|
|
2021-02-21 02:47:23 +00:00
|
|
|
void wireHooked(HookedService hooked) {
|
2017-07-09 18:07:02 +00:00
|
|
|
hooked.afterAll((HookedServiceEvent event) {
|
2021-06-18 10:17:13 +00:00
|
|
|
print('Just ${event.eventName}: ${event.result}');
|
2017-07-09 18:07:02 +00:00
|
|
|
print('Params: ${event.params}');
|
|
|
|
});
|
2016-06-21 20:34:52 +00:00
|
|
|
}
|
|
|
|
|
2021-02-21 02:47:23 +00:00
|
|
|
void main() {
|
2016-06-23 19:59:10 +00:00
|
|
|
group('Generic Tests', () {
|
2024-10-12 10:35:14 +00:00
|
|
|
Protevus app;
|
|
|
|
late ProtevusHttp transport;
|
2024-06-02 02:37:49 +00:00
|
|
|
late http.Client client;
|
2024-07-02 16:13:02 +00:00
|
|
|
var db = Db('mongodb://localhost:27017/testDB');
|
2024-06-23 03:15:41 +00:00
|
|
|
|
2021-06-18 11:10:38 +00:00
|
|
|
late DbCollection testData;
|
|
|
|
String? url;
|
2024-06-02 02:37:49 +00:00
|
|
|
late HookedService<String, Map<String, dynamic>, MongoService>
|
|
|
|
greetingService;
|
2016-05-09 22:51:07 +00:00
|
|
|
|
|
|
|
setUp(() async {
|
2024-10-12 10:35:14 +00:00
|
|
|
app = Protevus(reflector: MirrorsReflector());
|
|
|
|
transport = ProtevusHttp(app);
|
2021-02-21 02:47:23 +00:00
|
|
|
client = http.Client();
|
2016-05-09 22:51:07 +00:00
|
|
|
await db.open();
|
2024-07-04 16:04:58 +00:00
|
|
|
await db.authenticate("root", "Qwerty", authDb: "admin");
|
2024-07-02 16:13:02 +00:00
|
|
|
|
2024-06-23 03:15:41 +00:00
|
|
|
testData = db.collection('testData');
|
2016-05-09 22:51:07 +00:00
|
|
|
// Delete anything before we start
|
2018-10-18 23:00:55 +00:00
|
|
|
await testData.remove(<String, dynamic>{});
|
2016-06-21 20:34:52 +00:00
|
|
|
|
2022-02-27 01:19:15 +00:00
|
|
|
var service = MongoService(testData);
|
2021-02-21 02:47:23 +00:00
|
|
|
greetingService = HookedService(service);
|
2024-06-02 02:37:49 +00:00
|
|
|
wireHooked(greetingService);
|
2016-06-21 20:34:52 +00:00
|
|
|
|
2021-06-18 11:10:38 +00:00
|
|
|
app.use('/api', greetingService as Service);
|
2017-02-13 01:38:24 +00:00
|
|
|
|
2018-12-10 15:33:55 +00:00
|
|
|
await transport.startServer('127.0.0.1', 0);
|
|
|
|
url = transport.uri.toString();
|
2016-05-09 22:51:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
tearDown(() async {
|
|
|
|
// Delete anything left over
|
2018-10-18 23:00:55 +00:00
|
|
|
await testData.remove(<String, dynamic>{});
|
2016-05-09 22:51:07 +00:00
|
|
|
await db.close();
|
2018-10-18 22:58:03 +00:00
|
|
|
await transport.close();
|
2016-05-09 22:51:07 +00:00
|
|
|
});
|
|
|
|
|
2018-12-10 15:33:55 +00:00
|
|
|
test('query fields mapped to filters', () async {
|
2024-06-02 02:37:49 +00:00
|
|
|
await greetingService.create({'foo': 'bar'});
|
2018-12-10 15:33:55 +00:00
|
|
|
expect(
|
2024-06-02 02:37:49 +00:00
|
|
|
await greetingService.index({
|
2018-12-10 15:33:55 +00:00
|
|
|
'query': {'foo': 'not bar'}
|
|
|
|
}),
|
|
|
|
isEmpty,
|
|
|
|
);
|
|
|
|
expect(
|
2024-06-02 02:37:49 +00:00
|
|
|
await greetingService.index(),
|
2018-12-10 15:33:55 +00:00
|
|
|
isNotEmpty,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2016-05-09 22:51:07 +00:00
|
|
|
test('insert items', () async {
|
2024-06-02 02:37:49 +00:00
|
|
|
var response = await client.post(Uri.parse('$url/api'),
|
2016-06-22 18:34:28 +00:00
|
|
|
body: god.serialize(testGreeting), headers: headers);
|
2017-04-10 02:28:29 +00:00
|
|
|
expect(response.statusCode, isIn([200, 201]));
|
2016-05-09 22:51:07 +00:00
|
|
|
|
2024-06-02 02:37:49 +00:00
|
|
|
response = await client.get(Uri.parse('$url/api'));
|
2017-04-10 02:28:29 +00:00
|
|
|
expect(response.statusCode, isIn([200, 201]));
|
2019-04-20 18:59:22 +00:00
|
|
|
var users = god.deserialize(response.body,
|
|
|
|
outputType: <Map>[].runtimeType) as List<Map>;
|
2016-05-09 22:51:07 +00:00
|
|
|
expect(users.length, equals(1));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('read item', () async {
|
2024-06-02 02:37:49 +00:00
|
|
|
var response = await client.post(Uri.parse('$url/api'),
|
2016-06-22 18:34:28 +00:00
|
|
|
body: god.serialize(testGreeting), headers: headers);
|
2017-04-10 02:28:29 +00:00
|
|
|
expect(response.statusCode, isIn([200, 201]));
|
2019-04-20 18:59:22 +00:00
|
|
|
var created = god.deserialize(response.body) as Map;
|
2016-05-09 22:51:07 +00:00
|
|
|
|
2024-06-02 02:37:49 +00:00
|
|
|
response = await client.get(Uri.parse("$url/api/${created['id']}"));
|
2017-04-10 02:28:29 +00:00
|
|
|
expect(response.statusCode, isIn([200, 201]));
|
2019-04-20 18:59:22 +00:00
|
|
|
var read = god.deserialize(response.body) as Map;
|
2016-06-22 18:34:28 +00:00
|
|
|
expect(read['id'], equals(created['id']));
|
|
|
|
expect(read['to'], equals('world'));
|
2018-12-10 19:19:20 +00:00
|
|
|
//expect(read['createdAt'], isNot(null));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('findOne', () async {
|
2024-06-02 02:37:49 +00:00
|
|
|
var response = await client.post(Uri.parse('$url/api'),
|
2018-12-10 19:19:20 +00:00
|
|
|
body: god.serialize(testGreeting), headers: headers);
|
|
|
|
expect(response.statusCode, isIn([200, 201]));
|
2019-04-20 18:59:22 +00:00
|
|
|
var created = god.deserialize(response.body) as Map;
|
2018-12-10 19:19:20 +00:00
|
|
|
|
2021-02-21 02:47:23 +00:00
|
|
|
var id = ObjectId.fromHexString(created['id'] as String);
|
2024-06-02 02:37:49 +00:00
|
|
|
var read = await greetingService.findOne({'query': where.id(id)});
|
2018-12-10 19:19:20 +00:00
|
|
|
expect(read['id'], equals(created['id']));
|
|
|
|
expect(read['to'], equals('world'));
|
|
|
|
//expect(read['createdAt'], isNot(null));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('readMany', () async {
|
2024-06-02 02:37:49 +00:00
|
|
|
var response = await client.post(Uri.parse('$url/api'),
|
2018-12-10 19:19:20 +00:00
|
|
|
body: god.serialize(testGreeting), headers: headers);
|
|
|
|
expect(response.statusCode, isIn([200, 201]));
|
2019-04-20 18:59:22 +00:00
|
|
|
var created = god.deserialize(response.body) as Map;
|
2018-12-10 19:19:20 +00:00
|
|
|
|
2021-02-21 02:47:23 +00:00
|
|
|
var id = ObjectId.fromHexString(created['id'] as String);
|
2024-06-02 02:37:49 +00:00
|
|
|
var read = await greetingService.readMany([id.oid]);
|
2018-12-10 19:19:20 +00:00
|
|
|
expect(read, [created]);
|
2017-07-09 18:07:02 +00:00
|
|
|
//expect(read['createdAt'], isNot(null));
|
2016-05-09 22:51:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('modify item', () async {
|
2024-06-02 02:37:49 +00:00
|
|
|
var response = await client.post(Uri.parse('$url/api'),
|
2016-06-22 18:34:28 +00:00
|
|
|
body: god.serialize(testGreeting), headers: headers);
|
2017-04-10 02:28:29 +00:00
|
|
|
expect(response.statusCode, isIn([200, 201]));
|
2024-06-23 03:15:41 +00:00
|
|
|
var createdDoc = god.deserialize(response.body) as Map;
|
2016-05-09 22:51:07 +00:00
|
|
|
|
2024-06-23 03:15:41 +00:00
|
|
|
response = await client.patch(Uri.parse("$url/api/${createdDoc['id']}"),
|
2021-06-18 10:17:13 +00:00
|
|
|
body: god.serialize({'to': 'Mom'}), headers: headers);
|
2024-06-23 03:15:41 +00:00
|
|
|
var modifiedDoc = god.deserialize(response.body) as Map;
|
2017-04-10 02:28:29 +00:00
|
|
|
expect(response.statusCode, isIn([200, 201]));
|
2024-06-23 03:15:41 +00:00
|
|
|
expect(modifiedDoc['id'], equals(createdDoc['id']));
|
|
|
|
expect(modifiedDoc['to'], equals('Mom'));
|
|
|
|
expect(modifiedDoc['updatedAt'], isNot(null));
|
2016-05-09 22:51:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('update item', () async {
|
2024-06-02 02:37:49 +00:00
|
|
|
var response = await client.post(Uri.parse('$url/api'),
|
2016-06-22 18:34:28 +00:00
|
|
|
body: god.serialize(testGreeting), headers: headers);
|
2017-04-10 02:28:29 +00:00
|
|
|
expect(response.statusCode, isIn([200, 201]));
|
2024-06-23 03:15:41 +00:00
|
|
|
var createdDoc = god.deserialize(response.body) as Map;
|
2016-05-09 22:51:07 +00:00
|
|
|
|
2024-06-23 03:15:41 +00:00
|
|
|
response = await client.post(Uri.parse("$url/api/${createdDoc['id']}"),
|
2021-06-18 10:17:13 +00:00
|
|
|
body: god.serialize({'to': 'Updated'}), headers: headers);
|
2024-06-23 03:15:41 +00:00
|
|
|
var modifiedDoc = god.deserialize(response.body) as Map;
|
2017-04-10 02:28:29 +00:00
|
|
|
expect(response.statusCode, isIn([200, 201]));
|
2024-06-23 03:15:41 +00:00
|
|
|
expect(modifiedDoc['id'], equals(createdDoc['id']));
|
|
|
|
expect(modifiedDoc['to'], equals('Updated'));
|
|
|
|
expect(modifiedDoc['updatedAt'], isNot(null));
|
2016-05-09 22:51:07 +00:00
|
|
|
});
|
|
|
|
|
2016-06-23 04:58:21 +00:00
|
|
|
test('remove item', () async {
|
2024-06-02 02:37:49 +00:00
|
|
|
var response = await client.post(Uri.parse('$url/api'),
|
2016-06-23 04:58:21 +00:00
|
|
|
body: god.serialize(testGreeting), headers: headers);
|
2019-04-20 18:59:22 +00:00
|
|
|
var created = god.deserialize(response.body) as Map;
|
2016-06-23 04:58:21 +00:00
|
|
|
|
2024-06-02 02:37:49 +00:00
|
|
|
var lastCount = (await greetingService.index()).length;
|
2016-05-09 22:51:07 +00:00
|
|
|
|
2024-06-02 02:37:49 +00:00
|
|
|
await client.delete(Uri.parse("$url/api/${created['id']}"));
|
|
|
|
expect((await greetingService.index()).length, equals(lastCount - 1));
|
2016-05-09 22:51:07 +00:00
|
|
|
});
|
|
|
|
|
2019-04-20 18:59:22 +00:00
|
|
|
test('cannot remove all unless explicitly set', () async {
|
2024-06-02 02:37:49 +00:00
|
|
|
var response = await client.delete(Uri.parse('$url/api/null'));
|
2019-04-20 18:59:22 +00:00
|
|
|
expect(response.statusCode, 403);
|
|
|
|
});
|
|
|
|
|
2016-06-23 19:59:10 +00:00
|
|
|
test('\$sort and query parameters', () async {
|
|
|
|
// Search by where.eq
|
2024-06-02 02:37:49 +00:00
|
|
|
Map world = await greetingService.create({'to': 'world'});
|
|
|
|
await greetingService.create({'to': 'Mom'});
|
|
|
|
await greetingService.create({'to': 'Updated'});
|
2016-06-23 19:59:10 +00:00
|
|
|
|
2024-06-02 02:37:49 +00:00
|
|
|
var response = await client.get(Uri.parse('$url/api?to=world'));
|
2016-06-23 19:59:10 +00:00
|
|
|
print(response.body);
|
2019-04-20 18:59:22 +00:00
|
|
|
var queried = god.deserialize(response.body,
|
|
|
|
outputType: <Map>[].runtimeType) as List<Map>;
|
2016-06-23 19:59:10 +00:00
|
|
|
expect(queried.length, equals(1));
|
2017-07-09 18:07:02 +00:00
|
|
|
expect(queried[0].keys.length, equals(2));
|
2021-06-18 10:17:13 +00:00
|
|
|
expect(queried[0]['id'], equals(world['id']));
|
|
|
|
expect(queried[0]['to'], equals(world['to']));
|
2017-07-09 18:07:02 +00:00
|
|
|
//expect(queried[0]["createdAt"], equals(world["createdAt"]));
|
2016-06-23 19:59:10 +00:00
|
|
|
|
2021-06-18 10:17:13 +00:00
|
|
|
/*response = await client.get(Uri.parse("$url/api?\$sort.createdAt=-1"));
|
2016-06-23 19:59:10 +00:00
|
|
|
print(response.body);
|
|
|
|
queried = god.deserialize(response.body);
|
|
|
|
expect(queried[0]["id"], equals(Updated["id"]));
|
|
|
|
expect(queried[1]["id"], equals(Mom["id"]));
|
2017-02-13 01:38:24 +00:00
|
|
|
expect(queried[2]["id"], equals(world["id"]));*/
|
|
|
|
|
2024-06-02 02:37:49 +00:00
|
|
|
queried = await greetingService.index({
|
2021-06-18 10:17:13 +00:00
|
|
|
'\$query': {
|
|
|
|
'_id': where.id(ObjectId.fromHexString(world['id'] as String))
|
2018-10-18 22:58:03 +00:00
|
|
|
}
|
2018-12-10 15:33:55 +00:00
|
|
|
});
|
2016-06-23 19:59:10 +00:00
|
|
|
print(queried);
|
|
|
|
expect(queried.length, equals(1));
|
|
|
|
expect(queried[0], equals(world));
|
|
|
|
});
|
2016-05-09 22:51:07 +00:00
|
|
|
});
|
2016-06-22 18:34:28 +00:00
|
|
|
}
|