Skip to content

Drop FieldValue from tests #1216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,25 @@
import com.google.firebase.firestore.model.DocumentSet;
import com.google.firebase.firestore.model.ResourcePath;
import com.google.firebase.firestore.model.SnapshotVersion;
import com.google.firebase.firestore.model.value.FieldValue;
import com.google.firebase.firestore.model.value.ObjectValue;
import com.google.firestore.v1.Value;
import java.util.Comparator;
import java.util.Map;

/** A set of utilities for tests */
public class TestUtil {

public static FieldValue wrap(Object value) {
public static Value wrap(Object value) {
DatabaseId databaseId = DatabaseId.forProject("project");
UserDataReader dataReader = new UserDataReader(databaseId);
// HACK: We use parseQueryValue() since it accepts scalars as well as arrays / objects, and
// our tests currently use wrap() pretty generically so we don't know the intent.
return new FieldValue(dataReader.parseQueryValue(value));
return dataReader.parseQueryValue(value);
}

public static ObjectValue wrapObject(Map<String, Object> value) {
// Cast is safe here because value passed in is a map
return new ObjectValue(wrap(value).getProto());
return new ObjectValue(wrap(value));
}

public static DocumentKey key(String key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class QuerySnapshotTests {
val qs = TestUtil.querySnapshot(
"rooms",
mapOf(),
mapOf("id" to ObjectValue.fromMap(mapOf("a" to wrap(1).proto, "b" to wrap(2).proto))),
mapOf("id" to ObjectValue.fromMap(mapOf("a" to wrap(1), "b" to wrap(2)))),
false,
false)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,18 @@
import static com.google.firebase.firestore.testutil.TestUtil.field;
import static com.google.firebase.firestore.testutil.TestUtil.map;
import static com.google.firebase.firestore.testutil.TestUtil.ref;
import static com.google.firebase.firestore.testutil.TestUtil.valueOf;
import static com.google.firebase.firestore.testutil.TestUtil.wrap;
import static com.google.firebase.firestore.testutil.TestUtil.wrapObject;
import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.google.firebase.Timestamp;
import com.google.firebase.firestore.model.DatabaseId;
import com.google.firebase.firestore.model.DocumentKey;
import com.google.firebase.firestore.model.value.FieldValue;
import com.google.firebase.firestore.model.value.ObjectValue;
import com.google.firebase.firestore.model.value.ProtoValues;
import com.google.firestore.v1.ArrayValue;
Expand All @@ -54,15 +53,16 @@ public class UserDataWriterTest {

@Test
public void testConvertsNullValue() {
FieldValue value = wrap(null);
assertEquals(value, new FieldValue(ProtoValues.NULL_VALUE));
Value value = wrap(null);
Object convertedValue = convertValue(value);
assertNull(convertedValue);
}

@Test
public void testConvertsBooleanValue() {
List<Boolean> testCases = asList(true, false);
for (Boolean b : testCases) {
FieldValue value = wrap(b);
Value value = wrap(b);
assertValueType(Value.ValueTypeCase.BOOLEAN_VALUE, value);
Object convertedValue = convertValue(value);
assertEquals(b, convertedValue);
Expand All @@ -73,7 +73,7 @@ public void testConvertsBooleanValue() {
public void testConvertsIntegerValue() {
List<Integer> testCases = asList(Integer.MIN_VALUE, -1, 0, 1, Integer.MAX_VALUE);
for (Integer i : testCases) {
FieldValue value = wrap(i);
Value value = wrap(i);
assertValueType(Value.ValueTypeCase.INTEGER_VALUE, value);
Object convertedValue = convertValue(value);
assertEquals(i.longValue(), convertedValue);
Expand All @@ -93,7 +93,7 @@ public void testConvertsLongValue() {
Long.valueOf(Integer.MAX_VALUE),
Long.MAX_VALUE);
for (Long l : testCases) {
FieldValue value = wrap(l);
Value value = wrap(l);
assertValueType(Value.ValueTypeCase.INTEGER_VALUE, value);
Object convertedValue = convertValue(value);
assertEquals(l, convertedValue);
Expand All @@ -115,7 +115,7 @@ public void testConvertsFloatValue() {
Long.MAX_VALUE * 1.0f,
Float.MAX_VALUE);
for (Float f : testCases) {
FieldValue value = wrap(f);
Value value = wrap(f);
assertValueType(Value.ValueTypeCase.DOUBLE_VALUE, value);
Object convertedValue = convertValue(value);
assertEquals(f.doubleValue(), convertedValue);
Expand Down Expand Up @@ -146,7 +146,7 @@ public void testConvertsDoubleValue() {
Double.POSITIVE_INFINITY,
Double.NaN);
for (Double d : testCases) {
FieldValue value = wrap(d);
Value value = wrap(d);
assertValueType(Value.ValueTypeCase.DOUBLE_VALUE, value);
Object convertedValue = convertValue(value);
assertEquals(d, convertedValue);
Expand All @@ -162,9 +162,9 @@ public void testConvertsDateValue() {
DocumentSnapshot.ServerTimestampBehavior.DEFAULT);
List<Date> testCases = asList(new Date(0), new Date(1356048000000L));
for (Date d : testCases) {
FieldValue value = wrap(d);
Value value = wrap(d);
assertValueType(Value.ValueTypeCase.TIMESTAMP_VALUE, value);
Object convertedValue = dateWriter.convertValue(value.getProto());
Object convertedValue = dateWriter.convertValue(value);
assertEquals(d, convertedValue);
}
}
Expand All @@ -173,7 +173,7 @@ public void testConvertsDateValue() {
public void testConvertsTimestampValue() {
List<Timestamp> testCases = asList(new Timestamp(0, 0), new Timestamp(1356048000L, 0));
for (Timestamp t : testCases) {
FieldValue value = wrap(t);
Value value = wrap(t);
assertValueType(Value.ValueTypeCase.TIMESTAMP_VALUE, value);
Object convertedValue = convertValue(value);
assertEquals(t, convertedValue);
Expand All @@ -184,7 +184,7 @@ public void testConvertsTimestampValue() {
public void testConvertsStringValue() {
List<String> testCases = asList("", "foo");
for (String s : testCases) {
FieldValue value = wrap(s);
Value value = wrap(s);
Object convertedValue = convertValue(value);
assertEquals(s, convertedValue);
}
Expand All @@ -194,7 +194,7 @@ public void testConvertsStringValue() {
public void testConvertsBlobValue() {
List<Blob> testCases = asList(blob(1, 2, 3), blob(1, 2));
for (Blob b : testCases) {
FieldValue value = wrap(b);
Value value = wrap(b);
Object convertedValue = convertValue(value);
assertEquals(b, convertedValue);
}
Expand All @@ -205,7 +205,7 @@ public void testConvertsResourceName() {
DatabaseId id = DatabaseId.forProject("project");
List<DocumentReference> testCases = asList(ref("foo/bar"), ref("foo/baz"));
for (DocumentReference docRef : testCases) {
Value value = valueOf(docRef);
Value value = wrap(docRef);
assertTrue(ProtoValues.isReferenceValue(value));
assertEquals(
TestAccessHelper.referenceKey(docRef), DocumentKey.fromName(value.getReferenceValue()));
Expand All @@ -217,7 +217,7 @@ public void testConvertsResourceName() {
public void testConvertsGeoPointValue() {
List<GeoPoint> testCases = asList(new GeoPoint(1.24, 4.56), new GeoPoint(-20, 100));
for (GeoPoint p : testCases) {
FieldValue value = wrap(p);
Value value = wrap(p);
Object convertedValue = convertValue(value);
assertEquals(p, convertedValue);
}
Expand Down Expand Up @@ -248,7 +248,7 @@ public void testConvertsSimpleObjects() {
private static ObjectValue fromMap(Object... entries) {
Map<String, Value> res = new HashMap<>();
for (int i = 0; i < entries.length; i += 2) {
res.put((String) entries[i], ((FieldValue) entries[i + 1]).getProto());
res.put((String) entries[i], (Value) entries[i + 1]);
}
return ObjectValue.fromMap(res);
}
Expand All @@ -257,19 +257,17 @@ private static ObjectValue fromMap(Object... entries) {
public void testConvertsNestedObjects() {
ObjectValue actual = wrapObject("a", map("b", map("c", "foo"), "d", true));
ObjectValue.Builder expected = ObjectValue.newBuilder();
expected.set(field("a.b.c"), valueOf("foo"));
expected.set(field("a.d"), valueOf(true));
expected.set(field("a.b.c"), wrap("foo"));
expected.set(field("a.d"), wrap(true));
assertEquals(expected.build(), actual);
}

@Test
public void testConvertsLists() {
ArrayValue.Builder expectedArray =
ArrayValue.newBuilder().addValues(valueOf("value")).addValues(valueOf(true));
FieldValue actual = wrap(asList("value", true));
assertTrue(
ProtoValues.equals(
Value.newBuilder().setArrayValue(expectedArray).build(), actual.getProto()));
ArrayValue.newBuilder().addValues(wrap("value")).addValues(wrap(true));
Value actual = wrap(asList("value", true));
assertTrue(ProtoValues.equals(Value.newBuilder().setArrayValue(expectedArray).build(), actual));
}

@Test
Expand All @@ -283,11 +281,11 @@ public void testRejectsJavaArrays() {
}
}

private Object convertValue(FieldValue value) {
return writer.convertValue(value.getProto());
private Object convertValue(Value value) {
return writer.convertValue(value);
}

private void assertValueType(Value.ValueTypeCase booleanValue, FieldValue value) {
assertEquals(booleanValue, value.getProto().getValueTypeCase());
private void assertValueType(Value.ValueTypeCase booleanValue, Value value) {
assertEquals(booleanValue, value.getValueTypeCase());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import static com.google.firebase.firestore.testutil.TestUtil.path;
import static com.google.firebase.firestore.testutil.TestUtil.ref;
import static com.google.firebase.firestore.testutil.TestUtil.testEquality;
import static com.google.firebase.firestore.testutil.TestUtil.valueOf;
import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
Expand All @@ -35,6 +34,7 @@
import com.google.firebase.firestore.model.Document;
import com.google.firebase.firestore.model.ResourcePath;
import com.google.firebase.firestore.testutil.ComparatorTester;
import com.google.firebase.firestore.testutil.TestUtil;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -603,13 +603,17 @@ public void testCanonicalIdsAreStable() {
baseQuery
.orderBy(orderBy("a"))
.startAt(
new Bound(Arrays.asList(valueOf("foo"), valueOf(Arrays.asList(1, 2, 3))), true)),
new Bound(
Arrays.asList(TestUtil.wrap("foo"), TestUtil.wrap(Arrays.asList(1, 2, 3))),
true)),
"collection|f:|ob:aasc__name__asc|lb:b:foo,[1,2,3]");
assertCanonicalId(
baseQuery
.orderBy(orderBy("a"))
.endAt(
new Bound(Arrays.asList(valueOf("foo"), valueOf(Arrays.asList(1, 2, 3))), false)),
new Bound(
Arrays.asList(TestUtil.wrap("foo"), TestUtil.wrap(Arrays.asList(1, 2, 3))),
false)),
"collection|f:|ob:aasc__name__asc|ub:a:foo,[1,2,3]");
assertCanonicalId(baseQuery.limitToFirst(5), "collection|f:|ob:__name__asc|l:5");
assertCanonicalId(baseQuery.limitToLast(5), "collection|f:|ob:__name__desc|l:5");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import static com.google.firebase.firestore.testutil.TestUtil.transformMutation;
import static com.google.firebase.firestore.testutil.TestUtil.unknownDoc;
import static com.google.firebase.firestore.testutil.TestUtil.updateRemoteEvent;
import static com.google.firebase.firestore.testutil.TestUtil.valueOf;
import static com.google.firebase.firestore.testutil.TestUtil.values;
import static com.google.firebase.firestore.testutil.TestUtil.version;
import static com.google.firebase.firestore.testutil.TestUtil.viewChanges;
Expand Down Expand Up @@ -154,7 +153,9 @@ private void acknowledgeMutation(long documentVersion, @Nullable Object transfor
MutationResult mutationResult =
new MutationResult(
version,
transformResult != null ? Collections.singletonList(valueOf(transformResult)) : null);
transformResult != null
? Collections.singletonList(TestUtil.wrap(transformResult))
: null);
MutationBatchResult result =
MutationBatchResult.create(
batch, version, singletonList(mutationResult), WriteStream.EMPTY_STREAM_TOKEN);
Expand Down
Loading