Skip to content

Commit 5255342

Browse files
committed
Scope & Collection: add new api & fix unit tests
1 parent b2c1960 commit 5255342

File tree

3 files changed

+105
-34
lines changed

3 files changed

+105
-34
lines changed

src/collection.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ use crate::{
22
CblRef, Listener, ListenerToken, release, retain,
33
c_api::{
44
CBLCollection, CBLCollectionChange, CBLCollection_AddChangeListener, CBLCollection_Scope,
5-
CBLCollection_Name, CBLCollection_Count,
5+
CBLCollection_Name, CBLCollection_Count, CBLCollection_FullName, CBLCollection_Database,
66
},
77
scope::Scope,
8+
Database,
89
};
910

1011
pub static DEFAULT_NAME: &str = "_default";
@@ -35,6 +36,20 @@ impl Collection {
3536
}
3637
}
3738

39+
/** Returns the collection full name */
40+
pub fn full_name(&self) -> String {
41+
unsafe {
42+
CBLCollection_FullName(self.get_ref())
43+
.to_string()
44+
.unwrap_or_default()
45+
}
46+
}
47+
48+
/** Returns the collection's database */
49+
pub fn database(&self) -> Database {
50+
unsafe { Database::wrap(CBLCollection_Database(self.get_ref())) }
51+
}
52+
3853
/** Returns the number of documents in the collection. */
3954
pub fn count(&self) -> u64 {
4055
unsafe { CBLCollection_Count(self.get_ref()) }

src/scope.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
use crate::{
22
CblRef, check_error, release, retain,
3-
c_api::{CBLScope, CBLScope_Name, CBLScope_CollectionNames, CBLScope_Collection, CBLError},
3+
c_api::{
4+
CBLScope, CBLScope_Name, CBLScope_CollectionNames, CBLScope_Collection, CBLError,
5+
CBLScope_Database,
6+
},
47
collection::Collection,
58
error::Result,
69
MutableArray,
710
slice::from_str,
11+
Database,
812
};
913

1014
#[derive(Debug, PartialEq, Eq)]
@@ -28,6 +32,11 @@ impl Scope {
2832
}
2933
}
3034

