Skip to content

Commit

Permalink
Merge pull request #58 from osher-sade/reality-integration-tests
Browse files Browse the repository at this point in the history
Reality integration tests
  • Loading branch information
osher-sade authored Jun 11, 2019
2 parents f5a4a69 + 59f7fbe commit f8a9fdd
Show file tree
Hide file tree
Showing 20 changed files with 279 additions and 71 deletions.
7 changes: 3 additions & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
module.exports = {
preset: 'ts-jest',
setupTestFrameworkScriptFile: './jest.setup.js',
testEnvironment: 'node',
clearMocks : true,
moduleFileExtensions: [
"ts",
"js",
],
preset: 'ts-jest',
testEnvironment: 'node',
testMatch: [
"**/*.test.ts"
],
};
};
41 changes: 30 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/schema/common/common-entity-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export const CommonEntityInterface = `interface CommonEntity {
id: ID!
creationDate: Date,
lastUpdateDate: Date,
dataVersion: Int!
dataVersion: Int!,
realityId: Int!
}`;
2 changes: 1 addition & 1 deletion test/integration-tests/test-server/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
return graphQLClient.request(data, variables);
};
Expand Down
16 changes: 16 additions & 0 deletions test/integration-tests/test-server/dal/author-model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { getModelCreator, RepositoryModel } from '@enigmatis/mongo-driver';
import { Schema } from 'mongoose';

export interface Author extends RepositoryModel {
testId: string;
firstName: string;
lastName: string;
}

export const authorSchema: Schema = new Schema({
testId: String,
firstName: String,
lastName: String,
});

export const AuthorModelPerReality = getModelCreator<Author>('author', authorSchema);
24 changes: 14 additions & 10 deletions test/integration-tests/test-server/dal/book-model.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import { getModelCreator, RepositoryModel } from '@enigmatis/mongo-driver';
import { getModelCreator, RepositoryModel, SchemaCreator } from '@enigmatis/mongo-driver';
import { Schema } from 'mongoose';
import { Author } from './author-model';

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

export const bookSchema: Schema = new Schema({
testId: String,
title: String,
author: String,
otherBook: Object,
dataVersion: Number,
});
export const bookSchema: SchemaCreator = refNameCreator => {
return new Schema({
testId: String,
title: String,
author: {
type: Schema.Types.ObjectId,
ref: refNameCreator('author'),
},
dataVersion: Number,
});
};

export const BookModelPerReality = getModelCreator<Book>('book', bookSchema);
5 changes: 3 additions & 2 deletions test/integration-tests/test-server/run-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -12,7 +13,7 @@ export async function init() {
server.start();
}

export async function finish() {
export async function stopTestServer() {
await closeConnection();
await server.stop();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { RepositoryEntity } from '../../../../../src/main';

export interface Author extends RepositoryEntity {
testId: string;
id: string;
firstName: string;
lastName: string;
}
4 changes: 2 additions & 2 deletions test/integration-tests/test-server/schema/definitions/book.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { RepositoryEntity } from '../../../../../src/main';
import { Author } from './author';

export interface Book extends RepositoryEntity {
testId: string;
id: string;
title: string;
author: string;
otherBook?: Book;
author: Author;
}
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -34,9 +36,18 @@ 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 zeroRealityHeaders = { ...context.headers };
zeroRealityHeaders.realityId = 0;
const zeroRealityContext = { ...context };
zeroRealityContext.headers = zeroRealityHeaders;
return BookModelPerReality(context)
.find({})
.populate({ path: 'author', model: AuthorModelPerReality(zeroRealityContext) })
.lean();
} else {
return BookModelPerReality(context)
.find({})
Expand Down
12 changes: 10 additions & 2 deletions test/integration-tests/test-server/schema/schema.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const AuthorInput = `input AuthorInput {
id: ID!,
testId: String!,
firstName: String,
lastName: String
},
input UpdateAuthorInput {
firstName: String,
lastName: String
}`;
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ export const BookInput = `input BookInput {
id: ID,
testId: String!,
title: String,
author: String,
otherBook: BookInput
author: AuthorInput,
},
input UpdateBookInput {
title: String,
author: String,
otherBook: BookInput
author: AuthorInput,
}`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const Author = `type Author implements CommonEntity {
id: ID!
firstName: String,
lastName: String,
creationDate: Date,
lastUpdateDate: Date,
dataVersion: Int!,
realityId: Int!
}`;
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
export const Book = `type Book implements CommonEntity {
testId: String,
id: ID!,
title: String,
author: Author,
creationDate: Date,
lastUpdateDate: Date,
dataVersion: Int!,
title: String,
author: String,
otherBook: ID,
realityId: Int!
}`;
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Loading

0 comments on commit f8a9fdd

Please sign in to comment.