senter-mongo-repository
Advanced tools
Comparing version 1.0.14 to 1.0.15
@@ -253,2 +253,3 @@ const MongoClient = require('mongodb').MongoClient; | ||
static get ProfilesCollectionName() { return 'profiles'; } | ||
static get AppsCollectionName() { return 'apps'; } | ||
@@ -255,0 +256,0 @@ get _collection() { |
{ | ||
"name": "senter-mongo-repository", | ||
"version": "1.0.14", | ||
"version": "1.0.15", | ||
"description": "Contain methods to work with mongo db", | ||
@@ -5,0 +5,0 @@ "main": "mongoReposotory.js", |
@@ -5,3 +5,3 @@ const MongoRepository = require("../mongoReposotory"); | ||
const connectionString = 'mongodb+srv://dev:xeVfBSSXEmDCu32aQUHtRte572jOA03z@sandbox-5zqkh.mongodb.net/test?retryWrites=true&w=majority'; | ||
const connectionString = ''; | ||
const dbName = 'dev'; | ||
@@ -15,96 +15,96 @@ const collectionName = 'TestRun'; | ||
// describe("create", () => { | ||
// var repository; | ||
// var userId; | ||
// var client; | ||
// var id; | ||
describe("create", () => { | ||
var repository; | ||
var userId; | ||
var client; | ||
var id; | ||
// beforeEach(async () => { | ||
// userId = lastUserId.toString(); | ||
// lastUserId++; | ||
// repository = new MongoRepository(connectionString, dbName, collectionName); | ||
// client = new MongoClient(connectionString, options); | ||
// await repository.init(); | ||
// await client.connect(); | ||
// }); | ||
beforeEach(async () => { | ||
userId = lastUserId.toString(); | ||
lastUserId++; | ||
repository = new MongoRepository(connectionString, dbName, collectionName); | ||
client = new MongoClient(connectionString, options); | ||
await repository.init(); | ||
await client.connect(); | ||
}); | ||
// afterEach(async () => { | ||
// const col = client.db(dbName).collection(collectionName); | ||
// await col.deleteOne({ _id: id }); | ||
// await client.close(); | ||
// await repository.close(); | ||
// }); | ||
afterEach(async () => { | ||
const col = client.db(dbName).collection(collectionName); | ||
await col.deleteOne({ _id: id }); | ||
await client.close(); | ||
await repository.close(); | ||
}); | ||
// test("should create", async () => { | ||
// const document = { | ||
// name: "should create" | ||
// }; | ||
test("should create", async () => { | ||
const document = { | ||
name: "should create" | ||
}; | ||
// id = await repository.create(userId, document); | ||
id = await repository.create(userId, document); | ||
// const col = client.db(dbName).collection(collectionName); | ||
// const docs = await col.find({ _id: id }).toArray(); | ||
// expect(docs.length).toBe(1); | ||
// }); | ||
const col = client.db(dbName).collection(collectionName); | ||
const docs = await col.find({ _id: id }).toArray(); | ||
expect(docs.length).toBe(1); | ||
}); | ||
// test("should create with provided id", async () => { | ||
// id = uuidv4(); | ||
// const document = { | ||
// name: "should create with provided id", | ||
// id: id | ||
// }; | ||
test("should create with provided id", async () => { | ||
id = uuidv4(); | ||
const document = { | ||
name: "should create with provided id", | ||
id: id | ||
}; | ||
// await repository.create(userId, document); | ||
await repository.create(userId, document); | ||
// const col = client.db(dbName).collection(collectionName); | ||
// const docs = await col.find({ _id: id }).toArray(); | ||
// expect(docs.length).toBe(1); | ||
// expect(docs[0].id).toBeUndefined(); | ||
// }); | ||
const col = client.db(dbName).collection(collectionName); | ||
const docs = await col.find({ _id: id }).toArray(); | ||
expect(docs.length).toBe(1); | ||
expect(docs[0].id).toBeUndefined(); | ||
}); | ||
// test("should create and return id", async () => { | ||
// const document = { | ||
// name: "should create and return id" | ||
// }; | ||
test("should create and return id", async () => { | ||
const document = { | ||
name: "should create and return id" | ||
}; | ||
// id = await repository.create(userId, document); | ||
id = await repository.create(userId, document); | ||
// expect(id).toBeDefined(); | ||
// }); | ||
expect(id).toBeDefined(); | ||
}); | ||
// test("should set createdAt and updatedAt", async () => { | ||
// const document = { | ||
// name: "should set createdAt and updatedAt" | ||
// }; | ||
test("should set createdAt and updatedAt", async () => { | ||
const document = { | ||
name: "should set createdAt and updatedAt" | ||
}; | ||
// id = await repository.create(userId, document); | ||
id = await repository.create(userId, document); | ||
// expect(document.createdAt).toBeDefined(); | ||
// expect(document.updatedAt).toBeDefined(); | ||
// expect(document.createdAt).toBe(document.updatedAt); | ||
// var differenceFromNow = document.createdAt.getTime() - new Date(); | ||
// expect(differenceFromNow).toBeLessThan(1000); | ||
// }); | ||
expect(document.createdAt).toBeDefined(); | ||
expect(document.updatedAt).toBeDefined(); | ||
expect(document.createdAt).toBe(document.updatedAt); | ||
var differenceFromNow = document.createdAt.getTime() - new Date(); | ||
expect(differenceFromNow).toBeLessThan(1000); | ||
}); | ||
// test("should set userId", async () => { | ||
// const document = { | ||
// name: "should set userId" | ||
// }; | ||
test("should set userId", async () => { | ||
const document = { | ||
name: "should set userId" | ||
}; | ||
// id = await repository.create(userId, document); | ||
id = await repository.create(userId, document); | ||
// expect(document.userId).toBe(userId); | ||
// }); | ||
expect(document.userId).toBe(userId); | ||
}); | ||
// test("should set id but not _id", async () => { | ||
// const document = { | ||
// name: "should set id but not _id" | ||
// }; | ||
test("should set id but not _id", async () => { | ||
const document = { | ||
name: "should set id but not _id" | ||
}; | ||
// id = await repository.create(userId, document); | ||
id = await repository.create(userId, document); | ||
// expect(document.id).toBe(id); | ||
// expect(document._id).toBeUndefined(); | ||
// }); | ||
// }); | ||
expect(document.id).toBe(id); | ||
expect(document._id).toBeUndefined(); | ||
}); | ||
}); | ||
@@ -158,526 +158,526 @@ describe("createMany", () => { | ||
// describe("update", () => { | ||
// var repository; | ||
// var userId; | ||
// var client; | ||
// var id; | ||
describe("update", () => { | ||
var repository; | ||
var userId; | ||
var client; | ||
var id; | ||
// beforeEach(async () => { | ||
// userId = lastUserId.toString(); | ||
// lastUserId++; | ||
// repository = new MongoRepository(connectionString, dbName, collectionName); | ||
// client = new MongoClient(connectionString, options); | ||
// await repository.init(); | ||
// await client.connect(); | ||
// }); | ||
beforeEach(async () => { | ||
userId = lastUserId.toString(); | ||
lastUserId++; | ||
repository = new MongoRepository(connectionString, dbName, collectionName); | ||
client = new MongoClient(connectionString, options); | ||
await repository.init(); | ||
await client.connect(); | ||
}); | ||
// afterEach(async () => { | ||
// const col = client.db(dbName).collection(collectionName); | ||
// await col.deleteOne({ _id: id }); | ||
// await client.close(); | ||
// await repository.close(); | ||
// }); | ||
afterEach(async () => { | ||
const col = client.db(dbName).collection(collectionName); | ||
await col.deleteOne({ _id: id }); | ||
await client.close(); | ||
await repository.close(); | ||
}); | ||
// test("should update", async () => { | ||
// const expectedName = "another name"; | ||
// const document = { | ||
// name: "should update" | ||
// }; | ||
test("should update", async () => { | ||
const expectedName = "another name"; | ||
const document = { | ||
name: "should update" | ||
}; | ||
// id = await repository.create(userId, document); | ||
// expect(id).toBeDefined(); | ||
id = await repository.create(userId, document); | ||
expect(id).toBeDefined(); | ||
// document.name = expectedName; | ||
document.name = expectedName; | ||
// await repository.update(id, userId, document); | ||
await repository.update(id, userId, document); | ||
// const col = client.db(dbName).collection(collectionName); | ||
// const docs = await col.find({ _id: id }).toArray(); | ||
// expect(docs.length).toBe(1); | ||
// expect(docs[0].name).toBe(expectedName); | ||
// }); | ||
const col = client.db(dbName).collection(collectionName); | ||
const docs = await col.find({ _id: id }).toArray(); | ||
expect(docs.length).toBe(1); | ||
expect(docs[0].name).toBe(expectedName); | ||
}); | ||
// test("should set updatedAt", async () => { | ||
// const expectedName = "another name"; | ||
// const document = { | ||
// name: "should set updatedAt" | ||
// }; | ||
test("should set updatedAt", async () => { | ||
const expectedName = "another name"; | ||
const document = { | ||
name: "should set updatedAt" | ||
}; | ||
// id = await repository.create(userId, document); | ||
// expect(id).toBeDefined(); | ||
id = await repository.create(userId, document); | ||
expect(id).toBeDefined(); | ||
// document.name = expectedName; | ||
// document.createdAt = new Date(2019, 10, 10); | ||
// document.updatedAt = new Date(2019, 10, 10); | ||
document.name = expectedName; | ||
document.createdAt = new Date(2019, 10, 10); | ||
document.updatedAt = new Date(2019, 10, 10); | ||
// await repository.update(id, userId, document); | ||
await repository.update(id, userId, document); | ||
// var differenceFromNow = document.updatedAt.getTime() - new Date(); | ||
// expect(differenceFromNow).toBeLessThan(1000); | ||
// }); | ||
var differenceFromNow = document.updatedAt.getTime() - new Date(); | ||
expect(differenceFromNow).toBeLessThan(1000); | ||
}); | ||
// test("should not update when object is not found", async () => { | ||
// const document = { | ||
// name: "should not update" | ||
// }; | ||
test("should not update when object is not found", async () => { | ||
const document = { | ||
name: "should not update" | ||
}; | ||
// id = uuidv4(); | ||
id = uuidv4(); | ||
// await repository.update(id, userId, document); | ||
await repository.update(id, userId, document); | ||
// const col = client.db(dbName).collection(collectionName); | ||
// const docs = await col.find({ _id: id }).toArray(); | ||
// expect(docs.length).toBe(0); | ||
// }); | ||
// }); | ||
const col = client.db(dbName).collection(collectionName); | ||
const docs = await col.find({ _id: id }).toArray(); | ||
expect(docs.length).toBe(0); | ||
}); | ||
}); | ||
// describe("delete", () => { | ||
// var repository; | ||
// var userId; | ||
// var client; | ||
// var id; | ||
describe("delete", () => { | ||
var repository; | ||
var userId; | ||
var client; | ||
var id; | ||
// beforeEach(async () => { | ||
// userId = lastUserId.toString(); | ||
// lastUserId++; | ||
// repository = new MongoRepository(connectionString, dbName, collectionName); | ||
// client = new MongoClient(connectionString, options); | ||
// await client.connect(); | ||
// await repository.init(); | ||
// }); | ||
beforeEach(async () => { | ||
userId = lastUserId.toString(); | ||
lastUserId++; | ||
repository = new MongoRepository(connectionString, dbName, collectionName); | ||
client = new MongoClient(connectionString, options); | ||
await client.connect(); | ||
await repository.init(); | ||
}); | ||
// afterEach(async () => { | ||
// await client.close(); | ||
// await repository.close(); | ||
// }); | ||
afterEach(async () => { | ||
await client.close(); | ||
await repository.close(); | ||
}); | ||
// test("should delete", async () => { | ||
// id = uuidv4(); | ||
// const document = { | ||
// _id: id, | ||
// userId: userId, | ||
// name: "should delete" | ||
// }; | ||
test("should delete", async () => { | ||
id = uuidv4(); | ||
const document = { | ||
_id: id, | ||
userId: userId, | ||
name: "should delete" | ||
}; | ||
// const col = client.db(dbName).collection(collectionName); | ||
// await col.insertOne(document); | ||
const col = client.db(dbName).collection(collectionName); | ||
await col.insertOne(document); | ||
// await repository.delete(id, userId); | ||
await repository.delete(id, userId); | ||
// const docs = await col.find({ _id: id }).toArray(); | ||
// expect(docs.length).toBe(0); | ||
// }); | ||
// }); | ||
const docs = await col.find({ _id: id }).toArray(); | ||
expect(docs.length).toBe(0); | ||
}); | ||
}); | ||
// describe("markDeleted", () => { | ||
// var repository; | ||
// var userId; | ||
// var client; | ||
// var id; | ||
describe("markDeleted", () => { | ||
var repository; | ||
var userId; | ||
var client; | ||
var id; | ||
// beforeEach(async () => { | ||
// userId = lastUserId.toString(); | ||
// lastUserId++; | ||
// repository = new MongoRepository(connectionString, dbName, collectionName); | ||
// client = new MongoClient(connectionString, options); | ||
// await client.connect(); | ||
// await repository.init(); | ||
// }); | ||
beforeEach(async () => { | ||
userId = lastUserId.toString(); | ||
lastUserId++; | ||
repository = new MongoRepository(connectionString, dbName, collectionName); | ||
client = new MongoClient(connectionString, options); | ||
await client.connect(); | ||
await repository.init(); | ||
}); | ||
// afterEach(async () => { | ||
// const col = client.db(dbName).collection(collectionName); | ||
// await col.deleteOne({ _id: id }); | ||
// await client.close(); | ||
// await repository.close(); | ||
// }); | ||
afterEach(async () => { | ||
const col = client.db(dbName).collection(collectionName); | ||
await col.deleteOne({ _id: id }); | ||
await client.close(); | ||
await repository.close(); | ||
}); | ||
// test("should mark deleted", async () => { | ||
// id = uuidv4(); | ||
// const document = { | ||
// _id: id, | ||
// userId: userId, | ||
// name: "should mark deleted" | ||
// }; | ||
test("should mark deleted", async () => { | ||
id = uuidv4(); | ||
const document = { | ||
_id: id, | ||
userId: userId, | ||
name: "should mark deleted" | ||
}; | ||
// const col = client.db(dbName).collection(collectionName); | ||
// await col.insertOne(document); | ||
const col = client.db(dbName).collection(collectionName); | ||
await col.insertOne(document); | ||
// await repository.markDeleted(id, userId); | ||
await repository.markDeleted(id, userId); | ||
// const docs = await col.find({ _id: id }).toArray(); | ||
// expect(docs.length).toBe(1); | ||
// expect(docs[0].deletedAt).toBeDefined(); | ||
// var differenceFromNow = docs[0].deletedAt.getTime() - new Date(); | ||
// expect(differenceFromNow).toBeLessThan(1000); | ||
// }); | ||
// }); | ||
const docs = await col.find({ _id: id }).toArray(); | ||
expect(docs.length).toBe(1); | ||
expect(docs[0].deletedAt).toBeDefined(); | ||
var differenceFromNow = docs[0].deletedAt.getTime() - new Date(); | ||
expect(differenceFromNow).toBeLessThan(1000); | ||
}); | ||
}); | ||
// describe("getById", () => { | ||
// var repository; | ||
// var userId; | ||
// var client; | ||
// var id; | ||
describe("getById", () => { | ||
var repository; | ||
var userId; | ||
var client; | ||
var id; | ||
// beforeEach(async () => { | ||
// userId = lastUserId.toString(); | ||
// lastUserId++; | ||
// repository = new MongoRepository(connectionString, dbName, collectionName); | ||
// client = new MongoClient(connectionString, options); | ||
// await client.connect(); | ||
// await repository.init(); | ||
// }); | ||
beforeEach(async () => { | ||
userId = lastUserId.toString(); | ||
lastUserId++; | ||
repository = new MongoRepository(connectionString, dbName, collectionName); | ||
client = new MongoClient(connectionString, options); | ||
await client.connect(); | ||
await repository.init(); | ||
}); | ||
// afterEach(async () => { | ||
// const col = client.db(dbName).collection(collectionName); | ||
// await col.deleteOne({ _id: id }); | ||
// await client.close(); | ||
// await repository.close(); | ||
// }); | ||
afterEach(async () => { | ||
const col = client.db(dbName).collection(collectionName); | ||
await col.deleteOne({ _id: id }); | ||
await client.close(); | ||
await repository.close(); | ||
}); | ||
// test("should retrieve exisiting object by id and userId", async () => { | ||
// id = uuidv4(); | ||
// const document = { | ||
// _id: id, | ||
// userId: userId, | ||
// name: "should retrieve exisiting object by id and userId" | ||
// }; | ||
test("should retrieve exisiting object by id and userId", async () => { | ||
id = uuidv4(); | ||
const document = { | ||
_id: id, | ||
userId: userId, | ||
name: "should retrieve exisiting object by id and userId" | ||
}; | ||
// const col = client.db(dbName).collection(collectionName); | ||
// await col.insertOne(document); | ||
const col = client.db(dbName).collection(collectionName); | ||
await col.insertOne(document); | ||
// const result = await repository.getById(id, userId); | ||
const result = await repository.getById(id, userId); | ||
// expect(result).toBeDefined(); | ||
// expect(result._id).toBeUndefined(); | ||
// expect(result.id).toBe(id); | ||
// expect(result.userId).toBe(userId); | ||
// expect(result.name).toBe(document.name); | ||
// }); | ||
expect(result).toBeDefined(); | ||
expect(result._id).toBeUndefined(); | ||
expect(result.id).toBe(id); | ||
expect(result.userId).toBe(userId); | ||
expect(result.name).toBe(document.name); | ||
}); | ||
// test("should return null if only userId does not match", async () => { | ||
// id = uuidv4(); | ||
// const document = { | ||
// _id: id, | ||
// userId: userId, | ||
// name: "should retrieve exisiting object by id and userId" | ||
// }; | ||
test("should return null if only userId does not match", async () => { | ||
id = uuidv4(); | ||
const document = { | ||
_id: id, | ||
userId: userId, | ||
name: "should retrieve exisiting object by id and userId" | ||
}; | ||
// const col = client.db(dbName).collection(collectionName); | ||
// await col.insertOne(document); | ||
const col = client.db(dbName).collection(collectionName); | ||
await col.insertOne(document); | ||
// const result = await repository.getById(id, 'anotherid'); | ||
const result = await repository.getById(id, 'anotherid'); | ||
// expect(result).toBeNull(); | ||
// }); | ||
expect(result).toBeNull(); | ||
}); | ||
// test("should retrieve exisiting object by id", async () => { | ||
// id = uuidv4(); | ||
// const document = { | ||
// _id: id, | ||
// userId: userId, | ||
// name: "should retrieve exisiting object by id and userId" | ||
// }; | ||
test("should retrieve exisiting object by id", async () => { | ||
id = uuidv4(); | ||
const document = { | ||
_id: id, | ||
userId: userId, | ||
name: "should retrieve exisiting object by id and userId" | ||
}; | ||
// const col = client.db(dbName).collection(collectionName); | ||
// await col.insertOne(document); | ||
const col = client.db(dbName).collection(collectionName); | ||
await col.insertOne(document); | ||
// const result = await repository.getById(id); | ||
const result = await repository.getById(id); | ||
// expect(result).toBeDefined(); | ||
// expect(result._id).toBeUndefined(); | ||
// expect(result.id).toBe(id); | ||
// expect(result.userId).toBe(userId); | ||
// expect(result.name).toBe(document.name); | ||
// }); | ||
expect(result).toBeDefined(); | ||
expect(result._id).toBeUndefined(); | ||
expect(result.id).toBe(id); | ||
expect(result.userId).toBe(userId); | ||
expect(result.name).toBe(document.name); | ||
}); | ||
// test("should return null when object doesn't exist. By id and userId", async () => { | ||
// id = uuidv4(); | ||
test("should return null when object doesn't exist. By id and userId", async () => { | ||
id = uuidv4(); | ||
// const result = await repository.getById(id, userId); | ||
const result = await repository.getById(id, userId); | ||
// expect(result).toBeNull(); | ||
// id = null; | ||
// }); | ||
expect(result).toBeNull(); | ||
id = null; | ||
}); | ||
// test("should retrieve exisiting object by id", async () => { | ||
// id = uuidv4(); | ||
test("should retrieve exisiting object by id", async () => { | ||
id = uuidv4(); | ||
// const result = await repository.getById(id); | ||
const result = await repository.getById(id); | ||
// expect(result).toBeNull(); | ||
// id = null; | ||
// }); | ||
expect(result).toBeNull(); | ||
id = null; | ||
}); | ||
// test("should return null when exisiting object was deleted", async () => { | ||
// const document = { | ||
// userId: userId, | ||
// name: "should return null when exisiting object was deleted" | ||
// }; | ||
test("should return null when exisiting object was deleted", async () => { | ||
const document = { | ||
userId: userId, | ||
name: "should return null when exisiting object was deleted" | ||
}; | ||
// id = await repository.create(userId, document); | ||
// await repository.delete(id, userId); | ||
id = await repository.create(userId, document); | ||
await repository.delete(id, userId); | ||
// const col = client.db(dbName).collection(collectionName); | ||
// await col.insertOne(document); | ||
const col = client.db(dbName).collection(collectionName); | ||
await col.insertOne(document); | ||
// const result = await repository.getById(id); | ||
const result = await repository.getById(id); | ||
// expect(result).toBeNull(); | ||
// }); | ||
// }); | ||
expect(result).toBeNull(); | ||
}); | ||
}); | ||
// describe("search", () => { | ||
// var repository; | ||
// var userId; | ||
// var client; | ||
// var ids = []; | ||
describe("search", () => { | ||
var repository; | ||
var userId; | ||
var client; | ||
var ids = []; | ||
// beforeEach(async () => { | ||
// userId = lastUserId.toString(); | ||
// lastUserId++; | ||
// repository = new MongoRepository(connectionString, dbName, collectionName); | ||
// client = new MongoClient(connectionString, options); | ||
// await client.connect(); | ||
// await repository.init(); | ||
// }); | ||
beforeEach(async () => { | ||
userId = lastUserId.toString(); | ||
lastUserId++; | ||
repository = new MongoRepository(connectionString, dbName, collectionName); | ||
client = new MongoClient(connectionString, options); | ||
await client.connect(); | ||
await repository.init(); | ||
}); | ||
// afterEach(async () => { | ||
// const col = client.db(dbName).collection(collectionName); | ||
// for (let index = 0; index < ids.length; index++) { | ||
// const id = ids[index]; | ||
// await col.deleteOne({ _id: id }); | ||
// } | ||
// await client.close(); | ||
// await repository.close(); | ||
// }); | ||
afterEach(async () => { | ||
const col = client.db(dbName).collection(collectionName); | ||
for (let index = 0; index < ids.length; index++) { | ||
const id = ids[index]; | ||
await col.deleteOne({ _id: id }); | ||
} | ||
await client.close(); | ||
await repository.close(); | ||
}); | ||
// test("should return all records by filter condition", async () => { | ||
// ids.push(uuidv4()); | ||
// ids.push(uuidv4()); | ||
// ids.push(uuidv4()); | ||
// ids.push(uuidv4()); | ||
test("should return all records by filter condition", async () => { | ||
ids.push(uuidv4()); | ||
ids.push(uuidv4()); | ||
ids.push(uuidv4()); | ||
ids.push(uuidv4()); | ||
// const col = client.db(dbName).collection(collectionName); | ||
// await col.insertOne({ | ||
// _id: ids[0], | ||
// userId: userId, | ||
// name: "should return all records by filter condition" | ||
// }); | ||
// await col.insertOne({ | ||
// _id: ids[1], | ||
// userId: userId, | ||
// name: "should return all records by filter condition" | ||
// }); | ||
// await col.insertOne({ | ||
// _id: ids[2], | ||
// userId: userId, | ||
// name: "should return all records by filter condition" | ||
// }); | ||
// await col.insertOne({ | ||
// _id: ids[3], | ||
// userId: 'another', | ||
// name: "should return all records by filter condition" | ||
// }); | ||
const col = client.db(dbName).collection(collectionName); | ||
await col.insertOne({ | ||
_id: ids[0], | ||
userId: userId, | ||
name: "should return all records by filter condition" | ||
}); | ||
await col.insertOne({ | ||
_id: ids[1], | ||
userId: userId, | ||
name: "should return all records by filter condition" | ||
}); | ||
await col.insertOne({ | ||
_id: ids[2], | ||
userId: userId, | ||
name: "should return all records by filter condition" | ||
}); | ||
await col.insertOne({ | ||
_id: ids[3], | ||
userId: 'another', | ||
name: "should return all records by filter condition" | ||
}); | ||
// const result = await repository.search({ | ||
// userId: userId | ||
// }); | ||
const result = await repository.search({ | ||
userId: userId | ||
}); | ||
// expect(result).toBeDefined(); | ||
// expect(result.length).toBe(3); | ||
// }); | ||
expect(result).toBeDefined(); | ||
expect(result.length).toBe(3); | ||
}); | ||
// test("should return empty list if no records found", async () => { | ||
// ids.push(uuidv4()); | ||
// ids.push(uuidv4()); | ||
// ids.push(uuidv4()); | ||
// ids.push(uuidv4()); | ||
test("should return empty list if no records found", async () => { | ||
ids.push(uuidv4()); | ||
ids.push(uuidv4()); | ||
ids.push(uuidv4()); | ||
ids.push(uuidv4()); | ||
// const col = client.db(dbName).collection(collectionName); | ||
// await col.insertOne({ | ||
// _id: ids[0], | ||
// userId: userId, | ||
// name: "should return empty list if no records found" | ||
// }); | ||
// await col.insertOne({ | ||
// _id: ids[1], | ||
// userId: userId, | ||
// name: "should return empty list if no records found" | ||
// }); | ||
// await col.insertOne({ | ||
// _id: ids[2], | ||
// userId: userId, | ||
// name: "should return empty list if no records found" | ||
// }); | ||
// await col.insertOne({ | ||
// _id: ids[3], | ||
// userId: 'another', | ||
// name: "should return empty list if no records found" | ||
// }); | ||
const col = client.db(dbName).collection(collectionName); | ||
await col.insertOne({ | ||
_id: ids[0], | ||
userId: userId, | ||
name: "should return empty list if no records found" | ||
}); | ||
await col.insertOne({ | ||
_id: ids[1], | ||
userId: userId, | ||
name: "should return empty list if no records found" | ||
}); | ||
await col.insertOne({ | ||
_id: ids[2], | ||
userId: userId, | ||
name: "should return empty list if no records found" | ||
}); | ||
await col.insertOne({ | ||
_id: ids[3], | ||
userId: 'another', | ||
name: "should return empty list if no records found" | ||
}); | ||
// const result = await repository.search({ | ||
// userId: 'no records found' | ||
// }); | ||
const result = await repository.search({ | ||
userId: 'no records found' | ||
}); | ||
// expect(result).toBeDefined(); | ||
// expect(result.length).toBe(0); | ||
// }); | ||
expect(result).toBeDefined(); | ||
expect(result.length).toBe(0); | ||
}); | ||
// test("should return requested fields only if projection is set", async () => { | ||
// ids.push(uuidv4()); | ||
// ids.push(uuidv4()); | ||
// ids.push(uuidv4()); | ||
// ids.push(uuidv4()); | ||
test("should return requested fields only if projection is set", async () => { | ||
ids.push(uuidv4()); | ||
ids.push(uuidv4()); | ||
ids.push(uuidv4()); | ||
ids.push(uuidv4()); | ||
// const col = client.db(dbName).collection(collectionName); | ||
// await col.insertOne({ | ||
// _id: ids[0], | ||
// userId: userId, | ||
// name: "should return requested fields only if projection is set", | ||
// array: [1, 2, 3, 4, 5] | ||
// }); | ||
// await col.insertOne({ | ||
// _id: ids[1], | ||
// userId: userId, | ||
// name: "should return requested fields only if projection is set", | ||
// array: [1, 2, 3, 4, 5] | ||
// }); | ||
// await col.insertOne({ | ||
// _id: ids[2], | ||
// userId: userId, | ||
// name: "should return requested fields only if projection is set", | ||
// array: [1, 2, 3, 4, 5] | ||
// }); | ||
const col = client.db(dbName).collection(collectionName); | ||
await col.insertOne({ | ||
_id: ids[0], | ||
userId: userId, | ||
name: "should return requested fields only if projection is set", | ||
array: [1, 2, 3, 4, 5] | ||
}); | ||
await col.insertOne({ | ||
_id: ids[1], | ||
userId: userId, | ||
name: "should return requested fields only if projection is set", | ||
array: [1, 2, 3, 4, 5] | ||
}); | ||
await col.insertOne({ | ||
_id: ids[2], | ||
userId: userId, | ||
name: "should return requested fields only if projection is set", | ||
array: [1, 2, 3, 4, 5] | ||
}); | ||
// const result = await repository.search( | ||
// { | ||
// userId: userId | ||
// }, | ||
// 100, | ||
// { | ||
// array: 0 | ||
// }); | ||
const result = await repository.search( | ||
{ | ||
userId: userId | ||
}, | ||
100, | ||
{ | ||
array: 0 | ||
}); | ||
// expect(result).toBeDefined(); | ||
// expect(result.length).toBe(3); | ||
// expect(result[0].userId).toBe(userId); | ||
// expect(result[0].name).toBe("should return requested fields only if projection is set"); | ||
// expect(result[0].id).toBeDefined(); | ||
// expect(result[0].array).toBeUndefined(); | ||
// expect(result[1].array).toBeUndefined(); | ||
// expect(result[2].array).toBeUndefined(); | ||
// }); | ||
expect(result).toBeDefined(); | ||
expect(result.length).toBe(3); | ||
expect(result[0].userId).toBe(userId); | ||
expect(result[0].name).toBe("should return requested fields only if projection is set"); | ||
expect(result[0].id).toBeDefined(); | ||
expect(result[0].array).toBeUndefined(); | ||
expect(result[1].array).toBeUndefined(); | ||
expect(result[2].array).toBeUndefined(); | ||
}); | ||
// test("should not return deleted records ", async () => { | ||
// ids.push(uuidv4()); | ||
// ids.push(uuidv4()); | ||
// ids.push(uuidv4()); | ||
// ids.push(uuidv4()); | ||
test("should not return deleted records ", async () => { | ||
ids.push(uuidv4()); | ||
ids.push(uuidv4()); | ||
ids.push(uuidv4()); | ||
ids.push(uuidv4()); | ||
// const col = client.db(dbName).collection(collectionName); | ||
// await col.insertOne({ | ||
// _id: ids[0], | ||
// userId: userId, | ||
// name: "should return all records by filter condition" | ||
// }); | ||
// await col.insertOne({ | ||
// _id: ids[1], | ||
// userId: userId, | ||
// name: "should return all records by filter condition" | ||
// }); | ||
// await col.insertOne({ | ||
// _id: ids[2], | ||
// userId: userId, | ||
// name: "should return all records by filter condition" | ||
// }); | ||
// await col.insertOne({ | ||
// _id: ids[3], | ||
// userId: userId, | ||
// name: "should return all records by filter condition", | ||
// deletedAt: new Date() | ||
// }); | ||
const col = client.db(dbName).collection(collectionName); | ||
await col.insertOne({ | ||
_id: ids[0], | ||
userId: userId, | ||
name: "should return all records by filter condition" | ||
}); | ||
await col.insertOne({ | ||
_id: ids[1], | ||
userId: userId, | ||
name: "should return all records by filter condition" | ||
}); | ||
await col.insertOne({ | ||
_id: ids[2], | ||
userId: userId, | ||
name: "should return all records by filter condition" | ||
}); | ||
await col.insertOne({ | ||
_id: ids[3], | ||
userId: userId, | ||
name: "should return all records by filter condition", | ||
deletedAt: new Date() | ||
}); | ||
// const result = await repository.search({ | ||
// userId: userId | ||
// }); | ||
const result = await repository.search({ | ||
userId: userId | ||
}); | ||
// expect(result).toBeDefined(); | ||
// expect(result.length).toBe(3); | ||
// }); | ||
// }); | ||
expect(result).toBeDefined(); | ||
expect(result.length).toBe(3); | ||
}); | ||
}); | ||
// describe("upsert", () => { | ||
// var repository; | ||
// var userId; | ||
// var client; | ||
// var id; | ||
describe("upsert", () => { | ||
var repository; | ||
var userId; | ||
var client; | ||
var id; | ||
// beforeEach(async () => { | ||
// userId = lastUserId.toString(); | ||
// lastUserId++; | ||
// repository = new MongoRepository(connectionString, dbName, collectionName); | ||
// client = new MongoClient(connectionString, options); | ||
// await repository.init(); | ||
// await client.connect(); | ||
// }); | ||
beforeEach(async () => { | ||
userId = lastUserId.toString(); | ||
lastUserId++; | ||
repository = new MongoRepository(connectionString, dbName, collectionName); | ||
client = new MongoClient(connectionString, options); | ||
await repository.init(); | ||
await client.connect(); | ||
}); | ||
// afterEach(async () => { | ||
// const col = client.db(dbName).collection(collectionName); | ||
// await col.deleteOne({ _id: id }); | ||
// await client.close(); | ||
// await repository.close(); | ||
// }); | ||
afterEach(async () => { | ||
const col = client.db(dbName).collection(collectionName); | ||
await col.deleteOne({ _id: id }); | ||
await client.close(); | ||
await repository.close(); | ||
}); | ||
// test("should update", async () => { | ||
// const expectedName = "another name"; | ||
// const document = { | ||
// name: "should update" | ||
// }; | ||
test("should update", async () => { | ||
const expectedName = "another name"; | ||
const document = { | ||
name: "should update" | ||
}; | ||
// id = await repository.create(userId, document); | ||
// expect(id).toBeDefined(); | ||
id = await repository.create(userId, document); | ||
expect(id).toBeDefined(); | ||
// document.name = expectedName; | ||
document.name = expectedName; | ||
// await repository.upsert(id, userId, document); | ||
await repository.upsert(id, userId, document); | ||
// const col = client.db(dbName).collection(collectionName); | ||
// const docs = await col.find({ _id: id }).toArray(); | ||
// expect(docs.length).toBe(1); | ||
// expect(docs[0].name).toBe(expectedName); | ||
// }); | ||
const col = client.db(dbName).collection(collectionName); | ||
const docs = await col.find({ _id: id }).toArray(); | ||
expect(docs.length).toBe(1); | ||
expect(docs[0].name).toBe(expectedName); | ||
}); | ||
// test("should set updatedAt", async () => { | ||
// const expectedName = "another name"; | ||
// const document = { | ||
// name: "should set updatedAt" | ||
// }; | ||
test("should set updatedAt", async () => { | ||
const expectedName = "another name"; | ||
const document = { | ||
name: "should set updatedAt" | ||
}; | ||
// id = await repository.create(userId, document); | ||
// expect(id).toBeDefined(); | ||
id = await repository.create(userId, document); | ||
expect(id).toBeDefined(); | ||
// document.name = expectedName; | ||
// document.createdAt = new Date(2019, 10, 10); | ||
// document.updatedAt = new Date(2019, 10, 10); | ||
document.name = expectedName; | ||
document.createdAt = new Date(2019, 10, 10); | ||
document.updatedAt = new Date(2019, 10, 10); | ||
// await repository.upsert(id, userId, document); | ||
await repository.upsert(id, userId, document); | ||
// var differenceFromNow = document.updatedAt.getTime() - new Date(); | ||
// expect(differenceFromNow).toBeLessThan(1000); | ||
// }); | ||
var differenceFromNow = document.updatedAt.getTime() - new Date(); | ||
expect(differenceFromNow).toBeLessThan(1000); | ||
}); | ||
// test("should create new object if not found", async () => { | ||
// const document = { | ||
// name: "should not update" | ||
// }; | ||
test("should create new object if not found", async () => { | ||
const document = { | ||
name: "should not update" | ||
}; | ||
// id = uuidv4(); | ||
id = uuidv4(); | ||
// await repository.upsert(id, userId, document); | ||
await repository.upsert(id, userId, document); | ||
// const col = client.db(dbName).collection(collectionName); | ||
// const docs = await col.find({ _id: id }).toArray(); | ||
// expect(docs.length).toBe(1); | ||
// expect(docs[0].name).toBe(document.name); | ||
// }); | ||
// }); | ||
const col = client.db(dbName).collection(collectionName); | ||
const docs = await col.find({ _id: id }).toArray(); | ||
expect(docs.length).toBe(1); | ||
expect(docs[0].name).toBe(document.name); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
802
29683
2