35+
/** Returns the scope's database */
36+
pub fn database(&self) -> Database {
37+
unsafe { Database::wrap(CBLScope_Database(self.get_ref())) }
38+
}
39+
3140
/** Returns the names of all collections in the scope. */
3241
pub fn collection_names(&self) -> Result<Vec<String>> {
3342
let mut error = CBLError::default();

tests/collection_tests.rs

Lines changed: 79 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ fn create_delete_scopes_collections() {
2929
assert!(db.scope(DEFAULT_NAME.to_string()).unwrap().is_some());
3030
assert!(db.scope(unknown.clone()).unwrap().is_none());
3131
assert_eq!(
32-
db.default_scope().unwrap(),
33-
db.scope(DEFAULT_NAME.to_string()).unwrap().unwrap()
32+
db.default_scope().unwrap().name(),
33+
db.scope(DEFAULT_NAME.to_string()).unwrap().unwrap().name()
3434
);
3535

3636
// Get collection
@@ -46,10 +46,11 @@ fn create_delete_scopes_collections() {
4646
.collection(DEFAULT_NAME.to_string(), unknown.clone())
4747
.is_err()); // Invalid scope => Err
4848
assert_eq!(
49-
db.default_collection().unwrap().unwrap(),
49+
db.default_collection().unwrap().unwrap().full_name(),
5050
db.collection(DEFAULT_NAME.to_string(), DEFAULT_NAME.to_string())
5151
.unwrap()
5252
.unwrap()
53+
.full_name()
5354
);
5455

5556
// Add collection in default scope
@@ -61,13 +62,15 @@ fn create_delete_scopes_collections() {
6162
assert_eq!(
6263
db.collection(new_collection_1.clone(), DEFAULT_NAME.to_string())
6364
.unwrap()
64-
.unwrap(),
65-
c1_default_scope
65+
.unwrap()
66+
.full_name(),
67+
c1_default_scope.full_name()
6668
);
6769
assert_eq!(
6870
db.create_collection(new_collection_1.clone(), DEFAULT_NAME.to_string())
69-
.unwrap(),
70-
c1_default_scope
71+
.unwrap()
72+
.full_name(),
73+
c1_default_scope.full_name()
7174
);
7275

7376
assert_eq!(
@@ -85,13 +88,15 @@ fn create_delete_scopes_collections() {
8588
assert_eq!(
8689
db.collection(new_collection_2.clone(), new_scope.clone())
8790
.unwrap()
88-
.unwrap(),
89-
c2_new_scope
91+
.unwrap()
92+
.full_name(),
93+
c2_new_scope.full_name()
9094
);
9195
assert_eq!(
9296
db.create_collection(new_collection_2.clone(), new_scope.clone())
93-
.unwrap(),
94-
c2_new_scope
97+
.unwrap()
98+
.full_name(),
99+
c2_new_scope.full_name()
95100
);
96101

97102
assert_eq!(
@@ -158,8 +163,9 @@ fn collections_accessors() {
158163
assert_eq!(
159164
db.collection(new_collection_1.clone(), DEFAULT_NAME.to_string())
160165
.unwrap()
161-
.unwrap(),
162-
c1_default_scope
166+
.unwrap()
167+
.full_name(),
168+
c1_default_scope.full_name()
163169
);
164170

165171
let c2_new_scope = db
@@ -168,8 +174,9 @@ fn collections_accessors() {
168174
assert_eq!(
169175
db.collection(new_collection_2.clone(), new_scope.clone())
170176
.unwrap()
171-
.unwrap(),
172-
c2_new_scope
177+
.unwrap()
178+
.full_name(),
179+
c2_new_scope.full_name()
173180
);
174181

175182
let c1_new_scope = db
@@ -178,23 +185,43 @@ fn collections_accessors() {
178185
assert_eq!(
179186
db.collection(new_collection_1.clone(), new_scope.clone())
180187
.unwrap()
181-
.unwrap(),
182-
c1_new_scope
188+
.unwrap()
189+
.full_name(),
190+
c1_new_scope.full_name()
183191
);
184192

185193
let default_scope = db.scope(DEFAULT_NAME.to_string()).unwrap().unwrap();
186194
let new_actual_scope = db.scope(new_scope.clone()).unwrap().unwrap();
187195

188196
// Scope
189-
assert_eq!(c1_default_scope.scope(), default_scope);
190-
assert_eq!(c2_new_scope.scope(), new_actual_scope);
191-
assert_eq!(c1_new_scope.scope(), new_actual_scope);
197+
assert_eq!(c1_default_scope.scope().name(), default_scope.name());
198+
assert_eq!(c2_new_scope.scope().name(), new_actual_scope.name());
199+
assert_eq!(c1_new_scope.scope().name(), new_actual_scope.name());
192200

193201
// Name
194202
assert_eq!(c1_default_scope.name(), new_collection_1.clone());
195203
assert_eq!(c2_new_scope.name(), new_collection_2.clone());
196204
assert_eq!(c1_new_scope.name(), new_collection_1.clone());
197205

206+
// Full name
207+
assert_eq!(
208+
c1_default_scope.full_name(),
209+
format!("{}.{}", DEFAULT_NAME, new_collection_1)
210+
);
211+
assert_eq!(
212+
c2_new_scope.full_name(),
213+
format!("{}.{}", new_scope, new_collection_2)
214+
);
215+
assert_eq!(
216+
c1_new_scope.full_name(),
217+
format!("{}.{}", new_scope, new_collection_1)
218+
);
219+
220+
// Database
221+
assert_eq!(c1_default_scope.database().get_ref(), db.get_ref());
222+
assert_eq!(c2_new_scope.database().get_ref(), db.get_ref());
223+
assert_eq!(c1_new_scope.database().get_ref(), db.get_ref());
224+
198225
// Count
199226
assert_eq!(c1_default_scope.count(), 0);
200227
assert_eq!(c2_new_scope.count(), 0);
@@ -216,8 +243,9 @@ fn scope_accessors() {
216243
assert_eq!(
217244
db.collection(new_collection_1.clone(), DEFAULT_NAME.to_string())
218245
.unwrap()
219-
.unwrap(),
220-
c1_default_scope
246+
.unwrap()
247+
.full_name(),
248+
c1_default_scope.full_name()
221249
);
222250

223251
let c2_new_scope = db
@@ -226,8 +254,9 @@ fn scope_accessors() {
226254
assert_eq!(
227255
db.collection(new_collection_2.clone(), new_scope.clone())
228256
.unwrap()
229-
.unwrap(),
230-
c2_new_scope
257+
.unwrap()
258+
.full_name(),
259+
c2_new_scope.full_name()
231260
);
232261

233262
let c1_new_scope = db
@@ -236,8 +265,9 @@ fn scope_accessors() {
236265
assert_eq!(
237266
db.collection(new_collection_1.clone(), new_scope.clone())
238267
.unwrap()
239-
.unwrap(),
240-
c1_new_scope
268+
.unwrap()
269+
.full_name(),
270+
c1_new_scope.full_name()
241271
);
242272

243273
let default_scope = db.scope(DEFAULT_NAME.to_string()).unwrap().unwrap();
@@ -247,6 +277,20 @@ fn scope_accessors() {
247277
assert_eq!(default_scope.name(), DEFAULT_NAME.to_string());
248278
assert_eq!(new_actual_scope.name(), new_scope.clone());
249279

280+
// Database
281+
assert_eq!(
282+
db.get_ref(),
283+
db.default_scope().unwrap().database().get_ref()
284+
);
285+
assert_eq!(
286+
db.get_ref(),
287+
db.scope(new_scope.clone())
288+
.unwrap()
289+
.unwrap()
290+
.database()
291+
.get_ref()
292+
);
293+
250294
// Collections
251295
assert_eq!(
252296
default_scope.collection_names().unwrap(),
@@ -266,22 +310,25 @@ fn scope_accessors() {
266310
default_scope
267311
.collection(new_collection_1.clone())
268312
.unwrap()
269-
.unwrap(),
270-
c1_default_scope
313+
.unwrap()
314+
.full_name(),
315+
c1_default_scope.full_name()
271316
);
272317
assert_eq!(
273318
new_actual_scope
274319
.collection(new_collection_2.clone())
275320
.unwrap()
276-
.unwrap(),
277-
c2_new_scope
321+
.unwrap()
322+
.full_name(),
323+
c2_new_scope.full_name()
278324
);
279325
assert_eq!(
280326
new_actual_scope
281327
.collection(new_collection_1.clone())
282328
.unwrap()
283-
.unwrap(),
284-
c1_new_scope
329+
.unwrap()
330+
.full_name(),
331+
c1_new_scope.full_name()
285332
);
286333
});
287334
}

0 commit comments

Comments
 (0)