git.delta.rocks / unique-network / refs/commits / d0295dcff9ad

difftreelog

refactor make scheduler test more explicit

Daniel Shiposha2022-08-17parent: #e5b072a.patch.diff
in: master

1 file changed

modifiedtests/src/.outdated/scheduler.test.tsdiffbeforeafterboth
102 const period = 2;102 const period = 2;
103 const repetitions = 2;103 const repetitions = 2;
104
105 const amount = 1n * UNIQUE;
104106
105 await scheduleTransferFundsExpectSuccess(api, 1n * UNIQUE, alice, bob, waitForBlocks, makeScheduledId(), period, repetitions);107 await scheduleTransferFundsExpectSuccess(api, amount, alice, bob, waitForBlocks, makeScheduledId(), period, repetitions);
106 const bobsBalanceBefore = await getFreeBalance(bob);108 const bobsBalanceBefore = await getFreeBalance(bob);
107109
108 await waitNewBlocks(waitForBlocks + 1);110 await waitNewBlocks(waitForBlocks + 1);
109 const bobsBalanceAfterFirst = await getFreeBalance(bob);111 const bobsBalanceAfterFirst = await getFreeBalance(bob);
110 expect(bobsBalanceAfterFirst > bobsBalanceBefore, '#1 Balance of the recipient did not increase').to.be.true;112 expect(bobsBalanceAfterFirst)
113 .to.be.equal(
114 bobsBalanceBefore + 1n * amount,
115 '#1 Balance of the recipient should be increased by 1 * amount',
116 );
111117
112 await waitNewBlocks(period);118 await waitNewBlocks(period);
113 const bobsBalanceAfterSecond = await getFreeBalance(bob);119 const bobsBalanceAfterSecond = await getFreeBalance(bob);
114 expect(bobsBalanceAfterSecond > bobsBalanceAfterFirst, '#2 Balance of the recipient did not increase').to.be.true;120 expect(bobsBalanceAfterSecond)
121 .to.be.equal(
122 bobsBalanceBefore + 2n * amount,
123 '#2 Balance of the recipient should be increased by 2 * amount',
124 );
115 });125 });
116 });126 });
117127
122 const scheduledId = makeScheduledId();132 const scheduledId = makeScheduledId();
123 const waitForBlocks = 4;133 const waitForBlocks = 4;
134
135 const amount = 1;
124136
125 await scheduleTransferExpectSuccess(api, collectionId, tokenId, alice, bob, 1, waitForBlocks, scheduledId);137 await scheduleTransferExpectSuccess(api, collectionId, tokenId, alice, bob, amount, waitForBlocks, scheduledId);
126 await expect(cancelScheduled(api, alice, scheduledId)).to.not.be.rejected;138 await expect(cancelScheduled(api, alice, scheduledId)).to.not.be.rejected;
127139
128 await waitNewBlocks(waitForBlocks);140 await waitNewBlocks(waitForBlocks);
138 const repetitions = 2;150 const repetitions = 2;
139151
140 const scheduledId = makeScheduledId();152 const scheduledId = makeScheduledId();
153 const amount = 1n * UNIQUE;
141154
142 const bobsBalanceBefore = await getFreeBalance(bob);155 const bobsBalanceBefore = await getFreeBalance(bob);
143 await scheduleTransferFundsExpectSuccess(api, 1n * UNIQUE, alice, bob, waitForBlocks, scheduledId, period, repetitions);156 await scheduleTransferFundsExpectSuccess(api, amount, alice, bob, waitForBlocks, scheduledId, period, repetitions);
144157
145 await waitNewBlocks(waitForBlocks + 1);158 await waitNewBlocks(waitForBlocks + 1);
146 const bobsBalanceAfterFirst = await getFreeBalance(bob);159 const bobsBalanceAfterFirst = await getFreeBalance(bob);
147 expect(bobsBalanceAfterFirst > bobsBalanceBefore, '#1 Balance of the recipient did not increase').to.be.true;160 expect(bobsBalanceAfterFirst)
161 .to.be.equal(
162 bobsBalanceBefore + 1n * amount,
163 '#1 Balance of the recipient should be increased by 1 * amount',
164 );
148165
149 await expect(cancelScheduled(api, alice, scheduledId)).to.not.be.rejected;166 await expect(cancelScheduled(api, alice, scheduledId)).to.not.be.rejected;
150167
151 await waitNewBlocks(period);168 await waitNewBlocks(period);
152 const bobsBalanceAfterSecond = await getFreeBalance(bob);169 const bobsBalanceAfterSecond = await getFreeBalance(bob);
153 expect(bobsBalanceAfterSecond == bobsBalanceAfterFirst, '#2 Balance of the recipient changed').to.be.true;170 expect(bobsBalanceAfterSecond)
171 .to.be.equal(
172 bobsBalanceAfterFirst,
173 '#2 Balance of the recipient should not be changed',
174 );
154 });175 });
155 });176 });
156177
178 // FIXME What purpose of this test?
157 it('Can schedule a scheduled operation of canceling the scheduled operation', async () => {179 it.skip('Can schedule a scheduled operation of canceling the scheduled operation', async () => {
158 await usingApi(async api => {180 await usingApi(async api => {
159 const scheduledId = makeScheduledId();181 const scheduledId = makeScheduledId();
160182
176 await waitNewBlocks(waitForBlocks);197 await waitNewBlocks(waitForBlocks);
177198
178 // todo:scheduler debug line; doesn't work (and doesn't appear in events) when executed in the same block as the scheduled transaction199 // todo:scheduler debug line; doesn't work (and doesn't appear in events) when executed in the same block as the scheduled transaction
179 //await expect(executeTransaction(api, alice, api.tx.scheduler.cancelNamed(scheduledId))).to.not.be.rejected;200 await expect(submitTransactionAsync(alice, api.tx.scheduler.cancelNamed(scheduledId))).to.not.be.rejected;
180201
181 let schedulerEvents = 0;202 let schedulerEvents = 0;
182 for (let i = 0; i < period * repetitions; i++) {203 for (let i = 0; i < period * repetitions; i++) {
205 });226 });
206 });227 });
207228
208 it('Can\'t overwrite a scheduled ID', async () => {229 it("Can't overwrite a scheduled ID", async () => {
209 await usingApi(async api => {230 await usingApi(async api => {
210 const collectionId = await createCollectionExpectSuccess();231 const collectionId = await createCollectionExpectSuccess();
211 const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT');232 const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT');
212 const scheduledId = makeScheduledId();233 const scheduledId = makeScheduledId();
234 const waitForBlocks = 4;
235 const amount = 1;
213236
214 await scheduleTransferExpectSuccess(api, collectionId, tokenId, alice, bob, 1, 4, scheduledId);237 await scheduleTransferExpectSuccess(api, collectionId, tokenId, alice, bob, amount, waitForBlocks, scheduledId);
215 await expect(scheduleAfter(238 await expect(scheduleAfter(
216 api, 239 api,
217 api.tx.balances.transfer(alice.address, 1n * UNIQUE), 240 api.tx.balances.transfer(alice.address, 1n * UNIQUE),
218 bob, 241 bob,
219 2, 242 /* period = */ 2,
220 scheduledId,243 scheduledId,
221 )).to.be.rejectedWith(/scheduler\.FailedToSchedule/);244 )).to.be.rejectedWith(/scheduler\.FailedToSchedule/);
245
222 const bobsBalance = await getFreeBalance(bob);246 const bobsBalanceBefore = await getFreeBalance(bob);
223247
224 await waitNewBlocks(3);248 await waitNewBlocks(waitForBlocks);
225249
226 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal(normalizeAccountId(bob.address));250 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal(normalizeAccountId(bob.address));
227 expect(bobsBalance).to.be.equal(await getFreeBalance(bob));251 expect(bobsBalanceBefore).to.be.equal(await getFreeBalance(bob));
228 });252 });
229 });253 });
230254
231 it('Can\'t cancel an operation which is not scheduled', async () => {255 it("Can't cancel an operation which is not scheduled", async () => {
232 await usingApi(async api => {256 await usingApi(async api => {
233 const scheduledId = makeScheduledId();257 const scheduledId = makeScheduledId();
234 await expect(cancelScheduled(api, alice, scheduledId)).to.be.rejectedWith(/scheduler\.NotFound/);258 await expect(cancelScheduled(api, alice, scheduledId)).to.be.rejectedWith(/scheduler\.NotFound/);
235 });259 });
236 });260 });
237261
238 it('Can\'t cancel a non-owned scheduled operation', async () => {262 it("Can't cancel a non-owned scheduled operation", async () => {
239 await usingApi(async api => {263 await usingApi(async api => {
240 const collectionId = await createCollectionExpectSuccess();264 const collectionId = await createCollectionExpectSuccess();
241 const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT');265 const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT');
242 const scheduledId = makeScheduledId();266 const scheduledId = makeScheduledId();
243 const waitForBlocks = 3;267 const waitForBlocks = 3;
268
269 const amount = 1;
244270
245 await scheduleTransferExpectSuccess(api, collectionId, tokenId, alice, bob, 1, waitForBlocks, scheduledId);271 await scheduleTransferExpectSuccess(api, collectionId, tokenId, alice, bob, amount, waitForBlocks, scheduledId);
246 await expect(cancelScheduled(api, bob, scheduledId)).to.be.rejected;272 await expect(cancelScheduled(api, bob, scheduledId)).to.be.rejected;
247273
248 await waitNewBlocks(waitForBlocks);274 await waitNewBlocks(waitForBlocks);