Skip to content
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

CBL-6349: Implement Document's getRevisionHistory() for E2E Test Server #3335

Merged
merged 6 commits into from
Oct 24, 2024
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
3 changes: 3 additions & 0 deletions Objective-C/CBLDocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ NS_ASSUME_NONNULL_BEGIN
/** Return document data as JSON String. */
- (NSString*) toJSON;

/** <Unsupported API> Internally used for testing purpose. */
- (nullable NSString*) _getRevisionHistory;

@end

NS_ASSUME_NONNULL_END
15 changes: 15 additions & 0 deletions Objective-C/CBLDocument.mm
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#import "CBLFleece.hh"
#import "MRoot.hh"
#import "CBLErrorMessage.h"
#import <limits.h>

using namespace fleece;

Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 3 additions & 4 deletions Objective-C/CBLMutableDocument.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -111,7 +111,6 @@ - (instancetype) initWithID: (nullable NSString*)documentID
_dict = [dict mutableCopy];
}
return self;

}

#pragma mark - Edit
Expand Down
5 changes: 4 additions & 1 deletion Objective-C/Internal/CBLC4Document.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;

Expand Down
6 changes: 5 additions & 1 deletion Objective-C/Internal/CBLC4Document.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -49,6 +49,10 @@ - (C4SequenceNumber) sequence {
return _rawDoc->selectedRev.sequence;
}

- (C4String) docID {
return _rawDoc->docID;
}

- (C4String) revID {
return _rawDoc->selectedRev.revID;
}
Expand Down
34 changes: 34 additions & 0 deletions Objective-C/Tests/DocumentTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions Swift/Document.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

/// <Unsupported API> Internally used for testing purpose.
public func _getRevisionHistory() -> String? {
return impl._getRevisionHistory()
}

// MARK: Edit

/// Returns a mutable copy of the document.
Expand Down
27 changes: 27 additions & 0 deletions Swift/Tests/DocumentTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Loading