2016-05-09 22:51:07 +00:00
|
|
|
import 'dart:io';
|
|
|
|
import 'package:angel_framework/angel_framework.dart';
|
|
|
|
import 'package:angel_mongo/angel_mongo.dart';
|
|
|
|
import 'package:http/http.dart' as http;
|
2016-06-22 18:34:28 +00:00
|
|
|
import 'package:json_god/json_god.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 = {
|
|
|
|
HttpHeaders.ACCEPT: ContentType.JSON.mimeType,
|
|
|
|
HttpHeaders.CONTENT_TYPE: ContentType.JSON.mimeType
|
|
|
|
};
|
|
|
|
|
2016-06-22 18:34:28 +00:00
|
|
|
final Map testGreeting = {'to': 'world'};
|
2016-05-09 22:51:07 +00:00
|
|
|
|
2016-06-22 18:34:28 +00:00
|
|
|
wireHooked(HookedService hooked) {
|
|
|
|
hooked
|
|
|
|
..afterCreated.listen((HookedServiceEvent event) {
|
|
|
|
print("Just created: ${event.result}");
|
|
|
|
})
|
|
|
|
..afterModified.listen((HookedServiceEvent event) {
|
|
|
|
print("Just modified: ${event.result}");
|
|
|
|
})
|
|
|
|
..afterUpdated.listen((HookedServiceEvent event) {
|
|
|
|
print("Just updated: ${event.result}");
|
2016-06-23 04:58:21 +00:00
|
|
|
})
|
|
|
|
..afterRemoved.listen((HookedServiceEvent event) {
|
|
|
|
print("Just removed: ${event.result}");
|
2016-06-22 18:34:28 +00:00
|
|
|
});
|
2016-06-21 20:34:52 +00:00
|
|
|
}
|
|
|
|
|
2016-05-09 22:51:07 +00:00
|
|
|
main() {
|
2016-06-23 19:59:10 +00:00
|
|
|
group('Generic Tests', () {
|
2016-05-09 22:51:07 +00:00
|
|
|
Angel app = new Angel();
|
|
|
|
http.Client client;
|
|
|
|
Db db = new Db('mongodb://localhost:27017/angel_mongo');
|
|
|
|
DbCollection testData;
|
|
|
|
String url;
|
2017-02-13 01:38:24 +00:00
|
|
|
HookedService greetingService;
|
2016-05-09 22:51:07 +00:00
|
|
|
|
|
|
|
setUp(() async {
|
|
|
|
client = new http.Client();
|
|
|
|
await db.open();
|
|
|
|
testData = db.collection('test_data');
|
|
|
|
// Delete anything before we start
|
|
|
|
await testData.remove();
|
2016-06-21 20:34:52 +00:00
|
|
|
|
2017-02-13 01:38:24 +00:00
|
|
|
var service = new MongoService(testData, debug: true);
|
|
|
|
greetingService = new HookedService(service);
|
|
|
|
wireHooked(greetingService);
|
2016-06-21 20:34:52 +00:00
|
|
|
|
2017-02-13 01:38:24 +00:00
|
|
|
app.use('/api', greetingService);
|
|
|
|
|
|
|
|
app.fatalErrorStream.listen((AngelFatalError e) {
|
|
|
|
print('Fatal error: ${e.error}');
|
|
|
|
print(e.stack);
|
|
|
|
});
|
|
|
|
|
|
|
|
var server = await app.startServer(InternetAddress.LOOPBACK_IP_V4, 0);
|
2016-05-09 22:51:07 +00:00
|
|
|
url = "http://${server.address.host}:${server.port}";
|
|
|
|
});
|
|
|
|
|
|
|
|
tearDown(() async {
|
|
|
|
// Delete anything left over
|
|
|
|
await testData.remove();
|
|
|
|
await db.close();
|
|
|
|
await app.httpServer.close(force: true);
|
|
|
|
client = null;
|
|
|
|
url = null;
|
2017-02-13 01:38:24 +00:00
|
|
|
greetingService = null;
|
2016-05-09 22:51:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('insert items', () async {
|
2016-06-22 18:34:28 +00:00
|
|
|
var response = await client.post("$url/api",
|
|
|
|
body: god.serialize(testGreeting), headers: headers);
|
2016-05-09 22:51:07 +00:00
|
|
|
expect(response.statusCode, equals(HttpStatus.OK));
|
|
|
|
|
|
|
|
response = await client.get("$url/api");
|
|
|
|
expect(response.statusCode, 200);
|
|
|
|
List<Map> users = god.deserialize(response.body);
|
|
|
|
expect(users.length, equals(1));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('read item', () async {
|
2016-06-22 18:34:28 +00:00
|
|
|
var response = await client.post("$url/api",
|
|
|
|
body: god.serialize(testGreeting), headers: headers);
|
2016-05-09 22:51:07 +00:00
|
|
|
expect(response.statusCode, equals(HttpStatus.OK));
|
|
|
|
Map created = god.deserialize(response.body);
|
|
|
|
|
2016-06-22 18:34:28 +00:00
|
|
|
response = await client.get("$url/api/${created['id']}");
|
2016-05-09 22:51:07 +00:00
|
|
|
expect(response.statusCode, equals(HttpStatus.OK));
|
|
|
|
Map read = god.deserialize(response.body);
|
2016-06-22 18:34:28 +00:00
|
|
|
expect(read['id'], equals(created['id']));
|
|
|
|
expect(read['to'], equals('world'));
|
2016-05-09 22:51:07 +00:00
|
|
|
expect(read['createdAt'], isNot(null));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('modify item', () async {
|
2016-06-22 18:34:28 +00:00
|
|
|
var response = await client.post("$url/api",
|
|
|
|
body: god.serialize(testGreeting), headers: headers);
|
|
|
|
expect(response.statusCode, equals(HttpStatus.OK));
|
|
|
|
Map created = god.deserialize(response.body);
|
2016-05-09 22:51:07 +00:00
|
|
|
|
2016-06-23 04:58:21 +00:00
|
|
|
response = await client.patch("$url/api/${created['id']}",
|
|
|
|
body: god.serialize({"to": "Mom"}), headers: headers);
|
2016-06-22 18:34:28 +00:00
|
|
|
Map modified = god.deserialize(response.body);
|
|
|
|
expect(response.statusCode, equals(HttpStatus.OK));
|
|
|
|
expect(modified['id'], equals(created['id']));
|
|
|
|
expect(modified['to'], equals('Mom'));
|
|
|
|
expect(modified['updatedAt'], isNot(null));
|
2016-05-09 22:51:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test('update item', () async {
|
2016-06-22 18:34:28 +00:00
|
|
|
var response = await client.post("$url/api",
|
|
|
|
body: god.serialize(testGreeting), headers: headers);
|
|
|
|
expect(response.statusCode, equals(HttpStatus.OK));
|
|
|
|
Map created = god.deserialize(response.body);
|
2016-05-09 22:51:07 +00:00
|
|
|
|
2016-06-23 04:58:21 +00:00
|
|
|
response = await client.post("$url/api/${created['id']}",
|
|
|
|
body: god.serialize({"to": "Updated"}), headers: headers);
|
2016-06-22 18:34:28 +00:00
|
|
|
Map modified = god.deserialize(response.body);
|
|
|
|
expect(response.statusCode, equals(HttpStatus.OK));
|
|
|
|
expect(modified['id'], equals(created['id']));
|
|
|
|
expect(modified['to'], equals('Updated'));
|
|
|
|
expect(modified['updatedAt'], isNot(null));
|
2016-05-09 22:51:07 +00:00
|
|
|
});
|
|
|
|
|
2016-06-23 04:58:21 +00:00
|
|
|
test('remove item', () async {
|
|
|
|
var response = await client.post("$url/api",
|
|
|
|
body: god.serialize(testGreeting), headers: headers);
|
|
|
|
Map created = god.deserialize(response.body);
|
|
|
|
|
2017-02-13 01:38:24 +00:00
|
|
|
int lastCount = (await greetingService.index()).length;
|
2016-05-09 22:51:07 +00:00
|
|
|
|
2016-06-23 04:58:21 +00:00
|
|
|
await client.delete("$url/api/${created['id']}");
|
2017-02-13 01:38:24 +00:00
|
|
|
expect((await greetingService.index()).length, equals(lastCount - 1));
|
2016-05-09 22:51:07 +00:00
|
|
|
});
|
|
|
|
|
2016-06-23 19:59:10 +00:00
|
|
|
test('\$sort and query parameters', () async {
|
|
|
|
// Search by where.eq
|
2017-02-13 01:38:24 +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
|
|
|
|
|
|
|
var response = await client.get("$url/api?to=world");
|
|
|
|
print(response.body);
|
|
|
|
List<Map> queried = god.deserialize(response.body);
|
|
|
|
expect(queried.length, equals(1));
|
|
|
|
expect(queried[0].keys.length, equals(3));
|
|
|
|
expect(queried[0]["id"], equals(world["id"]));
|
|
|
|
expect(queried[0]["to"], equals(world["to"]));
|
|
|
|
expect(queried[0]["createdAt"],
|
|
|
|
equals(world["createdAt"].toIso8601String()));
|
|
|
|
|
2017-02-13 01:38:24 +00:00
|
|
|
/*response = await client.get("$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"]));*/
|
|
|
|
|
|
|
|
queried = await greetingService.index({
|
2016-06-23 19:59:10 +00:00
|
|
|
"\$query": {"_id": where.id(new ObjectId.fromHexString(world["id"]))}
|
|
|
|
});
|
|
|
|
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
|
|
|
}
|