@@ -8,7 +8,6 @@ const start = require('./common');
8
8
9
9
const Query = require ( '../lib/query' ) ;
10
10
const assert = require ( 'assert' ) ;
11
- const co = require ( 'co' ) ;
12
11
const random = require ( '../lib/utils' ) . random ;
13
12
const util = require ( './util' ) ;
14
13
@@ -944,31 +943,29 @@ describe('model: querying:', function() {
944
943
} ) ;
945
944
} ) ;
946
945
947
- it ( 'where $exists' , function ( ) {
946
+ it ( 'where $exists' , async function ( ) {
948
947
const ExistsSchema = new Schema ( { a : Number , b : String } ) ;
949
948
const Exists = db . model ( 'Test' , ExistsSchema ) ;
950
949
951
- return co ( function * ( ) {
952
- yield Exists . create ( { a : 1 } , { b : 'hi' } ) ;
953
- let docs = yield Exists . find ( { b : { $exists : true } } ) ;
954
- assert . equal ( docs . length , 1 ) ;
955
- assert . equal ( docs [ 0 ] . b , 'hi' ) ;
956
-
957
- docs = yield Exists . find ( { b : { $exists : 'true' } } ) ;
958
- assert . equal ( docs . length , 1 ) ;
959
- assert . equal ( docs [ 0 ] . b , 'hi' ) ;
960
-
961
- let threw = false ;
962
- try {
963
- yield Exists . find ( { b : { $exists : 'foo' } } ) ;
964
- } catch ( error ) {
965
- threw = true ;
966
- assert . equal ( error . path , 'b' ) ;
967
- assert . equal ( error . value , 'foo' ) ;
968
- assert . equal ( error . name , 'CastError' ) ;
969
- }
970
- assert . ok ( threw ) ;
971
- } ) ;
950
+ await Exists . create ( { a : 1 } , { b : 'hi' } ) ;
951
+ let docs = await Exists . find ( { b : { $exists : true } } ) ;
952
+ assert . equal ( docs . length , 1 ) ;
953
+ assert . equal ( docs [ 0 ] . b , 'hi' ) ;
954
+
955
+ docs = await Exists . find ( { b : { $exists : 'true' } } ) ;
956
+ assert . equal ( docs . length , 1 ) ;
957
+ assert . equal ( docs [ 0 ] . b , 'hi' ) ;
958
+
959
+ let threw = false ;
960
+ try {
961
+ await Exists . find ( { b : { $exists : 'foo' } } ) ;
962
+ } catch ( error ) {
963
+ threw = true ;
964
+ assert . equal ( error . path , 'b' ) ;
965
+ assert . equal ( error . value , 'foo' ) ;
966
+ assert . equal ( error . name , 'CastError' ) ;
967
+ }
968
+ assert . ok ( threw ) ;
972
969
} ) ;
973
970
974
971
it ( 'works with $elemMatch (gh-1100)' , function ( done ) {
@@ -1435,7 +1432,7 @@ describe('model: querying:', function() {
1435
1432
} ) ;
1436
1433
} ) ;
1437
1434
1438
- it ( 'works when text search is called by a schema (gh-3824) (gh-6851)' , function ( ) {
1435
+ it ( 'works when text search is called by a schema (gh-3824) (gh-6851)' , async function ( ) {
1439
1436
if ( ! mongo26_or_greater ) {
1440
1437
return this . skip ( ) ;
1441
1438
}
@@ -1448,29 +1445,27 @@ describe('model: querying:', function() {
1448
1445
1449
1446
const Example = db . model ( 'Test' , exampleSchema ) ;
1450
1447
1451
- return co ( function * ( ) {
1452
- yield Example . init ( ) ; // Wait for index build
1453
- // Should not error
1454
- yield Example . findOne ( { $text : { $search : 'text search' } } ) ;
1448
+ await Example . init ( ) ; // Wait for index build
1449
+ // Should not error
1450
+ await Example . findOne ( { $text : { $search : 'text search' } } ) ;
1455
1451
1456
- yield Example . create ( { name : '1234 ABCD' , tag : 'test1' } ) ;
1457
- let doc = yield Example . findOne ( {
1458
- $text : {
1459
- $search : 1234 // Will be casted to a string
1460
- }
1461
- } ) ;
1462
- assert . ok ( doc ) ;
1463
- assert . equal ( doc . tag , 'test1' ) ;
1452
+ await Example . create ( { name : '1234 ABCD' , tag : 'test1' } ) ;
1453
+ let doc = await Example . findOne ( {
1454
+ $text : {
1455
+ $search : 1234 // Will be casted to a string
1456
+ }
1457
+ } ) ;
1458
+ assert . ok ( doc ) ;
1459
+ assert . equal ( doc . tag , 'test1' ) ;
1464
1460
1465
- doc = yield Example . findOne ( {
1466
- $text : {
1467
- $search : 'abcd' ,
1468
- $caseSensitive : 'no' // Casted to boolean
1469
- }
1470
- } ) ;
1471
- assert . ok ( doc ) ;
1472
- assert . equal ( doc . tag , 'test1' ) ;
1461
+ doc = await Example . findOne ( {
1462
+ $text : {
1463
+ $search : 'abcd' ,
1464
+ $caseSensitive : 'no' // Casted to boolean
1465
+ }
1473
1466
} ) ;
1467
+ assert . ok ( doc ) ;
1468
+ assert . equal ( doc . tag , 'test1' ) ;
1474
1469
} ) ;
1475
1470
} ) ;
1476
1471
} ) ;
@@ -1838,7 +1833,7 @@ describe('model: querying:', function() {
1838
1833
} ) ;
1839
1834
} ) ;
1840
1835
1841
- it ( 'with conditionals' , function ( done ) {
1836
+ it ( 'with conditionals' , async function ( ) {
1842
1837
// $in $nin etc
1843
1838
const BufSchema = new Schema ( { name : String , block : Buffer } ) ;
1844
1839
const Test = db . model ( 'Test' , BufSchema ) ;
@@ -1848,79 +1843,72 @@ describe('model: querying:', function() {
1848
1843
const docC = { name : 'C' , block : new MongooseBuffer ( 'aGVsbG8gd29ybGQ=' , 'base64' ) } ;
1849
1844
const docD = { name : 'D' , block : new MongooseBuffer ( { type : 'Buffer' , data : [ 103 , 104 , 45 , 54 , 56 , 54 , 51 ] } ) } ;
1850
1845
1851
- Test . create ( docA , docB , docC , docD , function ( err , a , b , c , d ) {
1852
- if ( err ) return done ( err ) ;
1853
-
1854
- co ( function * ( ) {
1855
- assert . equal ( a . block . toString ( 'utf8' ) , 'über' ) ;
1856
- assert . equal ( b . block . toString ( 'utf8' ) , 'buffer shtuffs are neat' ) ;
1857
- assert . equal ( c . block . toString ( 'utf8' ) , 'hello world' ) ;
1858
- assert . equal ( d . block . toString ( 'utf8' ) , 'gh-6863' ) ;
1859
-
1860
- const testPromises = [
1861
- Test . find ( { block : {
1862
- $in : [
1863
- [ 195 , 188 , 98 , 101 , 114 ] ,
1864
- 'buffer shtuffs are neat' ,
1865
- Buffer . from ( 'aGVsbG8gd29ybGQ=' , 'base64' ) ,
1866
- { type : 'Buffer' , data : [ 103 , 104 , 45 , 54 , 56 , 54 , 51 ] } // gh-6863
1867
- ] } } ) . exec ( ) . then ( tests => {
1868
- assert . ifError ( err ) ;
1869
- assert . equal ( tests . length , 4 ) ;
1870
- } ) ,
1871
- Test . find ( { block : { $in : [ 'über' , 'hello world' ] } } ) . exec ( ) . then ( tests => {
1872
- assert . equal ( tests . length , 2 ) ;
1873
- } ) ,
1874
- Test . find ( { block : { $in : [ 'über' ] } } ) . exec ( ) . then ( tests => {
1875
- assert . equal ( tests . length , 1 ) ;
1876
- assert . equal ( tests [ 0 ] . block . toString ( 'utf8' ) , 'über' ) ;
1877
- } ) ,
1878
- Test . find ( { block : { $nin : [ 'über' ] } } ) . exec ( ) . then ( tests => {
1879
- assert . equal ( tests . length , 3 ) ;
1880
- } ) ,
1881
- Test . find ( { block : {
1882
- $nin : [
1883
- [ 195 , 188 , 98 , 101 , 114 ] ,
1884
- Buffer . from ( 'aGVsbG8gd29ybGQ=' , 'base64' ) ,
1885
- { type : 'Buffer' , data : [ 103 , 104 , 45 , 54 , 56 , 54 , 51 ] } // gh-6863
1886
- ] } } ) . exec ( ) . then ( tests => {
1887
- assert . ifError ( err ) ;
1888
- assert . equal ( tests . length , 1 ) ;
1889
- assert . equal ( tests [ 0 ] . block . toString ( 'utf8' ) , 'buffer shtuffs are neat' ) ;
1890
- } ) ,
1891
- Test . find ( { block : { $ne : 'über' } } ) . exec ( ) . then ( tests => {
1892
- assert . equal ( tests . length , 3 ) ;
1893
- } ) ,
1894
- Test . find ( { block : { $gt : 'über' } } ) . exec ( ) . then ( tests => {
1895
- assert . equal ( tests . length , 3 ) ;
1896
- } ) ,
1897
- Test . find ( { block : { $gte : 'über' } } ) . exec ( ) . then ( tests => {
1898
- assert . equal ( tests . length , 4 ) ;
1899
- } ) ,
1900
- Test . find ( { block : { $lt : Buffer . from ( 'buffer shtuffs are neat' ) } } ) . exec ( ) . then ( tests => {
1901
- assert . ifError ( err ) ;
1902
- assert . equal ( tests . length , 3 ) ;
1903
- const ret = { } ;
1904
- ret [ tests [ 0 ] . block . toString ( 'utf8' ) ] = 1 ;
1905
- ret [ tests [ 1 ] . block . toString ( 'utf8' ) ] = 1 ;
1906
- ret [ tests [ 2 ] . block . toString ( 'utf8' ) ] = 1 ;
1907
-
1908
- assert . ok ( ret [ 'über' ] !== undefined ) ;
1909
- } ) ,
1910
- Test . find ( { block : { $lte : 'buffer shtuffs are neat' } } ) . exec ( ) . then ( tests => {
1911
- assert . equal ( tests . length , 4 ) ;
1912
- } ) ,
1913
- Test . find ( { block : { $gt : { type : 'Buffer' , data : [ 103 , 104 , 45 , 54 , 56 , 54 , 51 ] } } } ) . exec ( ) . then ( tests => {
1914
- assert . equal ( tests . length , 2 ) ;
1915
- } )
1916
- ] ;
1917
-
1918
- // run all of the tests in parallel
1919
- yield testPromises ;
1920
- yield Test . deleteOne ( { } ) ;
1921
- done ( ) ;
1922
- } ) . catch ( done ) ;
1923
- } ) ;
1846
+ const [ a , b , c , d ] = await Test . create ( [ docA , docB , docC , docD ] ) ;
1847
+
1848
+
1849
+
1850
+ assert . equal ( a . block . toString ( 'utf8' ) , 'über' ) ;
1851
+ assert . equal ( b . block . toString ( 'utf8' ) , 'buffer shtuffs are neat' ) ;
1852
+ assert . equal ( c . block . toString ( 'utf8' ) , 'hello world' ) ;
1853
+ assert . equal ( d . block . toString ( 'utf8' ) , 'gh-6863' ) ;
1854
+
1855
+
1856
+ // run all of the tests in parallel
1857
+ await Promise . all ( [
1858
+ Test . find ( { block : {
1859
+ $in : [
1860
+ [ 195 , 188 , 98 , 101 , 114 ] ,
1861
+ 'buffer shtuffs are neat' ,
1862
+ Buffer . from ( 'aGVsbG8gd29ybGQ=' , 'base64' ) ,
1863
+ { type : 'Buffer' , data : [ 103 , 104 , 45 , 54 , 56 , 54 , 51 ] } // gh-6863
1864
+ ] } } ) . exec ( ) . then ( tests => {
1865
+ assert . equal ( tests . length , 4 ) ;
1866
+ } ) ,
1867
+ Test . find ( { block : { $in : [ 'über' , 'hello world' ] } } ) . exec ( ) . then ( tests => {
1868
+ assert . equal ( tests . length , 2 ) ;
1869
+ } ) ,
1870
+ Test . find ( { block : { $in : [ 'über' ] } } ) . exec ( ) . then ( tests => {
1871
+ assert . equal ( tests . length , 1 ) ;
1872
+ assert . equal ( tests [ 0 ] . block . toString ( 'utf8' ) , 'über' ) ;
1873
+ } ) ,
1874
+ Test . find ( { block : { $nin : [ 'über' ] } } ) . exec ( ) . then ( tests => {
1875
+ assert . equal ( tests . length , 3 ) ;
1876
+ } ) ,
1877
+ Test . find ( { block : {
1878
+ $nin : [
1879
+ [ 195 , 188 , 98 , 101 , 114 ] ,
1880
+ Buffer . from ( 'aGVsbG8gd29ybGQ=' , 'base64' ) ,
1881
+ { type : 'Buffer' , data : [ 103 , 104 , 45 , 54 , 56 , 54 , 51 ] } // gh-6863
1882
+ ] } } ) . exec ( ) . then ( tests => {
1883
+ assert . equal ( tests . length , 1 ) ;
1884
+ assert . equal ( tests [ 0 ] . block . toString ( 'utf8' ) , 'buffer shtuffs are neat' ) ;
1885
+ } ) ,
1886
+ Test . find ( { block : { $ne : 'über' } } ) . exec ( ) . then ( tests => {
1887
+ assert . equal ( tests . length , 3 ) ;
1888
+ } ) ,
1889
+ Test . find ( { block : { $gt : 'über' } } ) . exec ( ) . then ( tests => {
1890
+ assert . equal ( tests . length , 3 ) ;
1891
+ } ) ,
1892
+ Test . find ( { block : { $gte : 'über' } } ) . exec ( ) . then ( tests => {
1893
+ assert . equal ( tests . length , 4 ) ;
1894
+ } ) ,
1895
+ Test . find ( { block : { $lt : Buffer . from ( 'buffer shtuffs are neat' ) } } ) . exec ( ) . then ( tests => {
1896
+ assert . equal ( tests . length , 3 ) ;
1897
+ const ret = { } ;
1898
+ ret [ tests [ 0 ] . block . toString ( 'utf8' ) ] = 1 ;
1899
+ ret [ tests [ 1 ] . block . toString ( 'utf8' ) ] = 1 ;
1900
+ ret [ tests [ 2 ] . block . toString ( 'utf8' ) ] = 1 ;
1901
+
1902
+ assert . ok ( ret [ 'über' ] !== undefined ) ;
1903
+ } ) ,
1904
+ Test . find ( { block : { $lte : 'buffer shtuffs are neat' } } ) . exec ( ) . then ( tests => {
1905
+ assert . equal ( tests . length , 4 ) ;
1906
+ } ) ,
1907
+ Test . find ( { block : { $gt : { type : 'Buffer' , data : [ 103 , 104 , 45 , 54 , 56 , 54 , 51 ] } } } ) . exec ( ) . then ( tests => {
1908
+ assert . equal ( tests . length , 2 ) ;
1909
+ } )
1910
+ ] ) ;
1911
+ await Test . deleteOne ( { } ) ;
1924
1912
} ) ;
1925
1913
1926
1914
it ( 'with previously existing null values in the db' , function ( done ) {
@@ -2358,20 +2346,18 @@ describe('model: querying:', function() {
2358
2346
} ) ;
2359
2347
}
2360
2348
} ) ;
2361
- it ( 'works with legacy 2dsphere pair in schema (gh-6937)' , function ( ) {
2349
+ it ( 'works with legacy 2dsphere pair in schema (gh-6937)' , async function ( ) {
2362
2350
if ( ! mongo24_or_greater ) {
2363
2351
return it . skip ( ) ;
2364
2352
}
2365
2353
2366
- return co ( function * ( ) {
2367
- const Model = db . model ( 'Test' , schema2dsphere ) ;
2368
- yield Model . init ( ) ;
2369
- const model = new Model ( ) ;
2370
- model . loc = [ 1 , 2 ] ;
2371
- yield model . save ( ) ;
2372
- const result = yield Model . where ( 'loc' ) . near ( { center : { type : 'Point' , coordinates : [ 1 , 2 ] } , maxDistance : 10 } ) ;
2373
- assert . equal ( result . length , 1 ) ;
2374
- } ) ;
2354
+ const Model = db . model ( 'Test' , schema2dsphere ) ;
2355
+ await Model . init ( ) ;
2356
+ const model = new Model ( ) ;
2357
+ model . loc = [ 1 , 2 ] ;
2358
+ await model . save ( ) ;
2359
+ const result = await Model . where ( 'loc' ) . near ( { center : { type : 'Point' , coordinates : [ 1 , 2 ] } , maxDistance : 10 } ) ;
2360
+ assert . equal ( result . length , 1 ) ;
2375
2361
} ) ;
2376
2362
} ) ;
2377
2363
} ) ;
0 commit comments