Skip to content

Commit aab8eef

Browse files
committed
Fix tests after builder refactoring.
1 parent d67e7e3 commit aab8eef

4 files changed

Lines changed: 78 additions & 21 deletions

File tree

src/main/java/com/google/gcloud/datastore/Key.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.io.UnsupportedEncodingException;
1212
import java.net.URLDecoder;
1313
import java.net.URLEncoder;
14+
import java.util.Objects;
1415

1516
/**
1617
* A key that is guaranteed to be complete and could be used to reference a
@@ -76,14 +77,14 @@ protected Key build(String dataset, String namespace, ImmutableList<KeyPathEleme
7677
String kind, String name) {
7778
super(dataset, namespace, ancestors, kind);
7879
this.name = name;
79-
this.id = null;
80+
id = null;
8081
}
8182

8283
Key(String dataset, String namespace, ImmutableList<KeyPathElement> ancestors,
8384
String kind, long id) {
8485
super(dataset, namespace, ancestors, kind);
8586
this.id = id;
86-
this.name = null;
87+
name = null;
8788
}
8889

8990
public boolean hasId() {
@@ -143,6 +144,29 @@ public static Key fromUrlSafe(String urlSafe) {
143144
}
144145
}
145146

147+
@Override
148+
public int hashCode() {
149+
return Objects.hash(super.hashCode(), name, id);
150+
}
151+
152+
@Override
153+
public boolean equals(Object obj) {
154+
if (!(obj instanceof Key)) {
155+
return false;
156+
}
157+
Key other = (Key) obj;
158+
return Objects.equals(name, other.name)
159+
&& Objects.equals(id, other.id)
160+
&& super.equals(obj);
161+
}
162+
163+
@Override
164+
protected void addLeaf(DatastoreV1.Key.Builder keyPb) {
165+
KeyPathElement leaf =
166+
hasId() ? new KeyPathElement(kind(), id) : new KeyPathElement(kind(), name);
167+
keyPb.addPathElement(leaf.toPb());
168+
}
169+
146170
@Override
147171
protected Object fromPb(byte[] bytesPb) throws InvalidProtocolBufferException {
148172
return fromPb(DatastoreV1.Key.parseFrom(bytesPb));

src/main/java/com/google/gcloud/datastore/ListValue.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ public Builder addValue(Value<?, ?, ?> first, Value<?, ?, ?>... other) {
6767
return this;
6868
}
6969

70+
@Override
71+
public Builder indexed(boolean indexed) {
72+
DatastoreServiceException.throwInvalidRequest("ListValue can't specify index");
73+
return this;
74+
}
75+
7076
/**
7177
* Copy the list of values.
7278
*
@@ -106,6 +112,6 @@ private ListValue(Builder builder) {
106112
}
107113

108114
public static Builder builder() {
109-
return new Builder().indexed(false);
115+
return new Builder();
110116
}
111117
}

src/test/java/com/google/gcloud/datastore/DatastoreServiceTest.java

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public class DatastoreServiceTest {
4040
.setProperty("bool", BOOL_VALUE)
4141
.setProperty("list", LIST_VALUE1)
4242
.build();
43+
private static final PartialEntity PARTIAL_ENTITY2 = PartialEntity.builder(PARTIAL_ENTITY1)
44+
.removeProperty("str")
45+
.setBooleanProperty("bool", true)
46+
.setListProperty("list", LIST_VALUE1.get())
47+
.build();
4348
private static final Entity ENTITY1 = Entity.builder(KEY1)
4449
.setProperty("str", STR_VALUE)
4550
.setProperty("bool", BOOL_VALUE)
@@ -49,13 +54,14 @@ public class DatastoreServiceTest {
4954
private static final Entity ENTITY2 = Entity.builder(ENTITY1)
5055
.key(KEY2)
5156
.removeProperty("str")
52-
.setProperty("null", NULL_VALUE)
57+
.setNullProperty("null")
5358
.build();
5459
private static final Entity ENTITY3 = Entity.builder(ENTITY1)
5560
.key(KEY3)
5661
.removeProperty("str")
5762
.setProperty("null", NULL_VALUE)
58-
.setProperty("partial2", new PartialEntityValue(ENTITY1))
63+
.setPartialEntityProperty("partial1", PARTIAL_ENTITY2)
64+
.setPartialEntityProperty("partial2", ENTITY2)
5965
.build();
6066

6167
private DatastoreServiceOptions options;
@@ -169,23 +175,24 @@ public void testAllocateId() {
169175
@Test
170176
public void testAllocateIdArray() {
171177
KeyBuilder keyBuilder = datastore.newKeyBuilder(KIND1);
172-
PartialKey pKey1 = keyBuilder.build();
173-
PartialKey pKey2 = keyBuilder.kind(KIND2).addAncestor(KIND1, 10).build();
178+
PartialKey partialKey1 = keyBuilder.build();
179+
PartialKey partialKey2 = keyBuilder.kind(KIND2).addAncestor(KIND1, 10).build();
174180
Key key3 = keyBuilder.build("name");
175181
Key key4 = keyBuilder.build(1);
176-
Iterator<Key> result = datastore.allocateId(pKey1, pKey2, key3, key4, pKey1, key3);
182+
Iterator<Key> result =
183+
datastore.allocateId(partialKey1, partialKey2, key3, key4, partialKey1, key3);
177184
Map<Integer, Key> map = new HashMap<>();
178185
int count = 0;
179186
while (result.hasNext()) {
180187
map.put(++count, result.next());
181188
}
182189
assertEquals(6, map.size());
183-
assertEquals(pKey1.newKey(map.get(1).id()), map.get(1));
184-
assertEquals(pKey1.newKey(map.get(5).id()), map.get(5));
185-
assertEquals(pKey2.newKey(map.get(2).id()), map.get(2));
186-
assertEquals(key3.builder().id(map.get(3).id()).build(), map.get(3));
187-
assertEquals(key3.builder().id(map.get(6).id()).build(), map.get(6));
188-
assertEquals(key4.builder().id(map.get(4).id()).build(), map.get(4));
190+
assertEquals(partialKey1.newKey(map.get(1).id()), map.get(1));
191+
assertEquals(partialKey1.newKey(map.get(5).id()), map.get(5));
192+
assertEquals(partialKey2.newKey(map.get(2).id()), map.get(2));
193+
assertEquals(Key.builder(key3).id(map.get(3).id()).build(), map.get(3));
194+
assertEquals(Key.builder(key3).id(map.get(6).id()).build(), map.get(6));
195+
assertEquals(Key.builder(key4).id(map.get(4).id()).build(), map.get(4));
189196
}
190197

191198
@Test
@@ -194,24 +201,43 @@ public void testGet() {
194201
assertNull(entity);
195202

196203
entity = datastore.get(KEY1);
204+
assertEquals(ENTITY1, entity);
197205
StringValue value1 = entity.property("str");
198206
BooleanValue value2 = entity.property("bool");
199-
PartialEntityValue value3 = entity.property("partial1");
207+
ListValue value3 = entity.property("list");
200208
assertEquals(value1, STR_VALUE);
201209
assertEquals(value2, BOOL_VALUE);
202-
assertEquals(value3, new PartialEntityValue(PARTIAL_ENTITY1));
203-
assertEquals(3, entity.propertyNames().size());
204-
assertTrue(entity.propertyNames().contains("str"));
205-
assertTrue(entity.propertyNames().contains("bool"));
210+
assertEquals(value3, LIST_VALUE2);
211+
assertEquals(PARTIAL_ENTITY1, entity.partialEntityProperty("partial1"));
212+
assertEquals(4, entity.propertyNames().size());
206213
assertFalse(entity.hasProperty("bla"));
207214
}
208215

209216
@Test
210217
public void testGetArray() {
211-
Iterator<Entity> result = datastore.get(KEY1, KEY1.builder().name("bla").build(), KEY2);
218+
datastore.put(ENTITY3);
219+
Iterator<Entity> result =
220+
datastore.get(KEY1, Key.builder(KEY1).name("bla").build(), KEY2, KEY3);
212221
assertEquals(ENTITY1, result.next());
213222
assertNull(result.next());
214223
assertEquals(ENTITY2, result.next());
224+
Entity entity3 = result.next();
225+
assertEquals(ENTITY3, entity3);
226+
assertTrue(entity3.isNullProperty("null"));
227+
assertEquals(false, entity3.booleanProperty("bool"));
228+
assertEquals(LIST_VALUE2.get(), entity3.listProperty("list"));
229+
PartialEntity partial1 = entity3.partialEntityProperty("partial1");
230+
Entity partial2 = (Entity) entity3.partialEntityProperty("partial2");
231+
assertEquals(partial1, PARTIAL_ENTITY2);
232+
assertEquals(partial2, ENTITY2);
233+
assertEquals(Value.Type.BOOLEAN, entity3.propertyType("bool"));
234+
assertEquals(5, entity3.propertyNames().size());
235+
assertFalse(entity3.hasProperty("bla"));
236+
try {
237+
entity3.stringProperty("str");
238+
} catch (DatastoreServiceException expected) {
239+
// expected - no such property
240+
}
215241
assertFalse(result.hasNext());
216242
}
217243

@@ -297,6 +323,6 @@ public void testNewKeyBuilder() {
297323
assertEquals(PartialKey.builder(PARTIAL_KEY1).kind(KIND2).build(),
298324
datastore.newKeyBuilder(KIND2).build());
299325
assertEquals(KEY1, keyBuilder.build("name"));
300-
assertEquals(KEY1.builder().id(2).build(), keyBuilder.build(2));
326+
assertEquals(Key.builder(KEY1).id(2).build(), keyBuilder.build(2));
301327
}
302328
}

src/test/java/com/google/gcloud/datastore/SerializationTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public void testTypes() throws Exception {
9696
Object[] types = { KEY1, KEY2, INCOMPLETE_KEY1, INCOMPLETE_KEY2, ENTITY1, ENTITY2,
9797
ENTITY3, EMBEDDED_ENTITY1, EMBEDDED_ENTITY2, EMBEDDED_ENTITY3};
9898
for (Object obj : types) {
99+
System.out.println("koko->" + obj);
99100
Object copy = serialiazeAndDeserialize(obj);
100101
assertEquals(obj, obj);
101102
assertEquals(obj, copy);

0 commit comments

Comments
 (0)