assai 0.1.2 → 0.1.3
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.
- package/package.json +3 -2
- package/src/usecases/operation/count.test.mjs +0 -49
- package/src/usecases/operation/delete_one.test.mjs +0 -30
- package/src/usecases/operation/find_one.test.mjs +0 -28
- package/src/usecases/operation/insert_many.test.mjs +0 -44
- package/src/usecases/operation/insert_one.test.mjs +0 -74
- package/src/usecases/operation/update_one.test.mjs +0 -56
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "assai",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"repository": {
|
|
5
5
|
"url": "https://github.com/TimeLord2010/assai",
|
|
6
6
|
"type": "git"
|
|
@@ -19,7 +19,8 @@
|
|
|
19
19
|
],
|
|
20
20
|
"files": [
|
|
21
21
|
"index.mjs",
|
|
22
|
-
"src/"
|
|
22
|
+
"src/",
|
|
23
|
+
"!src/**/*.test.mjs"
|
|
23
24
|
],
|
|
24
25
|
"author": "Vinícius Gabriel",
|
|
25
26
|
"license": "ISC",
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { fakerPT_BR } from '@faker-js/faker'
|
|
2
|
-
import assert from 'node:assert'
|
|
3
|
-
import { after, before, describe, it } from 'node:test'
|
|
4
|
-
import { mockGetCollection } from '../../../test/mock_get_collection.mjs'
|
|
5
|
-
import { closeMongoClient } from '../../mongo_client.mjs'
|
|
6
|
-
import { generateNewId } from '../generate_new_id.mjs'
|
|
7
|
-
import { count } from './count.mjs'
|
|
8
|
-
import { insertMany } from './insert_many.mjs'
|
|
9
|
-
|
|
10
|
-
describe('count', () => {
|
|
11
|
-
const tag = generateNewId()
|
|
12
|
-
before(async () => {
|
|
13
|
-
/** @type {import('../../../test/mock_get_collection.mjs').ItestCollection[]} */
|
|
14
|
-
const items = []
|
|
15
|
-
for (let i = 0; i < 50; i++) {
|
|
16
|
-
items.push({
|
|
17
|
-
name: fakerPT_BR.person.fullName(),
|
|
18
|
-
tag,
|
|
19
|
-
})
|
|
20
|
-
}
|
|
21
|
-
await insertMany({
|
|
22
|
-
docs: items,
|
|
23
|
-
// @ts-ignore
|
|
24
|
-
getCollection: mockGetCollection,
|
|
25
|
-
})
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
after(async () => {
|
|
29
|
-
await closeMongoClient()
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
it('should count all inserted documents', async () => {
|
|
33
|
-
const counter = await count({
|
|
34
|
-
query: { tag },
|
|
35
|
-
// @ts-ignore
|
|
36
|
-
getCollection: mockGetCollection,
|
|
37
|
-
})
|
|
38
|
-
assert.equal(counter, 50)
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
it('should return 0 if the query does not match any documents', async () => {
|
|
42
|
-
const counter = await count({
|
|
43
|
-
query: { tag: generateNewId() },
|
|
44
|
-
// @ts-ignore
|
|
45
|
-
getCollection: mockGetCollection,
|
|
46
|
-
})
|
|
47
|
-
assert.equal(counter, 0)
|
|
48
|
-
})
|
|
49
|
-
})
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import assert from 'node:assert'
|
|
2
|
-
import { describe, it } from 'node:test'
|
|
3
|
-
import { manageMockRegistry } from '../../../test/manage_mock_registry.mjs'
|
|
4
|
-
import { mockGetCollection } from '../../../test/mock_get_collection.mjs'
|
|
5
|
-
import { count } from './count.mjs'
|
|
6
|
-
import { deleteOne } from './delete_one.mjs'
|
|
7
|
-
|
|
8
|
-
describe('deleteOne', () => {
|
|
9
|
-
const { id } = manageMockRegistry({
|
|
10
|
-
name: 'Made in heaven',
|
|
11
|
-
}, {
|
|
12
|
-
deleteAtEnd: false,
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
it('should succeed on deleting by using registered id', async () => {
|
|
16
|
-
const deleted = await deleteOne({
|
|
17
|
-
// @ts-ignore
|
|
18
|
-
getCollection: mockGetCollection,
|
|
19
|
-
query: { id }
|
|
20
|
-
})
|
|
21
|
-
assert(deleted)
|
|
22
|
-
|
|
23
|
-
const docCount = await count({
|
|
24
|
-
// @ts-ignore
|
|
25
|
-
getCollection: mockGetCollection,
|
|
26
|
-
query: { id }
|
|
27
|
-
})
|
|
28
|
-
assert(docCount == 0)
|
|
29
|
-
})
|
|
30
|
-
})
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import assert from 'node:assert'
|
|
2
|
-
import { describe, it } from 'node:test'
|
|
3
|
-
import { manageMockRegistry } from '../../../test/manage_mock_registry.mjs'
|
|
4
|
-
import { mockGetCollection } from '../../../test/mock_get_collection.mjs'
|
|
5
|
-
import { findOne } from './find_one.mjs'
|
|
6
|
-
|
|
7
|
-
describe('findOne', () => {
|
|
8
|
-
|
|
9
|
-
const { id, name } = manageMockRegistry({
|
|
10
|
-
name: 'Anna'
|
|
11
|
-
})
|
|
12
|
-
|
|
13
|
-
async function _findOne(query) {
|
|
14
|
-
const r = await findOne({
|
|
15
|
-
// @ts-ignore
|
|
16
|
-
getCollection: mockGetCollection,
|
|
17
|
-
query: query,
|
|
18
|
-
})
|
|
19
|
-
return r
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
it('should succeed at finding a saved document', async () => {
|
|
23
|
-
const savedDoc = await _findOne({ id })
|
|
24
|
-
assert(savedDoc)
|
|
25
|
-
assert.equal(savedDoc.id, id)
|
|
26
|
-
assert.equal(savedDoc.name, name)
|
|
27
|
-
})
|
|
28
|
-
})
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { fakerPT_BR } from '@faker-js/faker'
|
|
2
|
-
import { ObjectId } from 'mongodb'
|
|
3
|
-
import assert from 'node:assert'
|
|
4
|
-
import { after, describe, it } from 'node:test'
|
|
5
|
-
import { mockGetCollection } from '../../../test/mock_get_collection.mjs'
|
|
6
|
-
import { closeMongoClient } from '../../mongo_client.mjs'
|
|
7
|
-
import { generateNewId } from '../generate_new_id.mjs'
|
|
8
|
-
import { insertMany } from './insert_many.mjs'
|
|
9
|
-
|
|
10
|
-
describe('insertMany', () => {
|
|
11
|
-
|
|
12
|
-
after(async () => await closeMongoClient())
|
|
13
|
-
|
|
14
|
-
it('should succeed at inserting multiple data', async () => {
|
|
15
|
-
const tag = generateNewId()
|
|
16
|
-
/** @type {import('../../../test/mock_get_collection.mjs').ItestCollection[]} */
|
|
17
|
-
const items = []
|
|
18
|
-
for (let i = 0; i < 50; i++) {
|
|
19
|
-
items.push({
|
|
20
|
-
name: fakerPT_BR.person.fullName(),
|
|
21
|
-
createdAt: fakerPT_BR.date.birthdate(),
|
|
22
|
-
tag: tag,
|
|
23
|
-
})
|
|
24
|
-
}
|
|
25
|
-
const r = await insertMany({
|
|
26
|
-
docs: items,
|
|
27
|
-
// @ts-ignore
|
|
28
|
-
getCollection: mockGetCollection,
|
|
29
|
-
})
|
|
30
|
-
const ids = r.map(x => x.id)
|
|
31
|
-
const allHaveId = ids.every(x => x && ObjectId.isValid(x))
|
|
32
|
-
assert(allHaveId)
|
|
33
|
-
|
|
34
|
-
const col = await mockGetCollection()
|
|
35
|
-
|
|
36
|
-
const docs = await col.find({
|
|
37
|
-
tag: ObjectId.createFromHexString(tag),
|
|
38
|
-
}).toArray()
|
|
39
|
-
assert(docs.length == items.length)
|
|
40
|
-
|
|
41
|
-
const tagFound = docs[0].tag
|
|
42
|
-
assert(tagFound instanceof ObjectId)
|
|
43
|
-
})
|
|
44
|
-
})
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { ObjectId } from 'mongodb'
|
|
2
|
-
import assert from 'node:assert'
|
|
3
|
-
import { after, describe, it } from 'node:test'
|
|
4
|
-
import { mockGetCollection } from '../../../test/mock_get_collection.mjs'
|
|
5
|
-
import { closeMongoClient } from '../../mongo_client.mjs'
|
|
6
|
-
import { generateNewId } from '../generate_new_id.mjs'
|
|
7
|
-
import { insertOne } from './insert_one.mjs'
|
|
8
|
-
|
|
9
|
-
describe('insertOne', () => {
|
|
10
|
-
|
|
11
|
-
after(async () => await closeMongoClient())
|
|
12
|
-
|
|
13
|
-
async function insert(doc) {
|
|
14
|
-
await insertOne({
|
|
15
|
-
doc,
|
|
16
|
-
// @ts-ignore
|
|
17
|
-
getCollection: mockGetCollection,
|
|
18
|
-
})
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
it('should accept a string ids', async () => {
|
|
22
|
-
const id = generateNewId()
|
|
23
|
-
const userId = generateNewId()
|
|
24
|
-
const postId = generateNewId()
|
|
25
|
-
const addressId = generateNewId()
|
|
26
|
-
const name = 'Joseph'
|
|
27
|
-
const doc = {
|
|
28
|
-
id,
|
|
29
|
-
name,
|
|
30
|
-
userId,
|
|
31
|
-
posts: [{ postId }],
|
|
32
|
-
address: { addressId },
|
|
33
|
-
}
|
|
34
|
-
await insert(doc)
|
|
35
|
-
const col = await mockGetCollection()
|
|
36
|
-
const savedDoc = await col.findOne({ _id: new ObjectId(id) })
|
|
37
|
-
assert(savedDoc)
|
|
38
|
-
|
|
39
|
-
// Checking if normal properties are correctly set
|
|
40
|
-
assert(savedDoc.name == name)
|
|
41
|
-
|
|
42
|
-
// Checking _id is indeed an ObjectId instance
|
|
43
|
-
const _id = savedDoc._id
|
|
44
|
-
assert(_id instanceof ObjectId)
|
|
45
|
-
assert(_id.toHexString() == id)
|
|
46
|
-
|
|
47
|
-
// Checking if ids inside arrays are correctly mapped
|
|
48
|
-
const savedPostId = savedDoc.posts?.[0].postId
|
|
49
|
-
assert(savedPostId instanceof ObjectId)
|
|
50
|
-
assert(savedPostId.toHexString() == postId)
|
|
51
|
-
|
|
52
|
-
// Checking if ids inside objects are correctly mapped
|
|
53
|
-
const savedAddressId = savedDoc.address.addressId
|
|
54
|
-
assert(savedAddressId instanceof ObjectId)
|
|
55
|
-
assert(savedAddressId.toHexString() == addressId)
|
|
56
|
-
})
|
|
57
|
-
|
|
58
|
-
it('should accept ObjectId as usual', async () => {
|
|
59
|
-
const id = new ObjectId()
|
|
60
|
-
const name = 'Jotoro'
|
|
61
|
-
const doc = { id, name }
|
|
62
|
-
await insert(doc)
|
|
63
|
-
|
|
64
|
-
const col = await mockGetCollection()
|
|
65
|
-
const savedDoc = await col.findOne({ _id: id })
|
|
66
|
-
assert(savedDoc)
|
|
67
|
-
|
|
68
|
-
assert(savedDoc.name == name)
|
|
69
|
-
|
|
70
|
-
const _id = savedDoc._id
|
|
71
|
-
assert(_id instanceof ObjectId)
|
|
72
|
-
assert(_id.toHexString() == id.toHexString())
|
|
73
|
-
})
|
|
74
|
-
})
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { ObjectId } from 'mongodb'
|
|
2
|
-
import assert from 'node:assert'
|
|
3
|
-
import { after, describe, it } from 'node:test'
|
|
4
|
-
import { manageMockRegistry } from '../../../test/manage_mock_registry.mjs'
|
|
5
|
-
import { mockGetCollection } from '../../../test/mock_get_collection.mjs'
|
|
6
|
-
import { closeMongoClient } from '../../mongo_client.mjs'
|
|
7
|
-
import { generateNewId } from '../generate_new_id.mjs'
|
|
8
|
-
import { updateOne } from './update_one.mjs'
|
|
9
|
-
|
|
10
|
-
describe('updateOne', () => {
|
|
11
|
-
|
|
12
|
-
const { id } = manageMockRegistry({
|
|
13
|
-
name: 'Jolyne Cujoh',
|
|
14
|
-
})
|
|
15
|
-
|
|
16
|
-
after(() => {
|
|
17
|
-
closeMongoClient()
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
it('should update document using string id', async () => {
|
|
21
|
-
const updatedName = 'Jolyne'
|
|
22
|
-
const updated = await updateOne({
|
|
23
|
-
// @ts-ignore
|
|
24
|
-
getCollection: mockGetCollection,
|
|
25
|
-
query: {
|
|
26
|
-
id,
|
|
27
|
-
},
|
|
28
|
-
update: {
|
|
29
|
-
$set: { name: updatedName }
|
|
30
|
-
},
|
|
31
|
-
})
|
|
32
|
-
assert(updated)
|
|
33
|
-
const col = await mockGetCollection()
|
|
34
|
-
const doc = await col.findOne({
|
|
35
|
-
_id: new ObjectId(id)
|
|
36
|
-
})
|
|
37
|
-
assert(doc)
|
|
38
|
-
assert(doc?.name == updatedName)
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
it('should not update any document using non registered id', async () => {
|
|
42
|
-
const updated = await updateOne({
|
|
43
|
-
// @ts-ignore
|
|
44
|
-
getCollection: mockGetCollection,
|
|
45
|
-
query: {
|
|
46
|
-
id: generateNewId(),
|
|
47
|
-
},
|
|
48
|
-
update: {
|
|
49
|
-
$set: {
|
|
50
|
-
createdAt: new Date()
|
|
51
|
-
},
|
|
52
|
-
},
|
|
53
|
-
})
|
|
54
|
-
assert(!updated)
|
|
55
|
-
})
|
|
56
|
-
})
|