Skip to content

Commit

Permalink
tags e2e test with in mem db config
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonalobont committed Feb 20, 2024
1 parent 684f9b7 commit 6a9aa97
Showing 1 changed file with 227 additions and 49 deletions.
276 changes: 227 additions & 49 deletions test/tags.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,125 @@
import { INestApplication, forwardRef } from '@nestjs/common';
import * as request from 'supertest';
//import { CleanupInterceptor, FieldsInterceptor } from '@multiversx/sdk-nestjs-http';
import { Test, TestingModule} from '@nestjs/testing';
import { TypeOrmModule, getRepositoryToken } from '@nestjs/typeorm';
//import { PersistenceService } from 'src/common/persistence/persistence.service';
import { Test, TestingModule } from '@nestjs/testing';
import { TypeOrmModule } from '@nestjs/typeorm';
import { TagEntity } from 'src/db/auctions/tags.entity';
import { AppModule } from 'src/app.module';
import { GraphQLModule } from '@nestjs/graphql';
import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo';
//import { getApplication } from './get-application';
import { TagsModuleGraph } from 'src/modules/tags/tags.module';
import { CommonModule } from 'src/common.module';
import { ComplexityPlugin } from 'src/modules/common/complexity.plugin';
import { GraphQLError, GraphQLFormattedError } from 'graphql';
import { ApolloServerPluginLandingPageLocalDefault } from '@apollo/server/plugin/landingPage/default';
import { PersistenceModule } from 'src/common/persistence/persistence.module';
import { AuctionEntity } from 'src/db/auctions/auction.entity';
import { AuctionStatusEnum, AuctionTypeEnum } from 'src/modules/auctions/models';
import { OrderEntity } from 'src/db/orders/order.entity';
import { Connection } from 'typeorm';
import { OrderStatusEnum } from 'src/modules/orders/models';

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

// beforeAll(async () => {
// try{
// console.log("Before each creating app.")
// app = await getApplication();
// } catch (error) {
// console.error("Error creating application: ", error)
// }
// });

beforeAll(async () => {
beforeEach(async () => {
moduleFixture = await Test.createTestingModule({
imports: [
// TypeOrmModule.forRoot({
// type: "sqlite",
// database: ":memory:",
// dropSchema: true,
// entities: ['src/db/**/*.entity.js'],
// synchronize: false,
// logging: false,
// }),
GraphQLModule.forRoot<ApolloDriverConfig>({ driver: ApolloDriver, playground: false, autoSchemaFile: 'schema.gql' }),
forwardRef(() => AppModule),
TypeOrmModule.forRoot({
type: 'sqlite',
database: ':memory:',
entities: [AuctionEntity, OrderEntity, TagEntity],
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 },
};
},
}),
}),
TagsModuleGraph,
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: 'erd1jhfd8zulqn8yuqeepkculuk0pyjp6ccez8fw3a0ru0379hz28kcqpwms50',
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',
});

await connection.getRepository(OrderEntity).save({
creationDate: new Date(),
auctionId: savedAuction.id,
priceToken: '70000000000000',
priceAmount: '7000000000000000',
priceNonce: 0,
status: OrderStatusEnum.Active,
ownerAddress: 'erd1jhfd8zulqn8yuqeepkculuk0pyjp6ccez8fw3a0ru0379hz28kcqpwms50',
priceAmountDenominated: 3.3,
blockHash: 'd7f4968fc0730bca82fa7b189e5365c873689c9c5296ca3c3e6e18a2277f9df9',
marketplaceKey: 'holoride',
});

await connection.getRepository(TagEntity).save({
creationDate: new Date(),
auctionId: savedAuction.id,
tag: 'test',
});

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

const tagsRepository = moduleFixture.get(getRepositoryToken(TagEntity));
const testTag = new TagEntity();
testTag.tag = 'newTag';
await tagsRepository.save(testTag);
}, 10000);
});

afterAll(async () => {
await connection.close();
await app.close();
const tagsRepository = moduleFixture.get(getRepositoryToken(TagEntity));
await tagsRepository.clear();
}, 10000);
});

describe('Query #tags', () => {
it('should return correct data for the tags query', async () => {
const query = `
query Query($filters: TagsFilter!) {
tags(filters: $filters) {
const query = `
query Query($filters: TagsFilter!, $pagination: ConnectionArgs) {
tags(filters: $filters, pagination: $pagination) {
edges {
node {
count
Expand All @@ -69,25 +130,142 @@ describe('TagsResolver', () => {
}
`;

const variables = {
filters: {
tagType: 'Nft'
},
};
const variables = {
filters: {
tagType: 'Nft',
},
pagination: {
first: 10,
},
};

const response = await request(app.getHttpServer())
const response = await request(app.getHttpServer())
.post('/graphql')
.send({
query,
variables,
})
.expect(200);
.expect((res) => {
expect(res.body).toEqual({
data: {
tags: {
edges: [
{
node: {
tag: 'multiversx',
count: 49500,
},
},
{
node: {
tag: 'beeser',
count: 44445,
},
},
{
node: {
tag: 'monster squad',
count: 18372,
},
},
{
node: {
tag: 'alien',
count: 15271,
},
},
{
node: {
tag: 'mini monster',
count: 15271,
},
},
{
node: {
tag: 'space robots',
count: 8126,
},
},
{
node: {
tag: 'robot',
count: 7795,
},
},
{
node: {
tag: 'sr-gen1',
count: 7795,
},
},
{
node: {
tag: 'vapor republic',
count: 5984,
},
},
{
node: {
tag: 'greek',
count: 5982,
},
},
],
},
},
});
});

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

it('should return correct data for the tags query (in mem value)', async () => {
const query = `
query Query($filters: TagsFilter!, $pagination: ConnectionArgs) {
tags(filters: $filters, pagination: $pagination) {
edges {
node {
count
tag
}
}
}
}
`;

const variables = {
filters: {
tagType: 'Auction',
},
pagination: {
first: 10,
},
};

const { data } = response.body;
console.log(data);
const response = await request(app.getHttpServer())
.post('/graphql')
.send({
query,
variables,
})
.expect((res) => {
expect(res.body).toEqual({
data: {
tags: {
edges: [
{
node: {
tag: 'test',
count: 1,
},
},
],
},
},
});
});

expect(data).toBeDefined();
expect(data.tags.edges.length).toBeGreaterThan(0);
expect(response.body).toBeDefined();
});
});
});

0 comments on commit 6a9aa97

Please sign in to comment.