@@ -50,7 +50,94 @@ module.exports = function() {
50
50
} ) ;
51
51
} ) ;
52
52
53
- describe ( 'top-level boolean operator' , function ( done ) {
53
+ describe ( 'filtering on special Share properties' , function ( ) {
54
+ // When sharedb-mongo persists a snapshot into Mongo, any properties
55
+ // underneath `data` get "promoted" to top-level, and Share properties
56
+ // get underscore-prefixed to avoid name conflicts, like `v` to `_v`.
57
+ //
58
+ // Query conditions don't undergo this transformation, so if you wanted
59
+ // to filter on snapshot version, you'd query with `{_v: 12}`. These tests
60
+ // check that sharedb-mingo-memory is consistent with sharedb-mongo for
61
+ // queries like those that filter on non-data Share properties.
62
+ var snapshots = [
63
+ { type : 'json0' , v : 1 , data : { x : 1 , y : 1 } , id : "test1" , m : { mtime : 1000 } } ,
64
+ { type : 'json0' , v : 1 , data : { x : 1 , y : 2 } , id : "test2" , m : { mtime : 1001 } } ,
65
+ { type : 'json0' , v : 1 , data : { x : 2 , y : 2 } , id : "test3" , m : { mtime : 1002 } }
66
+ ] ;
67
+ var snapshotsNoMeta = snapshots . map ( function ( snapshot ) {
68
+ var snapshotCopy = JSON . parse ( JSON . stringify ( snapshot ) ) ;
69
+ delete snapshotCopy . m ;
70
+ return snapshotCopy ;
71
+ } ) ;
72
+
73
+ beforeEach ( function ( done ) {
74
+ var db = this . db ;
75
+ async . each ( snapshots , function ( snapshot , cb ) {
76
+ db . commit ( 'testcollection' , snapshot . id , { v : 0 , create : { } } , snapshot , null , cb ) ;
77
+ } , done ) ;
78
+ } ) ;
79
+
80
+ it ( 'condition on Mongo _id (Share id)' , function ( done ) {
81
+ this . db . query ( 'testcollection' , { _id : 'test1' } , null , null , function ( err , results , extra ) {
82
+ if ( err ) throw err ;
83
+ expect ( results ) . eql ( [ snapshotsNoMeta [ 0 ] ] ) ;
84
+ done ( ) ;
85
+ } ) ;
86
+ } ) ;
87
+
88
+ // The simpler query-casting approach doesn't handle queries that filter on
89
+ // sub-properties of the Share metadata object. An alternative would be to
90
+ // rewrite this module to cast Share snapshots to Mongo docs and vice versa,
91
+ // the same way that sharedb-mongo does:
92
+ // https://github.com/share/sharedb-mingo-memory/pull/3#pullrequestreview-99017385
93
+ it . skip ( 'condition on sub-property under Share metadata' , function ( done ) {
94
+ this . db . query ( 'testcollection' , { '_m.mtime' : 1001 } , null , null , function ( err , results , extra ) {
95
+ if ( err ) throw err ;
96
+ expect ( results ) . eql ( [ snapshotsNoMeta [ 1 ] ] ) ;
97
+ done ( ) ;
98
+ } ) ;
99
+ } ) ;
100
+
101
+ it ( 'condition on Mongo _id and Share data' , function ( done ) {
102
+ this . db . query ( 'testcollection' , { y : 2 , _id : { $nin : [ 'test2' ] } } , null , null , function ( err , results , extra ) {
103
+ if ( err ) throw err ;
104
+ expect ( results ) . eql ( [ snapshotsNoMeta [ 2 ] ] ) ;
105
+ done ( ) ;
106
+ } ) ;
107
+ } ) ;
108
+
109
+ it ( 'top-level boolean operator' , function ( done ) {
110
+ this . db . query ( 'testcollection' , { $or : [ { y : 1 } , { _id : 'test2' } ] } , null , null , function ( err , results , extra ) {
111
+ if ( err ) throw err ;
112
+ expect ( results ) . eql ( [ snapshotsNoMeta [ 0 ] , snapshotsNoMeta [ 1 ] ] ) ;
113
+ done ( ) ;
114
+ } ) ;
115
+ } ) ;
116
+ } ) ;
117
+
118
+ it ( 'filters with null condition' , function ( done ) {
119
+ var snapshots = [
120
+ { type : 'json0' , v : 1 , data : { x : 1 , y : 1 } , id : "test1" } ,
121
+ { type : 'json0' , v : 1 , data : { x : 1 } , id : "test2" } , // y value intentionally omitted
122
+ { type : 'json0' , v : 1 , data : { x : 2 , y : 2 } , id : "test3" }
123
+ ] ;
124
+ var query = { y : null } ;
125
+
126
+ var db = this . db ;
127
+ async . each ( snapshots , function ( snapshot , cb ) {
128
+ db . commit ( 'testcollection' , snapshot . id , { v : 0 , create : { } } , snapshot , null , cb ) ;
129
+ } , function ( err ) {
130
+ if ( err ) return done ( err ) ;
131
+
132
+ db . query ( 'testcollection' , query , null , null , function ( err , results , extra ) {
133
+ if ( err ) throw err ;
134
+ expect ( results ) . eql ( [ snapshots [ 1 ] ] ) ;
135
+ done ( ) ;
136
+ } ) ;
137
+ } ) ;
138
+ } ) ;
139
+
140
+ describe ( 'top-level boolean operator' , function ( ) {
54
141
var snapshots = [
55
142
{ type : 'json0' , v : 1 , data : { x : 1 , y : 1 } , id : "test1" } ,
56
143
{ type : 'json0' , v : 1 , data : { x : 1 , y : 2 } , id : "test2" } ,
0 commit comments