diff --git a/__tests__/integration/cache.ts b/__tests__/integration/cache.ts index 0e5f0fa..92cc534 100644 --- a/__tests__/integration/cache.ts +++ b/__tests__/integration/cache.ts @@ -2,6 +2,7 @@ import redisStore from 'cache-manager-redis-store'; import chai from 'chai'; import Chance from 'chance'; import { Datastore } from '@google-cloud/datastore'; +import { entity } from '@google-cloud/datastore/build/src/entity'; import { Gstore, EntityKey, Model } from '../../src'; @@ -71,30 +72,46 @@ describe('Integration Tests (Cache)', () => { test('should set KEY symbol on query result', () => { const id = uniqueId(); const user = new MyModel({ email: 'test@test.com' }, id); - return user.save().then((entity) => { - addKey(entity.entityKey); - return MyModel.get(entity.entityKey.name!).then((e) => { + return user.save().then((result) => { + addKey(result.entityKey); + return MyModel.get(result.entityKey.name!).then((e) => { expect(e.email).equal('test@test.com'); }); }); }); - test('should get one or multiple entities fron the cache', async () => { + test('should get one or multiple entities from the cache', async () => { const id1 = uniqueId(); const id2 = uniqueId(); const user1 = new MyModel({ email: 'test1@test.com' }, id1); const user2 = new MyModel({ email: 'test2@test.com' }, id2); - const result = await Promise.all([user1.save(), user2.save()]); + const results = await Promise.all([user1.save(), user2.save()]); - result.forEach((entity) => addKey(entity.entityKey)); + results.forEach((result) => addKey(result.entityKey)); - const responseSingle = await MyModel.get(result[0].entityKey.name!); - const responseMultiple = await MyModel.get([result[0].entityKey.name!, result[1].entityKey.name!]); + const responseSingle = 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('test1@test.com'); expect(responseMultiple[0].email).to.equal('test1@test.com'); expect(responseMultiple[1].email).to.equal('test2@test.com'); }); + + test('should find one entity from the cache', async () => { + const id = uniqueId(); + + const user = new MyModel({ email: 'test2@test.com' }, id); + + const result = await user.save(); + + addKey(result.entityKey); + + const response = await MyModel.findOne({ email: 'test2@test.com' }); + + expect(response!.email).to.eq('test2@test.com'); + expect(response!.entityKey.name).to.eq(id); + expect(response!.entityKey instanceof entity.Key).to.eq(true); + }); }); diff --git a/__tests__/integration/model.ts b/__tests__/integration/model.ts index 6115686..fab2c15 100644 --- a/__tests__/integration/model.ts +++ b/__tests__/integration/model.ts @@ -5,6 +5,7 @@ import sinon from 'sinon'; import Chance from 'chance'; import Joi from '@hapi/joi'; import { Datastore } from '@google-cloud/datastore'; +import { entity } from '@google-cloud/datastore/build/src/entity'; import { Gstore, Entity, EntityKey } from '../../src'; import GstoreEntity from '../../src/entity'; @@ -36,8 +37,8 @@ const cleanUp = (cb: any): Promise => { cb(); }) .catch((err) => { - console.log('Error cleaning up'); // eslint-disable-line - console.log(err); // eslint-disable-line + console.log('Error cleaning up'); // eslint-disable-line + console.log(err); // eslint-disable-line }); }; @@ -75,7 +76,9 @@ const addCompany = (): Promise<{ name: string; entityKey: EntityKey }> => { }); }; -const addUser = (company: EntityKey | null = null): Promise => { +const addUser = ( + company: EntityKey | null = null, +): Promise<{ entityKey: EntityKey; name: string; email: string; company: unknown; privateVal: string }> => { const name = randomName(); const email = chance.email(); const privateVal = randomName(); @@ -95,7 +98,7 @@ const addUser = (company: EntityKey | null = null): Promise => { }); }; -const addPost = (userKey = null, publicationKey = null): Promise => { +const addPost = (userKey: EntityKey | null = null, publicationKey: EntityKey | null = null): Promise => { const title = randomName(); const post = new PostModel({ title, user: userKey, publication: publicationKey }, randomName()); return post.save().then(({ entityKey }) => { @@ -104,7 +107,7 @@ const addPost = (userKey = null, publicationKey = null): Promise => { }); }; -const addPublication = (userKey = null): Promise => { +const addPublication = (userKey: EntityKey | null = null): Promise => { const title = randomName(); const publication = new PublicationModel({ title, user: userKey }); return publication.save().then(({ entityKey }) => { @@ -191,7 +194,7 @@ describe('Model (Integration Tests)', () => { // eslint-disable-next-line @typescript-eslint/ban-ts-ignore // @ts-ignore await PostModel.get(postKey.name).populate(['user', 'publication'], ['email', 'privateVal']); - throw new Error('Shoud not get here.'); + throw new Error('Should not get here.'); } catch (err) { expect(err.message).equal('Only 1 property can be populated when fields to select are provided'); } @@ -263,6 +266,18 @@ describe('Model (Integration Tests)', () => { }); describe('findOne()', () => { + test('should return a correctly formed entityKey', async () => { + const { + email, + entityKey: { name: entityName }, + } = await addUser(); + + const user = await UserModel.findOne({ email }); + expect(user!.entityKey instanceof entity.Key).to.eq(true); + expect(user!.entityKey.name).to.eq(entityName); + expect(user!.privateVal).to.equal(null); + }); + test('should allow the `option.readAll`', async () => { const { email, privateVal } = await addUser(); @@ -299,7 +314,7 @@ describe('Model (Integration Tests)', () => { toUser: Entity & MyUser, amount: number, ): Promise { - return new Promise((resolve, reject): void => { + return new Promise((resolve, reject): void => { const transaction = gstore.transaction(); transaction .run() diff --git a/package.json b/package.json index 7eb8ad2..d28bd6c 100755 --- a/package.json +++ b/package.json @@ -51,6 +51,10 @@ { "name": "jfbenckhuijsen", "url": "https://github.com/jfbenckhuijsen" + }, + { + "name": "Hendrik J. Schalekamp", + "url": "https://github.com/carnun" } ], "license": "MIT",