Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

accounts stats e2e test with in mem db #1067

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 109 additions & 34 deletions test/accounts-stats.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,144 @@
import { INestApplication } from '@nestjs/common';
import { AccountStatsEntity } from 'src/db/account-stats/account-stats';
import { AccountsStatsService } from 'src/modules/account-stats/accounts-stats.service';
import * as request from 'supertest';
import { getApplication } from './get-application';
import { Test, TestingModule } from '@nestjs/testing';
import { Connection } from 'typeorm';
import { TypeOrmModule } from '@nestjs/typeorm';
import { GraphQLModule } from '@nestjs/graphql';
import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo';
import { ApolloServerPluginLandingPageLocalDefault } from '@apollo/server/plugin/landingPage/default';
import { PersistenceModule } from 'src/common/persistence/persistence.module';
import { CommonModule } from 'src/common.module';
import { GraphQLError, GraphQLFormattedError } from 'graphql';
import { ComplexityPlugin } from 'src/modules/common/complexity.plugin';
import { AccountsStatsModuleGraph } from 'src/modules/account-stats/accounts-stats.module';
import { AuctionStatusEnum, AuctionTypeEnum } from 'src/modules/auctions/models';
import { AuctionEntity } from 'src/db/auctions/auction.entity';

describe('AccountStatsResolver', () => {
let app: INestApplication;
let moduleFixture: TestingModule;
let connection: Connection;

beforeAll(async () => {
app = await getApplication();
moduleFixture = await Test.createTestingModule({
imports: [
TypeOrmModule.forRoot({
type: 'sqlite',
database: ':memory:',
entities: [AccountStatsEntity],
synchronize: true,
logging: false,
}),
GraphQLModule.forRootAsync<ApolloDriverConfig>({
driver: ApolloDriver,
imports: [CommonModule],
useFactory: async () => ({
autoSchemaFile: 'schema.gql',
introspection: process.env.NODE_ENV !== 'production',
playground: false,
plugins: [ApolloServerPluginLandingPageLocalDefault(), new ComplexityPlugin()],
sortSchema: true,
formatError: (error: GraphQLError) => {
const graphQLFormattedError: GraphQLFormattedError = {
...error,
message: error.message,
};
console.error(graphQLFormattedError);

return {
...graphQLFormattedError,
extensions: { ...graphQLFormattedError.extensions, exception: null },
};
},
}),
}),
AccountsStatsModuleGraph,
CommonModule,
PersistenceModule,
],
}).compile();

connection = moduleFixture.get<Connection>(Connection);
await connection.runMigrations();

const savedAuction = await connection.getRepository(AuctionEntity).save({
creationDate: new Date(),
modifiedDate: new Date(),
marketplaceAuctionId: 24,
marketplaceKey: 'holoride',
collection: 'AGENTS-ad9b3a',
identifier: 'AGENTS-ad9b3a-0b',
type: AuctionTypeEnum.Nft,
ownerAddress: 'erd1dc3yzxxeq69wvf583gw0h67td226gu2ahpk3k50qdgzzym8npltq7ndgha',
nrAuctionedTokens: 2,
nonce: 42,
status: AuctionStatusEnum.Running,
paymentToken: 'EGLD',
paymentNonce: 0,
minBid: '7000000000000000',
maxBid: '0',
minBidDiff: '7000000000000000',
startDate: 10,
endDate: 15,
tags: 'test',
blockHash: 'd7f4968fc0730bca82fa7b189e5365c873689c9c5296ca3c3e6e18a2277f9df9',
});

app = moduleFixture.createNestApplication();
await app.init();
});

afterAll(async () => {
await connection.close();
await app.close();
});

describe('Query #accountStats', () => {
const mutation = () => `
{ accountStats(filters: { address: "erd1dc3yzxxeq69wvf583gw0h67td226gu2ahpk3k50qdgzzym8npltq7ndgha" }) {
address
auctions
biddingBalance
claimable
collected
collections
creations
orders
it('should return correct data for the accountStats query', async () => {
const query = `
query AccountStats($filters: AccountStatsFilter!) {
accountStats(filters: $filters) {
address
auctions
biddingBalance
claimable
collections
creations
orders
}
}
}
`;
`;

it('get stats for account', async () => {
const accountStatsService = app.get<AccountsStatsService>(AccountsStatsService);
jest.spyOn(accountStatsService, 'getStats').mockResolvedValue(
new AccountStatsEntity({
auctions: '2',
orders: '0',
biddingBalance: '0',
const variables = {
filters: {
address: 'erd1dc3yzxxeq69wvf583gw0h67td226gu2ahpk3k50qdgzzym8npltq7ndgha',
}),
);
jest.spyOn(accountStatsService, 'getClaimableCount').mockResolvedValue(5);
jest.spyOn(accountStatsService, 'getCollectedCount').mockResolvedValue(5);
jest.spyOn(accountStatsService, 'getCollectionsCount').mockResolvedValue(5);
},
};

return request(app.getHttpServer())
const response = await request(app.getHttpServer())
.post('/graphql')
.send({
query: mutation(),
query,
variables,
})
.expect((res) => {
expect(res.body).toEqual({
data: {
accountStats: {
address: 'erd1dc3yzxxeq69wvf583gw0h67td226gu2ahpk3k50qdgzzym8npltq7ndgha',
auctions: '2',
auctions: '1',
biddingBalance: '0',
claimable: '5',
collected: '5',
collections: '5',
creations: '5',
claimable: '0',
collections: '3',
creations: '13',
orders: '0',
},
},
});
});

expect(response.body).toBeDefined();
});
});
});
Loading