-
Notifications
You must be signed in to change notification settings - Fork 126
/
ViewsTest.cs
1512 lines (1312 loc) · 57.4 KB
/
ViewsTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// ViewsTest.cs
//
// Author:
// Zachary Gramana <[email protected]>
//
// Copyright (c) 2013, 2014 Xamarin Inc (http://www.xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
/*
* Original iOS version by Jens Alfke
* Ported to Android by Marty Schoch, Traun Leyden
*
* Copyright (c) 2012, 2013, 2014 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. 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.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using Couchbase.Lite;
using Couchbase.Lite.Internal;
using Couchbase.Lite.Util;
using NUnit.Framework;
using Sharpen;
using Newtonsoft.Json.Linq;
using System.Threading;
using Couchbase.Lite.Views;
namespace Couchbase.Lite
{
public class ViewsTest : LiteTestCase
{
public const string Tag = "Views";
[Test]
public void TestViewValueIsEntireDoc()
{
var view = database.GetView("vu");
view.SetMap((doc, emit) => emit(doc["_id"], doc), "0.1");
CreateDocuments(database, 10);
var rows = view.CreateQuery().Run();
foreach (var row in rows) {
Assert.IsNotNull(row.Value);
var dict = row.Value.AsDictionary<string, object>();
Assert.IsNotNull(dict);
Assert.AreEqual(row.Key, dict["_id"]);
}
}
[Test]
public void TestLiveQueryUpdateWhenOptionsChanged()
{
var view = database.GetView("vu");
view.SetMap((doc, emit) =>
emit(doc.Get("sequence"), null), "1");
CreateDocuments(database, 5);
var query = view.CreateQuery();
var result = query.Run();
Assert.AreEqual(5, result.Count);
int expectedKey = 0;
foreach (var row in result) {
Assert.AreEqual(expectedKey++, row.Key);
}
var liveQuery = view.CreateQuery().ToLiveQuery();
var changeCount = 0;
liveQuery.Changed += (sender, e) => changeCount++;
liveQuery.Start();
Thread.Sleep(1000);
Assert.AreEqual(1, changeCount);
Assert.AreEqual(5, liveQuery.Rows.Count);
expectedKey = 0;
foreach (var row in liveQuery.Rows) {
Assert.AreEqual(expectedKey++, row.Key);
}
liveQuery.StartKey = 2;
liveQuery.QueryOptionsChanged();
Thread.Sleep(1000);
Assert.AreEqual(2, changeCount);
Assert.AreEqual(3, liveQuery.Rows.Count);
expectedKey = 2;
foreach (var row in liveQuery.Rows) {
Assert.AreEqual(expectedKey++, row.Key);
}
liveQuery.Stop();
}
[Test]
public void TestQueryDefaultIndexUpdateMode()
{
View view = database.GetView("aview");
Query query = view.CreateQuery();
Assert.AreEqual(IndexUpdateMode.Before, query.IndexUpdateMode);
}
[Test]
public void TestViewCreation()
{
Assert.IsNull(database.GetExistingView("aview"));
var view = database.GetView("aview");
Assert.IsNotNull(view);
Assert.AreEqual(database, view.Database);
Assert.AreEqual("aview", view.Name);
Assert.IsNull(view.Map);
Assert.AreEqual(view, database.GetExistingView("aview"));
//no-op
var changed = view.SetMapReduce((IDictionary<string, object> document, EmitDelegate emitter)=> { }, null, "1");
Assert.IsTrue(changed);
Assert.AreEqual(1, database.GetAllViews().Count);
Assert.AreEqual(view, database.GetAllViews()[0]);
//no-op
changed = view.SetMapReduce((IDictionary<string, object> document, EmitDelegate emitter)=> { }, null, "1");
Assert.IsFalse(changed);
changed = view.SetMapReduce((IDictionary<string, object> document, EmitDelegate emitter)=> { }, null, "2");
//no-op
Assert.IsTrue(changed);
}
/// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
private RevisionInternal PutDoc(Database db, IDictionary<string, object> props)
{
var rev = new RevisionInternal(props);
var status = new Status();
rev = db.PutRevision(rev, null, false, status);
Assert.IsTrue(status.IsSuccessful);
return rev;
}
/// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
private void PutDocViaUntitledDoc(Database db, IDictionary<string, object> props)
{
var document = db.CreateDocument();
document.PutProperties(props);
}
/// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
private IList<RevisionInternal> PutDocs(Database db)
{
var result = new List<RevisionInternal>();
var dict2 = new Dictionary<string, object>();
dict2["_id"] = "22222";
dict2["key"] = "two";
result.AddItem(PutDoc(db, dict2));
var dict4 = new Dictionary<string, object>();
dict4["_id"] = "44444";
dict4["key"] = "four";
result.AddItem(PutDoc(db, dict4));
var dict1 = new Dictionary<string, object>();
dict1["_id"] = "11111";
dict1["key"] = "one";
result.AddItem(PutDoc(db, dict1));
var dict3 = new Dictionary<string, object>();
dict3["_id"] = "33333";
dict3["key"] = "three";
result.AddItem(PutDoc(db, dict3));
var dict5 = new Dictionary<string, object>();
dict5["_id"] = "55555";
dict5["key"] = "five";
result.AddItem(PutDoc(db, dict5));
return result;
}
// http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views#Linked_documents
/// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
private IList<RevisionInternal> PutLinkedDocs(Database db)
{
var result = new List<RevisionInternal>();
var dict1 = new Dictionary<string, object>();
dict1["_id"] = "11111";
result.AddItem(PutDoc(db, dict1));
var dict2 = new Dictionary<string, object>();
dict2["_id"] = "22222";
dict2["value"] = "hello";
dict2["ancestors"] = new string[] { "11111" };
result.AddItem(PutDoc(db, dict2));
var dict3 = new Dictionary<string, object>();
dict3["_id"] = "33333";
dict3["value"] = "world";
dict3["ancestors"] = new string[] { "22222", "11111" };
result.AddItem(PutDoc(db, dict3));
return result;
}
/// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
public virtual void PutNDocs(Database db, int n)
{
for (int i = 0; i < n; i++)
{
var doc = new Dictionary<string, object>();
doc.Put("_id", string.Format("{0}", i));
var key = new List<string>();
for (int j = 0; j < 256; j++)
{
key.AddItem("key");
}
key.AddItem(string.Format("key-{0}", i));
doc["key"] = key;
PutDocViaUntitledDoc(db, doc);
}
}
public static View CreateView(Database db)
{
var view = db.GetView("aview");
view.SetMapReduce((IDictionary<string, object> document, EmitDelegate emitter)=>
{
Assert.IsNotNull(document["_id"]);
Assert.IsNotNull(document["_rev"]);
if (document["key"] != null)
{
emitter(document["key"], null);
}
}, null, "1");
return view;
}
/// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
[Test]
public void TestViewIndex()
{
int numTimesMapFunctionInvoked = 0;
var dict1 = new Dictionary<string, object>();
dict1["key"] = "one";
var dict2 = new Dictionary<string, object>();
dict2["key"] = "two";
var dict3 = new Dictionary<string, object>();
dict3["key"] = "three";
var dictX = new Dictionary<string, object>();
dictX["clef"] = "quatre";
var rev1 = PutDoc(database, dict1);
var rev2 = PutDoc(database, dict2);
var rev3 = PutDoc(database, dict3);
PutDoc(database, dictX);
var view = database.GetView("aview");
var numTimesInvoked = 0;
MapDelegate mapBlock = (document, emitter) =>
{
numTimesInvoked += 1;
Assert.IsNotNull(document["_id"]);
Assert.IsNotNull(document["_rev"]);
if (document.ContainsKey("key") && document["key"] != null)
{
emitter(document["key"], null);
}
};
view.SetMap(mapBlock, "1");
//Assert.AreEqual(1, view.Id);
Assert.IsTrue(view.IsStale);
view.UpdateIndex();
IList<IDictionary<string, object>> dumpResult = view.Storage.Dump().ToList();
Log.V(Tag, "View dump: " + dumpResult);
Assert.AreEqual(3, dumpResult.Count);
Assert.AreEqual("\"one\"", dumpResult[0]["key"]);
Assert.AreEqual(1, dumpResult[0]["seq"]);
Assert.AreEqual("\"two\"", dumpResult[2]["key"]);
Assert.AreEqual(2, dumpResult[2]["seq"]);
Assert.AreEqual("\"three\"", dumpResult[1]["key"]);
Assert.AreEqual(3, dumpResult[1]["seq"]);
//no-op reindex
Assert.IsFalse(view.IsStale);
view.UpdateIndex();
// Now add a doc and update a doc:
var threeUpdated = new RevisionInternal(rev3.GetDocId(), rev3.GetRevId(), false);
numTimesMapFunctionInvoked = numTimesInvoked;
var newdict3 = new Dictionary<string, object>();
newdict3["key"] = "3hree";
threeUpdated.SetProperties(newdict3);
Status status = new Status();
rev3 = database.PutRevision(threeUpdated, rev3.GetRevId(), false, status);
Assert.IsTrue(status.IsSuccessful);
// Reindex again:
Assert.IsTrue(view.IsStale);
view.UpdateIndex();
// Make sure the map function was only invoked one more time (for the document that was added)
Assert.AreEqual(numTimesMapFunctionInvoked + 1, numTimesInvoked);
var dict4 = new Dictionary<string, object>();
dict4["key"] = "four";
var rev4 = PutDoc(database, dict4);
var twoDeleted = new RevisionInternal(rev2.GetDocId(), rev2.GetRevId(), true);
database.PutRevision(twoDeleted, rev2.GetRevId(), false, status);
Assert.IsTrue(status.IsSuccessful);
// Reindex again:
Assert.IsTrue(view.IsStale);
view.UpdateIndex();
dumpResult = view.Storage.Dump().ToList();
Log.V(Tag, "View dump: " + dumpResult);
Assert.AreEqual(3, dumpResult.Count);
Assert.AreEqual("\"one\"", dumpResult[2]["key"]);
Assert.AreEqual(1, dumpResult[2]["seq"]);
Assert.AreEqual("\"3hree\"", dumpResult[0]["key"]);
Assert.AreEqual(5, dumpResult[0]["seq"]);
Assert.AreEqual("\"four\"", dumpResult[1]["key"]);
Assert.AreEqual(6, dumpResult[1]["seq"]);
// Now do a real query:
IList<QueryRow> rows = view.QueryWithOptions(null).ToList();
Assert.AreEqual(3, rows.Count);
Assert.AreEqual("one", rows[2].Key);
Assert.AreEqual(rev1.GetDocId(), rows[2].DocumentId);
Assert.AreEqual("3hree", rows[0].Key);
Assert.AreEqual(rev3.GetDocId(), rows[0].DocumentId);
Assert.AreEqual("four", rows[1].Key);
Assert.AreEqual(rev4.GetDocId(), rows[1].DocumentId);
view.DeleteIndex();
}
/// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
[Test]
public void TestViewQuery()
{
PutDocs(database);
var view = CreateView(database);
view.UpdateIndex();
// Query all rows:
QueryOptions options = new QueryOptions();
IList<QueryRow> rows = view.QueryWithOptions(options).ToList();
var expectedRows = new List<object>();
var dict5 = new Dictionary<string, object>();
dict5["id"] = "55555";
dict5["key"] = "five";
expectedRows.AddItem(dict5);
var dict4 = new Dictionary<string, object>();
dict4["id"] = "44444";
dict4["key"] = "four";
expectedRows.AddItem(dict4);
var dict1 = new Dictionary<string, object>();
dict1["id"] = "11111";
dict1["key"] = "one";
expectedRows.AddItem(dict1);
var dict3 = new Dictionary<string, object>();
dict3["id"] = "33333";
dict3["key"] = "three";
expectedRows.AddItem(dict3);
var dict2 = new Dictionary<string, object>();
dict2["id"] = "22222";
dict2["key"] = "two";
expectedRows.AddItem(dict2);
Assert.AreEqual(5, rows.Count);
Assert.AreEqual(dict5["key"], rows[0].Key);
Assert.AreEqual(dict4["key"], rows[1].Key);
Assert.AreEqual(dict1["key"], rows[2].Key);
Assert.AreEqual(dict3["key"], rows[3].Key);
Assert.AreEqual(dict2["key"], rows[4].Key);
// Start/end key query:
options = new QueryOptions();
options.StartKey = "a";
options.EndKey = "one";
rows = view.QueryWithOptions(options).ToList();
expectedRows = new List<object>();
expectedRows.AddItem(dict5);
expectedRows.AddItem(dict4);
expectedRows.AddItem(dict1);
Assert.AreEqual(3, rows.Count);
Assert.AreEqual(dict5["key"], rows[0].Key);
Assert.AreEqual(dict4["key"], rows[1].Key);
Assert.AreEqual(dict1["key"], rows[2].Key);
// Start/end query without inclusive end:
options.InclusiveEnd = false;
rows = view.QueryWithOptions(options).ToList();
expectedRows = new List<object>();
expectedRows.AddItem(dict5);
expectedRows.AddItem(dict4);
Assert.AreEqual(2, rows.Count);
Assert.AreEqual(dict5["key"], rows[0].Key);
Assert.AreEqual(dict4["key"], rows[1].Key);
// Reversed:
options.Descending = true;
options.StartKey = "o";
options.EndKey = "five";
options.InclusiveEnd = true;
rows = view.QueryWithOptions(options).ToList();
expectedRows = new List<object>();
expectedRows.AddItem(dict4);
expectedRows.AddItem(dict5);
Assert.AreEqual(2, rows.Count);
Assert.AreEqual(dict4["key"], rows[0].Key);
Assert.AreEqual(dict5["key"], rows[1].Key);
// Reversed, no inclusive end:
options.InclusiveEnd = false;
rows = view.QueryWithOptions(options).ToList();
expectedRows = new List<object>();
expectedRows.AddItem(dict4);
Assert.AreEqual(1, rows.Count);
Assert.AreEqual(dict4["key"], rows[0].Key);
// Specific keys: (note that rows should be in same order as input keys, not sorted)
options = new QueryOptions();
var keys = new List<object>();
keys.AddItem("two");
keys.AddItem("four");
options.Keys = keys;
rows = view.QueryWithOptions(options).ToList();
expectedRows = new List<object>();
expectedRows.AddItem(dict4);
expectedRows.AddItem(dict2);
Assert.AreEqual(2, rows.Count);
Assert.AreEqual(dict2["key"], rows[0].Key);
Assert.AreEqual(dict4["key"], rows[1].Key);
}
[Test]
public void TestLiveQueryStartEndKey()
{
var view = CreateView(database);
var query = view.CreateQuery();
query.StartKey = "one";
query.EndKey = "one\uFEFF";
var liveQuery = query.ToLiveQuery();
Assert.IsNotNull(liveQuery.StartKey);
Assert.IsNotNull(liveQuery.EndKey);
liveQuery.Start();
Thread.Sleep(2000);
Assert.AreEqual(0, liveQuery.Rows.Count);
PutDocs(database);
Thread.Sleep(2000);
Assert.AreEqual(1, liveQuery.Rows.Count);
}
/// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
[Test]
public void TestAllDocsQuery()
{
var docs = PutDocs(database);
var expectedRowBase = new List<IDictionary<string, object>>(docs.Count);
foreach (RevisionInternal rev in docs)
{
expectedRowBase.Add(new Dictionary<string, object> {
{ "id", rev.GetDocId() },
{ "key", rev.GetDocId() },
{ "value", new Dictionary<string, object> {
{ "rev", rev.GetRevId() }
}
}
});
}
// Create a conflict, won by the old revision:
var props = new Dictionary<string, object> {
{ "_id", "44444" },
{ "_rev", "1-00" }, // lower revID, will lose conflict
{ "key", "40ur" }
};
var leaf2 = new RevisionInternal(props);
database.ForceInsert(leaf2, null, null);
Assert.AreEqual(docs[1].GetRevId(), database.GetDocument("44444", null, true).GetRevId());
// Query all rows:
var options = new QueryOptions();
var allDocs = database.GetAllDocs(options);
var expectedRows = new List<IDictionary<string, object>> {
expectedRowBase[2],
expectedRowBase[0],
expectedRowBase[3],
expectedRowBase[1],
expectedRowBase[4]
};
Assert.AreEqual(expectedRows, RowsToDicts(allDocs));
// Limit:
options.Limit = 1;
allDocs = database.GetAllDocs(options);
expectedRows = new List<IDictionary<string, object>>() { expectedRowBase[2] };
Assert.AreEqual(expectedRows, RowsToDicts(allDocs));
// Limit+Skip:
options.Skip = 2;
allDocs = database.GetAllDocs(options);
expectedRows = new List<IDictionary<string, object>>() { expectedRowBase[3] };
Assert.AreEqual(expectedRows, RowsToDicts(allDocs));
// Start/end key query:
options = new QueryOptions();
options.StartKey = "2";
options.EndKey = "44444";
allDocs = database.GetAllDocs(options);
expectedRows = new List<IDictionary<string, object>>() { expectedRowBase[0], expectedRowBase[3], expectedRowBase[1] };
Assert.AreEqual(expectedRows, RowsToDicts(allDocs));
// Start/end query without inclusive end:
options.InclusiveEnd = false;
allDocs = database.GetAllDocs(options);
expectedRows = new List<IDictionary<string, object>>() { expectedRowBase[0], expectedRowBase[3] };
Assert.AreEqual(expectedRows, RowsToDicts(allDocs));
// Get zero specific documents:
options = new QueryOptions();
options.Keys = new List<object>();
allDocs = database.GetAllDocs(options);
Assert.IsNull(allDocs);
// Get specific documents:
options = new QueryOptions();
options.Keys = new List<object> {
expectedRowBase[2].GetCast<string>("id"),
expectedRowBase[3].GetCast<string>("id")
};
allDocs = database.GetAllDocs(options);
expectedRows = new List<IDictionary<string, object>>() { expectedRowBase[2], expectedRowBase[3] };
Assert.AreEqual(expectedRows, RowsToDicts(allDocs));
// Delete a document:
var del = docs[0];
del = new RevisionInternal(del.GetDocId(), del.GetRevId(), true);
var status = new Status();
del = database.PutRevision(del, del.GetRevId(), false, status);
Assert.AreEqual(StatusCode.Ok, status.Code);
// Get deleted doc, and one bogus one:
options = new QueryOptions();
options.Keys = new List<object> { "BOGUS", expectedRowBase[0].GetCast<string>("id") };
allDocs = database.GetAllDocs(options);
var expectedResult = new List<IDictionary<string, object>> {
new Dictionary<string, object> {
{ "key", "BOGUS" },
{ "error", "not_found" }
},
new Dictionary<string, object> {
{ "id", del.GetDocId() },
{ "key", del.GetDocId() },
{ "value", new Dictionary<string, object> {
{ "rev", del.GetRevId() },
{ "deleted", true }
}
}
}
};
Assert.AreEqual(expectedResult, RowsToDicts(allDocs));
// Get conflicts:
options = new QueryOptions();
options.AllDocsMode = AllDocsMode.ShowConflicts;
allDocs = database.GetAllDocs(options);
var curRevId = docs[1].GetRevId();
var expectedConflict1 = new Dictionary<string, object> {
{ "id", "44444" },
{ "key", "44444" },
{ "value", new Dictionary<string, object> {
{ "rev", curRevId },
{ "_conflicts", new List<string> {
curRevId, "1-00"
}
}
}
}
};
expectedRows = new List<IDictionary<string, object>>() { expectedRowBase[2], expectedRowBase[3], expectedConflict1,
expectedRowBase[4]
};
Assert.AreEqual(expectedRows, RowsToDicts(allDocs));
// Get _only_ conflicts:
options.AllDocsMode = AllDocsMode.OnlyConflicts;
allDocs = database.GetAllDocs(options);
expectedRows = new List<IDictionary<string, object>>() { expectedConflict1 };
Assert.AreEqual(expectedRows, RowsToDicts(allDocs));
}
private IDictionary<string, object> CreateExpectedQueryResult(IList<QueryRow> rows, int offset)
{
var result = new Dictionary<string, object>();
result["rows"] = rows;
result["total_rows"] = rows.Count;
result["offset"] = offset;
return result;
}
/// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
[Test]
public void TestViewReduce()
{
var docProperties1 = new Dictionary<string, object>();
docProperties1["_id"] = "CD";
docProperties1["cost"] = 8.99;
PutDoc(database, docProperties1);
var docProperties2 = new Dictionary<string, object>();
docProperties2["_id"] = "App";
docProperties2["cost"] = 1.95;
PutDoc(database, docProperties2);
IDictionary<string, object> docProperties3 = new Dictionary<string, object>();
docProperties3["_id"] = "Dessert";
docProperties3["cost"] = 6.50;
PutDoc(database, docProperties3);
View view = database.GetView("totaler");
view.SetMapReduce((document, emitter) => {
Assert.IsNotNull (document.Get ("_id"));
Assert.IsNotNull (document.Get ("_rev"));
object cost = document.Get ("cost");
if (cost != null) {
emitter (document.Get ("_id"), cost);
}
}, BuiltinReduceFunctions.Sum, "1");
view.UpdateIndex();
IList<IDictionary<string, object>> dumpResult = view.Storage.Dump().ToList();
Log.V(Tag, "View dump: " + dumpResult);
Assert.AreEqual(3, dumpResult.Count);
Assert.AreEqual("\"App\"", dumpResult[0]["key"]);
Assert.AreEqual("1.95", dumpResult[0]["val"]);
Assert.AreEqual(2, dumpResult[0]["seq"]);
Assert.AreEqual("\"CD\"", dumpResult[1]["key"]);
Assert.AreEqual("8.99", dumpResult[1]["val"]);
Assert.AreEqual(1, dumpResult[1]["seq"]);
Assert.AreEqual("\"Dessert\"", dumpResult[2]["key"]);
Assert.AreEqual("6.5", dumpResult[2]["val"]);
Assert.AreEqual(3, dumpResult[2]["seq"]);
QueryOptions options = new QueryOptions();
options.Reduce = true;
IList<QueryRow> reduced = view.QueryWithOptions(options).ToList();
Assert.AreEqual(1, reduced.Count);
object value = reduced[0].Value;
double numberValue = (double)value;
Assert.IsTrue(Math.Abs(numberValue - 17.44) < 0.001);
}
/// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
[Test]
public void TestIndexUpdateMode()
{
View view = CreateView(database);
Query query = view.CreateQuery();
query.IndexUpdateMode = IndexUpdateMode.Before;
int numRowsBefore = query.Run().Count;
Assert.AreEqual(0, numRowsBefore);
// do a query and force re-indexing, number of results should be +4
PutNDocs(database, 1);
query.IndexUpdateMode = IndexUpdateMode.Before;
Assert.AreEqual(1, query.Run().Count);
// do a query without re-indexing, number of results should be the same
PutNDocs(database, 4);
query.IndexUpdateMode = IndexUpdateMode.Never;
Assert.AreEqual(1, query.Run().Count);
// do a query and force re-indexing, number of results should be +4
query.IndexUpdateMode = IndexUpdateMode.Before;
Assert.AreEqual(5, query.Run().Count);
// do a query which will kick off an async index
PutNDocs(database, 1);
query.IndexUpdateMode = IndexUpdateMode.After;
query.Run();
// wait until indexing is (hopefully) done
try
{
Thread.Sleep(1 * 1000);
}
catch (Exception e)
{
Sharpen.Runtime.PrintStackTrace(e);
}
Assert.AreEqual(6, query.Run().Count);
}
/// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
[Test]
public void TestViewGrouped()
{
IDictionary<string, object> docProperties1 = new Dictionary<string, object>();
docProperties1["_id"] = "1";
docProperties1["artist"] = "Gang Of Four";
docProperties1["album"] = "Entertainment!";
docProperties1["track"] = "Ether";
docProperties1["time"] = 231;
PutDoc(database, docProperties1);
IDictionary<string, object> docProperties2 = new Dictionary<string, object>();
docProperties2["_id"] = "2";
docProperties2["artist"] = "Gang Of Four";
docProperties2["album"] = "Songs Of The Free";
docProperties2["track"] = "I Love A Man In Uniform";
docProperties2["time"] = 248;
PutDoc(database, docProperties2);
IDictionary<string, object> docProperties3 = new Dictionary<string, object>();
docProperties3["_id"] = "3";
docProperties3["artist"] = "Gang Of Four";
docProperties3["album"] = "Entertainment!";
docProperties3["track"] = "Natural's Not In It";
docProperties3["time"] = 187;
PutDoc(database, docProperties3);
IDictionary<string, object> docProperties4 = new Dictionary<string, object>();
docProperties4["_id"] = "4";
docProperties4["artist"] = "PiL";
docProperties4["album"] = "Metal Box";
docProperties4["track"] = "Memories";
docProperties4["time"] = 309;
PutDoc(database, docProperties4);
IDictionary<string, object> docProperties5 = new Dictionary<string, object>();
docProperties5["_id"] = "5";
docProperties5["artist"] = "Gang Of Four";
docProperties5["album"] = "Entertainment!";
docProperties5["track"] = "Not Great Men";
docProperties5["time"] = 187;
PutDoc(database, docProperties5);
View view = database.GetView("grouper");
view.SetMapReduce((document, emitter) =>
{
IList<object> key = new List<object>();
key.AddItem(document["artist"]);
key.AddItem(document["album"]);
key.AddItem(document["track"]);
emitter(key, document["time"]);
}, BuiltinReduceFunctions.Sum, "1");
view.UpdateIndex();
QueryOptions options = new QueryOptions();
options.Reduce = true;
IList<QueryRow> rows = view.QueryWithOptions(options).ToList();
IList<IDictionary<string, object>> expectedRows = new List<IDictionary<string, object>>();
IDictionary<string, object> row1 = new Dictionary<string, object>();
row1["key"] = null;
row1["value"] = 1162.0;
expectedRows.AddItem(row1);
Assert.AreEqual(row1["key"], rows[0].Key);
Assert.AreEqual(row1["value"], rows[0].Value);
//now group
options.Group = true;
rows = view.QueryWithOptions(options).ToList();
expectedRows = new List<IDictionary<string, object>>();
row1 = new Dictionary<string, object>();
IList<string> key1 = new List<string>();
key1.AddItem("Gang Of Four");
key1.AddItem("Entertainment!");
key1.AddItem("Ether");
row1["key"] = key1;
row1["value"] = 231.0;
expectedRows.AddItem(row1);
IDictionary<string, object> row2 = new Dictionary<string, object>();
IList<string> key2 = new List<string>();
key2.AddItem("Gang Of Four");
key2.AddItem("Entertainment!");
key2.AddItem("Natural's Not In It");
row2["key"] = key2;
row2["value"] = 187.0;
expectedRows.AddItem(row2);
IDictionary<string, object> row3 = new Dictionary<string, object>();
IList<string> key3 = new List<string>();
key3.AddItem("Gang Of Four");
key3.AddItem("Entertainment!");
key3.AddItem("Not Great Men");
row3["key"] = key3;
row3["value"] = 187.0;
expectedRows.AddItem(row3);
IDictionary<string, object> row4 = new Dictionary<string, object>();
IList<string> key4 = new List<string>();
key4.AddItem("Gang Of Four");
key4.AddItem("Songs Of The Free");
key4.AddItem("I Love A Man In Uniform");
row4["key"] = key4;
row4["value"] = 248.0;
expectedRows.AddItem(row4);
IDictionary<string, object> row5 = new Dictionary<string, object>();
IList<string> key5 = new List<string>();
key5.AddItem("PiL");
key5.AddItem("Metal Box");
key5.AddItem("Memories");
row5["key"] = key5;
row5["value"] = 309.0;
expectedRows.AddItem(row5);
Assert.AreEqual(row1["key"], rows[0].Key.AsList<string>());
Assert.AreEqual(row1["value"], rows[0].Value);
Assert.AreEqual(row2["key"], rows[1].Key.AsList<string>());
Assert.AreEqual(row2["value"], rows[1].Value);
Assert.AreEqual(row3["key"], rows[2].Key.AsList<string>());
Assert.AreEqual(row3["value"], rows[2].Value);
Assert.AreEqual(row4["key"], rows[3].Key.AsList<string>());
Assert.AreEqual(row4["value"], rows[3].Value);
Assert.AreEqual(row5["key"], rows[4].Key.AsList<string>());
Assert.AreEqual(row5["value"], rows[4].Value);
//group level 1
options.GroupLevel = 1;
rows = view.QueryWithOptions(options).ToList();
expectedRows = new List<IDictionary<string, object>>();
row1 = new Dictionary<string, object>();
key1 = new List<string>();
key1.AddItem("Gang Of Four");
row1["key"] = key1;
row1["value"] = 853.0;
expectedRows.AddItem(row1);
row2 = new Dictionary<string, object>();
key2 = new List<string>();
key2.AddItem("PiL");
row2["key"] = key2;
row2["value"] = 309.0;
expectedRows.AddItem(row2);
Assert.AreEqual(row1["key"], rows[0].Key.AsList<object>());
Assert.AreEqual(row1["value"], rows[0].Value);
Assert.AreEqual(row2["key"], rows[1].Key.AsList<object>());
Assert.AreEqual(row2["value"], rows[1].Value);
//group level 2
options.GroupLevel = 2;
rows = view.QueryWithOptions(options).ToList();
expectedRows = new List<IDictionary<string, object>>();
row1 = new Dictionary<string, object>();
key1 = new List<string>();
key1.AddItem("Gang Of Four");
key1.AddItem("Entertainment!");
row1["key"] = key1;
row1["value"] = 605.0;
expectedRows.AddItem(row1);
row2 = new Dictionary<string, object>();
key2 = new List<string>();
key2.AddItem("Gang Of Four");
key2.AddItem("Songs Of The Free");
row2["key"] = key2;
row2["value"] = 248.0;
expectedRows.AddItem(row2);
row3 = new Dictionary<string, object>();
key3 = new List<string>();
key3.AddItem("PiL");
key3.AddItem("Metal Box");
row3["key"] = key3;
row3["value"] = 309.0;
expectedRows.AddItem(row3);
Assert.AreEqual(row1["key"], rows[0].Key.AsList<object>());
Assert.AreEqual(row1["value"], rows[0].Value);
Assert.AreEqual(row2["key"], rows[1].Key.AsList<object>());
Assert.AreEqual(row2["value"], rows[1].Value);
Assert.AreEqual(row3["key"], rows[2].Key.AsList<object>());
Assert.AreEqual(row3["value"], rows[2].Value);
}
/// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
[Test]
public void TestViewGroupedStrings()
{
IDictionary<string, object> docProperties1 = new Dictionary<string, object>();
docProperties1["name"] = "Alice";
PutDoc(database, docProperties1);
IDictionary<string, object> docProperties2 = new Dictionary<string, object>();
docProperties2["name"] = "Albert";
PutDoc(database, docProperties2);
IDictionary<string, object> docProperties3 = new Dictionary<string, object>();
docProperties3["name"] = "Naomi";
PutDoc(database, docProperties3);
IDictionary<string, object> docProperties4 = new Dictionary<string, object>();
docProperties4["name"] = "Jens";
PutDoc(database, docProperties4);
IDictionary<string, object> docProperties5 = new Dictionary<string, object>();
docProperties5["name"] = "Jed";
PutDoc(database, docProperties5);
View view = database.GetView("default/names");
view.SetMapReduce((document, emitter) =>
{
string name = (string)document["name"];
if (name != null)
{
emitter(name.Substring(0, 1), 1);
}
}, BuiltinReduceFunctions.Sum, "1.0");
view.UpdateIndex();
QueryOptions options = new QueryOptions();
options.GroupLevel = 1;
IList<QueryRow> rows = view.QueryWithOptions(options).ToList();
IList<IDictionary<string, object>> expectedRows = new List<IDictionary<string, object>>();
IDictionary<string, object> row1 = new Dictionary<string, object>();
row1["key"] = "A";
row1["value"] = 2;
expectedRows.AddItem(row1);
IDictionary<string, object> row2 = new Dictionary<string, object>();
row2["key"] = "J";
row2["value"] = 2;
expectedRows.AddItem(row2);
IDictionary<string, object> row3 = new Dictionary<string, object>();
row3["key"] = "N";
row3["value"] = 1;
expectedRows.AddItem(row3);
Assert.AreEqual(row1["key"], rows[0].Key);
Assert.AreEqual(row1["value"], rows[0].Value);
Assert.AreEqual(row2["key"], rows[1].Key);
Assert.AreEqual(row2["value"], rows[1].Value);
Assert.AreEqual(row3["key"], rows[2].Key);
Assert.AreEqual(row3["value"], rows[2].Value);
}
/// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
[Test]
public void TestViewCollation()
{
IList<object> list1 = new List<object>();
list1.AddItem("a");
IList<object> list2 = new List<object>();
list2.AddItem("b");
IList<object> list3 = new List<object>();
list3.AddItem("b");