OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library gcloud.db.properties_test; | 5 library gcloud.db.properties_test; |
6 | 6 |
7 import 'dart:typed_data'; | 7 import 'dart:typed_data'; |
8 | 8 |
9 import 'package:gcloud/db.dart'; | 9 import 'package:gcloud/db.dart'; |
10 import 'package:gcloud/datastore.dart' as datastore; | 10 import 'package:gcloud/datastore.dart' as datastore; |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 expect(prop.decodePrimitiveValue(null, 99*1000), equals(utc99)); | 104 expect(prop.decodePrimitiveValue(null, 99*1000), equals(utc99)); |
105 expect(prop.decodePrimitiveValue(null, 99*1000 + 1), equals(utc99)); | 105 expect(prop.decodePrimitiveValue(null, 99*1000 + 1), equals(utc99)); |
106 expect(prop.decodePrimitiveValue(null, utc99), equals(utc99)); | 106 expect(prop.decodePrimitiveValue(null, utc99), equals(utc99)); |
107 }); | 107 }); |
108 | 108 |
109 test('list_property', () { | 109 test('list_property', () { |
110 var prop = const ListProperty(const BoolProperty()); | 110 var prop = const ListProperty(const BoolProperty()); |
111 | 111 |
112 expect(prop.validate(null, null), isFalse); | 112 expect(prop.validate(null, null), isFalse); |
113 expect(prop.validate(null, []), isTrue); | 113 expect(prop.validate(null, []), isTrue); |
| 114 expect(prop.validate(null, [true]), isTrue); |
114 expect(prop.validate(null, [true, false]), isTrue); | 115 expect(prop.validate(null, [true, false]), isTrue); |
115 expect(prop.validate(null, [true, false, 1]), isFalse); | 116 expect(prop.validate(null, [true, false, 1]), isFalse); |
116 expect(prop.encodeValue(null, []), equals(null)); | 117 expect(prop.encodeValue(null, []), equals(null)); |
117 expect(prop.encodeValue(null, [true]), equals(true)); | 118 expect(prop.encodeValue(null, [true]), equals(true)); |
118 expect(prop.encodeValue(null, [true, false]), equals([true, false])); | 119 expect(prop.encodeValue(null, [true, false]), equals([true, false])); |
119 expect(prop.decodePrimitiveValue(null, null), equals([])); | 120 expect(prop.decodePrimitiveValue(null, null), equals([])); |
120 expect(prop.decodePrimitiveValue(null, []), equals([])); | 121 expect(prop.decodePrimitiveValue(null, []), equals([])); |
121 expect(prop.decodePrimitiveValue(null, true), equals([true])); | 122 expect(prop.decodePrimitiveValue(null, true), equals([true])); |
122 expect(prop.decodePrimitiveValue(null, [true, false]), | 123 expect(prop.decodePrimitiveValue(null, [true, false]), |
123 equals([true, false])); | 124 equals([true, false])); |
124 }); | 125 }); |
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 |
126 test('modelkey_property', () { | 149 test('modelkey_property', () { |
127 var datastoreKey = new datastore.Key( | 150 var datastoreKey = new datastore.Key( |
128 [new datastore.KeyElement('MyKind', 42)], | 151 [new datastore.KeyElement('MyKind', 42)], |
129 partition: new datastore.Partition('foonamespace')); | 152 partition: new datastore.Partition('foonamespace')); |
130 var dbKey = new KeyMock(datastoreKey); | 153 var dbKey = new KeyMock(datastoreKey); |
131 var modelDBMock = new ModelDBMock(datastoreKey, dbKey); | 154 var modelDBMock = new ModelDBMock(datastoreKey, dbKey); |
132 | 155 |
133 var prop = const ModelKeyProperty(required: true); | 156 var prop = const ModelKeyProperty(required: true); |
134 expect(prop.validate(modelDBMock, null), isFalse); | 157 expect(prop.validate(modelDBMock, null), isFalse); |
135 | 158 |
136 prop = const ModelKeyProperty(required: false); | 159 prop = const ModelKeyProperty(required: false); |
137 expect(prop.validate(modelDBMock, null), isTrue); | 160 expect(prop.validate(modelDBMock, null), isTrue); |
138 expect(prop.validate(modelDBMock, dbKey), isTrue); | 161 expect(prop.validate(modelDBMock, dbKey), isTrue); |
139 expect(prop.validate(modelDBMock, datastoreKey), isFalse); | 162 expect(prop.validate(modelDBMock, datastoreKey), isFalse); |
140 expect(prop.encodeValue(modelDBMock, null), equals(null)); | 163 expect(prop.encodeValue(modelDBMock, null), equals(null)); |
141 expect(prop.encodeValue(modelDBMock, dbKey), equals(datastoreKey)); | 164 expect(prop.encodeValue(modelDBMock, dbKey), equals(datastoreKey)); |
142 expect(prop.decodePrimitiveValue(modelDBMock, null), equals(null)); | 165 expect(prop.decodePrimitiveValue(modelDBMock, null), equals(null)); |
143 expect(prop.decodePrimitiveValue(modelDBMock, datastoreKey), | 166 expect(prop.decodePrimitiveValue(modelDBMock, datastoreKey), |
144 equals(dbKey)); | 167 equals(dbKey)); |
145 }); | 168 }); |
146 }); | 169 }); |
147 } | 170 } |
148 | 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 |
149 class KeyMock implements Key { | 202 class KeyMock implements Key { |
150 datastore.Key _datastoreKey; | 203 datastore.Key _datastoreKey; |
151 | 204 |
152 KeyMock(this._datastoreKey); | 205 KeyMock(this._datastoreKey); |
153 | 206 |
154 Object id = 1; | 207 Object id = 1; |
155 Type type = null; | 208 Type type = null; |
156 Key get parent => this; | 209 Key get parent => this; |
157 bool get isEmpty => false; | 210 bool get isEmpty => false; |
158 Partition get partition => null; | 211 Partition get partition => null; |
(...skipping 20 matching lines...) Expand all Loading... |
179 } | 232 } |
180 return _datastoreKey; | 233 return _datastoreKey; |
181 } | 234 } |
182 | 235 |
183 Map<String, Property> propertiesForModel(modelDescription) => null; | 236 Map<String, Property> propertiesForModel(modelDescription) => null; |
184 Model fromDatastoreEntity(datastore.Entity entity) => null; | 237 Model fromDatastoreEntity(datastore.Entity entity) => null; |
185 datastore.Entity toDatastoreEntity(Model model) => null; | 238 datastore.Entity toDatastoreEntity(Model model) => null; |
186 String fieldNameToPropertyName(String kind, String fieldName) => null; | 239 String fieldNameToPropertyName(String kind, String fieldName) => null; |
187 String kindName(Type type) => null; | 240 String kindName(Type type) => null; |
188 } | 241 } |
OLD | NEW |