Skip to content

test(core-api): increate test coverage to 100% #4061

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

Merged
merged 11 commits into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
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
168 changes: 168 additions & 0 deletions __tests__/unit/core-api/__support__/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import { Application, Container, Contracts, Providers, Services } from "@packages/core-kernel";
import { Identifiers } from "@packages/core-kernel/src/ioc";
import {
BridgechainRegistrationTransactionHandler,
BusinessRegistrationTransactionHandler,
} from "@packages/core-magistrate-transactions/src/handlers";
import {
bridgechainIndexer,
businessIndexer,
MagistrateIndex,
} from "@packages/core-magistrate-transactions/src/wallet-indexes";
import { Wallets } from "@packages/core-state";
import {
addressesIndexer,
ipfsIndexer,
locksIndexer,
publicKeysIndexer,
usernamesIndexer,
} from "@packages/core-state/src/wallets/indexers/indexers";
import { Mocks } from "@packages/core-test-framework";
import passphrases from "@packages/core-test-framework/src/internal/passphrases.json";
import { One, Two } from "@packages/core-transactions/src/handlers";
import { TransactionHandlerProvider } from "@packages/core-transactions/src/handlers/handler-provider";
import { TransactionHandlerRegistry } from "@packages/core-transactions/src/handlers/handler-registry";
import { Identities, Utils } from "@packages/crypto";

export type PaginatedResponse = {
totalCount: number;
results: object[];
meta: object;
};

export type ItemResponse = {
data: object;
};

export const parseObjectWithBigInt = (item) => {
return JSON.parse(JSON.stringify(item, (key, value) => (typeof value === "bigint" ? value.toString() : value)));
};

export const buildSenderWallet = (app: Application, passphrase: string | null = null): Contracts.State.Wallet => {
const walletRepository = app.get<Wallets.WalletRepository>(Identifiers.WalletRepository);

const wallet: Contracts.State.Wallet = walletRepository.createWallet(
Identities.Address.fromPassphrase(passphrase ? passphrase : passphrases[0]),
);

wallet.publicKey = Identities.PublicKey.fromPassphrase(passphrase ? passphrase : passphrases[0]);
wallet.balance = Utils.BigNumber.make(7527654310);

return wallet;
};

