-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: added test server and some tests
- Loading branch information
1 parent
e925948
commit 5165c02
Showing
23 changed files
with
662 additions
and
91 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Connection, ConnectionOptions, createPolarisConnection } from '@enigmatis/polaris-typeorm'; | ||
import { polarisGraphQLLogger } from './logger'; | ||
|
||
export let connection: Connection; | ||
export async function initConnection(connectionOptions: ConnectionOptions) { | ||
connection = await createPolarisConnection( | ||
connectionOptions, | ||
polarisGraphQLLogger.getPolarisLogger() as any, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Column, CommonModel, Entity, OneToMany } from '@enigmatis/polaris-typeorm'; | ||
import { Book } from './book'; | ||
|
||
@Entity() | ||
export class Author extends CommonModel { | ||
@Column() | ||
public firstName: string; | ||
|
||
@Column() | ||
public lastName: string; | ||
|
||
@OneToMany( | ||
() => Book, | ||
book => book.author, | ||
) | ||
public books: Book[]; | ||
constructor(firstName: string, lastName: string, books?: Book[]) { | ||
super(); | ||
this.firstName = firstName; | ||
this.lastName = lastName | ||
this.books = books ? books : []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Column, CommonModel, Entity, ManyToOne } from '@enigmatis/polaris-typeorm'; | ||
import { Author } from './author'; | ||
|
||
@Entity() | ||
export class Book extends CommonModel { | ||
@Column() | ||
public title: string; | ||
|
||
@ManyToOne( | ||
() => Author, | ||
author => author.books, | ||
) | ||
public author: Author; | ||
|
||
constructor(title: string, author: Author) { | ||
super(); | ||
this.title = title; | ||
this.author = author; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { connection } from './connection-manager'; | ||
import { Author } from './dal/author'; | ||
import { Book } from './dal/book'; | ||
import { polarisGraphQLLogger } from './logger'; | ||
|
||
export async function deleteTables() { | ||
const tables = ['book', 'author', 'dataVersion']; | ||
for (const table of tables) { | ||
if (connection) { | ||
try { | ||
await connection.getRepository(table).query('DELETE FROM "' + table + '";'); | ||
} catch (e) { | ||
polarisGraphQLLogger.debug("Couldn't delete table (might never existed)"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
function getAuthors(): Author[] { | ||
return [new Author('Author', 'First'), new Author('Author', 'Two')]; | ||
} | ||
|
||
function getBooks(authors: Author[]): Book[] { | ||
return [ | ||
new Book('Book1', authors[0]), | ||
new Book('Book2', authors[1]), | ||
new Book('Book3', authors[0]), | ||
new Book('Book4', authors[0]), | ||
new Book('Book5', authors[1]), | ||
]; | ||
} | ||
|
||
async function createExampleData(authors: Author[], books: Book[]) { | ||
const authorRepo = connection.getRepository(Author); | ||
const bookRepo = connection.getRepository(Book); | ||
await authorRepo.save(authors); | ||
await bookRepo.save([books[0], books[1]]); | ||
connection.manager.queryRunner.data = { requestHeaders: { realityId: 3 } }; | ||
await bookRepo.save(books[2]); | ||
connection.manager.queryRunner.data.returnedExtensions = {}; | ||
await bookRepo.save(books[3]); | ||
books[4].setDeleted(true); | ||
await bookRepo.save(books[4]); | ||
delete connection.manager.queryRunner.data.requestHeaders; | ||
delete connection.manager.queryRunner.data.returnedExtensions; | ||
} | ||
|
||
export async function initializeDatabase() { | ||
await deleteTables(); | ||
await connection.synchronize(); | ||
const authors: Author[] = getAuthors(); | ||
const books: Book[] = getBooks(authors); | ||
await createExampleData(authors, books); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { GraphQLClient } from 'graphql-request'; | ||
import * as polarisProperties from './polaris-properties.json'; | ||
|
||
export const url = `http://localhost:${polarisProperties.port}/${polarisProperties.version}/graphql`; | ||
|
||
export const graphQLRequest = async (data: string, headers: any, variables: any = undefined) => { | ||
const graphQLClient = new GraphQLClient(url, { headers }); | ||
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { LoggerConfiguration, PolarisGraphQLLogger } from '../../../src/index'; | ||
|
||
export const loggerConfig: LoggerConfiguration = { | ||
loggerLevel: 'debug', | ||
writeToConsole: true, | ||
writeFullMessageToConsole: false, | ||
}; | ||
|
||
const applicationLogProperties = { | ||
id: 'example', | ||
name: 'example', | ||
component: 'repo', | ||
environment: 'dev', | ||
version: '1', | ||
}; | ||
|
||
export const polarisGraphQLLogger = new PolarisGraphQLLogger( | ||
{ | ||
loggerLevel: 'debug', | ||
writeToConsole: true, | ||
writeFullMessageToConsole: false, | ||
}, | ||
applicationLogProperties, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { CommonModel, ConnectionOptions, DataVersion } from '@enigmatis/polaris-typeorm'; | ||
import { PolarisServer } from '../../../src/index'; | ||
import { connection, initConnection } from './connection-manager'; | ||
import { initializeDatabase } from './data-initalizer'; | ||
import { loggerConfig } from './logger'; | ||
import * as polarisProperties from './polaris-properties.json'; | ||
import { resolvers } from './schema/resolvers'; | ||
import { typeDefs } from './schema/type-defs'; | ||
|
||
let server: PolarisServer; | ||
const connectionOptions: ConnectionOptions = { | ||
type: 'postgres', | ||
url: process.env.CONNECTION_STRING || '', | ||
entities: [__dirname + '/dal/*.ts', CommonModel, DataVersion], | ||
synchronize: false, | ||
logging: true, | ||
}; | ||
const startApp = async () => { | ||
await initConnection(connectionOptions); | ||
await initializeDatabase(); | ||
server = new PolarisServer({ | ||
typeDefs, | ||
resolvers, | ||
port: polarisProperties.port, | ||
loggerConfiguration: loggerConfig, | ||
connection, | ||
}); | ||
await server.start(); | ||
}; | ||
try { | ||
startApp(); | ||
} catch (e) { | ||
if (server) { | ||
server.stop(); | ||
} | ||
process.exit(0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"port": 8082, | ||
"id": "p0laris-ex5mpl3", | ||
"name": "polaris-example", | ||
"version": "v1", | ||
"environment": "environment", | ||
"component": "component" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Like } from '../../../../src/index'; | ||
import { connection } from '../connection-manager'; | ||
import { Author } from '../dal/author'; | ||
import { Book } from '../dal/book'; | ||
import { polarisGraphQLLogger } from '../logger'; | ||
|
||
export const resolvers = { | ||
Query: { | ||
allBooks: async (): Promise<Book[]> => { | ||
polarisGraphQLLogger.debug("I'm the resolver of all books"); | ||
return connection.getRepository(Book).find({ relations: ['author'] }); | ||
}, | ||
bookByTitle: (parent: any, args: any): Promise<Book[]> => { | ||
return connection.getRepository(Book).find({ | ||
where: { title: Like(`%${args.title}%`) }, | ||
relations: ['author'], | ||
}); | ||
}, | ||
authorById: async (parent: any, args: any): Promise<Author | {}> => | ||
connection.getRepository(Author).findOne({ where: { id: args.id } }) || {}, | ||
}, | ||
Mutation: { | ||
createAuthor: async (parent: any, args: any): Promise<Author> => { | ||
const authorRepo = connection.getRepository(Author); | ||
const newAuthor = new Author(args.firstName, args.lastName, []); | ||
await authorRepo.save(newAuthor); | ||
return newAuthor; | ||
}, | ||
updateBook: async (parent: any, args: any): Promise<Book> => { | ||
const bookRepo = connection.getRepository(Book); | ||
const result = await bookRepo.find({ where: { title: Like(`%${args.title}%`) } }); | ||
const bookToUpdate = result.length > 0 ? result[0] : undefined; | ||
if (bookToUpdate) { | ||
bookToUpdate.title = args.newTitle; | ||
await bookRepo.save(bookToUpdate); | ||
} | ||
return bookToUpdate; | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
export const typeDefs = ` | ||
type Query { | ||
allBooks: [Book]! | ||
authorById(id: String!): Author | ||
bookByTitle(title: String!): [Book]! | ||
} | ||
type Mutation{ | ||
createAuthor(firstName: String!, lastName: String!): Author! | ||
updateBook(title: String!, newTitle: String!): Book | ||
} | ||
type Book implements RepositoryEntity { | ||
id: String! | ||
deleted: Boolean! | ||
createdBy: String! | ||
creationTime: DateTime! | ||
lastUpdatedBy: String | ||
lastUpdateTime: DateTime | ||
realityId: Int! | ||
title: String | ||
author: Author | ||
} | ||
type Author implements RepositoryEntity{ | ||
id: String! | ||
deleted: Boolean! | ||
createdBy: String! | ||
creationTime: DateTime! | ||
lastUpdatedBy: String | ||
lastUpdateTime: DateTime | ||
realityId: Int! | ||
firstName: String | ||
lastName: String | ||
books: [Book] | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { CommonModel, ConnectionOptions, DataVersion } from '@enigmatis/polaris-typeorm'; | ||
import { PolarisServer } from '../../../src'; | ||
import { connection, initConnection } from './connection-manager'; | ||
import { deleteTables } from './data-initalizer'; | ||
import { loggerConfig, polarisGraphQLLogger } from './logger'; | ||
import * as polarisProperties from './polaris-properties.json'; | ||
import { resolvers } from './schema/resolvers'; | ||
import { typeDefs } from './schema/type-defs'; | ||
|
||
const connectionOptions: ConnectionOptions = { | ||
type: 'postgres', | ||
url: process.env.CONNECTION_STRING || '', | ||
entities: [__dirname + '/dal/*.ts', CommonModel, DataVersion], | ||
synchronize: false, | ||
logging: true, | ||
}; | ||
|
||
let server: PolarisServer; | ||
|
||
export async function startTestServer(): Promise<PolarisServer> { | ||
jest.setTimeout(15000); | ||
await initConnection(connectionOptions); | ||
server = new PolarisServer({ | ||
typeDefs, | ||
resolvers, | ||
port: polarisProperties.port, | ||
loggerConfiguration: loggerConfig, | ||
connection, | ||
}); | ||
await server.start(); | ||
return server; | ||
} | ||
|
||
export async function stopTestServer() { | ||
await connection.close(); | ||
await server.stop(); | ||
} |
Oops, something went wrong.