Skip to content

Commit

Permalink
fix: merge irrelevant int tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chenshoo committed Jun 4, 2019
2 parents 9f4e0d0 + 0858c5e commit 531ae66
Show file tree
Hide file tree
Showing 11 changed files with 345 additions and 115 deletions.
321 changes: 221 additions & 100 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
"devDependencies": {
"@commitlint/cli": "^7.5.2",
"@commitlint/config-conventional": "^7.3.1",
"@enigmatis/mongo-driver": "^1.8.2",
"@enigmatis/mongo-driver": "^1.8.3",
"@semantic-release/changelog": "^3.0.2",
"@semantic-release/git": "^7.0.8",
"@types/cors": "^2.8.4",
Expand All @@ -107,7 +107,7 @@
"mongoose": "^5.5.1",
"prettier": "^1.17.0",
"rimraf": "^2.6.3",
"semantic-release": "^15.13.12",
"semantic-release": "^15.13.14",
"shx": "^0.3.2",
"travis-deploy-once": "^5.0.11",
"ts-jest": "^24.0.2",
Expand Down
8 changes: 6 additions & 2 deletions test/integration-tests/test-server/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export const url = `http://localhost:${polarisPropertiesPath.port}${

export const graphqlRequest = async (data: string, headers: any, variables: any) => {
const graphQLClient = new GraphQLClient(url, { headers });
const result = await graphQLClient.request(data, variables);
return result;
return graphQLClient.request(data, variables);
};

export const graphqlRawRequest = async (data: string, headers: any, variables: any = undefined) => {
const graphQLClient = new GraphQLClient(url, { headers });
return graphQLClient.rawRequest(data, variables);
};
4 changes: 2 additions & 2 deletions test/integration-tests/test-server/dal/book-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { getModelCreator, RepositoryModel } from '@enigmatis/mongo-driver';
import { Schema } from 'mongoose';

export interface Book extends RepositoryModel {
id: string;
testId: string;
title: string;
author: string;
otherBook: Book;
dataVersion: number;
}

export const bookSchema: Schema = new Schema({
id: String,
testId: String,
title: String,
author: String,
otherBook: Object,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RepositoryEntity } from '../../../../../src/main';

export interface Book extends RepositoryEntity {
testId: string;
id: string;
title: string;
author: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const updateBookResolver = async (
if (!Number.isInteger(realityId as any)) {
throw new UserInputError('please provide reality-id header as number');
} else {
return BookModelPerReality(context).update({ id: bookId }, update, { new: true });
return BookModelPerReality(context).update({ testId: bookId }, update, { new: true });
}
};
export const bookResolver = async (
Expand All @@ -53,7 +53,7 @@ export const bookByIdResolver = async (
throw new UserInputError('please provide reality-id header');
} else {
return BookModelPerReality(context)
.findOne({ id: bookId })
.findOne({ testId: bookId })
.lean();
}
};
Expand Down Expand Up @@ -98,7 +98,7 @@ export const deletedBookByIdResolver = async (
if (!Number.isInteger(realityId as any)) {
throw new UserInputError('please provide reality-id header');
} else {
return BookModelPerReality(context).find({ id: bookId, deleted: true });
return BookModelPerReality(context).find({ testId: bookId, deleted: true });
}
};

Expand All @@ -111,6 +111,6 @@ export const deleteBookResolver = async (
if (!Number.isInteger(realityId as any)) {
throw new UserInputError('please provide reality-id header as number');
} else {
return BookModelPerReality(context).deleteOne({ id: bookId });
return BookModelPerReality(context).deleteOne({ testId: bookId });
}
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const BookInput = `input BookInput {
id: ID!
id: ID,
testId: String!,
title: String,
author: String,
otherBook: BookInput
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const Book = `type Book implements CommonEntity {
id: ID!
testId: String,
id: ID!,
creationDate: Date,
lastUpdateDate: Date,
dataVersion: Int!,
Expand Down
102 changes: 102 additions & 0 deletions test/integration-tests/tests/irrelevant-entities.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { PolarisRequestHeaders } from '@enigmatis/utills';
import { graphqlRawRequest, graphqlRequest } from '../test-server/client';
import { BookModelPerReality } from '../test-server/dal/book-model';
import { finish, init } from '../test-server/run-test';
export const titles = ['first', 'second', 'third', 'fourth', 'fifth'];

const prepareDb = async (headers: PolarisRequestHeaders) => {
const books = [];
for (let i = 0; i < titles.length; i++) {
books.push({ author: 'arik', title: titles[i], testId: i, dataVersion: i + 1 });
}
await BookModelPerReality({ headers }).create(books);
};

const requestHeaders = { 'reality-id': 1, 'data-version': 1 };

beforeAll(async () => {
await init();
await prepareDb({ realityId: 1 });
});

afterAll(() => {
return finish();
});

describe('irrelevant entities tests', () => {
test('2 irrelevant entities not starting with f', async () => {
const queryBook = `{
booksStartsWith(startsWith:"f"){
id,
title
}
}`;
const { extensions }: any = await graphqlRawRequest(queryBook, requestHeaders);

expect(extensions.irrelevantEntities.booksStartsWith.length).toBe(2);
});

test('multiple queries entities not starting with f', async () => {
const queryBook = `{
a:booksStartsWith(startsWith:"f"){
id,
title
}
b:booksStartsWith(startsWith:"s"){
id,
title
}
}`;
const { extensions }: any = await graphqlRawRequest(queryBook, requestHeaders);

expect(extensions.irrelevantEntities.a.length).toBe(2);
expect(extensions.irrelevantEntities.b.length).toBe(3);
});
test('irrelevant id entities not in response', async () => {
const queryBook = `{
booksStartsWith(startsWith:"f"){
id,
title
}
}`;
const { data, extensions }: any = await graphqlRawRequest(queryBook, requestHeaders);
for (const entity of data.booksStartsWith) {
expect(extensions.irrelevantEntities.booksStartsWith.includes(entity.id)).toBeFalsy();
}
});

test('no irrelevant when no data version is sent', async () => {
const queryBook = `{
booksStartsWith(startsWith:"f"){
id,
title
}
}`;
const { extensions }: any = await graphqlRawRequest(queryBook, { 'reality-id': 1 });

expect(extensions).not.toBeDefined();
});

test('deleted entity is in irrelevant entities', async () => {
const queryBookStartsWith =
'query{\n' +
' booksStartsWith(startsWith:"f"){\n' +
' testId\n' +
' id\n' +
' }\n' +
'}';

const deleteBookMutation = `mutation deleteBook ($bookId:String!) {
deleteBook(bookId: $bookId){ title }}`;

const result: any = await graphqlRequest(queryBookStartsWith, requestHeaders, {});
const idToDelete = result.booksStartsWith[0].testId;
const expectedIrrelevantId = result.booksStartsWith[0].id;
await graphqlRequest(deleteBookMutation, { 'reality-id': 1 }, { bookId: idToDelete });
const { extensions }: any = await graphqlRawRequest(queryBookStartsWith, requestHeaders);

expect(
extensions.irrelevantEntities.booksStartsWith.includes(expectedIrrelevantId),
).toBeTruthy();
}, 300000);
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { graphqlRequest } from '../test-server/client';
import { finish, init } from '../test-server/run-test';

const headers = { 'reality-id': 1 };
const createBookMutation = `mutation createBook ($book:BookInput!) {createBook(book:$book){id}}`;
const createBookMutation = `mutation createBook ($book:BookInput!) {createBook(book:$book){testId}}`;
const updateBookMutation = `mutation updateBook ($bookId:String!, $update:UpdateBookInput) {
updateBook(bookId: $bookId, update: $update){ title }}`;

const findBookQuery = `query bookById ($bookId:String!) {bookById(bookId:$bookId){title}}`;
const deleteBookMutation = `mutation deleteBook ($bookId:String!) {
deleteBook(bookId: $bookId){ title }}`;
const defaultBookVariables = (title: string, id: string) => ({ author: 'chen', title, id });
const defaultBookVariables = (title: string, testId: string) => ({ author: 'chen', title, testId });

beforeEach(() => {
return init();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const titles = ['first', 'second', 'third', 'fourth', 'fifth'];
const prepareDb = async (headers: PolarisRequestHeaders) => {
const books = [];
for (let i = 0; i < titles.length; i++) {
books.push({ author: 'chen', title: titles[i], id: i });
books.push({ author: 'chen', title: titles[i], testId: i });
}
await BookModelPerReality({ headers }).create(books);
};
Expand Down

0 comments on commit 531ae66

Please sign in to comment.