export const initApp = (): Application => {
const app = new Application(new Container.Container());
const logger = { error: jest.fn(), notice: jest.fn() };

app.bind(Container.Identifiers.LogService).toConstantValue(logger);

app.bind(Container.Identifiers.PluginConfiguration).to(Providers.PluginConfiguration).inSingletonScope();

app.bind(Container.Identifiers.StateStore).toConstantValue(Mocks.StateStore.instance);

app.bind(Container.Identifiers.BlockchainService).toConstantValue(Mocks.Blockchain.instance);

app.bind(Container.Identifiers.DatabaseBlockRepository).toConstantValue(Mocks.BlockRepository.instance);

app.bind(Container.Identifiers.DatabaseTransactionRepository).toConstantValue(Mocks.TransactionRepository.instance);

app.bind(Container.Identifiers.PeerNetworkMonitor).toConstantValue(Mocks.NetworkMonitor.instance);

app.bind(Container.Identifiers.PeerStorage).toConstantValue(Mocks.PeerStorage.instance);

app.bind(Container.Identifiers.DatabaseRoundRepository).toConstantValue(Mocks.RoundRepository.instance);

app.bind(Container.Identifiers.TransactionPoolQuery).toConstantValue(Mocks.TransactionPoolQuery.instance);

app.bind(Container.Identifiers.TransactionPoolProcessor).toConstantValue(Mocks.TransactionPoolProcessor.instance);

app.bind(Container.Identifiers.TransactionPoolProcessorFactory).toFactory(() => () => {
return Mocks.TransactionPoolProcessor.instance;
});

app.bind(Container.Identifiers.EventDispatcherService).toConstantValue({});

app.bind(Identifiers.TransactionHandler).to(One.TransferTransactionHandler);
app.bind(Identifiers.TransactionHandler).to(Two.TransferTransactionHandler);
app.bind(Identifiers.TransactionHandler).to(One.SecondSignatureRegistrationTransactionHandler);
app.bind(Identifiers.TransactionHandler).to(Two.SecondSignatureRegistrationTransactionHandler);
app.bind(Identifiers.TransactionHandler).to(One.DelegateRegistrationTransactionHandler);
app.bind(Identifiers.TransactionHandler).to(Two.DelegateRegistrationTransactionHandler);
app.bind(Identifiers.TransactionHandler).to(One.VoteTransactionHandler);
app.bind(Identifiers.TransactionHandler).to(Two.VoteTransactionHandler);
app.bind(Identifiers.TransactionHandler).to(One.MultiSignatureRegistrationTransactionHandler);
app.bind(Identifiers.TransactionHandler).to(Two.MultiSignatureRegistrationTransactionHandler);
app.bind(Identifiers.TransactionHandler).to(Two.IpfsTransactionHandler);
app.bind(Identifiers.TransactionHandler).to(Two.MultiPaymentTransactionHandler);
app.bind(Identifiers.TransactionHandler).to(Two.DelegateResignationTransactionHandler);
app.bind(Identifiers.TransactionHandler).to(Two.HtlcLockTransactionHandler);
app.bind(Identifiers.TransactionHandler).to(Two.HtlcClaimTransactionHandler);
app.bind(Identifiers.TransactionHandler).to(Two.HtlcRefundTransactionHandler);

app.bind(Identifiers.TransactionHandlerProvider).to(TransactionHandlerProvider).inSingletonScope();
app.bind(Identifiers.TransactionHandlerRegistry).to(TransactionHandlerRegistry).inSingletonScope();

app.bind(Identifiers.TransactionHandler).to(BusinessRegistrationTransactionHandler);
app.bind(Identifiers.TransactionHandler).to(BridgechainRegistrationTransactionHandler);

app.bind<Services.Attributes.AttributeSet>(Identifiers.WalletAttributes)
.to(Services.Attributes.AttributeSet)
.inSingletonScope();

app.bind<Contracts.State.WalletIndexerIndex>(Identifiers.WalletRepositoryIndexerIndex).toConstantValue({
name: Contracts.State.WalletIndexes.Addresses,
indexer: addressesIndexer,
autoIndex: true,
});

app.bind<Contracts.State.WalletIndexerIndex>(Container.Identifiers.WalletRepositoryIndexerIndex).toConstantValue({
name: Contracts.State.WalletIndexes.PublicKeys,
indexer: publicKeysIndexer,
autoIndex: true,
});

app.bind<Contracts.State.WalletIndexerIndex>(Container.Identifiers.WalletRepositoryIndexerIndex).toConstantValue({
name: Contracts.State.WalletIndexes.Usernames,
indexer: usernamesIndexer,
autoIndex: true,
});

app.bind<Contracts.State.WalletIndexerIndex>(Container.Identifiers.WalletRepositoryIndexerIndex).toConstantValue({
name: Contracts.State.WalletIndexes.Ipfs,
indexer: ipfsIndexer,
autoIndex: true,
});

app.bind<Contracts.State.WalletIndexerIndex>(Container.Identifiers.WalletRepositoryIndexerIndex).toConstantValue({
name: Contracts.State.WalletIndexes.Locks,
indexer: locksIndexer,
autoIndex: true,
});

app.bind<Contracts.State.WalletIndexerIndex>(Container.Identifiers.WalletRepositoryIndexerIndex).toConstantValue({
name: MagistrateIndex.Businesses,
indexer: businessIndexer,
autoIndex: true,
});

app.bind<Contracts.State.WalletIndexerIndex>(Container.Identifiers.WalletRepositoryIndexerIndex).toConstantValue({
name: MagistrateIndex.Bridgechains,
indexer: bridgechainIndexer,
autoIndex: true,
});

app.bind(Identifiers.WalletFactory).toFactory<Contracts.State.Wallet>(
(context: Container.interfaces.Context) => (address: string) =>
new Wallets.Wallet(
address,
new Services.Attributes.AttributeMap(
context.container.get<Services.Attributes.AttributeSet>(Identifiers.WalletAttributes),
),
),
);

app.bind(Identifiers.WalletRepository).to(Wallets.WalletRepository).inSingletonScope();

return app;
};
170 changes: 2 additions & 168 deletions __tests__/unit/core-api/__support__/index.ts
Original file line number Diff line number Diff line change
@@ -1,168 +1,2 @@
import { Application, Container, Contracts, Providers, Services } from "@packages/core-kernel";
import { Identifiers } from "@packages/core-kernel/src/ioc";
import {
BridgechainRegistrationTransactionHandler,
BusinessRegistrationTransactionHandler,
} from "@packages/core-magistrate-transactions/src/handlers";
import {
bridgechainIndexer,
businessIndexer,
MagistrateIndex,
} from "@packages/core-magistrate-transactions/src/wallet-indexes";
import { Wallets } from "@packages/core-state";
import {
addressesIndexer,
ipfsIndexer,
locksIndexer,
publicKeysIndexer,
usernamesIndexer,
} from "@packages/core-state/src/wallets/indexers/indexers";
import { Mocks } from "@packages/core-test-framework";
import passphrases from "@packages/core-test-framework/src/internal/passphrases.json";
import { One, Two } from "@packages/core-transactions/src/handlers";
import { TransactionHandlerProvider } from "@packages/core-transactions/src/handlers/handler-provider";
import { TransactionHandlerRegistry } from "@packages/core-transactions/src/handlers/handler-registry";
import { Identities, Utils } from "@packages/crypto";

