From 9334e457d0ef203e7253c8d0ca3403de0bfaebba Mon Sep 17 00:00:00 2001 From: Vlad Velicu Date: Thu, 24 Oct 2024 12:25:40 +0100 Subject: [PATCH] CBL-6349: Implement Document's getRevisionHistory() for E2E Test Server (#3335) * get c4doc using kDocGetAll content lvl for this * added docID to CBL c4doc * LiteCore 3.2.1-12 --- Objective-C/CBLDocument.h | 3 +++ Objective-C/CBLDocument.mm | 15 ++++++++++++ Objective-C/CBLMutableDocument.mm | 7 +++--- Objective-C/Internal/CBLC4Document.h | 5 +++- Objective-C/Internal/CBLC4Document.mm | 6 ++++- Objective-C/Tests/DocumentTest.m | 34 +++++++++++++++++++++++++++ Swift/Document.swift | 7 ++++++ Swift/Tests/DocumentTest.swift | 27 +++++++++++++++++++++ vendor/couchbase-lite-core | 2 +- 9 files changed, 99 insertions(+), 7 deletions(-) diff --git a/Objective-C/CBLDocument.h b/Objective-C/CBLDocument.h index d1fb3e5a7..48208a184 100644 --- a/Objective-C/CBLDocument.h +++ b/Objective-C/CBLDocument.h @@ -58,6 +58,9 @@ NS_ASSUME_NONNULL_BEGIN /** Return document data as JSON String. */ - (NSString*) toJSON; +/** Internally used for testing purpose. */ +- (nullable NSString*) _getRevisionHistory; + @end NS_ASSUME_NONNULL_END diff --git a/Objective-C/CBLDocument.mm b/Objective-C/CBLDocument.mm index 105b70073..fa6e7c01c 100644 --- a/Objective-C/CBLDocument.mm +++ b/Objective-C/CBLDocument.mm @@ -29,6 +29,7 @@ #import "CBLFleece.hh" #import "MRoot.hh" #import "CBLErrorMessage.h" +#import using namespace fleece; @@ -199,6 +200,20 @@ - (void) replaceC4Doc: (CBLC4Document*)c4doc { } } +- (nullable NSString*) _getRevisionHistory { + if (!_collection) { + return nil; + } + + CBL_LOCK(self) { + C4Error err; + C4Document* doc = c4coll_getDoc(_collection.c4col, _c4Doc.docID, true, kDocGetAll, &err); + NSString* revHistory = doc ? sliceResult2string(c4doc_getRevisionHistory(doc, UINT_MAX, nil, 0)) : nil; + c4doc_release(doc); + return revHistory; + } +} + #pragma mark - Fleece Encoding - (FLSliceResult) encodeWithRevFlags: (C4RevisionFlags*)outRevFlags error:(NSError**)outError { diff --git a/Objective-C/CBLMutableDocument.mm b/Objective-C/CBLMutableDocument.mm index ae569e033..402d643ca 100644 --- a/Objective-C/CBLMutableDocument.mm +++ b/Objective-C/CBLMutableDocument.mm @@ -100,9 +100,9 @@ - (instancetype) initWithID: (nullable NSString*)documentID return self; } -/* internal */ - (instancetype) initAsCopyWithDocument: (CBLDocument*)doc - dict: (nullable CBLDictionary*)dict -{ +#pragma mark - Internal +- (instancetype) initAsCopyWithDocument: (CBLDocument*)doc + dict: (nullable CBLDictionary*)dict { self = [self initWithCollection: doc.collection documentID: doc.id c4Doc: doc.c4Doc]; @@ -111,7 +111,6 @@ - (instancetype) initWithID: (nullable NSString*)documentID _dict = [dict mutableCopy]; } return self; - } #pragma mark - Edit diff --git a/Objective-C/Internal/CBLC4Document.h b/Objective-C/Internal/CBLC4Document.h index 574f9c827..7608a869e 100644 --- a/Objective-C/Internal/CBLC4Document.h +++ b/Objective-C/Internal/CBLC4Document.h @@ -2,7 +2,7 @@ // CBLC4Document.h // CouchbaseLite // -// Copyright (c) 2017 Couchbase, Inc All rights reserved. +// Copyright (c) 2024 Couchbase, Inc All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -32,6 +32,9 @@ NS_ASSUME_NONNULL_BEGIN /** Sequence of the selected revision. */ @property (readonly, nonatomic) C4SequenceNumber sequence; +/** Document ID of the selected revision. */ +@property (readonly, nonatomic) C4String docID; + /** Revision ID of the selected revision. */ @property (readonly, nonatomic) C4String revID; diff --git a/Objective-C/Internal/CBLC4Document.mm b/Objective-C/Internal/CBLC4Document.mm index 477e9af3d..3d505c498 100644 --- a/Objective-C/Internal/CBLC4Document.mm +++ b/Objective-C/Internal/CBLC4Document.mm @@ -2,7 +2,7 @@ // CBLC4Document.mm // CouchbaseLite // -// Copyright (c) 2017 Couchbase, Inc All rights reserved. +// Copyright (c) 2024 Couchbase, Inc All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -49,6 +49,10 @@ - (C4SequenceNumber) sequence { return _rawDoc->selectedRev.sequence; } +- (C4String) docID { + return _rawDoc->docID; +} + - (C4String) revID { return _rawDoc->selectedRev.revID; } diff --git a/Objective-C/Tests/DocumentTest.m b/Objective-C/Tests/DocumentTest.m index 2d92a4bf2..956c9e1ac 100644 --- a/Objective-C/Tests/DocumentTest.m +++ b/Objective-C/Tests/DocumentTest.m @@ -2262,6 +2262,40 @@ - (void) testDocumentResaveInAnotherCollection { }]; } +#pragma mark - Revision history + +/** https://github.com/couchbaselabs/couchbase-lite-api/blob/master/spec/tests/T0005-Version-Vector.md */ + +/** + 2. TestDocumentRevisionHistory + Description + Test that the document's timestamp returns value as expected. + Steps + 1. Create a new document with id = "doc1" + 2. Get document's _revisionIDs and check that the value returned is an empty array. + 3. Save the document into the default collection. + 4. Get document's _revisionIDs and check that the value returned is an array containing a + single revision id which is the revision id of the documnt. + 5. Get the document id = "doc1" from the database. + 6. Get document's _revisionIDs and check that the value returned is an array containing a + single revision id which is the revision id of the documnt. + */ +- (void) testDocumentRevisionHistory { + NSError* err; + CBLCollection* defaultCollection = [self.db defaultCollection: &err]; + AssertNil(err); + + CBLMutableDocument* doc = [[CBLMutableDocument alloc] initWithID: @"doc1"]; + Assert(doc); + AssertNil(doc._getRevisionHistory); + + Assert([defaultCollection saveDocument:doc error: &err]); + Assert(doc._getRevisionHistory); + + doc = [[defaultCollection documentWithID: @"doc1" error: &err] toMutable]; + Assert(doc._getRevisionHistory); +} + #pragma clang diagnostic pop @end diff --git a/Swift/Document.swift b/Swift/Document.swift index 649c14dc6..48fa38295 100644 --- a/Swift/Document.swift +++ b/Swift/Document.swift @@ -45,6 +45,13 @@ public class Document : DictionaryProtocol, Equatable, Hashable, Sequence { /// The collection that the document belongs to. internal(set) public var collection: Collection? + // MARK: Unsupported - Internal use for testing + + /// Internally used for testing purpose. + public func _getRevisionHistory() -> String? { + return impl._getRevisionHistory() + } + // MARK: Edit /// Returns a mutable copy of the document. diff --git a/Swift/Tests/DocumentTest.swift b/Swift/Tests/DocumentTest.swift index d0a757a88..a461b8100 100644 --- a/Swift/Tests/DocumentTest.swift +++ b/Swift/Tests/DocumentTest.swift @@ -1855,4 +1855,31 @@ class DocumentTest: CBLTestCase { } } } + + // MARK: toJSONTimestamp & Revision history + + // https://github.com/couchbaselabs/couchbase-lite-api/blob/master/spec/tests/T0005-Version-Vector.md + + // 2. TestDocumentRevisionHistory + // Description + // Test that the document's timestamp returns value as expected. + // Steps + // 1. Create a new document with id = "doc1" + // 2. Get document's _revisionIDs and check that the value returned is an empty array. + // 3. Save the document into the default collection. + // 4. Get document's _revisionIDs and check that the value returned is an array containing a + // single revision id which is the revision id of the documnt. + // 5. Get the document id = "doc1" from the database. + // 6. Get document's _revisionIDs and check that the value returned is an array containing a + // single revision id which is the revision id of the documnt. + func testDocumentRevisionHistory() throws { + let doc = MutableDocument(id: "doc1") + assert(doc._getRevisionHistory() == nil) + + try defaultCollection!.save(document: doc) + assert(doc._getRevisionHistory() != nil) + + let remoteDoc = try defaultCollection!.document(id: "doc1")!.toMutable(); + assert(doc._getRevisionHistory() != nil) + } } diff --git a/vendor/couchbase-lite-core b/vendor/couchbase-lite-core index da8b267c0..fb6e666c1 160000 --- a/vendor/couchbase-lite-core +++ b/vendor/couchbase-lite-core @@ -1 +1 @@ -Subproject commit da8b267c0ef7ae36a814dc3f5af984f112dc2151 +Subproject commit fb6e666c1adebd873872540c7e0ffe0cc01320a3