use of com.google.firebase.firestore.model.mutation.SetMutation in project firebase-android-sdk by firebase.
the class MutationQueueTestCase method testAllMutationBatchesAffectingDocumentKey.
@Test
public void testAllMutationBatchesAffectingDocumentKey() {
List<Mutation> mutations = asList(setMutation("fob/bar", map("a", 1)), setMutation("foo/bar", map("a", 1)), patchMutation("foo/bar", map("b", 1)), setMutation("foo/bar/suffix/key", map("a", 1)), setMutation("foo/baz", map("a", 1)), setMutation("food/bar", map("a", 1)));
// Store all the mutations.
List<MutationBatch> batches = new ArrayList<>();
persistence.runTransaction("New mutation batch", () -> {
for (Mutation mutation : mutations) {
batches.add(mutationQueue.addMutationBatch(Timestamp.now(), Collections.emptyList(), asList(mutation)));
}
});
List<MutationBatch> expected = asList(batches.get(1), batches.get(2));
List<MutationBatch> matches = mutationQueue.getAllMutationBatchesAffectingDocumentKey(key("foo/bar"));
assertEquals(expected, matches);
}
use of com.google.firebase.firestore.model.mutation.SetMutation in project firebase-android-sdk by firebase.
the class MutationQueueTestCase method testAllMutationBatchesAffectingDocumentLotsOfDocumentKeys.
// PORTING NOTE: this test only applies to Android, because it's the only platform where the
// implementation of getAllMutationBatchesAffectingDocumentKeys might split the input into several
// queries.
@Test
public void testAllMutationBatchesAffectingDocumentLotsOfDocumentKeys() {
List<Mutation> mutations = new ArrayList<>();
// Make sure to force SQLite implementation to split the large query into several smaller ones.
int lotsOfMutations = 2000;
for (int i = 0; i < lotsOfMutations; i++) {
mutations.add(setMutation("foo/" + i, map("a", 1)));
}
List<MutationBatch> batches = new ArrayList<>();
persistence.runTransaction("New mutation batch", () -> {
for (Mutation mutation : mutations) {
batches.add(mutationQueue.addMutationBatch(Timestamp.now(), Collections.emptyList(), asList(mutation)));
}
});
// To make it easier validating the large resulting set, use a simple criteria to evaluate --
// query all keys with an even number in them and make sure the corresponding batches make it
// into the results.
ImmutableSortedSet<DocumentKey> evenKeys = DocumentKey.emptyKeySet();
List<MutationBatch> expected = new ArrayList<>();
for (int i = 2; i < lotsOfMutations; i += 2) {
evenKeys = evenKeys.insert(key("foo/" + i));
expected.add(batches.get(i));
}
List<MutationBatch> matches = mutationQueue.getAllMutationBatchesAffectingDocumentKeys(evenKeys);
assertThat(matches).containsExactlyElementsIn(expected).inOrder();
}
use of com.google.firebase.firestore.model.mutation.SetMutation in project firebase-android-sdk by firebase.
the class LocalSerializerTest method testEncodesMutationBatch.
@Test
public void testEncodesMutationBatch() {
Mutation baseWrite = new PatchMutation(key("foo/bar"), TestUtil.wrapObject(map("a", "b")), FieldMask.fromSet(Collections.singleton(field("a"))), com.google.firebase.firestore.model.mutation.Precondition.NONE);
Mutation set = setMutation("foo/bar", map("a", "b", "num", 1));
Mutation patch = new PatchMutation(key("bar/baz"), TestUtil.wrapObject(map("a", "b", "num", 1)), fieldMask("a"), com.google.firebase.firestore.model.mutation.Precondition.exists(true));
Mutation del = deleteMutation("baz/quux");
MutationBatch model = new MutationBatch(42, writeTime, Collections.singletonList(baseWrite), asList(set, patch, del));
Write baseWriteProto = Write.newBuilder().setUpdate(com.google.firestore.v1.Document.newBuilder().setName("projects/p/databases/d/documents/foo/bar").putFields("a", Value.newBuilder().setStringValue("b").build())).setUpdateMask(DocumentMask.newBuilder().addFieldPaths("a")).build();
com.google.firebase.firestore.proto.WriteBatch batchProto = com.google.firebase.firestore.proto.WriteBatch.newBuilder().setBatchId(42).addBaseWrites(baseWriteProto).addAllWrites(asList(setProto, patchProto, deleteProto)).setLocalWriteTime(writeTimeProto).build();
assertEquals(batchProto, serializer.encodeMutationBatch(model));
MutationBatch decoded = serializer.decodeMutationBatch(batchProto);
assertEquals(model.getBatchId(), decoded.getBatchId());
assertEquals(model.getLocalWriteTime(), decoded.getLocalWriteTime());
assertEquals(model.getMutations(), decoded.getMutations());
assertEquals(model.getBaseMutations(), decoded.getBaseMutations());
assertEquals(model.getKeys(), decoded.getKeys());
}
use of com.google.firebase.firestore.model.mutation.SetMutation in project firebase-android-sdk by firebase.
the class LocalSerializerTest method testSetMutationAndTransFormMutationAreSquashed.
// TODO(b/174608374): Remove these tests once we perform a schema migration.
@Test
public void testSetMutationAndTransFormMutationAreSquashed() {
WriteBatch batchProto = com.google.firebase.firestore.proto.WriteBatch.newBuilder().setBatchId(42).addAllWrites(asList(setProto, transformProto)).setLocalWriteTime(writeTimeProto).build();
MutationBatch decoded = serializer.decodeMutationBatch(batchProto);
assertEquals(1, decoded.getMutations().size());
assertTrue(decoded.getMutations().get(0) instanceof SetMutation);
Write encoded = remoteSerializer.encodeMutation(decoded.getMutations().get(0));
Write expected = new TestWriteBuilder().addSet().addUpdateTransforms().build();
assertEquals(expected, encoded);
}
use of com.google.firebase.firestore.model.mutation.SetMutation in project firebase-android-sdk by firebase.
the class TestUtil method setMutation.
public static SetMutation setMutation(String path, Map<String, Object> values) {
UserDataReader dataReader = new UserDataReader(DatabaseId.forProject("project"));
ParsedSetData parsed = dataReader.parseSetData(values);
// The order of the transforms doesn't matter, but we sort them so tests can assume a particular
// order.
ArrayList<FieldTransform> fieldTransforms = new ArrayList<>(parsed.getFieldTransforms());
Collections.sort(fieldTransforms, (ft1, ft2) -> ft1.getFieldPath().compareTo(ft2.getFieldPath()));
return new SetMutation(key(path), parsed.getData(), Precondition.NONE, fieldTransforms);
}
Aggregations