OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library metamodel_test; |
| 6 |
| 7 import 'dart:async'; |
| 8 |
| 9 import 'package:unittest/unittest.dart'; |
| 10 |
| 11 import 'package:appengine/src/appengine_context.dart'; |
| 12 import 'package:appengine/src/api_impl/raw_datastore_v3_impl.dart'; |
| 13 import 'package:appengine/src/protobuf_api/rpc/rpc_service_remote_api.dart'; |
| 14 import 'package:gcloud/datastore.dart'; |
| 15 import 'package:gcloud/datastore.dart' show Key, Query, Partition; |
| 16 import 'package:gcloud/db.dart' as db; |
| 17 import 'package:gcloud/db/metamodel.dart'; |
| 18 |
| 19 List<Entity> buildEntitiesWithDifferentNamespaces() { |
| 20 newKey(String namespace, String kind, int id) { |
| 21 var partition = new Partition(namespace); |
| 22 return new Key([new KeyElement(kind, id)], partition: partition); |
| 23 } |
| 24 |
| 25 newEntity(String namespace, String kind, {int id: 1}) { |
| 26 return new Entity(newKey(namespace, kind, id), {'ping': 'pong'}); |
| 27 } |
| 28 |
| 29 return [ |
| 30 newEntity(null, 'NullKind', id: 1), |
| 31 newEntity(null, 'NullKind', id: 2), |
| 32 newEntity(null, 'NullKind2', id: 1), |
| 33 newEntity(null, 'NullKind2', id: 2), |
| 34 |
| 35 newEntity('FooNamespace', 'FooKind', id: 1), |
| 36 newEntity('FooNamespace', 'FooKind', id: 2), |
| 37 newEntity('FooNamespace', 'FooKind2', id: 1), |
| 38 newEntity('FooNamespace', 'FooKind2', id: 2), |
| 39 |
| 40 newEntity('BarNamespace', 'BarKind', id: 1), |
| 41 newEntity('BarNamespace', 'BarKind', id: 2), |
| 42 newEntity('BarNamespace', 'BarKind2', id: 1), |
| 43 newEntity('BarNamespace', 'BarKind2', id: 2), |
| 44 ]; |
| 45 } |
| 46 |
| 47 Future sleep(Duration duration) { |
| 48 var completer = new Completer(); |
| 49 new Timer(duration, completer.complete); |
| 50 return completer.future; |
| 51 } |
| 52 |
| 53 runTests(datastore, db.DatastoreDB store) { |
| 54 final cond = predicate; |
| 55 |
| 56 group('e2e_db_metamodel', () { |
| 57 test('namespaces__insert_lookup_delete', () { |
| 58 var entities = buildEntitiesWithDifferentNamespaces(); |
| 59 var keys = entities.map((e) => e.key).toList(); |
| 60 |
| 61 return datastore.commit(inserts: entities).then((_) { |
| 62 return sleep(const Duration(seconds: 10)).then((_) { |
| 63 var namespaceQuery = store.query(Namespace); |
| 64 return namespaceQuery.run().toList() |
| 65 .then((List<Namespace> namespaces) { |
| 66 expect(namespaces.length, 3); |
| 67 expect(namespaces, contains(cond((ns) => ns.name == null))); |
| 68 expect(namespaces, |
| 69 contains(cond((ns) => ns.name == 'FooNamespace'))); |
| 70 expect(namespaces, |
| 71 contains(cond((ns) => ns.name == 'BarNamespace'))); |
| 72 |
| 73 var futures = []; |
| 74 for (var namespace in namespaces) { |
| 75 var partition = store.newPartition(namespace.name); |
| 76 var kindQuery = store.query(Kind, partition: partition); |
| 77 futures.add(kindQuery.run().toList().then((List<Kind> kinds) { |
| 78 expect(kinds.length, greaterThanOrEqualTo(2)); |
| 79 if (namespace.name == null) { |
| 80 expect(kinds, contains(cond((k) => k.name == 'NullKind'))); |
| 81 expect(kinds, contains(cond((k) => k.name == 'NullKind2'))); |
| 82 } else if (namespace.name == 'FooNamespace') { |
| 83 expect(kinds, contains(cond((k) => k.name == 'FooKind'))); |
| 84 expect(kinds, contains(cond((k) => k.name == 'FooKind2'))); |
| 85 } else if (namespace.name == 'BarNamespace') { |
| 86 expect(kinds, contains(cond((k) => k.name == 'BarKind'))); |
| 87 expect(kinds, contains(cond((k) => k.name == 'BarKind2'))); |
| 88 } |
| 89 })); |
| 90 } |
| 91 return Future.wait(futures).then((_) { |
| 92 expect(datastore.commit(deletes: keys), completes); |
| 93 }); |
| 94 }); |
| 95 }); |
| 96 }); |
| 97 }); |
| 98 }); |
| 99 } |
| 100 |
| 101 main() { |
| 102 var rpcService = new RPCServiceRemoteApi('127.0.0.1', 4444); |
| 103 var appengineContext = new AppengineContext( |
| 104 'dev', 'test-application', 'test-version', null, null, null); |
| 105 var datastore = |
| 106 new DatastoreV3RpcImpl(rpcService, appengineContext, '<invalid-ticket>'); |
| 107 |
| 108 runTests(datastore, new db.DatastoreDB(datastore)); |
| 109 } |
OLD | NEW |