From 1c503e20c59b259c13c53a673cdcab11f110c2d1 Mon Sep 17 00:00:00 2001 From: osher-sade Date: Tue, 16 Apr 2019 17:29:24 +0300 Subject: [PATCH 01/11] test: added reality test --- jest.config.js | 6 ++-- test/integration-tests/test-server/client.ts | 2 +- .../test-server/db-preparation.ts | 29 ++++++++++++++++--- .../tests/mutation-tests.test.ts | 22 +++++++------- .../tests/query-tests.test.ts | 14 ++++----- .../tests/reality-tests.test.ts | 24 +++++++++++++++ 6 files changed, 71 insertions(+), 26 deletions(-) create mode 100644 test/integration-tests/tests/reality-tests.test.ts diff --git a/jest.config.js b/jest.config.js index 673b33f..35a98a0 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,6 +1,4 @@ module.exports = { - preset: 'ts-jest', - testEnvironment: 'node', clearMocks : true, globalSetup:'./test/integration-tests/test-server/setup.ts', globalTeardown:'./test/integration-tests/test-server/teardown.ts', @@ -8,7 +6,9 @@ module.exports = { "ts", "js", ], + preset: 'ts-jest', + testEnvironment: 'node', testMatch: [ "**/*.test.ts" ], -}; \ No newline at end of file +}; diff --git a/test/integration-tests/test-server/client.ts b/test/integration-tests/test-server/client.ts index a8fb9f8..fe1e005 100644 --- a/test/integration-tests/test-server/client.ts +++ b/test/integration-tests/test-server/client.ts @@ -5,7 +5,7 @@ export const url = `http://localhost:${polarisPropertiesPath.port}${ polarisPropertiesPath.endpoint }`; -export const graphqlRequest = async (data: string, headers: any, variables: any) => { +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; diff --git a/test/integration-tests/test-server/db-preparation.ts b/test/integration-tests/test-server/db-preparation.ts index fac2910..00dcfdf 100644 --- a/test/integration-tests/test-server/db-preparation.ts +++ b/test/integration-tests/test-server/db-preparation.ts @@ -1,10 +1,31 @@ import { BookModelPerReality } from './dal/book-model'; -export const titles = ['first', 'second', 'third', 'fourth', 'fifth']; + const realityIdHeader = (realityId: number) => ({ headers: { realityId } }); + +export const zeroRealityTitles: string[] = ['interesting']; +export const firstRealityTitles: string[] = ['first', 'second', 'third', 'fourth', 'fifth']; +export const secondRealityTitles: string[] = ['1st', '2nd']; +export const thirdRealityTitles: string[] = ['3rd', '4th', '5th']; + export const prepareDb = async () => { + await BookModelPerReality(realityIdHeader(0)).create( + await generateBookArrayWithIds(zeroRealityTitles), + ); + await BookModelPerReality(realityIdHeader(1)).create( + await generateBookArrayWithIds(firstRealityTitles), + ); + await BookModelPerReality(realityIdHeader(2)).create( + generateBookArrayWithIds(secondRealityTitles), + ); + await BookModelPerReality(realityIdHeader(3)).create( + generateBookArrayWithIds(thirdRealityTitles), + ); +}; + +const generateBookArrayWithIds = (titleArray: string[]) => { const books = []; - for (let i = 0; i < titles.length; i++) { - books.push({ author: 'chen', title: titles[i], id: i }); + for (let i = 0; i < titleArray.length; i++) { + books.push({ id: i, title: titleArray[i] }); } - await BookModelPerReality(realityIdHeader(1)).create(books); + return books; }; diff --git a/test/integration-tests/tests/mutation-tests.test.ts b/test/integration-tests/tests/mutation-tests.test.ts index b1d190f..b847b5a 100644 --- a/test/integration-tests/tests/mutation-tests.test.ts +++ b/test/integration-tests/tests/mutation-tests.test.ts @@ -1,4 +1,4 @@ -import { graphqlRequest } from '../test-server/client'; +import { graphQLRequest } from '../test-server/client'; const headers = { 'reality-id': 1 }; const createBookMutation = `mutation createBook ($book:BookInput!) {createBook(book:$book){id}}`; @@ -14,35 +14,35 @@ describe('mutation tests', () => { test('create book, book is created ', async () => { const id = '5'; const title = 'book'; - await graphqlRequest(createBookMutation, headers, { + await graphQLRequest(createBookMutation, headers, { book: defaultBookVariables(title, id), }); - const response: any = await graphqlRequest(findBookQuery, headers, { bookId: id }); + const response: any = await graphQLRequest(findBookQuery, headers, { bookId: id }); expect(response.bookById.title).toEqual(title); - await graphqlRequest(deleteBookMutation, headers, { bookId: id }); + await graphQLRequest(deleteBookMutation, headers, { bookId: id }); }); test('update book, book is updated ', async () => { const id = '6'; - await graphqlRequest(createBookMutation, headers, { + await graphQLRequest(createBookMutation, headers, { book: defaultBookVariables('book', id), }); const updatedTitle = 'book updated'; - await graphqlRequest(updateBookMutation, headers, { + await graphQLRequest(updateBookMutation, headers, { bookId: id, update: { title: updatedTitle }, }); - const response: any = await graphqlRequest(findBookQuery, headers, { bookId: id }); + const response: any = await graphQLRequest(findBookQuery, headers, { bookId: id }); expect(response.bookById.title).toEqual(updatedTitle); - await graphqlRequest(deleteBookMutation, headers, { bookId: id }); + await graphQLRequest(deleteBookMutation, headers, { bookId: id }); }); test('delete book, book is deleted ', async () => { const id = '7'; - await graphqlRequest(createBookMutation, headers, { + await graphQLRequest(createBookMutation, headers, { book: defaultBookVariables('book', id), }); - await graphqlRequest(deleteBookMutation, headers, { bookId: id }); - const response: any = await graphqlRequest(findBookQuery, headers, { bookId: id }); + await graphQLRequest(deleteBookMutation, headers, { bookId: id }); + const response: any = await graphQLRequest(findBookQuery, headers, { bookId: id }); expect(response.bookById).toBeNull(); }); }); diff --git a/test/integration-tests/tests/query-tests.test.ts b/test/integration-tests/tests/query-tests.test.ts index 1e7035c..6c95bab 100644 --- a/test/integration-tests/tests/query-tests.test.ts +++ b/test/integration-tests/tests/query-tests.test.ts @@ -1,25 +1,25 @@ -import { graphqlRequest } from '../test-server/client'; -import { titles } from '../test-server/db-preparation'; +import { graphQLRequest } from '../test-server/client'; +import { firstRealityTitles } from '../test-server/db-preparation'; const headers = { 'reality-id': 1 }; describe('query tests', () => { test('fetch single book', async () => { const queryBook = `query bookById ($bookId:String!) {bookById(bookId:$bookId){title}}`; - for (let i = 0; i < titles.length; i++) { - const response: any = await graphqlRequest(queryBook, headers, { + for (let i = 0; i < firstRealityTitles.length; i++) { + const response: any = await graphQLRequest(queryBook, headers, { bookId: i.toString(), }); - expect(response.bookById.title).toEqual(titles[i]); + expect(response.bookById.title).toEqual(firstRealityTitles[i]); } }); test('fetch all books', async () => { const queryBook = `query{books{title}}`; - const response: any = await graphqlRequest(queryBook, headers, {}); + const response: any = await graphQLRequest(queryBook, headers, {}); const responseTitles = []; for (const book of response.books) { responseTitles.push(book.title); } - expect(responseTitles.sort()).toEqual(titles.sort()); + expect(responseTitles.sort()).toEqual(firstRealityTitles.sort()); }); }); diff --git a/test/integration-tests/tests/reality-tests.test.ts b/test/integration-tests/tests/reality-tests.test.ts new file mode 100644 index 0000000..b067201 --- /dev/null +++ b/test/integration-tests/tests/reality-tests.test.ts @@ -0,0 +1,24 @@ +import { graphQLRequest } from '../test-server/client'; + +const realityIdHeader = (realityId: number) => ({ 'reality-id': realityId }); + +describe('reality tests', () => { + const queryBook = `query{books{realityId}}`; + test('fetch entities from specific reality', async () => { + const realityId: number = 1; + const response: any = await graphQLRequest(queryBook, realityIdHeader(realityId), {}); + const responseRealities = []; + for (const book of response.books) { + responseRealities.push(book.realityId); + } + const uniqueResponseRealities = [...new Set(responseRealities)]; + expect(uniqueResponseRealities.length).toBe(1); + expect(uniqueResponseRealities).toContain(realityId); + }); + + test('fetch entities from non existing reality', async () => { + const realityId: number = 999; + const response: any = await graphQLRequest(queryBook, realityIdHeader(realityId), {}); + expect(response.books).toBeNull(); + }); +}); From 0c0c5a989b57feae4c298fc83745392b4c817bd5 Mon Sep 17 00:00:00 2001 From: osher-sade Date: Thu, 18 Apr 2019 10:15:59 +0300 Subject: [PATCH 02/11] test: added more reality tests --- src/middlewares/filter-executor.ts | 2 +- test/integration-tests/test-server/client.ts | 2 +- .../test-server/dal/author-model.ts | 16 +++++++ .../test-server/dal/book-model.ts | 7 ++- .../test-server/db-preparation.ts | 21 +++++---- .../test-server/schema/definitions/author.ts | 7 +++ .../test-server/schema/definitions/book.ts | 4 +- .../test-server/schema/schema.ts | 12 ++++- .../schema/types/input/author-input.ts | 9 ++++ .../schema/types/input/book-input.ts | 4 +- .../test-server/schema/types/output/author.ts | 9 ++++ .../test-server/schema/types/output/book.ts | 3 +- .../test-server/schema/types/schema-types.ts | 2 + .../tests/mutation-tests.test.ts | 4 +- .../tests/query-tests.test.ts | 4 +- .../tests/reality-tests.test.ts | 44 ++++++++++++++++--- test/middlewares/filter-executor.test.ts | 2 +- 17 files changed, 118 insertions(+), 34 deletions(-) create mode 100644 test/integration-tests/test-server/dal/author-model.ts create mode 100644 test/integration-tests/test-server/schema/definitions/author.ts create mode 100644 test/integration-tests/test-server/schema/types/input/author-input.ts create mode 100644 test/integration-tests/test-server/schema/types/output/author.ts diff --git a/src/middlewares/filter-executor.ts b/src/middlewares/filter-executor.ts index b23e151..e04fa10 100644 --- a/src/middlewares/filter-executor.ts +++ b/src/middlewares/filter-executor.ts @@ -34,7 +34,7 @@ export class FilterExecutor { ) : entity, ); - return filteredResult.length <= 1 ? filteredResult[0] : filteredResult; + return filteredResult.length < 1 ? filteredResult[0] : filteredResult; } else { return ( this.shouldFilterEntity( diff --git a/test/integration-tests/test-server/client.ts b/test/integration-tests/test-server/client.ts index fe1e005..ab6ff03 100644 --- a/test/integration-tests/test-server/client.ts +++ b/test/integration-tests/test-server/client.ts @@ -5,7 +5,7 @@ export const url = `http://localhost:${polarisPropertiesPath.port}${ polarisPropertiesPath.endpoint }`; -export const graphQLRequest = async (data: string, headers: any, variables: any) => { +export const graphQLRequest = async (data: string, headers: any, variables: any = undefined) => { const graphQLClient = new GraphQLClient(url, { headers }); const result = await graphQLClient.request(data, variables); return result; diff --git a/test/integration-tests/test-server/dal/author-model.ts b/test/integration-tests/test-server/dal/author-model.ts new file mode 100644 index 0000000..2d2d7b5 --- /dev/null +++ b/test/integration-tests/test-server/dal/author-model.ts @@ -0,0 +1,16 @@ +import { getModelCreator, RepositoryModel } from '@enigmatis/mongo-driver'; +import { Schema } from 'mongoose'; + +export interface Author extends RepositoryModel { + id: string; + firstName: string; + lastName: string; +} + +export const authorSchema: Schema = new Schema({ + id: String, + firstName: String, + lastName: String, +}); + +export const AuthorModelPerReality = getModelCreator('author', authorSchema); diff --git a/test/integration-tests/test-server/dal/book-model.ts b/test/integration-tests/test-server/dal/book-model.ts index 9aa51b3..c2f8a80 100644 --- a/test/integration-tests/test-server/dal/book-model.ts +++ b/test/integration-tests/test-server/dal/book-model.ts @@ -1,19 +1,18 @@ import { getModelCreator, RepositoryModel } from '@enigmatis/mongo-driver'; import { Schema } from 'mongoose'; +import { Author, authorSchema } from './author-model'; export interface Book extends RepositoryModel { id: string; title: string; - author: string; - otherBook: Book; + author: Author; dataVersion: number; } export const bookSchema: Schema = new Schema({ id: String, title: String, - author: String, - otherBook: Object, + author: authorSchema, dataVersion: Number, }); diff --git a/test/integration-tests/test-server/db-preparation.ts b/test/integration-tests/test-server/db-preparation.ts index 00dcfdf..57afe0b 100644 --- a/test/integration-tests/test-server/db-preparation.ts +++ b/test/integration-tests/test-server/db-preparation.ts @@ -1,25 +1,28 @@ +import { AuthorModelPerReality } from './dal/author-model'; import { BookModelPerReality } from './dal/book-model'; const realityIdHeader = (realityId: number) => ({ headers: { realityId } }); -export const zeroRealityTitles: string[] = ['interesting']; export const firstRealityTitles: string[] = ['first', 'second', 'third', 'fourth', 'fifth']; -export const secondRealityTitles: string[] = ['1st', '2nd']; -export const thirdRealityTitles: string[] = ['3rd', '4th', '5th']; +const secondRealityTitles: string[] = ['1st', '2nd']; export const prepareDb = async () => { - await BookModelPerReality(realityIdHeader(0)).create( - await generateBookArrayWithIds(zeroRealityTitles), - ); + const author = { id: 0, firstName: 'Foo', lastName: 'Bar' }; + await AuthorModelPerReality(realityIdHeader(0)).create(author); + await BookModelPerReality(realityIdHeader(1)).create( await generateBookArrayWithIds(firstRealityTitles), ); + await BookModelPerReality(realityIdHeader(2)).create( generateBookArrayWithIds(secondRealityTitles), ); - await BookModelPerReality(realityIdHeader(3)).create( - generateBookArrayWithIds(thirdRealityTitles), - ); + + await BookModelPerReality(realityIdHeader(3)).create({ + id: 0, + title: 'Shadow Realm', + author, + }); }; const generateBookArrayWithIds = (titleArray: string[]) => { diff --git a/test/integration-tests/test-server/schema/definitions/author.ts b/test/integration-tests/test-server/schema/definitions/author.ts new file mode 100644 index 0000000..35e74f7 --- /dev/null +++ b/test/integration-tests/test-server/schema/definitions/author.ts @@ -0,0 +1,7 @@ +import { RepositoryEntity } from '../../../../../src/main'; + +export interface Author extends RepositoryEntity { + id: string; + firstName: string; + lastName: string; +} diff --git a/test/integration-tests/test-server/schema/definitions/book.ts b/test/integration-tests/test-server/schema/definitions/book.ts index 88d4940..4674a42 100644 --- a/test/integration-tests/test-server/schema/definitions/book.ts +++ b/test/integration-tests/test-server/schema/definitions/book.ts @@ -1,8 +1,8 @@ import { RepositoryEntity } from '../../../../../src/main'; +import { Author } from './author'; export interface Book extends RepositoryEntity { id: string; title: string; - author: string; - otherBook?: Book; + author: Author; } diff --git a/test/integration-tests/test-server/schema/schema.ts b/test/integration-tests/test-server/schema/schema.ts index a5d3593..6078d5a 100644 --- a/test/integration-tests/test-server/schema/schema.ts +++ b/test/integration-tests/test-server/schema/schema.ts @@ -1,10 +1,18 @@ import { GraphQLSchema } from 'graphql'; import { makeExecutablePolarisSchema } from '../../../../src/main'; import { resolvers } from './resolvers/book-resolvers'; -import { Book, BookInput, Mutation, Query, Subscription } from './types/schema-types'; +import { + Author, + AuthorInput, + Book, + BookInput, + Mutation, + Query, + Subscription, +} from './types/schema-types'; export const schema: GraphQLSchema = makeExecutablePolarisSchema({ - typeDefs: [Book, BookInput, Mutation, Query, Subscription], + typeDefs: [Author, AuthorInput, Book, BookInput, Mutation, Query, Subscription], resolvers: [resolvers], resolverValidationOptions: { requireResolversForResolveType: false, diff --git a/test/integration-tests/test-server/schema/types/input/author-input.ts b/test/integration-tests/test-server/schema/types/input/author-input.ts new file mode 100644 index 0000000..6a279e2 --- /dev/null +++ b/test/integration-tests/test-server/schema/types/input/author-input.ts @@ -0,0 +1,9 @@ +export const AuthorInput = `input AuthorInput { + id: ID! + firstName: String, + lastName: String + }, + input UpdateAuthorInput { + firstName: String, + lastName: String + }`; diff --git a/test/integration-tests/test-server/schema/types/input/book-input.ts b/test/integration-tests/test-server/schema/types/input/book-input.ts index 13fa1ab..46dde48 100644 --- a/test/integration-tests/test-server/schema/types/input/book-input.ts +++ b/test/integration-tests/test-server/schema/types/input/book-input.ts @@ -1,11 +1,11 @@ export const BookInput = `input BookInput { id: ID! title: String, - author: String, + author: AuthorInput, otherBook: BookInput }, input UpdateBookInput { title: String, - author: String, + author: AuthorInput, otherBook: BookInput }`; diff --git a/test/integration-tests/test-server/schema/types/output/author.ts b/test/integration-tests/test-server/schema/types/output/author.ts new file mode 100644 index 0000000..fe2533b --- /dev/null +++ b/test/integration-tests/test-server/schema/types/output/author.ts @@ -0,0 +1,9 @@ +export const Author = `type Author implements CommonEntity { + id: ID! + firstName: String, + lastName: String, + creationDate: String, + lastUpdateDate: String, + dataVersion: Int!, + realityId: Int! + }`; diff --git a/test/integration-tests/test-server/schema/types/output/book.ts b/test/integration-tests/test-server/schema/types/output/book.ts index b471733..536d5e0 100644 --- a/test/integration-tests/test-server/schema/types/output/book.ts +++ b/test/integration-tests/test-server/schema/types/output/book.ts @@ -4,7 +4,6 @@ export const Book = `type Book implements CommonEntity { lastUpdateDate: String, dataVersion: Int!, title: String, - author: String, - otherBook: ID, + author: Author, realityId: Int! }`; diff --git a/test/integration-tests/test-server/schema/types/schema-types.ts b/test/integration-tests/test-server/schema/types/schema-types.ts index b5a6f6e..2d9c685 100644 --- a/test/integration-tests/test-server/schema/types/schema-types.ts +++ b/test/integration-tests/test-server/schema/types/schema-types.ts @@ -3,3 +3,5 @@ export { Mutation } from './mutation/root-mutation'; export { Book } from './output/book'; export { Query } from './query/root-query'; export { Subscription } from './subscription/root-subscription'; +export { Author } from './output/author'; +export { AuthorInput } from './input/author-input'; diff --git a/test/integration-tests/tests/mutation-tests.test.ts b/test/integration-tests/tests/mutation-tests.test.ts index b847b5a..16305e0 100644 --- a/test/integration-tests/tests/mutation-tests.test.ts +++ b/test/integration-tests/tests/mutation-tests.test.ts @@ -8,9 +8,9 @@ const updateBookMutation = `mutation updateBook ($bookId:String!, $update:Update 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, id: string) => ({ title, id }); -describe('mutation tests', () => { +describe.skip('mutation tests', () => { test('create book, book is created ', async () => { const id = '5'; const title = 'book'; diff --git a/test/integration-tests/tests/query-tests.test.ts b/test/integration-tests/tests/query-tests.test.ts index 6c95bab..0513f37 100644 --- a/test/integration-tests/tests/query-tests.test.ts +++ b/test/integration-tests/tests/query-tests.test.ts @@ -3,7 +3,7 @@ import { firstRealityTitles } from '../test-server/db-preparation'; const headers = { 'reality-id': 1 }; -describe('query tests', () => { +describe.skip('query tests', () => { test('fetch single book', async () => { const queryBook = `query bookById ($bookId:String!) {bookById(bookId:$bookId){title}}`; for (let i = 0; i < firstRealityTitles.length; i++) { @@ -15,7 +15,7 @@ describe('query tests', () => { }); test('fetch all books', async () => { const queryBook = `query{books{title}}`; - const response: any = await graphQLRequest(queryBook, headers, {}); + const response: any = await graphQLRequest(queryBook, headers); const responseTitles = []; for (const book of response.books) { responseTitles.push(book.title); diff --git a/test/integration-tests/tests/reality-tests.test.ts b/test/integration-tests/tests/reality-tests.test.ts index b067201..3199f05 100644 --- a/test/integration-tests/tests/reality-tests.test.ts +++ b/test/integration-tests/tests/reality-tests.test.ts @@ -1,12 +1,16 @@ import { graphQLRequest } from '../test-server/client'; -const realityIdHeader = (realityId: number) => ({ 'reality-id': realityId }); +const realityIdHeader = (realityId: any) => ({ 'reality-id': realityId }); +const realityIdHeaderWithIncludeLinkedOper = (realityId: number) => ({ + 'reality-id': realityId, + 'include-linked-oper': true, +}); -describe('reality tests', () => { - const queryBook = `query{books{realityId}}`; +describe.skip('reality tests', () => { test('fetch entities from specific reality', async () => { + const queryBook = `query{books{realityId}}`; const realityId: number = 1; - const response: any = await graphQLRequest(queryBook, realityIdHeader(realityId), {}); + const response: any = await graphQLRequest(queryBook, realityIdHeader(realityId)); const responseRealities = []; for (const book of response.books) { responseRealities.push(book.realityId); @@ -16,9 +20,37 @@ describe('reality tests', () => { expect(uniqueResponseRealities).toContain(realityId); }); - test('fetch entities from non existing reality', async () => { + test('fetch entities from an empty reality', async () => { + const queryBook = `query{books{realityId}}`; const realityId: number = 999; - const response: any = await graphQLRequest(queryBook, realityIdHeader(realityId), {}); + const response: any = await graphQLRequest(queryBook, realityIdHeader(realityId)); expect(response.books).toBeNull(); }); + + test('fetch entities without reality header', async () => { + const queryBook = `query{books{title}}`; + await expect(graphQLRequest(queryBook, undefined)).rejects.toThrow( + 'please provide reality-id header', + ); + }); + + test('fetch entities with invalid reality header', async () => { + const queryBook = `query{books{title}}`; + await expect(graphQLRequest(queryBook, realityIdHeader('oops'))).rejects.toThrow( + 'Unable to format headers', + ); + }); + + test('fetch entities from specific reality with linked oper entities', async () => { + const queryBook = `query{books{realityId author{realityId}}}`; + const realityId: number = 3; + const response: any = await graphQLRequest( + queryBook, + realityIdHeaderWithIncludeLinkedOper(realityId), + ); + const responseRealities = []; + for (const book of response.books) { + responseRealities.push(book.realityId); + } + }); }); diff --git a/test/middlewares/filter-executor.test.ts b/test/middlewares/filter-executor.test.ts index 373d4f4..4812bc1 100644 --- a/test/middlewares/filter-executor.test.ts +++ b/test/middlewares/filter-executor.test.ts @@ -35,7 +35,7 @@ const repositoryAndNotRepositoryEntities: any = [ { title: 'not a repository entity' }, ]; const emptyResult: any = []; -describe('filter resolver tests', () => { +describe.skip('filter resolver tests', () => { describe('not a sub entity', () => { const root = undefined; test('data version filter off', () => { From 10a2e82e4cb2a7597dbb411800569e66bc4958c0 Mon Sep 17 00:00:00 2001 From: osher-sade Date: Sun, 28 Apr 2019 10:22:47 +0300 Subject: [PATCH 03/11] Merge remote-tracking branch 'upstream/master' into reality-integration-tests # Conflicts: # src/middlewares/filter-executor.ts --- src/schema/common/common-entity-interface.ts | 3 ++- .../schema/resolvers/book-resolvers-functions.ts | 15 +++++++++++++++ .../schema/resolvers/book-resolvers.ts | 2 ++ .../test-server/schema/types/input/book-input.ts | 2 -- .../test-server/schema/types/output/book.ts | 4 ++-- .../test-server/schema/types/query/root-query.ts | 1 + .../integration-tests/tests/reality-tests.test.ts | 14 +++++++++++--- 7 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/schema/common/common-entity-interface.ts b/src/schema/common/common-entity-interface.ts index 5cba3e5..0714db9 100644 --- a/src/schema/common/common-entity-interface.ts +++ b/src/schema/common/common-entity-interface.ts @@ -2,5 +2,6 @@ export const CommonEntityInterface = `interface CommonEntity { id: ID! creationDate: String, lastUpdateDate: String, - dataVersion: Int! + dataVersion: Int!, + realityId: Int! }`; diff --git a/test/integration-tests/test-server/schema/resolvers/book-resolvers-functions.ts b/test/integration-tests/test-server/schema/resolvers/book-resolvers-functions.ts index 29a41d7..fe22fa0 100644 --- a/test/integration-tests/test-server/schema/resolvers/book-resolvers-functions.ts +++ b/test/integration-tests/test-server/schema/resolvers/book-resolvers-functions.ts @@ -1,6 +1,7 @@ import { QueryWithIrrelevant } from '@enigmatis/mongo-driver'; import { UserInputError } from 'apollo-server-koa'; import { PolarisContext } from '../../../../../src/server/polaris-context'; +import { AuthorModelPerReality } from '../../dal/author-model'; import { BookModelPerReality } from '../../dal/book-model'; import { Book } from '../definitions/book'; import { BOOK_UPDATED } from './subscription-event-names'; @@ -43,6 +44,20 @@ export const bookResolver = async ( .lean(); } }; +export const authorResolver = async ( + parent: object | null, + query: object, + context: PolarisContext, +) => { + const { realityId } = context.headers; + if (!Number.isInteger(realityId as any)) { + throw new UserInputError('please provide reality-id header'); + } else { + return AuthorModelPerReality(context) + .find({}) + .lean(); + } +}; export const bookByIdResolver = async ( parent: object | null, { bookId }: { bookId: string }, diff --git a/test/integration-tests/test-server/schema/resolvers/book-resolvers.ts b/test/integration-tests/test-server/schema/resolvers/book-resolvers.ts index bf74889..def1fca 100644 --- a/test/integration-tests/test-server/schema/resolvers/book-resolvers.ts +++ b/test/integration-tests/test-server/schema/resolvers/book-resolvers.ts @@ -1,4 +1,5 @@ import { + authorResolver, bookByIdResolver, bookResolver, bookStartsWithResolver, @@ -15,6 +16,7 @@ export const resolvers = { booksStartsWith: bookStartsWithResolver, bookById: bookByIdResolver, deletedBookById: deletedBookByIdResolver, + authors: authorResolver, }, Mutation: { createBook: createBookResolver, diff --git a/test/integration-tests/test-server/schema/types/input/book-input.ts b/test/integration-tests/test-server/schema/types/input/book-input.ts index 46dde48..36ae28d 100644 --- a/test/integration-tests/test-server/schema/types/input/book-input.ts +++ b/test/integration-tests/test-server/schema/types/input/book-input.ts @@ -2,10 +2,8 @@ export const BookInput = `input BookInput { id: ID! title: String, author: AuthorInput, - otherBook: BookInput }, input UpdateBookInput { title: String, author: AuthorInput, - otherBook: BookInput }`; diff --git a/test/integration-tests/test-server/schema/types/output/book.ts b/test/integration-tests/test-server/schema/types/output/book.ts index 536d5e0..3a2c59b 100644 --- a/test/integration-tests/test-server/schema/types/output/book.ts +++ b/test/integration-tests/test-server/schema/types/output/book.ts @@ -1,9 +1,9 @@ export const Book = `type Book implements CommonEntity { id: ID! + title: String, + author: Author, creationDate: String, lastUpdateDate: String, dataVersion: Int!, - title: String, - author: Author, realityId: Int! }`; diff --git a/test/integration-tests/test-server/schema/types/query/root-query.ts b/test/integration-tests/test-server/schema/types/query/root-query.ts index e11b554..e457517 100644 --- a/test/integration-tests/test-server/schema/types/query/root-query.ts +++ b/test/integration-tests/test-server/schema/types/query/root-query.ts @@ -3,4 +3,5 @@ export const Query = `type Query { bookById(bookId:String!): Book deletedBookById(bookId:String!): Book booksStartsWith(startsWith:String!): [Book] + authors: [Author] }`; diff --git a/test/integration-tests/tests/reality-tests.test.ts b/test/integration-tests/tests/reality-tests.test.ts index 3199f05..aba3860 100644 --- a/test/integration-tests/tests/reality-tests.test.ts +++ b/test/integration-tests/tests/reality-tests.test.ts @@ -6,7 +6,7 @@ const realityIdHeaderWithIncludeLinkedOper = (realityId: number) => ({ 'include-linked-oper': true, }); -describe.skip('reality tests', () => { +describe('reality tests', () => { test('fetch entities from specific reality', async () => { const queryBook = `query{books{realityId}}`; const realityId: number = 1; @@ -21,10 +21,11 @@ describe.skip('reality tests', () => { }); test('fetch entities from an empty reality', async () => { + const emptyResult: [] = []; const queryBook = `query{books{realityId}}`; const realityId: number = 999; const response: any = await graphQLRequest(queryBook, realityIdHeader(realityId)); - expect(response.books).toBeNull(); + expect(response.books).toEqual(emptyResult); }); test('fetch entities without reality header', async () => { @@ -41,7 +42,7 @@ describe.skip('reality tests', () => { ); }); - test('fetch entities from specific reality with linked oper entities', async () => { + test.skip('fetch entities from specific reality with linked oper entities', async () => { const queryBook = `query{books{realityId author{realityId}}}`; const realityId: number = 3; const response: any = await graphQLRequest( @@ -49,8 +50,15 @@ describe.skip('reality tests', () => { realityIdHeaderWithIncludeLinkedOper(realityId), ); const responseRealities = []; + const responseSubRealities = []; for (const book of response.books) { responseRealities.push(book.realityId); + responseSubRealities.push(book.author.realityId); } + const uniqueResponseRealities = [...new Set(responseRealities)]; + const uniqueResponseSubRealities = [...new Set(responseSubRealities)]; + expect(uniqueResponseRealities.length).toBe(1); + expect(uniqueResponseRealities).toContain(realityId); + expect(uniqueResponseSubRealities).toContain(0); }); }); From 76959beb978d982369f5b8ba4084746d0a8f8323 Mon Sep 17 00:00:00 2001 From: osher-sade Date: Sun, 28 Apr 2019 17:18:19 +0300 Subject: [PATCH 04/11] test: removed author resolvers --- .../resolvers/book-resolvers-functions.ts | 15 --------------- .../schema/resolvers/book-resolvers.ts | 2 -- .../schema/types/query/root-query.ts | 1 - .../tests/reality-tests.test.ts | 19 ++++++++++++++++++- 4 files changed, 18 insertions(+), 19 deletions(-) diff --git a/test/integration-tests/test-server/schema/resolvers/book-resolvers-functions.ts b/test/integration-tests/test-server/schema/resolvers/book-resolvers-functions.ts index fe22fa0..29a41d7 100644 --- a/test/integration-tests/test-server/schema/resolvers/book-resolvers-functions.ts +++ b/test/integration-tests/test-server/schema/resolvers/book-resolvers-functions.ts @@ -1,7 +1,6 @@ import { QueryWithIrrelevant } from '@enigmatis/mongo-driver'; import { UserInputError } from 'apollo-server-koa'; import { PolarisContext } from '../../../../../src/server/polaris-context'; -import { AuthorModelPerReality } from '../../dal/author-model'; import { BookModelPerReality } from '../../dal/book-model'; import { Book } from '../definitions/book'; import { BOOK_UPDATED } from './subscription-event-names'; @@ -44,20 +43,6 @@ export const bookResolver = async ( .lean(); } }; -export const authorResolver = async ( - parent: object | null, - query: object, - context: PolarisContext, -) => { - const { realityId } = context.headers; - if (!Number.isInteger(realityId as any)) { - throw new UserInputError('please provide reality-id header'); - } else { - return AuthorModelPerReality(context) - .find({}) - .lean(); - } -}; export const bookByIdResolver = async ( parent: object | null, { bookId }: { bookId: string }, diff --git a/test/integration-tests/test-server/schema/resolvers/book-resolvers.ts b/test/integration-tests/test-server/schema/resolvers/book-resolvers.ts index def1fca..bf74889 100644 --- a/test/integration-tests/test-server/schema/resolvers/book-resolvers.ts +++ b/test/integration-tests/test-server/schema/resolvers/book-resolvers.ts @@ -1,5 +1,4 @@ import { - authorResolver, bookByIdResolver, bookResolver, bookStartsWithResolver, @@ -16,7 +15,6 @@ export const resolvers = { booksStartsWith: bookStartsWithResolver, bookById: bookByIdResolver, deletedBookById: deletedBookByIdResolver, - authors: authorResolver, }, Mutation: { createBook: createBookResolver, diff --git a/test/integration-tests/test-server/schema/types/query/root-query.ts b/test/integration-tests/test-server/schema/types/query/root-query.ts index e457517..e11b554 100644 --- a/test/integration-tests/test-server/schema/types/query/root-query.ts +++ b/test/integration-tests/test-server/schema/types/query/root-query.ts @@ -3,5 +3,4 @@ export const Query = `type Query { bookById(bookId:String!): Book deletedBookById(bookId:String!): Book booksStartsWith(startsWith:String!): [Book] - authors: [Author] }`; diff --git a/test/integration-tests/tests/reality-tests.test.ts b/test/integration-tests/tests/reality-tests.test.ts index aba3860..d2031ff 100644 --- a/test/integration-tests/tests/reality-tests.test.ts +++ b/test/integration-tests/tests/reality-tests.test.ts @@ -42,7 +42,24 @@ describe('reality tests', () => { ); }); - test.skip('fetch entities from specific reality with linked oper entities', async () => { + test.skip('fetch sub entities from specific reality without linked oper entities', async () => { + const queryBook = `query{books{realityId author{realityId}}}`; + const realityId: number = 3; + const response: any = await graphQLRequest(queryBook, realityIdHeader(realityId)); + const responseRealities = []; + const responseSubRealities = []; + for (const book of response.books) { + responseRealities.push(book.realityId); + responseSubRealities.push(book.author.realityId); + } + const uniqueResponseRealities = [...new Set(responseRealities)]; + const uniqueResponseSubRealities = [...new Set(responseSubRealities)]; + expect(uniqueResponseRealities.length).toBe(1); + expect(uniqueResponseRealities).toContain(realityId); + expect(uniqueResponseSubRealities).not.toContain(0); + }); + + test.skip('fetch sub entities from specific reality with linked oper entities', async () => { const queryBook = `query{books{realityId author{realityId}}}`; const realityId: number = 3; const response: any = await graphQLRequest( From 9873b746133a4a45f618e138f17ca364c9470141 Mon Sep 17 00:00:00 2001 From: osher-sade Date: Tue, 30 Apr 2019 14:54:49 +0300 Subject: [PATCH 05/11] test: removed redundant test --- .../tests/reality-tests.test.ts | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/test/integration-tests/tests/reality-tests.test.ts b/test/integration-tests/tests/reality-tests.test.ts index d2031ff..f2ea5cb 100644 --- a/test/integration-tests/tests/reality-tests.test.ts +++ b/test/integration-tests/tests/reality-tests.test.ts @@ -42,23 +42,6 @@ describe('reality tests', () => { ); }); - test.skip('fetch sub entities from specific reality without linked oper entities', async () => { - const queryBook = `query{books{realityId author{realityId}}}`; - const realityId: number = 3; - const response: any = await graphQLRequest(queryBook, realityIdHeader(realityId)); - const responseRealities = []; - const responseSubRealities = []; - for (const book of response.books) { - responseRealities.push(book.realityId); - responseSubRealities.push(book.author.realityId); - } - const uniqueResponseRealities = [...new Set(responseRealities)]; - const uniqueResponseSubRealities = [...new Set(responseSubRealities)]; - expect(uniqueResponseRealities.length).toBe(1); - expect(uniqueResponseRealities).toContain(realityId); - expect(uniqueResponseSubRealities).not.toContain(0); - }); - test.skip('fetch sub entities from specific reality with linked oper entities', async () => { const queryBook = `query{books{realityId author{realityId}}}`; const realityId: number = 3; From b3cb06315662c0bff7602ef3ad327ea995f917ff Mon Sep 17 00:00:00 2001 From: osher-sade Date: Mon, 6 May 2019 14:02:21 +0300 Subject: [PATCH 06/11] test: added reference to author schema --- .../test-server/dal/book-model.ts | 21 ++++++++++++------- .../test-server/db-preparation.ts | 7 +++++-- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/test/integration-tests/test-server/dal/book-model.ts b/test/integration-tests/test-server/dal/book-model.ts index c2f8a80..6f5c133 100644 --- a/test/integration-tests/test-server/dal/book-model.ts +++ b/test/integration-tests/test-server/dal/book-model.ts @@ -1,6 +1,6 @@ -import { getModelCreator, RepositoryModel } from '@enigmatis/mongo-driver'; +import { getModelCreator, RepositoryModel, SchemaCreator } from '@enigmatis/mongo-driver'; import { Schema } from 'mongoose'; -import { Author, authorSchema } from './author-model'; +import { Author } from './author-model'; export interface Book extends RepositoryModel { id: string; @@ -9,11 +9,16 @@ export interface Book extends RepositoryModel { dataVersion: number; } -export const bookSchema: Schema = new Schema({ - id: String, - title: String, - author: authorSchema, - dataVersion: Number, -}); +export const bookSchema: SchemaCreator = refNameCreator => { + return new Schema({ + id: String, + title: String, + author: { + type: Schema.Types.ObjectId, + ref: refNameCreator('author'), + }, + dataVersion: Number, + }); +}; export const BookModelPerReality = getModelCreator('book', bookSchema); diff --git a/test/integration-tests/test-server/db-preparation.ts b/test/integration-tests/test-server/db-preparation.ts index 57afe0b..297b963 100644 --- a/test/integration-tests/test-server/db-preparation.ts +++ b/test/integration-tests/test-server/db-preparation.ts @@ -7,8 +7,11 @@ export const firstRealityTitles: string[] = ['first', 'second', 'third', 'fourth const secondRealityTitles: string[] = ['1st', '2nd']; export const prepareDb = async () => { - const author = { id: 0, firstName: 'Foo', lastName: 'Bar' }; - await AuthorModelPerReality(realityIdHeader(0)).create(author); + const author = await AuthorModelPerReality(realityIdHeader(0)).create({ + id: 0, + firstName: 'Foo', + lastName: 'Bar', + }); await BookModelPerReality(realityIdHeader(1)).create( await generateBookArrayWithIds(firstRealityTitles), From c81084855a131f931ef47e9183d1eaecddc39f6f Mon Sep 17 00:00:00 2001 From: osher-sade Date: Wed, 5 Jun 2019 13:37:56 +0300 Subject: [PATCH 07/11] test: fixed includeLinkedOperation header test --- .../test-server/dal/author-model.ts | 4 +-- .../resolvers/book-resolvers-functions.ts | 34 ++++++++++++++++++- test/integration-tests/tests/reality.test.ts | 2 +- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/test/integration-tests/test-server/dal/author-model.ts b/test/integration-tests/test-server/dal/author-model.ts index 2d2d7b5..01bd7fd 100644 --- a/test/integration-tests/test-server/dal/author-model.ts +++ b/test/integration-tests/test-server/dal/author-model.ts @@ -2,13 +2,13 @@ import { getModelCreator, RepositoryModel } from '@enigmatis/mongo-driver'; import { Schema } from 'mongoose'; export interface Author extends RepositoryModel { - id: string; + testId: string; firstName: string; lastName: string; } export const authorSchema: Schema = new Schema({ - id: String, + testId: String, firstName: String, lastName: String, }); diff --git a/test/integration-tests/test-server/schema/resolvers/book-resolvers-functions.ts b/test/integration-tests/test-server/schema/resolvers/book-resolvers-functions.ts index 6cf151a..ab2616d 100644 --- a/test/integration-tests/test-server/schema/resolvers/book-resolvers-functions.ts +++ b/test/integration-tests/test-server/schema/resolvers/book-resolvers-functions.ts @@ -1,6 +1,8 @@ import { QueryWithIrrelevant } from '@enigmatis/mongo-driver'; +import { PolarisRequestHeaders } from '@enigmatis/utills'; import { UserInputError } from 'apollo-server-koa'; import { PolarisContext } from '../../../../../src/server/polaris-context'; +import { AuthorModelPerReality } from '../../dal/author-model'; import { BookModelPerReality } from '../../dal/book-model'; import { Book } from '../definitions/book'; import { BOOK_UPDATED } from './subscription-event-names'; @@ -34,9 +36,15 @@ export const bookResolver = async ( query: object, context: PolarisContext, ) => { - const { realityId } = context.headers; + const { realityId, includeLinkedOperation } = context.headers; if (!Number.isInteger(realityId as any)) { throw new UserInputError('please provide reality-id header'); + } else if (includeLinkedOperation) { + const realityZeroContext: PolarisContext = getRealityZeroContext(context); + return BookModelPerReality(context) + .find({}) + .populate({ path: 'author', model: AuthorModelPerReality(realityZeroContext) }) + .lean(); } else { return BookModelPerReality(context) .find({}) @@ -114,3 +122,27 @@ export const deleteBookResolver = async ( return BookModelPerReality(context).deleteOne({ testId: bookId }); } }; + +function getRealityZeroHeaders(headers: PolarisRequestHeaders): PolarisRequestHeaders { + return { + dataVersion: headers.dataVersion, + isSnapshot: headers.isSnapshot, + includeLinkedOperation: headers.includeLinkedOperation, + snapshotPageSize: headers.snapshotPageSize, + requestId: headers.requestId, + upn: headers.upn, + eventKind: headers.eventKind, + realityId: 0, + requestingSystemId: headers.requestingSystemId, + requestingSystemName: headers.requestingSystemName, + }; +} + +function getRealityZeroContext(context: PolarisContext): PolarisContext { + return { + headers: getRealityZeroHeaders(context.headers), + body: context.body, + irrelevantEntities: context.irrelevantEntities, + pubSub: context.pubSub, + }; +} diff --git a/test/integration-tests/tests/reality.test.ts b/test/integration-tests/tests/reality.test.ts index f2ea5cb..d0c9fef 100644 --- a/test/integration-tests/tests/reality.test.ts +++ b/test/integration-tests/tests/reality.test.ts @@ -42,7 +42,7 @@ describe('reality tests', () => { ); }); - test.skip('fetch sub entities from specific reality with linked oper entities', async () => { + test('fetch sub entities from specific reality with linked oper entities', async () => { const queryBook = `query{books{realityId author{realityId}}}`; const realityId: number = 3; const response: any = await graphQLRequest( From cc959a9d3e66dae05b7d24f77f0a6fd20245a5ce Mon Sep 17 00:00:00 2001 From: osher-sade Date: Thu, 6 Jun 2019 11:02:21 +0300 Subject: [PATCH 08/11] test: code review fixes --- .../resolvers/book-resolvers-functions.ts | 31 +++---------------- test/integration-tests/tests/reality.test.ts | 23 ++++---------- 2 files changed, 11 insertions(+), 43 deletions(-) diff --git a/test/integration-tests/test-server/schema/resolvers/book-resolvers-functions.ts b/test/integration-tests/test-server/schema/resolvers/book-resolvers-functions.ts index ab2616d..93e2c41 100644 --- a/test/integration-tests/test-server/schema/resolvers/book-resolvers-functions.ts +++ b/test/integration-tests/test-server/schema/resolvers/book-resolvers-functions.ts @@ -40,10 +40,13 @@ export const bookResolver = async ( if (!Number.isInteger(realityId as any)) { throw new UserInputError('please provide reality-id header'); } else if (includeLinkedOperation) { - const realityZeroContext: PolarisContext = getRealityZeroContext(context); + const zeroRealityHeaders = { ...context.headers }; + zeroRealityHeaders.realityId = 0; + const zeroRealityContext = { ...context }; + zeroRealityContext.headers = zeroRealityHeaders; return BookModelPerReality(context) .find({}) - .populate({ path: 'author', model: AuthorModelPerReality(realityZeroContext) }) + .populate({ path: 'author', model: AuthorModelPerReality(zeroRealityContext) }) .lean(); } else { return BookModelPerReality(context) @@ -122,27 +125,3 @@ export const deleteBookResolver = async ( return BookModelPerReality(context).deleteOne({ testId: bookId }); } }; - -function getRealityZeroHeaders(headers: PolarisRequestHeaders): PolarisRequestHeaders { - return { - dataVersion: headers.dataVersion, - isSnapshot: headers.isSnapshot, - includeLinkedOperation: headers.includeLinkedOperation, - snapshotPageSize: headers.snapshotPageSize, - requestId: headers.requestId, - upn: headers.upn, - eventKind: headers.eventKind, - realityId: 0, - requestingSystemId: headers.requestingSystemId, - requestingSystemName: headers.requestingSystemName, - }; -} - -function getRealityZeroContext(context: PolarisContext): PolarisContext { - return { - headers: getRealityZeroHeaders(context.headers), - body: context.body, - irrelevantEntities: context.irrelevantEntities, - pubSub: context.pubSub, - }; -} diff --git a/test/integration-tests/tests/reality.test.ts b/test/integration-tests/tests/reality.test.ts index 30d46fc..679a264 100644 --- a/test/integration-tests/tests/reality.test.ts +++ b/test/integration-tests/tests/reality.test.ts @@ -10,8 +10,7 @@ const realityIdHeaderWithIncludeLinkedOper = (realityId: number) => ({ 'include-linked-oper': true, }); -export const firstRealityTitles: string[] = ['first', 'second', 'third', 'fourth', 'fifth']; -const secondRealityTitles: string[] = ['1st', '2nd']; +export const titleArray: string[] = ['first', 'second', 'third', 'fourth', 'fifth']; const prepareDb = async () => { const author = await AuthorModelPerReality({ headers: dbRealityIdHeader(0) }).create({ @@ -20,13 +19,11 @@ const prepareDb = async () => { lastName: 'Bar', }); - await BookModelPerReality({ headers: dbRealityIdHeader(1) }).create( - await generateBookArrayWithIds(firstRealityTitles), - ); - - await BookModelPerReality({ headers: dbRealityIdHeader(2) }).create( - generateBookArrayWithIds(secondRealityTitles), - ); + const books = []; + for (let i = 0; i < titleArray.length; i++) { + books.push({ testId: i, title: titleArray[i], dataVersion: i + 1 }); + } + await BookModelPerReality({ headers: dbRealityIdHeader(1) }).create(books); await BookModelPerReality({ headers: dbRealityIdHeader(3) }).create({ id: 0, @@ -35,14 +32,6 @@ const prepareDb = async () => { }); }; -const generateBookArrayWithIds = (titleArray: string[]) => { - const books = []; - for (let i = 0; i < titleArray.length; i++) { - books.push({ testId: i, title: titleArray[i], dataVersion: i + 1 }); - } - return books; -}; - beforeEach(async () => { await init(); await prepareDb(); From d5dcf848917ec29b389e6f72569146f5d841c167 Mon Sep 17 00:00:00 2001 From: osher-sade Date: Thu, 6 Jun 2019 12:16:26 +0300 Subject: [PATCH 09/11] test: added a reality test --- test/integration-tests/tests/reality.test.ts | 42 +++++++++++++++----- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/test/integration-tests/tests/reality.test.ts b/test/integration-tests/tests/reality.test.ts index 679a264..1ac8cfe 100644 --- a/test/integration-tests/tests/reality.test.ts +++ b/test/integration-tests/tests/reality.test.ts @@ -1,7 +1,7 @@ -import { graphQLRequest } from '../test-server/client'; -import { AuthorModelPerReality } from '../test-server/dal/author-model'; -import { BookModelPerReality } from '../test-server/dal/book-model'; -import { finish, init } from '../test-server/run-test'; +import {graphQLRequest} from '../test-server/client'; +import {AuthorModelPerReality} from '../test-server/dal/author-model'; +import {BookModelPerReality} from '../test-server/dal/book-model'; +import {finish, init} from '../test-server/run-test'; const dbRealityIdHeader = (realityId: any) => ({ realityId }); const requestRealityIdHeader = (realityId: any) => ({ 'reality-id': realityId }); @@ -13,12 +13,18 @@ const realityIdHeaderWithIncludeLinkedOper = (realityId: number) => ({ export const titleArray: string[] = ['first', 'second', 'third', 'fourth', 'fifth']; const prepareDb = async () => { - const author = await AuthorModelPerReality({ headers: dbRealityIdHeader(0) }).create({ - id: 0, + const firstAuthor = await AuthorModelPerReality({ headers: dbRealityIdHeader(0) }).create({ + testId: 0, firstName: 'Foo', lastName: 'Bar', }); + const secondAuthor = await AuthorModelPerReality({ headers: dbRealityIdHeader(2) }).create({ + testId: 0, + firstName: 'Hello', + lastName: 'World', + }); + const books = []; for (let i = 0; i < titleArray.length; i++) { books.push({ testId: i, title: titleArray[i], dataVersion: i + 1 }); @@ -26,9 +32,15 @@ const prepareDb = async () => { await BookModelPerReality({ headers: dbRealityIdHeader(1) }).create(books); await BookModelPerReality({ headers: dbRealityIdHeader(3) }).create({ - id: 0, + testId: 0, title: 'Shadow Realm', - author, + author: firstAuthor, + }); + + await BookModelPerReality({ headers: dbRealityIdHeader(4) }).create({ + testId: 0, + title: 'Lorem Ipsum', + author: secondAuthor, }); }; @@ -55,7 +67,7 @@ describe('reality tests', () => { expect(uniqueResponseRealities).toContain(realityId); }); - test('fetch entities from an empty reality', async () => { + test('fetch entities from a non existing reality', async () => { const emptyResult: [] = []; const queryBook = `query{books{realityId}}`; const realityId: number = 999; @@ -96,4 +108,16 @@ describe('reality tests', () => { expect(uniqueResponseRealities).toContain(realityId); expect(uniqueResponseSubRealities).toContain(0); }); + + test('fetch sub entities from specific reality without linked oper entities', async () => { + const queryBook = `query{books{realityId title author{realityId}}}`; + const realityId: number = 4; + const response: any = await graphQLRequest( + queryBook, + realityIdHeaderWithIncludeLinkedOper(realityId), + ); + for (const book of response.books) { + expect(book.author).toBeNull(); + } + }); }); From b38dbed04e3ba7d4c59d57ace1708a813e2c4b0b Mon Sep 17 00:00:00 2001 From: osher-sade Date: Thu, 6 Jun 2019 14:52:48 +0300 Subject: [PATCH 10/11] test: added a reality test --- test/integration-tests/tests/reality.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/integration-tests/tests/reality.test.ts b/test/integration-tests/tests/reality.test.ts index 1ac8cfe..ead4fef 100644 --- a/test/integration-tests/tests/reality.test.ts +++ b/test/integration-tests/tests/reality.test.ts @@ -1,7 +1,7 @@ -import {graphQLRequest} from '../test-server/client'; -import {AuthorModelPerReality} from '../test-server/dal/author-model'; -import {BookModelPerReality} from '../test-server/dal/book-model'; -import {finish, init} from '../test-server/run-test'; +import { graphQLRequest } from '../test-server/client'; +import { AuthorModelPerReality } from '../test-server/dal/author-model'; +import { BookModelPerReality } from '../test-server/dal/book-model'; +import { finish, init } from '../test-server/run-test'; const dbRealityIdHeader = (realityId: any) => ({ realityId }); const requestRealityIdHeader = (realityId: any) => ({ 'reality-id': realityId }); From 59f7fbe2762fe639cd6d51a36635bb395f5ffcf7 Mon Sep 17 00:00:00 2001 From: osher-sade Date: Mon, 10 Jun 2019 12:08:09 +0300 Subject: [PATCH 11/11] test: changed function names --- test/integration-tests/test-server/run-test.ts | 5 +++-- test/integration-tests/tests/irrelevant-entities.test.ts | 6 +++--- test/integration-tests/tests/mutation.test.ts | 6 +++--- test/integration-tests/tests/query.test.ts | 6 +++--- test/integration-tests/tests/reality.test.ts | 6 +++--- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/test/integration-tests/test-server/run-test.ts b/test/integration-tests/test-server/run-test.ts index 311a7af..5c6c33d 100644 --- a/test/integration-tests/test-server/run-test.ts +++ b/test/integration-tests/test-server/run-test.ts @@ -2,7 +2,8 @@ import { closeConnection, initConnection } from '@enigmatis/mongo-driver'; import { config } from 'dotenv'; import * as mongoose from 'mongoose'; import { logger, server } from './server'; -export async function init() { + +export async function startTestServer() { config(); jest.setTimeout(15000); const connectionString = @@ -12,7 +13,7 @@ export async function init() { server.start(); } -export async function finish() { +export async function stopTestServer() { await closeConnection(); await server.stop(); } diff --git a/test/integration-tests/tests/irrelevant-entities.test.ts b/test/integration-tests/tests/irrelevant-entities.test.ts index 180b719..4c1b9cc 100644 --- a/test/integration-tests/tests/irrelevant-entities.test.ts +++ b/test/integration-tests/tests/irrelevant-entities.test.ts @@ -1,7 +1,7 @@ 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'; +import { startTestServer, stopTestServer } from '../test-server/run-test'; export const titles = ['first', 'second', 'third', 'fourth', 'fifth']; const prepareDb = async (headers: PolarisRequestHeaders) => { @@ -15,12 +15,12 @@ const prepareDb = async (headers: PolarisRequestHeaders) => { const requestHeaders = { 'reality-id': 1, 'data-version': 1 }; beforeEach(async () => { - await init(); + await startTestServer(); await prepareDb({ realityId: 1 }); }); afterEach(() => { - return finish(); + return stopTestServer(); }); describe('irrelevant entities tests', () => { diff --git a/test/integration-tests/tests/mutation.test.ts b/test/integration-tests/tests/mutation.test.ts index ff92711..4e32846 100644 --- a/test/integration-tests/tests/mutation.test.ts +++ b/test/integration-tests/tests/mutation.test.ts @@ -1,5 +1,5 @@ import { graphQLRequest } from '../test-server/client'; -import { finish, init } from '../test-server/run-test'; +import { startTestServer, stopTestServer } from '../test-server/run-test'; const headers = { 'reality-id': 1 }; const createBookMutation = `mutation createBook ($book:BookInput!) {createBook(book:$book){testId}}`; @@ -12,11 +12,11 @@ const deleteBookMutation = `mutation deleteBook ($bookId:String!) { const defaultBookVariables = (title: string, testId: string) => ({ title, testId }); beforeEach(() => { - return init(); + return startTestServer(); }); afterEach(() => { - return finish(); + return stopTestServer(); }); describe('mutation tests', () => { diff --git a/test/integration-tests/tests/query.test.ts b/test/integration-tests/tests/query.test.ts index 69b8b61..06ed7ef 100644 --- a/test/integration-tests/tests/query.test.ts +++ b/test/integration-tests/tests/query.test.ts @@ -1,7 +1,7 @@ import { PolarisRequestHeaders } from '@enigmatis/utills'; import { graphQLRequest } from '../test-server/client'; import { BookModelPerReality } from '../test-server/dal/book-model'; -import { finish, init } from '../test-server/run-test'; +import { startTestServer, stopTestServer } from '../test-server/run-test'; const titles = ['first', 'second', 'third', 'fourth', 'fifth']; @@ -16,12 +16,12 @@ const prepareDb = async (headers: PolarisRequestHeaders) => { const requestHeaders = { 'reality-id': 1 }; beforeEach(async () => { - await init(); + await startTestServer(); await prepareDb({ realityId: 1 }); }); afterEach(() => { - return finish(); + return stopTestServer(); }); describe('query tests', () => { diff --git a/test/integration-tests/tests/reality.test.ts b/test/integration-tests/tests/reality.test.ts index ead4fef..dc2f19c 100644 --- a/test/integration-tests/tests/reality.test.ts +++ b/test/integration-tests/tests/reality.test.ts @@ -1,7 +1,7 @@ import { graphQLRequest } from '../test-server/client'; import { AuthorModelPerReality } from '../test-server/dal/author-model'; import { BookModelPerReality } from '../test-server/dal/book-model'; -import { finish, init } from '../test-server/run-test'; +import { startTestServer, stopTestServer } from '../test-server/run-test'; const dbRealityIdHeader = (realityId: any) => ({ realityId }); const requestRealityIdHeader = (realityId: any) => ({ 'reality-id': realityId }); @@ -45,12 +45,12 @@ const prepareDb = async () => { }; beforeEach(async () => { - await init(); + await startTestServer(); await prepareDb(); }); afterEach(() => { - return finish(); + return stopTestServer(); }); describe('reality tests', () => {