diff --git a/.gitignore b/.gitignore index 6d6d856..c6097d6 100644 --- a/.gitignore +++ b/.gitignore @@ -73,3 +73,4 @@ doc/api/ # (Library packages only! Remove pattern if developing an application package) pubspec.lock /sample_project/lib/src/services/ +/sample_project/test/services/ diff --git a/lib/src/commands/service.dart b/lib/src/commands/service.dart index 5e8a1c0..50eaef2 100644 --- a/lib/src/commands/service.dart +++ b/lib/src/commands/service.dart @@ -61,14 +61,21 @@ class ServiceCommand extends Command { var serviceFile = new File.fromUri(servicesDir.uri.resolve("${name.toLowerCase()}.dart")); var serviceLibrary = new File.fromUri( 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()) await servicesDir.create(recursive: true); + if (!await testDir.exists()) + await testDir.create(recursive: true); + await serviceFile.writeAsString(serviceSource); await serviceLibrary.writeAsString( "\nexport '${name.toLowerCase()}.dart';", mode: FileMode.APPEND); + await testFile.writeAsString(_generateTests(name, type)); + _pen.green(); _pen("${Icon.CHECKMARK} Successfully generated service $name."); _pen(); @@ -115,4 +122,85 @@ class ${name}Service extends MongoService { } '''.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 ""; + } } \ No newline at end of file diff --git a/sample_project/sample_project.dart b/sample_project/angel.dart similarity index 79% rename from sample_project/sample_project.dart rename to sample_project/angel.dart index e4adb20..67acefc 100644 --- a/sample_project/sample_project.dart +++ b/sample_project/angel.dart @@ -1,7 +1,7 @@ import "dart:async"; import "dart:io"; 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 { @override diff --git a/sample_project/pubspec.yaml b/sample_project/pubspec.yaml index 863d07e..9731a80 100644 --- a/sample_project/pubspec.yaml +++ b/sample_project/pubspec.yaml @@ -1,5 +1,8 @@ -name: sample_project +name: angel publish_to: none dependencies: angel_framework: ^1.0.0-dev - angel_mongo: ^1.0.0-dev \ No newline at end of file + angel_mongo: ^1.0.0-dev +dev_dependencies: + http: ^0.11.3 + test: ^0.12.13 \ No newline at end of file