1 | 'use strict';
|
2 |
|
3 | var testUtils = require('../test-utils');
|
4 | var sortById = testUtils.sortById;
|
5 |
|
6 | module.exports = function (dbType, context) {
|
7 |
|
8 | describe(dbType + ': $type', function () {
|
9 |
|
10 | beforeEach(function () {
|
11 | var db = context.db;
|
12 | return db.bulkDocs([
|
13 | {_id: 'a', foo: 'bar'},
|
14 | {_id: 'b', foo: 1},
|
15 | {_id: 'c', foo: null},
|
16 | {_id: 'd', foo: []},
|
17 | {_id: 'e', foo: {}},
|
18 | {_id: 'f', foo: false}
|
19 | ]);
|
20 | });
|
21 |
|
22 | it('does null', function () {
|
23 | var db = context.db;
|
24 | return db.find({
|
25 | selector: {
|
26 | _id: {$gt: null},
|
27 | 'foo': {$type: 'null'}
|
28 | },
|
29 | fields: ['_id']
|
30 | }).then(function (res) {
|
31 | res.docs.sort(sortById);
|
32 | res.docs.should.deep.equal([{_id: 'c'}]);
|
33 | });
|
34 | });
|
35 |
|
36 | it('does boolean', function () {
|
37 | var db = context.db;
|
38 | return db.find({
|
39 | selector: {
|
40 | _id: {$gt: null},
|
41 | 'foo': {$type: 'boolean'}
|
42 | },
|
43 | fields: ['_id']
|
44 | }).then(function (res) {
|
45 | res.docs.sort(sortById);
|
46 | res.docs.should.deep.equal([{_id: 'f'}]);
|
47 |
|
48 | });
|
49 | });
|
50 |
|
51 | it('does number', function () {
|
52 | var db = context.db;
|
53 | return db.find({
|
54 | selector: {
|
55 | _id: {$gt: null},
|
56 | 'foo': {$type: 'number'}
|
57 | },
|
58 | fields: ['_id']
|
59 | }).then(function (res) {
|
60 | res.docs.sort(sortById);
|
61 | res.docs.should.deep.equal([{_id: 'b'}]);
|
62 | });
|
63 | });
|
64 |
|
65 | it('does string', function () {
|
66 | var db = context.db;
|
67 | return db.find({
|
68 | selector: {
|
69 | _id: {$gt: null},
|
70 | 'foo': {$type: 'string'}
|
71 | },
|
72 | fields: ['_id']
|
73 | }).then(function (res) {
|
74 | res.docs.sort(sortById);
|
75 | res.docs.should.deep.equal([{_id: 'a'}]);
|
76 | });
|
77 | });
|
78 |
|
79 | it('does array', function () {
|
80 | var db = context.db;
|
81 | return db.find({
|
82 | selector: {
|
83 | _id: {$gt: null},
|
84 | 'foo': {$type: 'array'}
|
85 | },
|
86 | fields: ['_id']
|
87 | }).then(function (res) {
|
88 | res.docs.sort(sortById);
|
89 | res.docs.should.deep.equal([{_id: 'd'}]);
|
90 | });
|
91 | });
|
92 |
|
93 | it('does object', function () {
|
94 | var db = context.db;
|
95 | return db.find({
|
96 | selector: {
|
97 | _id: {$gt: null},
|
98 | 'foo': {$type: 'object'}
|
99 | },
|
100 | fields: ['_id']
|
101 | }).then(function (res) {
|
102 | res.docs.sort(sortById);
|
103 | res.docs.should.deep.equal([{_id: 'e'}]);
|
104 | });
|
105 | });
|
106 |
|
107 | it('throws error for unmatched type', function () {
|
108 | var db = context.db;
|
109 | return db.find({
|
110 | selector: {
|
111 | _id: {$gt: null},
|
112 | 'foo': {$type: 'made-up'}
|
113 | },
|
114 | fields: ['_id']
|
115 | }).catch(function (err) {
|
116 | err.message.should.match(/made-up not supported/);
|
117 | });
|
118 | });
|
119 | });
|
120 | };
|