Skip to content

Commit

Permalink
test(integration tests): add integration tests for fineOne and check …
Browse files Browse the repository at this point in the history
…entityKey construction (#246)
  • Loading branch information
carnun committed Feb 22, 2021
1 parent 605a848 commit b87a275
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
33 changes: 25 additions & 8 deletions __tests__/integration/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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);
});
});
29 changes: 22 additions & 7 deletions __tests__/integration/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -36,8 +37,8 @@ const cleanUp = (cb: any): Promise<any> => {
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
});
};

Expand Down Expand Up @@ -75,7 +76,9 @@ const addCompany = (): Promise<{ name: string; entityKey: EntityKey }> => {
});
};

const addUser = (company: EntityKey | null = null): Promise<any> => {
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();
Expand All @@ -95,7 +98,7 @@ const addUser = (company: EntityKey | null = null): Promise<any> => {
});
};

const addPost = (userKey = null, publicationKey = null): Promise<any> => {
const addPost = (userKey: EntityKey | null = null, publicationKey: EntityKey | null = null): Promise<any> => {
const title = randomName();
const post = new PostModel({ title, user: userKey, publication: publicationKey }, randomName());
return post.save().then(({ entityKey }) => {
Expand All @@ -104,7 +107,7 @@ const addPost = (userKey = null, publicationKey = null): Promise<any> => {
});
};

const addPublication = (userKey = null): Promise<any> => {
const addPublication = (userKey: EntityKey | null = null): Promise<any> => {
const title = randomName();
const publication = new PublicationModel({ title, user: userKey });
return publication.save().then(({ entityKey }) => {
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -299,7 +314,7 @@ describe('Model (Integration Tests)', () => {
toUser: Entity<MyUser> & MyUser,
amount: number,
): Promise<any> {
return new Promise((resolve, reject): void => {
return new Promise<void>((resolve, reject): void => {
const transaction = gstore.transaction();
transaction
.run()
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
{
"name": "jfbenckhuijsen",
"url": "https://github.com/jfbenckhuijsen"
},
{
"name": "Hendrik J. Schalekamp",
"url": "https://github.com/carnun"
}
],
"license": "MIT",
Expand Down

0 comments on commit b87a275

Please sign in to comment.