Skip to content

Commit 38081e8

Browse files
committed
add shared data for all tests
1 parent dedd5dd commit 38081e8

11 files changed

+255
-151
lines changed

packages/tfchain_client/lib/src/contracts.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class Contracts extends QueryContracts {
106106
}
107107

108108
Future<void> updateNode(
109-
{required int contractId,
109+
{required BigInt contractId,
110110
required List<int> deploymentHash,
111111
required List<int> deploymentData}) async {
112112
final extrinsic = client.api.tx.smartContractModule.updateNodeContract(

packages/tfchain_client/test/balances_test.dart

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,39 @@ import 'package:test/test.dart';
44
import 'package:tfchain_client/generated/dev/types/frame_system/account_info.dart';
55
import 'package:tfchain_client/tfchain_client.dart';
66

7+
import 'shared_setup.dart';
8+
79
void main() {
810
group("Query Balances Test", () {
911
late QueryClient queryClient;
1012
final String url =
1113
Platform.environment['URL'] ?? 'wss://tfchain.dev.grid.tf/ws';
14+
sharedSetup();
15+
1216
setUp(() async {
1317
queryClient = QueryClient(url);
1418
await queryClient.connect();
1519
});
1620

17-
tearDownAll(() async {
18-
await queryClient.disconnect();
19-
});
20-
2121
test('Test Get Balance', () async {
22-
String address = "5CJrCjZvsudNoJApTGG5PKcZfhAzAyGqgSK8bysoCV2oRBMC";
2322
AccountInfo? accountInfo =
24-
await queryClient.balances.get(address: address);
23+
await queryClient.balances.get(address: myAddress);
2524
expect(accountInfo, isNotNull);
2625
});
2726

2827
test('Test Get Balance with Invalid address', () async {
29-
String address = "address";
3028
try {
3129
AccountInfo? accountInfo =
32-
await queryClient.balances.get(address: address);
30+
await queryClient.balances.get(address: invalidAddress);
3331
expect(accountInfo, isNull);
3432
} catch (error) {
3533
expect(error, isNotNull);
3634
}
3735
});
36+
37+
tearDownAll(() async {
38+
await queryClient.disconnect();
39+
});
3840
});
3941

4042
group("Test Balances", () {
@@ -43,31 +45,26 @@ void main() {
4345
final String url =
4446
Platform.environment['URL'] ?? 'wss://tfchain.dev.grid.tf/ws';
4547
final String type = Platform.environment['KEYPAIR_TYPE'] ?? 'sr25519';
48+
sharedSetup();
4649

4750
setUp(() async {
4851
client = Client(url, mnemonic, type);
4952
await client.connect();
5053
});
5154

52-
tearDownAll(() async {
53-
await client.disconnect();
54-
});
55-
5655
test('Test Transfer TFTs with invalid amount', () async {
5756
try {
58-
await client.balances.transfer(
59-
address: "5EcSXeEH35LriE2aWxX6v4yZSMq47vdJ1GgHEXDdhJxg9XjG",
60-
amount: BigInt.zero);
57+
await client.balances
58+
.transfer(address: recipientAddress, amount: BigInt.zero);
6159
} catch (error) {
6260
expect(error, isNotNull);
6361
}
6462
});
6563

6664
test('Test Transfer TFTs', () async {
6765
try {
68-
await client.balances.transfer(
69-
address: "5EcSXeEH35LriE2aWxX6v4yZSMq47vdJ1GgHEXDdhJxg9XjG",
70-
amount: BigInt.from(10));
66+
await client.balances
67+
.transfer(address: recipientAddress, amount: BigInt.from(10));
7168
} catch (error) {
7269
print(error);
7370
expect(error, isNull);
@@ -78,5 +75,9 @@ void main() {
7875
AccountInfo? info = await client.balances.getMyBalance();
7976
expect(info, isNotNull);
8077
});
78+
79+
tearDownAll(() async {
80+
await client.disconnect();
81+
});
8182
});
8283
}

packages/tfchain_client/test/client_test.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ void main() {
2323
await queryClient.connect();
2424
});
2525

26-
tearDownAll(() async {
27-
await queryClient.disconnect();
28-
});
29-
3026
test('Initialization', () {
3127
expect(queryClient.url, equals("wss://tfchain.dev.grid.tf/ws"));
3228
expect(queryClient.contracts, isA<QueryContracts>());
@@ -48,6 +44,10 @@ void main() {
4844
await queryClient.disconnect();
4945
expect(false, queryClient.provider!.isConnected());
5046
});
47+
48+
tearDownAll(() async {
49+
await queryClient.disconnect();
50+
});
5151
});
5252

5353
group("Full Client Tests", () {
@@ -65,10 +65,6 @@ void main() {
6565
await client.connect();
6666
});
6767

68-
tearDownAll(() async {
69-
await client.disconnect();
70-
});
71-
7268
test('Initialization', () {
7369
expect(client.url, equals("wss://tfchain.dev.grid.tf/ws"));
7470
expect(client.contracts, isA<Contracts>());
@@ -88,5 +84,9 @@ void main() {
8884
await client.disconnect();
8985
expect(false, client.provider!.isConnected());
9086
});
87+
88+
tearDownAll(() async {
89+
await client.disconnect();
90+
});
9191
});
9292
}

packages/tfchain_client/test/contracts_test.dart

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,40 @@ import 'package:tfchain_client/generated/dev/types/pallet_smart_contract/types/c
55
import 'package:tfchain_client/generated/dev/types/pallet_smart_contract/types/contract_lock.dart';
66
import 'package:tfchain_client/tfchain_client.dart';
77

8+
import 'shared_setup.dart';
9+
810
void main() {
911
group("Query Contracts Test", () {
1012
late QueryClient queryClient;
1113
final String url =
1214
Platform.environment['URL'] ?? 'wss://tfchain.dev.grid.tf/ws';
15+
sharedSetup();
16+
1317
setUp(() async {
1418
queryClient = QueryClient(url);
1519
await queryClient.connect();
1620
});
1721

18-
tearDownAll(() async {
19-
await queryClient.disconnect();
20-
});
21-
2222
test('Test Get Deleted Contract by Id', () async {
2323
Contract? contract =
24-
await queryClient.contracts.get(contractId: BigInt.from(97897));
24+
await queryClient.contracts.get(contractId: deletedContract);
2525
expect(contract, null);
2626
});
2727

2828
test('Test Get Contract with wrong id', () async {
2929
try {
3030
Contract? contract =
31-
await queryClient.contracts.get(contractId: BigInt.from(-200));
31+
await queryClient.contracts.get(contractId: invalidContractId);
3232
expect(contract, null);
3333
} catch (error) {
3434
expect(error, isNotNull);
3535
}
3636
});
3737

38-
test('Test Get Contract Id by active rent for node', () async {
38+
test('Test Get Contract Id by active rent for invalid node id', () async {
3939
try {
4040
BigInt? contractId = await queryClient.contracts
41-
.getContractIdByActiveRentForNode(nodeId: -200);
41+
.getContractIdByActiveRentForNode(nodeId: invalidNodeId);
4242
expect(contractId, isNotNull);
4343
} catch (error) {
4444
expect(error, isNotNull);
@@ -47,15 +47,15 @@ void main() {
4747

4848
test('Test Get Active Contracts by node Id', () async {
4949
List<int> contracts =
50-
await queryClient.contracts.getActiveContracts(nodeId: 21);
50+
await queryClient.contracts.getActiveContracts(nodeId: nodeId);
5151
print(contracts);
5252
expect(contracts, isNotEmpty);
5353
});
5454

5555
test('Test Get Active Contracts by wrong node Id', () async {
5656
try {
57-
List<int> contracts =
58-
await queryClient.contracts.getActiveContracts(nodeId: -21);
57+
List<int> contracts = await queryClient.contracts
58+
.getActiveContracts(nodeId: invalidNodeId);
5959
expect(contracts, isNotEmpty);
6060
} catch (error) {
6161
expect(error, isNotNull);
@@ -65,7 +65,7 @@ void main() {
6565
test('Test Get Contract Id by name', () async {
6666
try {
6767
BigInt? contractId =
68-
await queryClient.contracts.getContractIdByName(name: "name");
68+
await queryClient.contracts.getContractIdByName(name: nameContract);
6969
expect(contractId, isNotEmpty);
7070
} catch (error) {
7171
expect(error, isNotNull);
@@ -75,30 +75,31 @@ void main() {
7575
test('Test Get Contract Id by node Id and hash', () async {
7676
try {
7777
BigInt? contractId = await queryClient.contracts
78-
.getContractIdByNodeIdAndHash(nodeId: 21, hash: []);
78+
.getContractIdByNodeIdAndHash(nodeId: nodeId, hash: []);
7979
expect(contractId, isNotEmpty);
8080
} catch (error) {
8181
expect(error, isNotNull);
8282
}
8383
});
8484

85-
test('Test Get Contract Lock by Id', () async {
85+
test('Test Get Contract Lock by deleted Contract Id', () async {
8686
try {
8787
ContractLock? contractLock =
88-
await queryClient.contracts.contractLock(id: BigInt.from(97897));
89-
print(contractLock);
90-
expect(contractLock, isNotEmpty);
88+
await queryClient.contracts.contractLock(id: deletedContract);
9189
} catch (error) {
9290
expect(error, isNotNull);
9391
}
9492
});
9593

9694
test('Test Get a Dedicated Node extra fee', () async {
9795
BigInt? fee =
98-
await queryClient.contracts.getDedicatedNodeExtraFee(nodeId: 140);
99-
print(fee);
96+
await queryClient.contracts.getDedicatedNodeExtraFee(nodeId: myNode);
10097
expect(fee, isNotNull);
10198
});
99+
100+
tearDownAll(() async {
101+
await queryClient.disconnect();
102+
});
102103
});
103104

104105
group("Test Contracts", () {
@@ -108,23 +109,13 @@ void main() {
108109
Platform.environment['URL'] ?? 'wss://tfchain.dev.grid.tf/ws';
109110
final String type = Platform.environment['KEYPAIR_TYPE'] ?? 'sr25519';
110111
BigInt? contractId = null;
112+
sharedSetup();
111113

112114
setUp(() async {
113115
client = Client(url, mnemonic, type);
114116
await client.connect();
115117
});
116118

117-
tearDownAll(() async {
118-
if (contractId != null) {
119-
try {
120-
await client.contracts.cancel(contractId: contractId!);
121-
} catch (error) {
122-
print("Error canceling contract: $error");
123-
}
124-
}
125-
await client.disconnect();
126-
});
127-
128119
test('Test Create Node Contract with empty data', () async {
129120
try {
130121
BigInt? contractId = await client.contracts.createNode(
@@ -136,22 +127,25 @@ void main() {
136127

137128
test('Test Update Node Contract with wrong data', () async {
138129
try {
139-
await client.contracts
140-
.updateNode(contractId: 11, deploymentData: [], deploymentHash: []);
130+
await client.contracts.updateNode(
131+
contractId: invalidContractId,
132+
deploymentData: [],
133+
deploymentHash: []);
141134
} catch (error) {
142135
expect(error, isNotNull);
143136
}
144137
});
145138

146139
test('Test Create Name Contract', () async {
147-
contractId = await client.contracts.createName(name: "xxx");
140+
contractId =
141+
await client.contracts.createName(name: generateRandomString(5));
148142
expect(contractId, isNotNull);
149143
});
150144

151145
test('Test Create Rent Contract with no solution provider', () async {
152146
try {
153147
BigInt? contractId = await client.contracts
154-
.createRent(nodeId: 72, solutionProviderId: BigInt.zero);
148+
.createRent(nodeId: rentNode, solutionProviderId: BigInt.zero);
155149
} catch (error) {
156150
expect(error.toString(), contains('NoSuchSolutionProvider'));
157151
}
@@ -160,10 +154,24 @@ void main() {
160154
test('Test set dedicated node extra fee', () async {
161155
try {
162156
await client.contracts
163-
.setDedicatedNodeExtraFee(nodeId: 140, extraFee: BigInt.two);
157+
.setDedicatedNodeExtraFee(nodeId: myNode, extraFee: BigInt.two);
164158
} catch (error) {
165159
expect(error, isNotNull);
166160
}
167161
});
162+
163+
tearDown(() async {
164+
if (contractId != null) {
165+
try {
166+
await client.contracts.cancel(contractId: contractId!);
167+
} catch (error) {
168+
print("Error canceling contract: $error");
169+
}
170+
}
171+
});
172+
173+
tearDownAll(() async {
174+
await client.disconnect();
175+
});
168176
});
169177
}

0 commit comments

Comments
 (0)