Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(77)

Side by Side Diff: pkg/gcloud/test/db/properties_test.dart

Issue 804973002: Add appengine/gcloud/mustache dependencies. (Closed) Base URL: git@github.com:dart-lang/pub-dartlang-dart.git@master
Patch Set: Added AUTHORS/LICENSE/PATENTS files Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 gcloud.db.properties_test;
6
7 import 'dart:typed_data';
8
9 import 'package:gcloud/db.dart';
10 import 'package:gcloud/datastore.dart' as datastore;
11 import 'package:unittest/unittest.dart';
12
13 main() {
14 group('properties', () {
15 test('bool_property', () {
16 var prop = const BoolProperty(required: true);
17 expect(prop.validate(null, null), isFalse);
18
19 prop = const BoolProperty(required: false);
20 expect(prop.validate(null, null), isTrue);
21 expect(prop.validate(null, true), isTrue);
22 expect(prop.validate(null, false), isTrue);
23 expect(prop.encodeValue(null, null), equals(null));
24 expect(prop.encodeValue(null, true), equals(true));
25 expect(prop.encodeValue(null, false), equals(false));
26 expect(prop.decodePrimitiveValue(null, null), equals(null));
27 expect(prop.decodePrimitiveValue(null, true), equals(true));
28 expect(prop.decodePrimitiveValue(null, false), equals(false));
29 });
30
31 test('int_property', () {
32 var prop = const IntProperty(required: true);
33 expect(prop.validate(null, null), isFalse);
34
35 prop = const IntProperty(required: false);
36 expect(prop.validate(null, null), isTrue);
37 expect(prop.validate(null, 33), isTrue);
38 expect(prop.encodeValue(null, null), equals(null));
39 expect(prop.encodeValue(null, 42), equals(42));
40 expect(prop.decodePrimitiveValue(null, null), equals(null));
41 expect(prop.decodePrimitiveValue(null, 99), equals(99));
42 });
43
44 test('double_property', () {
45 var prop = const DoubleProperty(required: true);
46 expect(prop.validate(null, null), isFalse);
47
48 prop = const DoubleProperty(required: false);
49 expect(prop.validate(null, null), isTrue);
50 expect(prop.validate(null, 33.0), isTrue);
51 expect(prop.encodeValue(null, null), equals(null));
52 expect(prop.encodeValue(null, 42.3), equals(42.3));
53 expect(prop.decodePrimitiveValue(null, null), equals(null));
54 expect(prop.decodePrimitiveValue(null, 99.1), equals(99.1));
55 });
56
57 test('string_property', () {
58 var prop = const StringProperty(required: true);
59 expect(prop.validate(null, null), isFalse);
60
61 prop = const StringProperty(required: false);
62 expect(prop.validate(null, null), isTrue);
63 expect(prop.validate(null, 'foobar'), isTrue);
64 expect(prop.encodeValue(null, null), equals(null));
65 expect(prop.encodeValue(null, 'foo'), equals('foo'));
66 expect(prop.decodePrimitiveValue(null, null), equals(null));
67 expect(prop.decodePrimitiveValue(null, 'bar'), equals('bar'));
68 });
69
70 test('blob_property', () {
71 var prop = const BlobProperty(required: true);
72 expect(prop.validate(null, null), isFalse);
73
74 prop = const BlobProperty(required: false);
75 expect(prop.validate(null, null), isTrue);
76 expect(prop.validate(null, [1,2]), isTrue);
77 expect(prop.encodeValue(null, null), equals(null));
78 expect(prop.encodeValue(null, []).bytes, equals([]));
79 expect(prop.encodeValue(null, [1,2]).bytes, equals([1,2]));
80 expect(prop.encodeValue(null, new Uint8List.fromList([1,2])).bytes,
81 equals([1,2]));
82 expect(prop.decodePrimitiveValue(null, null), equals(null));
83 expect(prop.decodePrimitiveValue(null, new datastore.BlobValue([])),
84 equals([]));
85 expect(prop.decodePrimitiveValue(null, new datastore.BlobValue([5,6])),
86 equals([5,6]));
87 expect(prop.decodePrimitiveValue(
88 null, new datastore.BlobValue(new Uint8List.fromList([5,6]))),
89 equals([5,6]));
90 });
91
92 test('datetime_property', () {
93 var utc99 = new DateTime.fromMillisecondsSinceEpoch(99, isUtc: true);
94
95 var prop = const DateTimeProperty(required: true);
96 expect(prop.validate(null, null), isFalse);
97
98 prop = const DateTimeProperty(required: false);
99 expect(prop.validate(null, null), isTrue);
100 expect(prop.validate(null, utc99), isTrue);
101 expect(prop.encodeValue(null, null), equals(null));
102 expect(prop.encodeValue(null, utc99), equals(utc99));
103 expect(prop.decodePrimitiveValue(null, null), equals(null));
104 expect(prop.decodePrimitiveValue(null, 99*1000), equals(utc99));
105 expect(prop.decodePrimitiveValue(null, 99*1000 + 1), equals(utc99));
106 expect(prop.decodePrimitiveValue(null, utc99), equals(utc99));
107 });
108
109 test('list_property', () {
110 var prop = const ListProperty(const BoolProperty());
111
112 expect(prop.validate(null, null), isFalse);
113 expect(prop.validate(null, []), isTrue);
114 expect(prop.validate(null, [true]), isTrue);
115 expect(prop.validate(null, [true, false]), isTrue);
116 expect(prop.validate(null, [true, false, 1]), isFalse);
117 expect(prop.encodeValue(null, []), equals(null));
118 expect(prop.encodeValue(null, [true]), equals(true));
119 expect(prop.encodeValue(null, [true, false]), equals([true, false]));
120 expect(prop.decodePrimitiveValue(null, null), equals([]));
121 expect(prop.decodePrimitiveValue(null, []), equals([]));
122 expect(prop.decodePrimitiveValue(null, true), equals([true]));
123 expect(prop.decodePrimitiveValue(null, [true, false]),
124 equals([true, false]));
125 });
126
127 test('composed_list_property', () {
128 var prop = const ListProperty(const CustomProperty());
129
130 var c1 = new Custom()..customValue = 'c1';
131 var c2 = new Custom()..customValue = 'c2';
132
133 expect(prop.validate(null, null), isFalse);
134 expect(prop.validate(null, []), isTrue);
135 expect(prop.validate(null, [c1]), isTrue);
136 expect(prop.validate(null, [c1, c2]), isTrue);
137 expect(prop.validate(null, [c1, c2, 1]), isFalse);
138 expect(prop.encodeValue(null, []), equals(null));
139 expect(prop.encodeValue(null, [c1]), equals(c1.customValue));
140 expect(prop.encodeValue(null, [c1, c2]),
141 equals([c1.customValue, c2.customValue]));
142 expect(prop.decodePrimitiveValue(null, null), equals([]));
143 expect(prop.decodePrimitiveValue(null, []), equals([]));
144 expect(prop.decodePrimitiveValue(null, c1.customValue), equals([c1]));
145 expect(prop.decodePrimitiveValue(null, [c1.customValue, c2.customValue]),
146 equals([c1, c2]));
147 });
148
149 test('modelkey_property', () {
150 var datastoreKey = new datastore.Key(
151 [new datastore.KeyElement('MyKind', 42)],
152 partition: new datastore.Partition('foonamespace'));
153 var dbKey = new KeyMock(datastoreKey);
154 var modelDBMock = new ModelDBMock(datastoreKey, dbKey);
155
156 var prop = const ModelKeyProperty(required: true);
157 expect(prop.validate(modelDBMock, null), isFalse);
158
159 prop = const ModelKeyProperty(required: false);
160 expect(prop.validate(modelDBMock, null), isTrue);
161 expect(prop.validate(modelDBMock, dbKey), isTrue);
162 expect(prop.validate(modelDBMock, datastoreKey), isFalse);
163 expect(prop.encodeValue(modelDBMock, null), equals(null));
164 expect(prop.encodeValue(modelDBMock, dbKey), equals(datastoreKey));
165 expect(prop.decodePrimitiveValue(modelDBMock, null), equals(null));
166 expect(prop.decodePrimitiveValue(modelDBMock, datastoreKey),
167 equals(dbKey));
168 });
169 });
170 }
171
172 class Custom {
173 String customValue;
174
175 int get hashCode => customValue.hashCode;
176
177 bool operator==(other) {
178 return other is Custom && other.customValue == customValue;
179 }
180 }
181
182 class CustomProperty extends StringProperty {
183 const CustomProperty(
184 {String propertyName: null, bool required: false, bool indexed: true});
185
186 bool validate(ModelDB db, Object value) {
187 if (required && value == null) return false;
188 return value == null || value is Custom;
189 }
190
191 Object decodePrimitiveValue(ModelDB db, Object value) {
192 if (value == null) return null;
193 return new Custom()..customValue = value;
194 }
195
196 Object encodeValue(ModelDB db, Object value) {
197 if (value == null) return null;
198 return (value as Custom).customValue;
199 }
200 }
201
202 class KeyMock implements Key {
203 datastore.Key _datastoreKey;
204
205 KeyMock(this._datastoreKey);
206
207 Object id = 1;
208 Type type = null;
209 Key get parent => this;
210 bool get isEmpty => false;
211 Partition get partition => null;
212 datastore.Key get datastoreKey => _datastoreKey;
213 Key append(Type modelType, {Object id}) => null;
214 int get hashCode => 1;
215 }
216
217 class ModelDBMock implements ModelDB {
218 final datastore.Key _datastoreKey;
219 final Key _dbKey;
220 ModelDBMock(this._datastoreKey, this._dbKey);
221
222 Key fromDatastoreKey(datastore.Key datastoreKey) {
223 if (!identical(_datastoreKey, datastoreKey)) {
224 throw "Broken test";
225 }
226 return _dbKey;
227 }
228
229 datastore.Key toDatastoreKey(Key key) {
230 if (!identical(_dbKey, key)) {
231 throw "Broken test";
232 }
233 return _datastoreKey;
234 }
235
236 Map<String, Property> propertiesForModel(modelDescription) => null;
237 Model fromDatastoreEntity(datastore.Entity entity) => null;
238 datastore.Entity toDatastoreEntity(Model model) => null;
239 String fieldNameToPropertyName(String kind, String fieldName) => null;
240 String kindName(Type type) => null;
241 }
OLDNEW
« no previous file with comments | « pkg/gcloud/test/db/model_dbs/no_default_constructor.dart ('k') | pkg/gcloud/test/db_all_e2e_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698