adjust according to Claus-Peters Suggestions

This commit is contained in:
einhorn_b 2023-07-12 17:33:06 +02:00
parent 8da7b90d3b
commit 6b37022fc2
6 changed files with 67 additions and 22 deletions

View File

@ -6,7 +6,7 @@ module.exports = {
collectCoverageFrom: ['src/**/*.ts', '!**/node_modules/**', '!src/seeds/**', '!build/**'],
coverageThreshold: {
global: {
lines: 62,
lines: 63,
},
},
setupFiles: ['<rootDir>/test/testSetup.ts'],
@ -14,6 +14,7 @@ module.exports = {
modulePathIgnorePatterns: ['<rootDir>/build/'],
moduleNameMapper: {
'@/(.*)': '<rootDir>/src/$1',
'@enum/(.*)': '<rootDir>/src/graphql/enum/$1',
'@resolver/(.*)': '<rootDir>/src/graphql/resolver/$1',
'@input/(.*)': '<rootDir>/src/graphql/input/$1',
'@proto/(.*)': '<rootDir>/src/proto/$1',

View File

@ -13,7 +13,7 @@ export class TransactionInput {
amount: Decimal
@Field(() => Number)
created: number
createdAt: number
// @protoField.d(4, 'string')
// @Field(() => Decimal)

View File

@ -1,4 +1,5 @@
import 'reflect-metadata'
import { ApolloServer } from '@apollo/server'
import { createApolloTestServer } from '@test/ApolloServerMock'
import assert from 'assert'
@ -10,23 +11,35 @@ jest.mock('@/client/IotaClient', () => {
}
})
let apolloTestServer: ApolloServer
describe('Transaction Resolver Test', () => {
beforeAll(async () => {
apolloTestServer = await createApolloTestServer()
})
it('test version query', async () => {
const response = await apolloTestServer.executeOperation({
query: '{ version }',
})
// Note the use of Node's assert rather than Jest's expect; if using
// TypeScript, `assert`` will appropriately narrow the type of `body`
// and `expect` will not.
// Source: https://www.apollographql.com/docs/apollo-server/testing/testing
assert(response.body.kind === 'single')
expect(response.body.singleResult.errors).toBeUndefined()
expect(response.body.singleResult.data?.version).toBe('0.1')
})
it('test mocked sendTransaction', async () => {
const apolloTestServer = await createApolloTestServer()
const response = await apolloTestServer.executeOperation({
query: 'mutation ($input: TransactionInput!) { sendTransaction(data: $input) }',
variables: {
input: {
type: 'SEND',
amount: '10',
created: 1688992436,
createdAt: 1688992436,
},
},
})
// Note the use of Node's assert rather than Jest's expect; if using
// TypeScript, `assert`` will appropriately narrow the type of `body`
// and `expect` will not.
// Source: https://www.apollographql.com/docs/apollo-server/testing/testing
assert(response.body.kind === 'single')
expect(response.body.singleResult.errors).toBeUndefined()
expect(response.body.singleResult.data?.sendTransaction).toBe(
@ -35,15 +48,13 @@ describe('Transaction Resolver Test', () => {
})
it('test mocked sendTransaction invalid transactionType ', async () => {
const apolloTestServer = await createApolloTestServer()
const response = await apolloTestServer.executeOperation({
query: 'mutation ($input: TransactionInput!) { sendTransaction(data: $input) }',
variables: {
input: {
type: 'INVALID',
amount: '10',
created: 1688992436,
createdAt: 1688992436,
},
},
})
@ -59,15 +70,13 @@ describe('Transaction Resolver Test', () => {
})
it('test mocked sendTransaction invalid amount ', async () => {
const apolloTestServer = await createApolloTestServer()
const response = await apolloTestServer.executeOperation({
query: 'mutation ($input: TransactionInput!) { sendTransaction(data: $input) }',
variables: {
input: {
type: 'SEND',
amount: 'no number',
created: 1688992436,
createdAt: 1688992436,
},
},
})
@ -83,15 +92,13 @@ describe('Transaction Resolver Test', () => {
})
it('test mocked sendTransaction invalid created date ', async () => {
const apolloTestServer = await createApolloTestServer()
const response = await apolloTestServer.executeOperation({
query: 'mutation ($input: TransactionInput!) { sendTransaction(data: $input) }',
variables: {
input: {
type: 'SEND',
amount: '10',
created: '2023-03-02T10:12:00',
createdAt: '2023-03-02T10:12:00',
},
},
})
@ -100,7 +107,7 @@ describe('Transaction Resolver Test', () => {
errors: [
{
message:
'Variable "$input" got invalid value "2023-03-02T10:12:00" at "input.created"; Float cannot represent non numeric value: "2023-03-02T10:12:00"',
'Variable "$input" got invalid value "2023-03-02T10:12:00" at "input.createdAt"; Float cannot represent non numeric value: "2023-03-02T10:12:00"',
},
],
})

View File

@ -12,9 +12,10 @@ export class TransactionResolver {
// GeneratingSchemaError: Some errors occurred while generating GraphQL schema:
// Type Query must define one or more fields.
// it seems that at least one query must be defined
// https://github.com/ardatan/graphql-tools/issues/764
@Query(() => String)
dummy(): string {
return 'nothing'
version(): string {
return '0.1'
}
@Mutation(() => String)

View File

@ -0,0 +1,36 @@
import 'reflect-metadata'
import { TransactionType } from '@enum/TransactionType'
import { TransactionInput } from '@input/TransactionInput'
import Decimal from 'decimal.js-light'
import { TransactionBody } from './TransactionBody'
describe('proto/TransactionBodyTest', () => {
it('test compatible with graphql/input/TransactionInput', async () => {
// test data
const type = TransactionType.SEND
const amount = new Decimal('10')
const createAt = 1688992436
// init both objects
// graphql input object
const transactionInput = new TransactionInput()
transactionInput.type = type
transactionInput.amount = amount
transactionInput.createdAt = createAt
// protobuf object
const transactionBody = new TransactionBody()
transactionBody.type = type
transactionBody.amount = amount.toString()
transactionBody.createdAt = createAt
// create protobuf object from graphql Input object
const message = TransactionBody.fromObject(transactionInput)
// serialize both protobuf objects
const messageBuffer = TransactionBody.encode(message).finish()
const messageBuffer2 = TransactionBody.encode(transactionBody).finish()
// compare
expect(messageBuffer).toStrictEqual(messageBuffer2)
})
})

View File

@ -9,10 +9,10 @@ export class TransactionBody extends Message<TransactionBody> {
type: TransactionType
@Field.d(2, 'string')
amount: Decimal
amount: string
@Field.d(3, 'uint64')
created: number
createdAt: number
// @protoField.d(4, 'string')
// communitySum: Decimal