2017-10-03 08:55:22 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright 2017 Google
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-11-16 20:52:01 -08:00
|
|
|
#import "Firestore/Source/Local/FSTLocalSerializer.h"
|
2017-10-03 08:55:22 -07:00
|
|
|
|
2018-02-20 17:33:57 -05:00
|
|
|
#import <FirebaseFirestore/FIRTimestamp.h>
|
2017-10-03 08:55:22 -07:00
|
|
|
#import <XCTest/XCTest.h>
|
|
|
|
|
|
2019-02-07 22:03:17 -05:00
|
|
|
#include <utility>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2017-11-16 20:52:01 -08:00
|
|
|
#import "Firestore/Protos/objc/firestore/local/MaybeDocument.pbobjc.h"
|
|
|
|
|
#import "Firestore/Protos/objc/firestore/local/Mutation.pbobjc.h"
|
|
|
|
|
#import "Firestore/Protos/objc/firestore/local/Target.pbobjc.h"
|
2018-12-21 11:28:48 -08:00
|
|
|
#import "Firestore/Protos/objc/google/firestore/v1/Common.pbobjc.h"
|
|
|
|
|
#import "Firestore/Protos/objc/google/firestore/v1/Document.pbobjc.h"
|
|
|
|
|
#import "Firestore/Protos/objc/google/firestore/v1/Firestore.pbobjc.h"
|
|
|
|
|
#import "Firestore/Protos/objc/google/firestore/v1/Query.pbobjc.h"
|
|
|
|
|
#import "Firestore/Protos/objc/google/firestore/v1/Write.pbobjc.h"
|
2017-11-16 20:52:01 -08:00
|
|
|
#import "Firestore/Protos/objc/google/type/Latlng.pbobjc.h"
|
|
|
|
|
#import "Firestore/Source/Core/FSTQuery.h"
|
|
|
|
|
#import "Firestore/Source/Local/FSTQueryData.h"
|
|
|
|
|
#import "Firestore/Source/Model/FSTDocument.h"
|
|
|
|
|
#import "Firestore/Source/Model/FSTFieldValue.h"
|
|
|
|
|
#import "Firestore/Source/Model/FSTMutation.h"
|
|
|
|
|
#import "Firestore/Source/Model/FSTMutationBatch.h"
|
|
|
|
|
#import "Firestore/Source/Remote/FSTSerializerBeta.h"
|
2017-10-03 08:55:22 -07:00
|
|
|
|
2017-11-16 20:52:01 -08:00
|
|
|
#import "Firestore/Example/Tests/Util/FSTHelpers.h"
|
2017-10-03 08:55:22 -07:00
|
|
|
|
2018-02-06 13:51:11 -05:00
|
|
|
#include "Firestore/core/src/firebase/firestore/model/database_id.h"
|
2018-04-04 12:40:46 -04:00
|
|
|
#include "Firestore/core/src/firebase/firestore/model/field_mask.h"
|
2018-04-12 21:32:19 -04:00
|
|
|
#include "Firestore/core/src/firebase/firestore/model/precondition.h"
|
2018-02-06 13:51:11 -05:00
|
|
|
#include "Firestore/core/src/firebase/firestore/util/string_apple.h"
|
2018-03-14 08:57:31 -04:00
|
|
|
#include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
|
2018-02-06 13:51:11 -05:00
|
|
|
|
2018-03-14 08:57:31 -04:00
|
|
|
namespace testutil = firebase::firestore::testutil;
|
2018-02-06 13:51:11 -05:00
|
|
|
using firebase::firestore::model::DatabaseId;
|
2018-04-04 12:40:46 -04:00
|
|
|
using firebase::firestore::model::FieldMask;
|
2018-04-12 21:32:19 -04:00
|
|
|
using firebase::firestore::model::Precondition;
|
2018-05-01 13:37:53 -04:00
|
|
|
using firebase::firestore::model::SnapshotVersion;
|
2018-08-15 15:56:45 -07:00
|
|
|
using firebase::firestore::model::TargetId;
|
2018-02-06 13:51:11 -05:00
|
|
|
|
2017-10-03 08:55:22 -07:00
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
|
|
|
|
|
@interface FSTSerializerBeta (Test)
|
|
|
|
|
- (GCFSValue *)encodedNull;
|
|
|
|
|
- (GCFSValue *)encodedBool:(BOOL)value;
|
|
|
|
|
- (GCFSValue *)encodedDouble:(double)value;
|
|
|
|
|
- (GCFSValue *)encodedInteger:(int64_t)value;
|
|
|
|
|
- (GCFSValue *)encodedString:(NSString *)value;
|
|
|
|
|
@end
|
|
|
|
|
|
2019-05-21 20:28:51 -07:00
|
|
|
@interface FSTLocalSerializerTests : XCTestCase
|
2017-10-03 08:55:22 -07:00
|
|
|
|
|
|
|
|
@property(nonatomic, strong) FSTLocalSerializer *serializer;
|
|
|
|
|
@property(nonatomic, strong) FSTSerializerBeta *remoteSerializer;
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation FSTLocalSerializerTests
|
|
|
|
|
|
|
|
|
|
- (void)setUp {
|
2019-05-21 20:28:51 -07:00
|
|
|
self.remoteSerializer = [[FSTSerializerBeta alloc] initWithDatabaseID:DatabaseId("p", "d")];
|
2017-10-03 08:55:22 -07:00
|
|
|
self.serializer = [[FSTLocalSerializer alloc] initWithRemoteSerializer:self.remoteSerializer];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)testEncodesMutationBatch {
|
2019-03-06 14:51:30 -08:00
|
|
|
FSTMutation *base = [[FSTPatchMutation alloc] initWithKey:FSTTestDocKey(@"bar/baz")
|
|
|
|
|
fieldMask:FieldMask{testutil::Field("a")}
|
|
|
|
|
value:FSTTestObjectValue(@{@"a" : @"b"})
|
|
|
|
|
precondition:Precondition::Exists(true)];
|
2018-08-13 14:18:02 -07:00
|
|
|
FSTMutation *set = FSTTestSetMutation(@"foo/bar", @{@"a" : @"b", @"num" : @1});
|
2019-01-02 09:27:31 -08:00
|
|
|
FSTMutation *patch =
|
|
|
|
|
[[FSTPatchMutation alloc] initWithKey:FSTTestDocKey(@"bar/baz")
|
|
|
|
|
fieldMask:FieldMask{testutil::Field("a")}
|
|
|
|
|
value:FSTTestObjectValue(@{@"a" : @"b", @"num" : @1})
|
|
|
|
|
precondition:Precondition::Exists(true)];
|
2017-10-03 08:55:22 -07:00
|
|
|
FSTMutation *del = FSTTestDeleteMutation(@"baz/quux");
|
2018-02-20 17:33:57 -05:00
|
|
|
FIRTimestamp *writeTime = [FIRTimestamp timestamp];
|
2017-10-03 08:55:22 -07:00
|
|
|
FSTMutationBatch *model = [[FSTMutationBatch alloc] initWithBatchID:42
|
|
|
|
|
localWriteTime:writeTime
|
2019-03-06 14:51:30 -08:00
|
|
|
baseMutations:{base}
|
2019-02-07 22:03:17 -05:00
|
|
|
mutations:{set, patch, del}];
|
2017-10-03 08:55:22 -07:00
|
|
|
|
2019-03-06 14:51:30 -08:00
|
|
|
GCFSWrite *baseProto = [GCFSWrite message];
|
|
|
|
|
baseProto.update.name = @"projects/p/databases/d/documents/bar/baz";
|
|
|
|
|
[baseProto.update.fields addEntriesFromDictionary:@{
|
|
|
|
|
@"a" : [self.remoteSerializer encodedString:@"b"],
|
|
|
|
|
}];
|
|
|
|
|
[baseProto.updateMask.fieldPathsArray addObjectsFromArray:@[ @"a" ]];
|
|
|
|
|
baseProto.currentDocument.exists = YES;
|
|
|
|
|
|
2017-10-03 08:55:22 -07:00
|
|
|
GCFSWrite *setProto = [GCFSWrite message];
|
|
|
|
|
setProto.update.name = @"projects/p/databases/d/documents/foo/bar";
|
|
|
|
|
[setProto.update.fields addEntriesFromDictionary:@{
|
|
|
|
|
@"a" : [self.remoteSerializer encodedString:@"b"],
|
|
|
|
|
@"num" : [self.remoteSerializer encodedInteger:1]
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
GCFSWrite *patchProto = [GCFSWrite message];
|
|
|
|
|
patchProto.update.name = @"projects/p/databases/d/documents/bar/baz";
|
|
|
|
|
[patchProto.update.fields addEntriesFromDictionary:@{
|
|
|
|
|
@"a" : [self.remoteSerializer encodedString:@"b"],
|
|
|
|
|
@"num" : [self.remoteSerializer encodedInteger:1]
|
|
|
|
|
}];
|
|
|
|
|
[patchProto.updateMask.fieldPathsArray addObjectsFromArray:@[ @"a" ]];
|
|
|
|
|
patchProto.currentDocument.exists = YES;
|
|
|
|
|
|
|
|
|
|
GCFSWrite *delProto = [GCFSWrite message];
|
|
|
|
|
delProto.delete_p = @"projects/p/databases/d/documents/baz/quux";
|
|
|
|
|
|
|
|
|
|
GPBTimestamp *writeTimeProto = [GPBTimestamp message];
|
|
|
|
|
writeTimeProto.seconds = writeTime.seconds;
|
2018-02-20 17:33:57 -05:00
|
|
|
writeTimeProto.nanos = writeTime.nanoseconds;
|
2017-10-03 08:55:22 -07:00
|
|
|
|
|
|
|
|
FSTPBWriteBatch *batchProto = [FSTPBWriteBatch message];
|
|
|
|
|
batchProto.batchId = 42;
|
2019-03-06 14:51:30 -08:00
|
|
|
[batchProto.baseWritesArray addObject:baseProto];
|
2017-10-03 08:55:22 -07:00
|
|
|
[batchProto.writesArray addObjectsFromArray:@[ setProto, patchProto, delProto ]];
|
|
|
|
|
batchProto.localWriteTime = writeTimeProto;
|
|
|
|
|
|
|
|
|
|
XCTAssertEqualObjects([self.serializer encodedMutationBatch:model], batchProto);
|
|
|
|
|
FSTMutationBatch *decoded = [self.serializer decodedMutationBatch:batchProto];
|
|
|
|
|
XCTAssertEqual(decoded.batchID, model.batchID);
|
|
|
|
|
XCTAssertEqualObjects(decoded.localWriteTime, model.localWriteTime);
|
2019-03-06 14:51:30 -08:00
|
|
|
FSTAssertEqualVectors(decoded.baseMutations, model.baseMutations);
|
2019-02-07 22:03:17 -05:00
|
|
|
FSTAssertEqualVectors(decoded.mutations, model.mutations);
|
2018-05-05 08:10:51 -07:00
|
|
|
XCTAssertEqual([decoded keys], [model keys]);
|
2017-10-03 08:55:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)testEncodesDocumentAsMaybeDocument {
|
2018-11-01 09:44:30 -07:00
|
|
|
FSTDocument *doc = FSTTestDoc("some/path", 42, @{@"foo" : @"bar"}, FSTDocumentStateSynced);
|
2017-10-03 08:55:22 -07:00
|
|
|
|
|
|
|
|
FSTPBMaybeDocument *maybeDocProto = [FSTPBMaybeDocument message];
|
|
|
|
|
maybeDocProto.document = [GCFSDocument message];
|
|
|
|
|
maybeDocProto.document.name = @"projects/p/databases/d/documents/some/path";
|
|
|
|
|
[maybeDocProto.document.fields addEntriesFromDictionary:@{
|
|
|
|
|
@"foo" : [self.remoteSerializer encodedString:@"bar"],
|
|
|
|
|
}];
|
|
|
|
|
maybeDocProto.document.updateTime.seconds = 0;
|
|
|
|
|
maybeDocProto.document.updateTime.nanos = 42000;
|
|
|
|
|
|
|
|
|
|
XCTAssertEqualObjects([self.serializer encodedMaybeDocument:doc], maybeDocProto);
|
|
|
|
|
FSTMaybeDocument *decoded = [self.serializer decodedMaybeDocument:maybeDocProto];
|
|
|
|
|
XCTAssertEqualObjects(decoded, doc);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-01 09:44:30 -07:00
|
|
|
- (void)testEncodesUnknownDocumentAsMaybeDocument {
|
|
|
|
|
FSTUnknownDocument *doc = FSTTestUnknownDoc("some/path", 42);
|
|
|
|
|
|
|
|
|
|
FSTPBMaybeDocument *maybeDocProto = [FSTPBMaybeDocument message];
|
|
|
|
|
maybeDocProto.unknownDocument = [FSTPBUnknownDocument message];
|
|
|
|
|
maybeDocProto.unknownDocument.name = @"projects/p/databases/d/documents/some/path";
|
|
|
|
|
maybeDocProto.unknownDocument.version.seconds = 0;
|
|
|
|
|
maybeDocProto.unknownDocument.version.nanos = 42000;
|
|
|
|
|
maybeDocProto.hasCommittedMutations = true;
|
|
|
|
|
|
|
|
|
|
XCTAssertEqualObjects([self.serializer encodedMaybeDocument:doc], maybeDocProto);
|
|
|
|
|
FSTMaybeDocument *decoded = [self.serializer decodedMaybeDocument:maybeDocProto];
|
|
|
|
|
XCTAssertEqualObjects(decoded, doc);
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-03 08:55:22 -07:00
|
|
|
- (void)testEncodesDeletedDocumentAsMaybeDocument {
|
2018-11-01 09:44:30 -07:00
|
|
|
FSTDeletedDocument *deletedDoc = FSTTestDeletedDoc("some/path", 42, false);
|
2017-10-03 08:55:22 -07:00
|
|
|
|
|
|
|
|
FSTPBMaybeDocument *maybeDocProto = [FSTPBMaybeDocument message];
|
|
|
|
|
maybeDocProto.noDocument = [FSTPBNoDocument message];
|
|
|
|
|
maybeDocProto.noDocument.name = @"projects/p/databases/d/documents/some/path";
|
|
|
|
|
maybeDocProto.noDocument.readTime.seconds = 0;
|
|
|
|
|
maybeDocProto.noDocument.readTime.nanos = 42000;
|
|
|
|
|
|
|
|
|
|
XCTAssertEqualObjects([self.serializer encodedMaybeDocument:deletedDoc], maybeDocProto);
|
|
|
|
|
FSTMaybeDocument *decoded = [self.serializer decodedMaybeDocument:maybeDocProto];
|
|
|
|
|
XCTAssertEqualObjects(decoded, deletedDoc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)testEncodesQueryData {
|
2018-03-14 08:57:31 -04:00
|
|
|
FSTQuery *query = FSTTestQuery("room");
|
2018-08-15 15:56:45 -07:00
|
|
|
TargetId targetID = 42;
|
2018-05-01 13:37:53 -04:00
|
|
|
SnapshotVersion version = testutil::Version(1039);
|
2017-10-03 08:55:22 -07:00
|
|
|
NSData *resumeToken = FSTTestResumeTokenFromSnapshotVersion(1039);
|
|
|
|
|
|
|
|
|
|
FSTQueryData *queryData = [[FSTQueryData alloc] initWithQuery:query
|
|
|
|
|
targetID:targetID
|
2018-01-17 12:23:45 -08:00
|
|
|
listenSequenceNumber:10
|
2017-10-03 08:55:22 -07:00
|
|
|
purpose:FSTQueryPurposeListen
|
|
|
|
|
snapshotVersion:version
|
|
|
|
|
resumeToken:resumeToken];
|
|
|
|
|
|
|
|
|
|
// Let the RPC serializer test various permutations of query serialization.
|
|
|
|
|
GCFSTarget_QueryTarget *queryTarget = [self.remoteSerializer encodedQueryTarget:query];
|
|
|
|
|
|
|
|
|
|
FSTPBTarget *expected = [FSTPBTarget message];
|
|
|
|
|
expected.targetId = targetID;
|
2018-01-17 12:23:45 -08:00
|
|
|
expected.lastListenSequenceNumber = 10;
|
2017-10-03 08:55:22 -07:00
|
|
|
expected.snapshotVersion.nanos = 1039000;
|
|
|
|
|
expected.resumeToken = [resumeToken copy];
|
|
|
|
|
expected.query.parent = queryTarget.parent;
|
|
|
|
|
expected.query.structuredQuery = queryTarget.structuredQuery;
|
|
|
|
|
|
|
|
|
|
XCTAssertEqualObjects([self.serializer encodedQueryData:queryData], expected);
|
|
|
|
|
FSTQueryData *decoded = [self.serializer decodedQueryData:expected];
|
|
|
|
|
XCTAssertEqualObjects(decoded, queryData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|