Skip to content

Commit c160612

Browse files
author
KovacZan
committed
test: test cases for builder, transaction and handler test suites
1 parent e074b15 commit c160612

File tree

8 files changed

+384
-3
lines changed

8 files changed

+384
-3
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
let mockBlock: any | null;
2+
3+
export const setMockBlock = (block: any | null) => {
4+
mockBlock = block;
5+
};
6+
7+
export const blockRepository = {
8+
getDelegatesForgedBlocks: async () => {
9+
return mockBlock ? [mockBlock] : [];
10+
},
11+
getLastForgedBlocks: async () => {
12+
return mockBlock ? [mockBlock] : [];
13+
},
14+
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { SearchFilter } from "@arkecosystem/core-database/src/repositories/search";
2+
import { ITransaction } from "@arkecosystem/crypto/src/interfaces";
3+
4+
let mockTransaction: ITransaction | null;
5+
let mockTransactions: [ITransaction];
6+
7+
export const setMockTransaction = (transaction: ITransaction | null) => {
8+
mockTransaction = transaction;
9+
};
10+
11+
export const setMockTransactions = (transactions: [ITransaction]) => {
12+
mockTransactions = transactions;
13+
};
14+
15+
export const transactionRepository = {
16+
findByIds: async () => {
17+
return mockTransaction ? [mockTransaction.data] : [];
18+
},
19+
findByType: async () => {
20+
return mockTransaction ? [mockTransaction.data] : [];
21+
},
22+
findReceivedTransactions() {
23+
return mockTransaction ? [mockTransaction.data] : [];
24+
},
25+
getOpenHtlcLocks() {
26+
return mockTransaction ? [mockTransaction.data] : [];
27+
},
28+
getClaimedHtlcLockBalances() {
29+
return mockTransaction
30+
? [{ amount: mockTransaction.data.amount, recipientId: mockTransaction.data.recipientId }]
31+
: [];
32+
},
33+
getRefundedHtlcLockBalances() {
34+
return mockTransaction
35+
? [{ amount: mockTransaction.data.amount, senderPublicKey: mockTransaction.data.senderPublicKey }]
36+
: [];
37+
},
38+
search(filter: SearchFilter): any {
39+
const type = filter.criteria.find(x => x.field === "type");
40+
return {
41+
// @ts-ignore
42+
rows: mockTransactions.filter(x => x.data.type === type.value).map(x => x.data),
43+
};
44+
},
45+
};

__tests__/unit/__support__/app.ts

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
import { Application, Container, Contracts, Providers, Services } from "@arkecosystem/core-kernel";
2+
import { Identifiers } from "@arkecosystem/core-kernel/src/ioc";
3+
import { NullEventDispatcher } from "@arkecosystem/core-kernel/src/services/events/drivers/null";
4+
import { Wallets } from "@arkecosystem/core-state";
5+
import { StateStore } from "@arkecosystem/core-state/src/stores/state";
6+
import {
7+
addressesIndexer,
8+
ipfsIndexer,
9+
locksIndexer,
10+
publicKeysIndexer,
11+
usernamesIndexer,
12+
} from "@arkecosystem/core-state/src/wallets/indexers/indexers";
13+
import { Collator } from "@arkecosystem/core-transaction-pool/src";
14+
import { DynamicFeeMatcher } from "@arkecosystem/core-transaction-pool/src/dynamic-fee-matcher";
15+
import { ExpirationService } from "@arkecosystem/core-transaction-pool/src/expiration-service";
16+
import { Query } from "@arkecosystem/core-transaction-pool/src/query";
17+
import { SenderState } from "@arkecosystem/core-transaction-pool/src/sender-state";
18+
import { One, Two } from "@arkecosystem/core-transactions/src/handlers";
19+
import { TransactionHandlerProvider } from "@arkecosystem/core-transactions/src/handlers/handler-provider";
20+
import { TransactionHandlerRegistry } from "@arkecosystem/core-transactions/src/handlers/handler-registry";
21+
import { Identities, Utils } from "@arkecosystem/crypto";
22+
import { Mempool } from "@arkecosystem/core-transaction-pool/src/mempool";
23+
24+
// eslint-disable-next-line jest/no-mocks-import
25+
import { blockRepository } from "../__mocks__/block-repository";
26+
// eslint-disable-next-line jest/no-mocks-import
27+
import { transactionRepository } from "../__mocks__/transaction-repository";
28+
29+
const logger = {
30+
notice: jest.fn(),
31+
debug: jest.fn(),
32+
warning: jest.fn(),
33+
};
34+
35+
export const initApp = (): Application => {
36+
const app: Application = new Application(new Container.Container());
37+
app.bind(Identifiers.ApplicationNamespace).toConstantValue("testnet");
38+
39+
app.bind(Identifiers.LogService).toConstantValue(logger);
40+
41+
app.bind<Services.Attributes.AttributeSet>(Identifiers.WalletAttributes)
42+
.to(Services.Attributes.AttributeSet)
43+
.inSingletonScope();
44+
45+
app.bind<Contracts.State.WalletIndexerIndex>(Identifiers.WalletRepositoryIndexerIndex).toConstantValue({
46+
name: Contracts.State.WalletIndexes.Addresses,
47+
indexer: addressesIndexer,
48+
});
49+
50+
app.bind<Contracts.State.WalletIndexerIndex>(Container.Identifiers.WalletRepositoryIndexerIndex).toConstantValue({
51+
name: Contracts.State.WalletIndexes.PublicKeys,
52+
indexer: publicKeysIndexer,
53+
});
54+
55+
app.bind<Contracts.State.WalletIndexerIndex>(Container.Identifiers.WalletRepositoryIndexerIndex).toConstantValue({
56+
name: Contracts.State.WalletIndexes.Usernames,
57+
indexer: usernamesIndexer,
58+
});
59+
60+
app.bind<Contracts.State.WalletIndexerIndex>(Container.Identifiers.WalletRepositoryIndexerIndex).toConstantValue({
61+
name: Contracts.State.WalletIndexes.Ipfs,
62+
indexer: ipfsIndexer,
63+
});
64+
65+
app.bind<Contracts.State.WalletIndexerIndex>(Container.Identifiers.WalletRepositoryIndexerIndex).toConstantValue({
66+
name: Contracts.State.WalletIndexes.Locks,
67+
indexer: locksIndexer,
68+
});
69+
70+
app.bind(Identifiers.WalletFactory).toFactory<Contracts.State.Wallet>(
71+
(context: Container.interfaces.Context) => (address: string) =>
72+
new Wallets.Wallet(
73+
address,
74+
new Services.Attributes.AttributeMap(
75+
context.container.get<Services.Attributes.AttributeSet>(Identifiers.WalletAttributes),
76+
),
77+
),
78+
);
79+
80+
app.bind(Container.Identifiers.PluginConfiguration).to(Providers.PluginConfiguration).inSingletonScope();
81+
82+
app.get<Providers.PluginConfiguration>(Container.Identifiers.PluginConfiguration).set("maxTransactionAge", 500);
83+
app.get<Providers.PluginConfiguration>(Container.Identifiers.PluginConfiguration).set(
84+
"maxTransactionBytes",
85+
2000000,
86+
);
87+
app.get<Providers.PluginConfiguration>(Container.Identifiers.PluginConfiguration).set(
88+
"maxTransactionsPerSender",
89+
300,
90+
);
91+
92+
app.bind(Container.Identifiers.StateStore).to(StateStore).inTransientScope();
93+
94+
app.bind(Identifiers.TransactionPoolMempool).to(Mempool).inSingletonScope();
95+
96+
app.bind(Identifiers.TransactionPoolQuery).to(Query).inSingletonScope();
97+
98+
app.bind(Container.Identifiers.TransactionPoolCollator).to(Collator);
99+
app.bind(Container.Identifiers.TransactionPoolDynamicFeeMatcher).to(DynamicFeeMatcher);
100+
app.bind(Container.Identifiers.TransactionPoolExpirationService).to(ExpirationService);
101+
102+
app.bind(Container.Identifiers.TransactionPoolSenderState).to(SenderState);
103+
app.bind(Container.Identifiers.TransactionPoolSenderMempoolFactory).toAutoFactory(
104+
Container.Identifiers.TransactionPoolSenderState,
105+
);
106+
107+
app.bind(Identifiers.WalletRepository).to(Wallets.WalletRepository).inSingletonScope();
108+
109+
app.bind(Identifiers.EventDispatcherService).to(NullEventDispatcher).inSingletonScope();
110+
111+
app.bind(Identifiers.BlockRepository).toConstantValue(blockRepository);
112+
113+
app.bind(Identifiers.TransactionRepository).toConstantValue(transactionRepository);
114+
115+
app.bind(Identifiers.TransactionHandler).to(One.TransferTransactionHandler);
116+
app.bind(Identifiers.TransactionHandler).to(Two.TransferTransactionHandler);
117+
app.bind(Identifiers.TransactionHandler).to(One.SecondSignatureRegistrationTransactionHandler);
118+
app.bind(Identifiers.TransactionHandler).to(Two.SecondSignatureRegistrationTransactionHandler);
119+
app.bind(Identifiers.TransactionHandler).to(One.DelegateRegistrationTransactionHandler);
120+
app.bind(Identifiers.TransactionHandler).to(Two.DelegateRegistrationTransactionHandler);
121+
app.bind(Identifiers.TransactionHandler).to(One.VoteTransactionHandler);
122+
app.bind(Identifiers.TransactionHandler).to(Two.VoteTransactionHandler);
123+
app.bind(Identifiers.TransactionHandler).to(One.MultiSignatureRegistrationTransactionHandler);
124+
app.bind(Identifiers.TransactionHandler).to(Two.MultiSignatureRegistrationTransactionHandler);
125+
app.bind(Identifiers.TransactionHandler).to(Two.IpfsTransactionHandler);
126+
app.bind(Identifiers.TransactionHandler).to(Two.MultiPaymentTransactionHandler);
127+
app.bind(Identifiers.TransactionHandler).to(Two.DelegateResignationTransactionHandler);
128+
app.bind(Identifiers.TransactionHandler).to(Two.HtlcLockTransactionHandler);
129+
app.bind(Identifiers.TransactionHandler).to(Two.HtlcClaimTransactionHandler);
130+
app.bind(Identifiers.TransactionHandler).to(Two.HtlcRefundTransactionHandler);
131+
132+
app.bind(Identifiers.TransactionHandlerProvider).to(TransactionHandlerProvider).inSingletonScope();
133+
app.bind(Identifiers.TransactionHandlerRegistry).to(TransactionHandlerRegistry).inSingletonScope();
134+
135+
return app;
136+
};
137+
138+
export const buildWallet = (app: Application, passphrase: string): Contracts.State.Wallet => {
139+
const walletRepository = app.get<Wallets.WalletRepository>(Identifiers.WalletRepository);
140+
141+
const wallet: Contracts.State.Wallet = walletRepository.createWallet(Identities.Address.fromPassphrase(passphrase));
142+
wallet.address = Identities.Address.fromPassphrase(passphrase);
143+
wallet.publicKey = Identities.PublicKey.fromPassphrase(passphrase);
144+
wallet.balance = Utils.BigNumber.make(7527654310);
145+
146+
return wallet;
147+
};

__tests__/test.test.ts renamed to __tests__/unit/builders/business-data.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import "jest-extended";
22

33
import { Managers, Transactions } from "@arkecosystem/crypto";
4-
import { BusinessDataTransaction } from "../src/transactions";
5-
import { BusinessDataBuilder } from "../src/builders";
4+
5+
import { BusinessDataBuilder } from "../../../src/builders";
6+
import { BusinessDataTransaction } from "../../../src/transactions";
67

78
describe("Test builder", () => {
89
it("should verify correctly", () => {
910
Managers.configManager.setFromPreset("testnet");
11+
Managers.configManager.setHeight(2);
1012
Transactions.TransactionRegistry.registerTransactionType(BusinessDataTransaction);
1113

1214
const actual = new BusinessDataBuilder()
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import "jest-extended";
2+
3+
import { Application, Contracts } from "@arkecosystem/core-kernel";
4+
import { Identifiers } from "@arkecosystem/core-kernel/src/ioc";
5+
import { Wallets } from "@arkecosystem/core-state";
6+
import { Generators } from "@arkecosystem/core-test-framework/src";
7+
import passphrases from "@arkecosystem/core-test-framework/src/internal/passphrases.json";
8+
import { TransactionHandler } from "@arkecosystem/core-transactions/src/handlers";
9+
import { TransactionHandlerRegistry } from "@arkecosystem/core-transactions/src/handlers/handler-registry";
10+
import { Managers, Transactions } from "@arkecosystem/crypto";
11+
import { configManager } from "@arkecosystem/crypto/src/managers";
12+
13+
import { setMockTransaction } from "../__mocks__/transaction-repository";
14+
import { buildWallet, initApp } from "../__support__/app";
15+
import { BusinessDataBuilder } from "../../../src/builders";
16+
import { BusinessDataType, BusinessDataTypeGroup } from "../../../src/constants";
17+
import { BusinessAlreadyHasData } from "../../../src/errors";
18+
import { BusinessDataHandler } from "../../../src/handlers";
19+
import { IBusinessData } from "../../../src/interfaces";
20+
import { BusinessDataTransaction } from "../../../src/transactions";
21+
22+
let app: Application;
23+
24+
let wallet: Contracts.State.Wallet;
25+
26+
let walletRepository: Contracts.State.WalletRepository;
27+
28+
let transactionHandlerRegistry: TransactionHandlerRegistry;
29+
30+
let handler: TransactionHandler;
31+
32+
beforeEach(() => {
33+
const config = Generators.generateCryptoConfigRaw();
34+
configManager.setConfig(config);
35+
Managers.configManager.setConfig(config);
36+
app = initApp();
37+
wallet = buildWallet(app, passphrases[0]);
38+
app.bind(Identifiers.TransactionHandler).to(BusinessDataHandler);
39+
40+
walletRepository = app.get<Wallets.WalletRepository>(Identifiers.WalletRepository);
41+
42+
transactionHandlerRegistry = app.get<TransactionHandlerRegistry>(Identifiers.TransactionHandlerRegistry);
43+
44+
handler = transactionHandlerRegistry.getRegisteredHandlerByType(
45+
Transactions.InternalTransactionType.from(BusinessDataType, BusinessDataTypeGroup),
46+
2,
47+
);
48+
walletRepository.index(wallet);
49+
});
50+
51+
afterEach(() => {
52+
Transactions.TransactionRegistry.deregisterTransactionType(BusinessDataTransaction);
53+
});
54+
55+
describe("Business data tests", () => {
56+
describe("bootstrap tests", () => {
57+
it("should test bootstrap method", async () => {
58+
const actual = new BusinessDataBuilder()
59+
.businessDataAsset({
60+
name: "google",
61+
website: "https://google.com",
62+
})
63+
.nonce("1")
64+
.sign(passphrases[0])
65+
.build();
66+
67+
setMockTransaction(actual);
68+
69+
await expect(handler.bootstrap()).toResolve();
70+
71+
expect(wallet.getAttribute<IBusinessData>("businessData")).toStrictEqual({
72+
name: "google",
73+
website: "https://google.com",
74+
});
75+
});
76+
});
77+
78+
describe("throwIfCannotBeApplied tests", () => {
79+
it("should rejects because wallet is already a business", async () => {
80+
wallet.setAttribute<IBusinessData>("businessData", {
81+
name: "google",
82+
website: "https://google.com",
83+
});
84+
85+
const actual = new BusinessDataBuilder()
86+
.businessDataAsset({
87+
name: "google",
88+
website: "https://google.com",
89+
})
90+
.nonce("1")
91+
.sign(passphrases[0])
92+
.build();
93+
94+
setMockTransaction(actual);
95+
96+
await expect(handler.throwIfCannotBeApplied(actual, wallet, walletRepository)).rejects.toThrow(
97+
BusinessAlreadyHasData,
98+
);
99+
});
100+
});
101+
102+
describe("apply tests", () => {
103+
it("should apply correctly", async () => {
104+
const actual = new BusinessDataBuilder()
105+
.businessDataAsset({
106+
name: "google",
107+
website: "https://google.com",
108+
})
109+
.nonce("1")
110+
.sign(passphrases[0])
111+
.build();
112+
113+
await expect(handler.apply(actual, walletRepository)).toResolve();
114+
115+
expect(wallet.getAttribute<IBusinessData>("businessData")).toStrictEqual({
116+
name: "google",
117+
website: "https://google.com",
118+
});
119+
});
120+
});
121+
122+
describe("revert tests", () => {
123+
it("should revert correctly", async () => {
124+
const actual = new BusinessDataBuilder()
125+
.businessDataAsset({
126+
name: "google",
127+
website: "https://google.com",
128+
})
129+
.nonce("1")
130+
.sign(passphrases[0])
131+
.build();
132+
133+
await handler.apply(actual, walletRepository);
134+
135+
await expect(handler.revert(actual, walletRepository)).toResolve();
136+
137+
expect(wallet.hasAttribute("businessData")).toBeFalsy();
138+
});
139+
});
140+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import "jest-extended";
2+
3+
import { Managers, Transactions } from "@arkecosystem/crypto";
4+
5+
import { BusinessDataBuilder } from "../../../src/builders";
6+
import { BusinessDataTransaction } from "../../../src/transactions";
7+
8+
describe("Test builder", () => {
9+
it("should verify correctly", () => {
10+
Managers.configManager.setFromPreset("testnet");
11+
Managers.configManager.setHeight(2);
12+
Transactions.TransactionRegistry.registerTransactionType(BusinessDataTransaction);
13+
14+
const actual = new BusinessDataBuilder()
15+
.businessDataAsset({
16+
name: "google",
17+
website: "https://google.com",
18+
})
19+
.nonce("3")
20+
.sign("clay harbor enemy utility margin pretty hub comic piece aerobic umbrella acquire")
21+
.getStruct();
22+
23+
const serialized = Transactions.TransactionFactory.fromData(actual).serialized.toString("hex");
24+
const deserialized = Transactions.Deserializer.deserialize(serialized);
25+
26+
// @ts-ignore
27+
expect(deserialized.data.asset.businessData).toStrictEqual({
28+
name: "google",
29+
website: "https://google.com",
30+
});
31+
});
32+
});

0 commit comments

Comments
 (0)