monastery 2.2.2 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Todo:
3
+ * - Test custom model rules
4
+ * - Test custom messages
5
+ * - Test for index and unique schema fields
6
+ * - Test blacklisting
7
+ * - Test deep/array population
8
+ * - Test default limit
9
+ * Notes:
10
+ * - expect().toEqual: strict deep match
11
+ * - expect().toMatchObject: received object can have random properties (try not to use since its not strict)
12
+ * - expect.objectContaining:
13
+ */
14
+
15
+ /* Run tests sequentially */
16
+ require('./util.js')
17
+ require('./manager.js')
18
+ require('./collection.js')
19
+ require('./model.js')
20
+ require('./crud.js')
21
+ require('./blacklisting.js')
22
+
23
+ require('./populate.js')
24
+ require('./validate.js')
25
+ require('./plugin-images.js')
26
+ require('./virtuals.js')
@@ -0,0 +1,77 @@
1
+ const { MongoClient } = require('mongodb')
2
+ const monastery = require('../lib/index.js')
3
+
4
+ test('manager > basics', async () => {
5
+ const manager = monastery('localhost/monastery', { serverSelectionTimeoutMS: 2000 })
6
+ // Manager is exposed
7
+ expect(manager).toEqual(monastery.manager)
8
+ // Basic find command
9
+ expect(await manager.db.collection('non-collection').findOne({})).toEqual(null)
10
+ // Raw MongoDB ping command
11
+ expect(await manager.command({ ping: 1 })).toEqual({ ok: 1 })
12
+ manager.close()
13
+ })
14
+
15
+ test('manager > uri error', async () => {
16
+ expect(() => monastery('', {})).toThrow('No connection URI provided.')
17
+ })
18
+
19
+ test('manager > catch', async () => {
20
+ // Bad port (thrown by MongoDB)
21
+ const db = monastery('localhost:1234/monastery', { serverSelectionTimeoutMS: 1000 }).catch((err) => {
22
+ expect(err.message).toEqual('connect ECONNREFUSED 127.0.0.1:1234')
23
+ })
24
+ // the manager is still retuned
25
+ expect(db?.open).toEqual(expect.any(Function))
26
+ db.close()
27
+ })
28
+
29
+ test('manager > return a promise', async () => {
30
+ const db = await monastery('localhost/monastery', { serverSelectionTimeoutMS: 2000, promise: true })
31
+ expect(db).toEqual(expect.any(Object))
32
+ db.close()
33
+ })
34
+
35
+ test('manager > return a promise with uri error', async () => {
36
+ await expect(monastery('badlocalhost/monastery', { serverSelectionTimeoutMS: 1000, promise: true }))
37
+ .rejects.toThrow('getaddrinfo EAI_AGAIN badlocalhost')
38
+ })
39
+
40
+ test('manager > reuse MongoDB Client', async () => {
41
+ const mongoClient = new MongoClient('mongodb://localhost/monastery', {})
42
+ const db = await monastery(mongoClient, { serverSelectionTimeoutMS: 2000, promise: true })
43
+ expect(db).toEqual(expect.any(Object))
44
+ expect(db.client).toEqual(expect.any(Object))
45
+ expect(db.catch).toEqual(expect.any(Function))
46
+ db.close()
47
+ })
48
+
49
+ test('manager > events', async () => {
50
+ // note: jests tests only wait for 5s
51
+ function eventTester(db, eventName) {
52
+ return new Promise((resolve) => {
53
+ db.on(eventName, () => resolve(true))
54
+ })
55
+ }
56
+ // Triggers on opening/open
57
+ const db = monastery('localhost/monastery', { serverSelectionTimeoutMS: 2000 })
58
+ expect(db._state).toEqual('opening')
59
+ // Start waiting for the following events
60
+ const promises = Promise.all([
61
+ expect(eventTester(db, 'open')).resolves.toEqual(true),
62
+ expect(eventTester(db, 'closing')).resolves.toEqual(true),
63
+ expect(eventTester(db, 'close')).resolves.toEqual(true),
64
+ ])
65
+ // Triggers closing/close
66
+ db.close()
67
+ return promises
68
+ })
69
+
70
+ test('Manager > get collection', async () => {
71
+ const manager = monastery('localhost/monastery', { serverSelectionTimeoutMS: 2000 })
72
+ // Basic aggregate command
73
+ expect(await manager.get('non-collection').aggregate([], {})).toEqual([])
74
+ // Basic find command
75
+ expect(await manager.get('non-collection').find({})).toEqual([])
76
+ manager.close()
77
+ })
@@ -13,7 +13,7 @@ module.exports.bird = {
13
13
  sizes: {
14
14
  one: { type: 'number' },
15
15
  two: { type: 'number' },
16
- }
16
+ },
17
17
  },
18
18
  },
19
19
  findBL: ['wing'],
@@ -39,35 +39,35 @@ module.exports.user = {
39
39
  pet: { type: 'string' },
40
40
  pets: [{
41
41
  name: { type: 'string'},
42
- age: { type: 'number'}
42
+ age: { type: 'number'},
43
43
  }],
44
44
  animals: {
45
45
  cat: { type: 'string' },
46
- dog: { type: 'string' }
46
+ dog: { type: 'string' },
47
47
  },
48
48
  hiddenPets: [{
49
- name: { type: 'string'}
49
+ name: { type: 'string'},
50
50
  }],
51
51
  hiddenList: [{ type: 'number'}],
52
52
  deep: {
53
53
  deep2: {
54
54
  deep3: {
55
- deep4: { type: 'string' }
56
- }
57
- }
55
+ deep4: { type: 'string' },
56
+ },
57
+ },
58
58
  },
59
59
  deeper: {
60
60
  deeper2: {
61
61
  deeper3: {
62
- deeper4: { type: 'string' }
63
- }
64
- }
62
+ deeper4: { type: 'string' },
63
+ },
64
+ },
65
65
  },
66
66
  deepModel: {
67
- myBird: { model: 'bird' }
67
+ myBird: { model: 'bird' },
68
68
  },
69
69
  hiddenDeepModel: {
70
- myBird: { model: 'bird' }
70
+ myBird: { model: 'bird' },
71
71
  },
72
72
  },
73
73
  findBL: [
@@ -91,32 +91,32 @@ module.exports.user = {
91
91
  pets: [{ name: 'Pluto', age: 5 }, { name: 'Milo', age: 4 }],
92
92
  animals: {
93
93
  cat: 'Ginger',
94
- dog: 'Max'
94
+ dog: 'Max',
95
95
  },
96
96
  hiddenPets: [{
97
- name: 'secretPet'
97
+ name: 'secretPet',
98
98
  }],
99
99
  hiddenList: [12, 23],
100
100
  deep: {
101
101
  deep2: {
102
102
  deep3: {
103
- deep4: 'hideme'
104
- }
105
- }
103
+ deep4: 'hideme',
104
+ },
105
+ },
106
106
  },
107
107
  deeper: {
108
108
  deeper2: {
109
109
  deeper3: {
110
- deeper4: 'hideme'
111
- }
112
- }
110
+ deeper4: 'hideme',
111
+ },
112
+ },
113
113
  },
114
114
  deepModel: {
115
- myBird: bird1._id
115
+ myBird: bird1._id,
116
116
  },
117
117
  hiddenDeepModel: {
118
- myBird: bird1._id
119
- }
118
+ myBird: bird1._id,
119
+ },
120
120
  }
121
121
  },
122
122
  }