Build test for services

This commit is contained in:
thosakwe 2016-09-15 01:19:06 -04:00
parent a7223ee254
commit 034ede4dad
4 changed files with 95 additions and 3 deletions

1
.gitignore vendored
View file

@ -73,3 +73,4 @@ doc/api/
# (Library packages only! Remove pattern if developing an application package) # (Library packages only! Remove pattern if developing an application package)
pubspec.lock pubspec.lock
/sample_project/lib/src/services/ /sample_project/lib/src/services/
/sample_project/test/services/

View file

@ -61,14 +61,21 @@ class ServiceCommand extends Command {
var serviceFile = new File.fromUri(servicesDir.uri.resolve("${name.toLowerCase()}.dart")); var serviceFile = new File.fromUri(servicesDir.uri.resolve("${name.toLowerCase()}.dart"));
var serviceLibrary = new File.fromUri( var serviceLibrary = new File.fromUri(
servicesDir.uri.resolve("services.dart")); servicesDir.uri.resolve("services.dart"));
var testDir = new Directory("test/services");
var testFile = new File.fromUri(testDir.uri.resolve("${name.toLowerCase()}.dart"));
if (!await servicesDir.exists()) if (!await servicesDir.exists())
await servicesDir.create(recursive: true); await servicesDir.create(recursive: true);
if (!await testDir.exists())
await testDir.create(recursive: true);
await serviceFile.writeAsString(serviceSource); await serviceFile.writeAsString(serviceSource);
await serviceLibrary.writeAsString( await serviceLibrary.writeAsString(
"\nexport '${name.toLowerCase()}.dart';", mode: FileMode.APPEND); "\nexport '${name.toLowerCase()}.dart';", mode: FileMode.APPEND);
await testFile.writeAsString(_generateTests(name, type));
_pen.green(); _pen.green();
_pen("${Icon.CHECKMARK} Successfully generated service $name."); _pen("${Icon.CHECKMARK} Successfully generated service $name.");
_pen(); _pen();
@ -115,4 +122,85 @@ class ${name}Service extends MongoService {
} }
'''.trim(); '''.trim();
} }
_generateTests(String name, String type) {
return '''
import 'dart:io';
import 'package:angel/src/services/${name.toLowerCase()}.dart';
import 'package:angel_framework/angel_framework.dart';
import 'package:http/http.dart' as http;
import 'package:json_god/json_god.dart' as god;${_includeMongo(type)}
import 'package:test/test.dart';
main() {
group('$name', () {
Angel app;
http.Client client;
HttpServer server;
String url;
HookedService ${name}s;${_createDb(type)}
setUp(() async {
app = new Angel();
client = new http.Client();${_openDb(type)}
app.use('/${name.toLowerCase()}s', new ${name}Service(${_dbCollection(name, type)}));
${name}s = app.service("${name.toLowerCase()}s");
server = await app.startServer(null, 0);
url = "http://\${server.address.host}:\${server.port}";
});
tearDown(() async {
app = null;
url = null;
client.close();
client = null;
${name}s = null;
await server.close(force: true);
});
test('Index ${name.toLowerCase()}s', () async {
var indexed${name}s = await ${name}s.index();
});
test('Index via REST', () async {
var response = await client.get('\$url/${name.toLowerCase()}s');
print(god.deserialize(response.body));
});
});
}
'''.trim();
}
_createDb(String type) {
if (type == MONGO || type == MONGO_TYPED) {
return "\n Db db;";
}
return "";
}
_dbCollection(String name, String type) {
if (type == MONGO || type == MONGO_TYPED) {
return "db.collection('${name.toLowerCase()}s')";
}
return "";
}
_includeMongo(String type) {
if (type == MONGO || type == MONGO_TYPED) {
return "\nimport 'package:mongo_dart/mongo_dart.dart';";
}
return "";
}
_openDb(String type) {
if (type == MONGO || type == MONGO_TYPED) {
return "\n await db.open();";
}
return "";
}
} }

View file

@ -1,7 +1,7 @@
import "dart:async"; import "dart:async";
import "dart:io"; import "dart:io";
import "package:angel_framework/angel_framework.dart"; import "package:angel_framework/angel_framework.dart";
import "package:sample_project/src/services/services.dart"; import "package:angel/src/services/services.dart";
class SampleServer extends Angel { class SampleServer extends Angel {
@override @override

View file

@ -1,5 +1,8 @@
name: sample_project name: angel
publish_to: none publish_to: none
dependencies: dependencies:
angel_framework: ^1.0.0-dev angel_framework: ^1.0.0-dev
angel_mongo: ^1.0.0-dev angel_mongo: ^1.0.0-dev
dev_dependencies:
http: ^0.11.3
test: ^0.12.13