-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: datatore update and improvements
- Loading branch information
Showing
10 changed files
with
443 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ const { expect } = chai; | |
const allKeys: EntityKey[] = []; | ||
|
||
const gstore = new Gstore({ | ||
errorOnEntityNotFound: false, | ||
cache: { | ||
stores: [{ store: redisStore }], | ||
config: { | ||
|
@@ -41,6 +42,7 @@ const addKey = (key: EntityKey): void => { | |
|
||
interface MyInterface { | ||
email: string; | ||
birthday?: Date; | ||
} | ||
|
||
describe('Integration Tests (Cache)', () => { | ||
|
@@ -57,6 +59,10 @@ describe('Integration Tests (Cache)', () => { | |
validate: 'isEmail', | ||
required: true, | ||
}, | ||
birthday: { | ||
type: Date, | ||
optional: true, | ||
}, | ||
}); | ||
|
||
MyModel = gstore.model('CacheTests-User', schema); | ||
|
@@ -82,31 +88,46 @@ describe('Integration Tests (Cache)', () => { | |
}); | ||
}); | ||
|
||
test('should get one or multiple entities from the cache', async () => { | ||
test('should successfully return if no entity exists', async () => { | ||
const id1 = uniqueId(); | ||
|
||
const response = await MyModel.get(id1); | ||
expect(response).to.equal(null, JSON.stringify(response)); | ||
}); | ||
|
||
test('should get one or multiple entities from the cache multiple times', async () => { | ||
const id1 = uniqueId(); | ||
const id2 = uniqueId(); | ||
|
||
const user1 = new MyModel({ email: '[email protected]' }, id1); | ||
const user1 = new MyModel({ email: '[email protected]', birthday: new Date('2000-01-01T00:00:00.000Z') }, id1); | ||
const user2 = new MyModel({ email: '[email protected]' }, id2); | ||
|
||
const results = await Promise.all([user1.save(), user2.save()]); | ||
|
||
results.forEach((result) => addKey(result.entityKey)); | ||
|
||
const responseSingle = await MyModel.get(results[0].entityKey.name!); | ||
const responseSingle0 = await MyModel.get(results[0].entityKey.name!); | ||
const responseSingle1 = await MyModel.get(results[0].entityKey.name!); | ||
const responseMultiple = await MyModel.get([results[0].entityKey.name!, results[1].entityKey.name!]); | ||
|
||
expect(responseSingle.email).to.equal('[email protected]'); | ||
expect(responseSingle0.email).to.equal('[email protected]'); | ||
expect(responseSingle0.birthday instanceof Date).to.equal(true); | ||
expect(+(responseSingle0?.birthday || 0)).to.equal(+new Date('2000-01-01T00:00:00.000Z')); | ||
expect(responseSingle1.email).to.equal('[email protected]'); | ||
expect(responseSingle1.birthday instanceof Date).to.equal(true); | ||
expect(+(responseSingle1?.birthday || 0)).to.equal(+new Date('2000-01-01T00:00:00.000Z')); | ||
expect(responseMultiple[0].email).to.equal('[email protected]'); | ||
expect(responseMultiple[0].birthday instanceof Date).to.eq(true); | ||
expect(responseMultiple[1].email).to.equal('[email protected]'); | ||
// expect(typeof responseMultiple[1].birthday).to.eq('string'); | ||
}); | ||
|
||
test('should load already cached entities with correct datastore entity keys', async () => { | ||
test('should load already cached entities with correct datastore entity keys and dates', async () => { | ||
const id1 = uniqueNumericId(); | ||
const id2 = uniqueNumericId(); | ||
|
||
const user1 = new MyModel({ email: '[email protected]' }, id1); | ||
const user2 = new MyModel({ email: '[email protected]' }, id2); | ||
const user1 = new MyModel({ email: '[email protected]', birthday: new Date('2000-01-01T00:00:00.000Z') }, id1); | ||
const user2 = new MyModel({ email: '[email protected]', birthday: new Date('2000-01-01T00:00:00.000Z') }, id2); | ||
|
||
const results = await Promise.all([user1.save(), user2.save()]); | ||
|
||
|
@@ -117,30 +138,42 @@ describe('Integration Tests (Cache)', () => { | |
responseMultiple0.entities.forEach((entry) => { | ||
expect(ds.isKey(entry?.entityKey)).to.equal(true); | ||
expect(typeof entry?.entityKey.id).to.equal('number'); | ||
expect(entry?.birthday instanceof Date).to.eq(true); | ||
}); | ||
|
||
const responseMultiple1 = await MyModel.list({ format: 'ENTITY', order: { property: 'email', descending: false } }); | ||
|
||
responseMultiple1.entities.forEach((entry) => { | ||
expect(ds.isKey(entry?.entityKey)).to.equal(true); | ||
expect(typeof entry?.entityKey.id).to.equal('number'); | ||
expect(entry?.birthday instanceof Date).to.eq(true); | ||
}); | ||
}); | ||
|
||
test('should find one entity from the cache', async () => { | ||
test('should find one entity from the cache multiple times', async () => { | ||
const id = uniqueId(); | ||
|
||
const user = new MyModel({ email: 'test2@test.com' }, id); | ||
const user = new MyModel({ email: 'test3@test.com', birthday: new Date('2000-01-01T00:00:00.000Z') }, id); | ||
|
||
const result = await user.save(); | ||
|
||
addKey(result.entityKey); | ||
|
||
const response = await MyModel.findOne({ email: 'test2@test.com' }); | ||
const response = await MyModel.findOne({ email: 'test3@test.com' }); | ||
expect(ds.isKey(response?.entityKey)).equal(true); | ||
|
||
expect(response!.email).to.eq('test2@test.com'); | ||
expect(response!.email).to.eq('test3@test.com'); | ||
expect(response!.entityKey.name).to.eq(id); | ||
expect(response!.entityKey instanceof entity.Key).to.eq(true); | ||
expect(response!.birthday instanceof Date).to.eq(true); | ||
|
||
const response2 = await MyModel.findOne({ email: '[email protected]' }); | ||
|
||
expect(ds.isKey(response2?.entityKey)).equal(true); | ||
|
||
expect(response2!.email).to.eq('[email protected]'); | ||
expect(response2!.entityKey.name).to.eq(id); | ||
expect(response2!.entityKey instanceof entity.Key).to.eq(true); | ||
expect(response2!.birthday instanceof Date).to.eq(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.