11'use strict' ;
22
3- describe ( 'test.regex.js' , function ( ) {
4- beforeEach ( function ( ) {
5- return context . db . bulkDocs ( [
3+ describe ( 'test.regex.js' , ( ) => {
4+ beforeEach ( async ( ) => {
5+ await context . db . bulkDocs ( [
66 { name : 'Mario' , _id : 'mario' , rank : 5 , series : 'Mario' , debut : 1981 , awesome : true } ,
77 { name : 'Jigglypuff' , _id : 'puff' , rank : 8 , series : 'Pokemon' , debut : 1996 ,
88 awesome : false } ,
@@ -22,191 +22,181 @@ describe('test.regex.js', function () {
2222 ] ) ;
2323 } ) ;
2424
25- it ( 'should do a basic regex search' , function ( ) {
26- var db = context . db ;
27- var index = {
25+ it ( 'should do a basic regex search' , async ( ) => {
26+ const db = context . db ;
27+ const index = {
2828 "index" : {
2929 "fields" : [ "name" ]
3030 }
3131 } ;
32- return db . createIndex ( index ) . then ( function ( ) {
33- return db . find ( {
34- selector : {
35- name : { $gte : null } ,
36- series : { $regex : "^Mario" }
37- } ,
38- sort : [ 'name' ]
39- } ) . then ( function ( resp ) {
40- var docs = resp . docs . map ( function ( doc ) {
41- delete doc . _rev ;
42- return doc ;
43- } ) ;
44-
45- docs . should . deep . equal ( [
46- { name : 'Donkey Kong' , rank : 7 , _id : 'dk' , series : 'Mario' ,
47- debut : 1981 , awesome : false } ,
48- { name : 'Luigi' , rank : 11 , _id : 'luigi' , series : 'Mario' , debut : 1983 , awesome : false } ,
49- { name : 'Mario' , _id : 'mario' , rank : 5 , series : 'Mario' , debut : 1981 , awesome : true } ,
50- { name : 'Yoshi' , _id : 'yoshi' , rank : 6 , series : 'Mario' , debut : 1990 , awesome : true } ,
51- ] ) ;
52- } ) ;
32+ await db . createIndex ( index ) ;
33+ const resp = await db . find ( {
34+ selector : {
35+ name : { $gte : null } ,
36+ series : { $regex : "^Mario" }
37+ } ,
38+ sort : [ 'name' ]
39+ } ) ;
40+ const docs = resp . docs . map ( ( doc ) => {
41+ delete doc . _rev ;
42+ return doc ;
5343 } ) ;
44+
45+ docs . should . deep . equal ( [
46+ { name : 'Donkey Kong' , rank : 7 , _id : 'dk' , series : 'Mario' ,
47+ debut : 1981 , awesome : false } ,
48+ { name : 'Luigi' , rank : 11 , _id : 'luigi' , series : 'Mario' , debut : 1983 , awesome : false } ,
49+ { name : 'Mario' , _id : 'mario' , rank : 5 , series : 'Mario' , debut : 1981 , awesome : true } ,
50+ { name : 'Yoshi' , _id : 'yoshi' , rank : 6 , series : 'Mario' , debut : 1990 , awesome : true } ,
51+ ] ) ;
5452 } ) ;
5553
56- it ( 'returns 0 docs for no match' , function ( ) {
57- var db = context . db ;
58- var index = {
54+ it ( 'returns 0 docs for no match' , async ( ) => {
55+ const db = context . db ;
56+ const index = {
5957 "index" : {
6058 "fields" : [ "name" ]
6159 }
6260 } ;
63- return db . createIndex ( index ) . then ( function ( ) {
64- return db . find ( {
65- selector : {
66- name : { $gte : null } ,
67- series : { $regex : "^Wrong" }
68- } ,
69- sort : [ 'name' ]
70- } ) . then ( function ( resp ) {
71- var docs = resp . docs . map ( function ( doc ) {
72- delete doc . _rev ;
73- return doc ;
74- } ) ;
75-
76- docs . should . deep . equal ( [ ] ) ;
77- } ) ;
61+ await db . createIndex ( index ) ;
62+ const resp = await db . find ( {
63+ selector : {
64+ name : { $gte : null } ,
65+ series : { $regex : "^Wrong" }
66+ } ,
67+ sort : [ 'name' ]
7868 } ) ;
69+ const docs = resp . docs . map ( ( doc ) => {
70+ delete doc . _rev ;
71+ return doc ;
72+ } ) ;
73+
74+ docs . should . deep . equal ( [ ] ) ;
7975 } ) ;
8076
81- it ( 'should ignore non-string field values' , function ( ) {
82- var db = context . db ;
83- return context . db . bulkDocs ( [
77+ it ( 'should ignore non-string field values' , async ( ) => {
78+ const db = context . db ;
79+ await context . db . bulkDocs ( [
8480 { _id : "number" , unknown : 10 } ,
8581 { _id : "number-as-string" , unknown : "10" } ,
86- ] ) . then ( function ( ) {
87- return db . find ( {
88- selector : {
89- unknown : { $regex : "10" }
90- } ,
91- } ) . then ( function ( resp ) {
92- var docs = resp . docs . map ( function ( doc ) {
93- delete doc . _rev ;
94- return doc ;
95- } ) ;
96-
97- docs . should . deep . equal ( [
98- { _id : "number-as-string" , unknown : "10" } ,
99- ] ) ;
100- } ) ;
82+ ] ) ;
83+ const resp = await db . find ( {
84+ selector : {
85+ unknown : { $regex : "10" }
86+ } ,
87+ } ) ;
88+ const docs = resp . docs . map ( ( doc ) => {
89+ delete doc . _rev ;
90+ return doc ;
10191 } ) ;
92+
93+ docs . should . deep . equal ( [
94+ { _id : "number-as-string" , unknown : "10" } ,
95+ ] ) ;
10296 } ) ;
103- it ( 'should error on non string or regex query values' , function ( ) {
104- var db = context . db ;
105- var index = {
97+
98+ it ( 'should error on non string or regex query values' , async ( ) => {
99+ const db = context . db ;
100+ const index = {
106101 "index" : {
107102 "fields" : [ "name" ]
108103 }
109104 } ;
110- return db . createIndex ( index ) . then ( function ( ) {
111- return db . find ( {
105+ await db . createIndex ( index ) ;
106+ try {
107+ await db . find ( {
112108 selector : {
113109 $and : [
114110 { name : { $regex : 1986 } } ,
115111 ]
116112 } ,
117113 sort : [ 'name' ]
118- } ) . then ( function ( ) {
119- throw new Error ( 'Function should throw' ) ;
120- } , function ( err ) {
121- if ( testUtils . adapterType ( ) === "http" ) {
122- err . message . should . eq ( 'Query operator $regex must be a string. Received number: 1986' ) ;
123- } else {
124- err . message . should . eq ( 'Query operator $regex must be a string or an instance of a javascript regular expression. Received number: 1986' ) ;
125- }
126114 } ) ;
127- } ) ;
115+ throw new Error ( 'Function should throw' ) ;
116+ } catch ( err ) {
117+ if ( testUtils . adapterType ( ) === "http" ) {
118+ err . message . should . eq ( 'Query operator $regex must be a string. Received number: 1986' ) ;
119+ } else {
120+ err . message . should . eq ( 'Query operator $regex must be a string or an instance of a javascript regular expression. Received number: 1986' ) ;
121+ }
122+ }
128123 } ) ;
129124
130- it ( 'should work with index on multiple fields' , function ( ) {
131- var db = context . db ;
132- var index = {
125+ it ( 'should work with index on multiple fields' , async ( ) => {
126+ const db = context . db ;
127+ const index = {
133128 "index" : {
134129 "fields" : [ "name" , "debut" ]
135130 }
136131 } ;
137- return db . createIndex ( index ) . then ( function ( ) {
138- return db . find ( {
139- selector : {
140- name : { $regex : "^Luig" } ,
141- debut : { $gt : 1980 }
142- } ,
143- sort : [ 'name' ]
144- } ) . then ( function ( resp ) {
145- var docs = resp . docs . map ( function ( doc ) {
146- delete doc . _rev ;
147- return doc ;
148- } ) ;
149-
150- docs . should . deep . equal ( [
151- { name : 'Luigi' , rank : 11 , _id : 'luigi' , series : 'Mario' , debut : 1983 , awesome : false } ,
152- ] ) ;
153- } ) ;
132+ await db . createIndex ( index ) ;
133+ const resp = await db . find ( {
134+ selector : {
135+ name : { $regex : "^Luig" } ,
136+ debut : { $gt : 1980 }
137+ } ,
138+ sort : [ 'name' ]
139+ } ) ;
140+ const docs = resp . docs . map ( ( doc ) => {
141+ delete doc . _rev ;
142+ return doc ;
154143 } ) ;
144+
145+ docs . should . deep . equal ( [
146+ { name : 'Luigi' , rank : 11 , _id : 'luigi' , series : 'Mario' , debut : 1983 , awesome : false } ,
147+ ] ) ;
155148 } ) ;
156149
157- it ( 'should works with $and with multiple $regex conditions on same field' , function ( ) {
158- var db = context . db ;
159- var index = {
150+ it ( 'should works with $and with multiple $regex conditions on same field' , async ( ) => {
151+ const db = context . db ;
152+ const index = {
160153 "index" : {
161154 "fields" : [ "name" ]
162155 }
163156 } ;
164- return db . createIndex ( index ) . then ( function ( ) {
165- return db . find ( {
166- selector : {
167- $and : [
168- { name : { $regex : "in" } } ,
169- { name : { $regex : "n" } } ,
170- ]
171- } ,
172- sort : [ 'name' ]
173- } ) . then ( function ( resp ) {
174- var docs = resp . docs . map ( function ( doc ) {
175- delete doc . _rev ;
176- return doc ;
177- } ) ;
178- docs . should . deep . equal ( [
179- {
180- name : 'Captain Falcon' , _id : 'falcon' , rank : 4 , series : 'F-Zero' , debut : 1990 ,
181- awesome : true
182- } ,
183- { name : 'Link' , rank : 10 , _id : 'link' , series : 'Zelda' , debut : 1986 , awesome : true } ,
184- ] ) ;
185- } ) ;
157+ await db . createIndex ( index ) ;
158+ const resp = await db . find ( {
159+ selector : {
160+ $and : [
161+ { name : { $regex : "in" } } ,
162+ { name : { $regex : "n" } } ,
163+ ]
164+ } ,
165+ sort : [ 'name' ]
166+ } ) ;
167+ const docs = resp . docs . map ( ( doc ) => {
168+ delete doc . _rev ;
169+ return doc ;
186170 } ) ;
171+ docs . should . deep . equal ( [
172+ {
173+ name : 'Captain Falcon' , _id : 'falcon' , rank : 4 , series : 'F-Zero' , debut : 1990 ,
174+ awesome : true
175+ } ,
176+ { name : 'Link' , rank : 10 , _id : 'link' , series : 'Zelda' , debut : 1986 , awesome : true } ,
177+ ] ) ;
187178 } ) ;
188179
189- it ( 'should find records within a simple $or condition' , function ( ) {
190- var db = context . db ;
191- return db . find ( {
180+ it ( 'should find records within a simple $or condition' , async ( ) => {
181+ const db = context . db ;
182+ const resp = await db . find ( {
192183 selector : {
193184 $or : [
194185 { name : { $regex : 'Captain Falcon' } } ,
195186 { name : { $regex : 'Link' } } ,
196187 ]
197188 }
198- } ) . then ( function ( resp ) {
199- var docs = resp . docs . map ( function ( doc ) {
200- delete doc . _rev ;
201- return doc ;
202- } ) ;
203- docs . should . deep . equal ( [
204- {
205- name : 'Captain Falcon' , _id : 'falcon' , rank : 4 , series : 'F-Zero' , debut : 1990 ,
206- awesome : true
207- } ,
208- { name : 'Link' , rank : 10 , _id : 'link' , series : 'Zelda' , debut : 1986 , awesome : true } ,
209- ] ) ;
210189 } ) ;
190+ const docs = resp . docs . map ( ( doc ) => {
191+ delete doc . _rev ;
192+ return doc ;
193+ } ) ;
194+ docs . should . deep . equal ( [
195+ {
196+ name : 'Captain Falcon' , _id : 'falcon' , rank : 4 , series : 'F-Zero' , debut : 1990 ,
197+ awesome : true
198+ } ,
199+ { name : 'Link' , rank : 10 , _id : 'link' , series : 'Zelda' , debut : 1986 , awesome : true } ,
200+ ] ) ;
211201 } ) ;
212202} ) ;
0 commit comments