export type PaginatedResponse = {
totalCount: number;
results: object[];
meta: object;
};

export type ItemResponse = {
data: object;
};

export const parseObjectWithBigInt = (item) => {
return JSON.parse(JSON.stringify(item, (key, value) => (typeof value === "bigint" ? value.toString() : value)));
};

export const buildSenderWallet = (app: Application, passphrase: string | null = null): Contracts.State.Wallet => {
const walletRepository = app.get<Wallets.WalletRepository>(Identifiers.WalletRepository);

const wallet: Contracts.State.Wallet = walletRepository.createWallet(
Identities.Address.fromPassphrase(passphrase ? passphrase : passphrases[0]),
);

wallet.publicKey = Identities.PublicKey.fromPassphrase(passphrase ? passphrase : passphrases[0]);
wallet.balance = Utils.BigNumber.make(7527654310);

return wallet;
};

export const initApp = (): Application => {
const app = new Application(new Container.Container());
const logger = { error: jest.fn() };

app.bind(Container.Identifiers.LogService).toConstantValue(logger);

app.bind(Container.Identifiers.PluginConfiguration).to(Providers.PluginConfiguration).inSingletonScope();

app.bind(Container.Identifiers.StateStore).toConstantValue(Mocks.StateStore.instance);

app.bind(Container.Identifiers.BlockchainService).toConstantValue(Mocks.Blockchain.instance);

app.bind(Container.Identifiers.DatabaseBlockRepository).toConstantValue(Mocks.BlockRepository.instance);

app.bind(Container.Identifiers.DatabaseTransactionRepository).toConstantValue(Mocks.TransactionRepository.instance);

app.bind(Container.Identifiers.PeerNetworkMonitor).toConstantValue(Mocks.NetworkMonitor.instance);

app.bind(Container.Identifiers.PeerStorage).toConstantValue(Mocks.PeerStorage.instance);

app.bind(Container.Identifiers.DatabaseRoundRepository).toConstantValue(Mocks.RoundRepository.instance);

app.bind(Container.Identifiers.TransactionPoolQuery).toConstantValue(Mocks.TransactionPoolQuery.instance);

app.bind(Container.Identifiers.TransactionPoolProcessor).toConstantValue(Mocks.TransactionPoolProcessor.instance);

app.bind(Container.Identifiers.TransactionPoolProcessorFactory).toFactory(() => () => {
return Mocks.TransactionPoolProcessor.instance;
});

app.bind(Container.Identifiers.EventDispatcherService).toConstantValue({});

app.bind(Identifiers.TransactionHandler).to(One.TransferTransactionHandler);
app.bind(Identifiers.TransactionHandler).to(Two.TransferTransactionHandler);
app.bind(Identifiers.TransactionHandler).to(One.SecondSignatureRegistrationTransactionHandler);
app.bind(Identifiers.TransactionHandler).to(Two.SecondSignatureRegistrationTransactionHandler);
app.bind(Identifiers.TransactionHandler).to(One.DelegateRegistrationTransactionHandler);
app.bind(Identifiers.TransactionHandler).to(Two.DelegateRegistrationTransactionHandler);
app.bind(Identifiers.TransactionHandler).to(One.VoteTransactionHandler);
app.bind(Identifiers.TransactionHandler).to(Two.VoteTransactionHandler);
app.bind(Identifiers.TransactionHandler).to(One.MultiSignatureRegistrationTransactionHandler);
app.bind(Identifiers.TransactionHandler).to(Two.MultiSignatureRegistrationTransactionHandler);
app.bind(Identifiers.TransactionHandler).to(Two.IpfsTransactionHandler);
app.bind(Identifiers.TransactionHandler).to(Two.MultiPaymentTransactionHandler);
app.bind(Identifiers.TransactionHandler).to(Two.DelegateResignationTransactionHandler);
app.bind(Identifiers.TransactionHandler).to(Two.HtlcLockTransactionHandler);
app.bind(Identifiers.TransactionHandler).to(Two.HtlcClaimTransactionHandler);
app.bind(Identifiers.TransactionHandler).to(Two.HtlcRefundTransactionHandler);

app.bind(Identifiers.TransactionHandlerProvider).to(TransactionHandlerProvider).inSingletonScope();
app.bind(Identifiers.TransactionHandlerRegistry).to(TransactionHandlerRegistry).inSingletonScope();

app.bind(Identifiers.TransactionHandler).to(BusinessRegistrationTransactionHandler);
app.bind(Identifiers.TransactionHandler).to(BridgechainRegistrationTransactionHandler);

app.bind<Services.Attributes.AttributeSet>(Identifiers.WalletAttributes)
.to(Services.Attributes.AttributeSet)
.inSingletonScope();

app.bind<Contracts.State.WalletIndexerIndex>(Identifiers.WalletRepositoryIndexerIndex).toConstantValue({
name: Contracts.State.WalletIndexes.Addresses,
indexer: addressesIndexer,
autoIndex: true,
});

app.bind<Contracts.State.WalletIndexerIndex>(Container.Identifiers.WalletRepositoryIndexerIndex).toConstantValue({
name: Contracts.State.WalletIndexes.PublicKeys,
indexer: publicKeysIndexer,
autoIndex: true,
});

app.bind<Contracts.State.WalletIndexerIndex>(Container.Identifiers.WalletRepositoryIndexerIndex).toConstantValue({
name: Contracts.State.WalletIndexes.Usernames,
indexer: usernamesIndexer,
autoIndex: true,
});

app.bind<Contracts.State.WalletIndexerIndex>(Container.Identifiers.WalletRepositoryIndexerIndex).toConstantValue({
name: Contracts.State.WalletIndexes.Ipfs,
indexer: ipfsIndexer,
autoIndex: true,
});

app.bind<Contracts.State.WalletIndexerIndex>(Container.Identifiers.WalletRepositoryIndexerIndex).toConstantValue({
name: Contracts.State.WalletIndexes.Locks,
indexer: locksIndexer,
autoIndex: true,
});

app.bind<Contracts.State.WalletIndexerIndex>(Container.Identifiers.WalletRepositoryIndexerIndex).toConstantValue({
name: MagistrateIndex.Businesses,
indexer: businessIndexer,
autoIndex: true,
});

app.bind<Contracts.State.WalletIndexerIndex>(Container.Identifiers.WalletRepositoryIndexerIndex).toConstantValue({
name: MagistrateIndex.Bridgechains,
indexer: bridgechainIndexer,
autoIndex: true,
});

app.bind(Identifiers.WalletFactory).toFactory<Contracts.State.Wallet>(
(context: Container.interfaces.Context) => (address: string) =>
new Wallets.Wallet(
address,
new Services.Attributes.AttributeMap(
context.container.get<Services.Attributes.AttributeSet>(Identifiers.WalletAttributes),
),
),
);

app.bind(Identifiers.WalletRepository).to(Wallets.WalletRepository).inSingletonScope();

return app;
};
export * from "./app";
export * from "./server";
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Server } from "@packages/core-api/src";
import { preparePlugins } from "@packages/core-api/src/plugins";
import { Application, Container, Providers } from "@packages/core-kernel";

export const initServer = async (app: Application, defaults: any, customRoute: any): Promise<Server> => {
export const initServer = async (app: Application, defaults: any, customRoute?: any): Promise<Server> => {
const serverConfig = {
enabled: true,
host: "0.0.0.0",
Expand All @@ -14,6 +14,8 @@ export const initServer = async (app: Application, defaults: any, customRoute: a

app.bind(Server).to(Server);
const server = app.get(Server);
// @ts-ignore
server.app.app = app;

const pluginConfiguration = app.get<Providers.PluginConfiguration>(Container.Identifiers.PluginConfiguration);
const pluginConfigurationInstance: Providers.PluginConfiguration = pluginConfiguration.from("core-api", defaults);
Expand Down
Loading