Skip to content

Commit

Permalink
test(findbyid): add test to validate nested entity
Browse files Browse the repository at this point in the history
Adds and Modifies existing tests to validate nested entities.
  • Loading branch information
vitorgamer58 committed Jul 12, 2023
1 parent 4f460ae commit 9818b6b
Showing 1 changed file with 58 additions and 11 deletions.
69 changes: 58 additions & 11 deletions testdb/findByID.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,18 @@ describe('Query Find by ID', () => {

await client.createCollection(collection)

await client.collection(collection).insertOne( { _id: new ObjectId("60edc25fc39277307ca9a7ff"), number_test: 100, boolean_test: true, string_test: 'aString' })
await client.collection(collection).insertOne( { _id: new ObjectId("70edc25fc39277307ca9a700"), number_test: 200, boolean_test: false })
await client.collection(collection).insertOne( { _id: new ObjectId("80edd25fc39272307ca9a712"), number_test: 300, boolean_test: false })
await client.collection(collection).insertOne({ _id: new ObjectId("60edc25fc39277307ca9a7ff"), number_test: 100, boolean_test: true, string_test: 'aString' })
await client.collection(collection).insertOne({ _id: new ObjectId("70edc25fc39277307ca9a700"), number_test: 200, boolean_test: false })
await client.collection(collection).insertOne({ _id: new ObjectId("80edd25fc39272307ca9a712"), number_test: 300, boolean_test: false })
await client.collection(collection).insertOne({
_id: new ObjectId("64acbc1ba6a28fbd4501c25c"),
number_test: 400,
boolean_test: true,
string_test: "aString",
child_entity: {
number_test: 100, boolean_test: true, string_test: 'aString'
}
})
})

after(async () => {
Expand All @@ -38,16 +47,24 @@ describe('Query Find by ID', () => {
}
}

const ChildEntity = entity('Child entity', {
numberTest: field(Number),
stringTest: field(String),
booleanTest: field(Boolean),
arrayTest: field([String])
})

const givenAnEntity = () => {
return entity('A entity', {
id: field(String),
numberTest: field(Number),
stringTest: field(String),
booleanTest: field(Boolean)
booleanTest: field(Boolean),
childEntity: field(ChildEntity)
})
}

it('should return entities', async () => {
it('should return entity', async () => {
//given
const anEntity = givenAnEntity()
const ItemRepository = givenAnRepositoryClass({
Expand All @@ -65,8 +82,38 @@ describe('Query Find by ID', () => {
const ret = await itemRepo.findByID(anEntity.id)

//then
assert.deepStrictEqual(ret[0].toJSON(), { id: '60edc25fc39277307ca9a7ff', stringTest: "aString", numberTest: 100, booleanTest: true })
assert.deepStrictEqual(ret[0].isValid(),true )
assert.deepStrictEqual(ret[0].toJSON(), { id: '60edc25fc39277307ca9a7ff', stringTest: "aString", numberTest: 100, booleanTest: true, childEntity: undefined })
assert.deepStrictEqual(ret[0].isValid(), true)
})

it('should return nested entitiy', async () => {
//given
const anEntity = givenAnEntity()
const ItemRepository = givenAnRepositoryClass({
entity: anEntity,
collection,
database,
ids: ['id'],
mongodb: await connection
})
const injection = {}
const itemRepo = new ItemRepository(injection)

anEntity.id = '64acbc1ba6a28fbd4501c25c'
//when
const ret = await itemRepo.findByID(anEntity.id)

//then
assert.deepStrictEqual(ret[0].toJSON(), {
id: '64acbc1ba6a28fbd4501c25c',
numberTest: 400,
booleanTest: true,
stringTest: "aString",
childEntity: {
numberTest: 100, booleanTest: true, stringTest: 'aString', arrayTest: null,
}
})
assert.deepStrictEqual(ret[0].isValid(), true)
})

it('should return multiple entities', async () => {
Expand All @@ -81,17 +128,17 @@ describe('Query Find by ID', () => {
})
const injection = {}
const itemRepo = new ItemRepository(injection)

const ids = [
'60edc25fc39277307ca9a7ff',
'80edd25fc39272307ca9a712',
]
]

//when
const ret = await itemRepo.findByID(ids)

//then
assert.deepStrictEqual(ret[0].toJSON(), { id: '60edc25fc39277307ca9a7ff', stringTest: "aString", numberTest: 100, booleanTest: true })
assert.deepStrictEqual(ret[0].isValid(),true )
assert.deepStrictEqual(ret[0].toJSON(), { id: '60edc25fc39277307ca9a7ff', stringTest: "aString", numberTest: 100, booleanTest: true, childEntity: undefined })
assert.deepStrictEqual(ret[0].isValid(), true)
})
})

0 comments on commit 9818b6b

Please sign in to comment.