UNPKG

4.21 kBJavaScriptView Raw
1'use strict';
2
3var testUtils = require('../test-utils');
4var should = testUtils.should;
5
6module.exports = function (dbType, context) {
7
8 describe(dbType + ': errors', function () {
9
10 it('error: gimme some args', function () {
11 var db = context.db;
12 return db.find().then(function () {
13 throw Error('should not be here');
14 }, function (err) {
15 should.exist(err);
16 });
17 });
18
19 it('error: missing required key selector', function () {
20 var db = context.db;
21 return db.find({}).then(function () {
22 throw Error('should not be here');
23 }, function (err) {
24 should.exist(err);
25 });
26 });
27
28 it('error: unsupported mixed sort', function () {
29 var db = context.db;
30 var index = {
31 "index": {
32 "fields": [
33 {"foo": "desc"},
34 "bar"
35 ]
36 },
37 "name": "foo-index",
38 "type": "json"
39 };
40 return db.createIndex(index).then(function () {
41 throw new Error('should not be here');
42 }, function (err) {
43 should.exist(err);
44 });
45 });
46
47 it('error: invalid sort json', function () {
48 var db = context.db;
49 var index = {
50 "index": {
51 "fields": ["foo"]
52 },
53 "name": "foo-index",
54 "type": "json"
55 };
56
57 return db.createIndex(index).then(function () {
58 return db.bulkDocs([
59 { _id: '1', foo: 'eyo'},
60 { _id: '2', foo: 'ebb'},
61 { _id: '3', foo: 'eba'},
62 { _id: '4', foo: 'abo'}
63 ]);
64 }).then(function () {
65 return db.find({
66 selector: {foo: {"$lte": "eba"}},
67 fields: ["_id", "foo"],
68 sort: {foo: "asc"}
69 });
70 }).then(function () {
71 throw new Error('shouldnt be here');
72 }, function (err) {
73 should.exist(err);
74 });
75 });
76
77 it('error: conflicting sort and selector', function () {
78 var db = context.db;
79 var index = {
80 "index": {
81 "fields": ["foo"]
82 },
83 "name": "foo-index",
84 "type": "json"
85 };
86 return db.createIndex(index).then(function () {
87 return db.find({
88 "selector": {"foo": {"$gt": "\u0000\u0000"}},
89 "fields": ["_id", "foo"],
90 "sort": [{"_id": "asc"}]
91 });
92 }).then(function (res) {
93 res.warning.should.match(/no matching index found/);
94 });
95 });
96
97 it('error - no selector', function () {
98 var db = context.db;
99 var index = {
100 "index": {
101 "fields": ["foo"]
102 },
103 "name": "foo-index",
104 "type": "json"
105 };
106 return db.createIndex(index).then(function () {
107 return db.find({
108 "fields": ["_id", "foo"],
109 "sort": [{"foo": "asc"}]
110 });
111 }).then(function () {
112 throw new Error('shouldnt be here');
113 }, function (err) {
114 should.exist(err);
115 });
116 });
117
118 it('invalid ddoc', function () {
119 var db = context.db;
120
121 var index = {
122 "index": {
123 "fields": ["foo"]
124 },
125 "name": "foo-index",
126 "ddoc": "myddoc",
127 "type": "json"
128 };
129
130 return db.put({
131 _id: '_design/myddoc',
132 views: {
133 'foo-index': {
134 map: "function (){emit(1)}"
135 }
136 }
137 }).then(function () {
138 return db.createIndex(index).then(function () {
139 throw new Error('expected an error');
140 }, function (err) {
141 should.exist(err);
142 });
143 });
144 });
145
146 it('non-logical errors with no other selector', function () {
147 var db = context.db;
148
149 return db.createIndex({
150 index: {
151 fields: ['foo']
152 }
153 }).then(function () {
154 return db.bulkDocs([
155 {_id: '1', foo: 1},
156 {_id: '2', foo: 2},
157 {_id: '3', foo: 3},
158 {_id: '4', foo: 4}
159 ]);
160 }).then(function () {
161 return db.find({
162 selector: {
163 foo: {$mod: {gte: 3}}
164 }
165 }).then(function () {
166 throw new Error('expected an error');
167 }, function (err) {
168 should.exist(err);
169 });
170 });
171 });
172 });
173};