fixtures.rs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  1. use super::mock::{
  2. Balances, Membership, System, Test, TestEvent, TestWorkingGroup, TestWorkingGroupInstance,
  3. };
  4. use crate::tests::fill_worker_position;
  5. use crate::types::{
  6. Application, Opening, OpeningPolicyCommitment, OpeningType, RewardPolicy, RoleStakeProfile,
  7. Worker,
  8. };
  9. use crate::Error;
  10. use crate::RawEvent;
  11. use common::constraints::InputValidationLengthConstraint;
  12. use srml_support::{StorageLinkedMap, StorageValue};
  13. use std::collections::BTreeSet;
  14. use system::{EventRecord, Phase, RawOrigin};
  15. pub struct IncreaseWorkerStakeFixture {
  16. origin: RawOrigin<u64>,
  17. worker_id: u64,
  18. balance: u64,
  19. account_id: u64,
  20. }
  21. impl IncreaseWorkerStakeFixture {
  22. pub fn default_for_worker_id(worker_id: u64) -> Self {
  23. let account_id = 1;
  24. Self {
  25. origin: RawOrigin::Signed(1),
  26. worker_id,
  27. balance: 10,
  28. account_id,
  29. }
  30. }
  31. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  32. Self { origin, ..self }
  33. }
  34. pub fn with_balance(self, balance: u64) -> Self {
  35. Self { balance, ..self }
  36. }
  37. pub fn call_and_assert(&self, expected_result: Result<(), Error>) {
  38. let stake_id = 0;
  39. let old_stake = <stake::Module<Test>>::stakes(stake_id);
  40. let old_balance = Balances::free_balance(&self.account_id);
  41. let actual_result = TestWorkingGroup::increase_stake(
  42. self.origin.clone().into(),
  43. self.worker_id,
  44. self.balance,
  45. );
  46. assert_eq!(actual_result, expected_result);
  47. if actual_result.is_ok() {
  48. let new_stake = <stake::Module<Test>>::stakes(stake_id);
  49. // stake increased
  50. assert_eq!(
  51. get_stake_balance(new_stake),
  52. get_stake_balance(old_stake) + self.balance
  53. );
  54. let new_balance = Balances::free_balance(&self.account_id);
  55. // worker balance decreased
  56. assert_eq!(new_balance, old_balance - self.balance,);
  57. }
  58. }
  59. }
  60. pub struct TerminateWorkerRoleFixture {
  61. worker_id: u64,
  62. origin: RawOrigin<u64>,
  63. text: Vec<u8>,
  64. constraint: InputValidationLengthConstraint,
  65. slash_stake: bool,
  66. }
  67. impl TerminateWorkerRoleFixture {
  68. pub fn default_for_worker_id(worker_id: u64) -> Self {
  69. Self {
  70. worker_id,
  71. origin: RawOrigin::Signed(1),
  72. text: b"rationale_text".to_vec(),
  73. constraint: InputValidationLengthConstraint {
  74. min: 1,
  75. max_min_diff: 20,
  76. },
  77. slash_stake: false,
  78. }
  79. }
  80. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  81. Self { origin, ..self }
  82. }
  83. pub fn with_text(self, text: Vec<u8>) -> Self {
  84. Self { text, ..self }
  85. }
  86. pub fn with_slashing(self) -> Self {
  87. Self {
  88. slash_stake: true,
  89. ..self
  90. }
  91. }
  92. pub fn call_and_assert(&self, expected_result: Result<(), Error>) {
  93. <crate::WorkerExitRationaleText<TestWorkingGroupInstance>>::put(self.constraint.clone());
  94. let actual_result = TestWorkingGroup::terminate_role(
  95. self.origin.clone().into(),
  96. self.worker_id,
  97. self.text.clone(),
  98. self.slash_stake,
  99. );
  100. assert_eq!(actual_result, expected_result);
  101. if actual_result.is_ok() {
  102. if actual_result.is_ok() {
  103. assert!(
  104. !<crate::WorkerById<Test, TestWorkingGroupInstance>>::exists(self.worker_id)
  105. );
  106. }
  107. }
  108. }
  109. }
  110. pub(crate) struct LeaveWorkerRoleFixture {
  111. worker_id: u64,
  112. origin: RawOrigin<u64>,
  113. }
  114. impl LeaveWorkerRoleFixture {
  115. pub fn default_for_worker_id(worker_id: u64) -> Self {
  116. Self {
  117. worker_id,
  118. origin: RawOrigin::Signed(1),
  119. }
  120. }
  121. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  122. Self { origin, ..self }
  123. }
  124. pub fn call_and_assert(&self, expected_result: Result<(), Error>) {
  125. let rationale_text = b"rationale_text".to_vec();
  126. let actual_result = TestWorkingGroup::leave_role(
  127. self.origin.clone().into(),
  128. self.worker_id,
  129. rationale_text.clone(),
  130. );
  131. assert_eq!(actual_result, expected_result);
  132. if actual_result.is_ok() {
  133. assert!(!<crate::WorkerById<Test, TestWorkingGroupInstance>>::exists(self.worker_id));
  134. }
  135. }
  136. }
  137. pub struct UpdateWorkerRewardAmountFixture {
  138. worker_id: u64,
  139. amount: u64,
  140. origin: RawOrigin<u64>,
  141. }
  142. impl UpdateWorkerRewardAmountFixture {
  143. pub fn default_for_worker_id(worker_id: u64) -> Self {
  144. let lead_account_id = get_current_lead_account_id();
  145. Self {
  146. worker_id,
  147. amount: 120,
  148. origin: RawOrigin::Signed(lead_account_id),
  149. }
  150. }
  151. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  152. Self { origin, ..self }
  153. }
  154. pub fn call_and_assert(&self, expected_result: Result<(), Error>) {
  155. let actual_result = TestWorkingGroup::update_reward_amount(
  156. self.origin.clone().into(),
  157. self.worker_id,
  158. self.amount,
  159. );
  160. assert_eq!(actual_result.clone(), expected_result);
  161. if actual_result.is_ok() {
  162. let worker = TestWorkingGroup::worker_by_id(self.worker_id);
  163. let relationship_id = worker.reward_relationship.unwrap();
  164. let relationship = recurringrewards::RewardRelationships::<Test>::get(relationship_id);
  165. assert_eq!(relationship.amount_per_payout, self.amount);
  166. }
  167. }
  168. }
  169. pub struct UpdateWorkerRewardAccountFixture {
  170. worker_id: u64,
  171. new_reward_account_id: u64,
  172. origin: RawOrigin<u64>,
  173. }
  174. impl UpdateWorkerRewardAccountFixture {
  175. pub fn default_with_ids(worker_id: u64, new_reward_account_id: u64) -> Self {
  176. Self {
  177. worker_id,
  178. new_reward_account_id,
  179. origin: RawOrigin::Signed(1),
  180. }
  181. }
  182. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  183. Self { origin, ..self }
  184. }
  185. pub fn call_and_assert(&self, expected_result: Result<(), Error>) {
  186. let actual_result = TestWorkingGroup::update_reward_account(
  187. self.origin.clone().into(),
  188. self.worker_id,
  189. self.new_reward_account_id,
  190. );
  191. assert_eq!(actual_result.clone(), expected_result);
  192. if actual_result.is_ok() {
  193. let worker = TestWorkingGroup::worker_by_id(self.worker_id);
  194. let relationship_id = worker.reward_relationship.unwrap();
  195. let relationship = recurringrewards::RewardRelationships::<Test>::get(relationship_id);
  196. assert_eq!(relationship.account, self.new_reward_account_id);
  197. }
  198. }
  199. }
  200. pub struct UpdateWorkerRoleAccountFixture {
  201. worker_id: u64,
  202. new_role_account_id: u64,
  203. origin: RawOrigin<u64>,
  204. }
  205. impl UpdateWorkerRoleAccountFixture {
  206. pub fn default_with_ids(worker_id: u64, new_role_account_id: u64) -> Self {
  207. Self {
  208. worker_id,
  209. new_role_account_id,
  210. origin: RawOrigin::Signed(1),
  211. }
  212. }
  213. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  214. Self { origin, ..self }
  215. }
  216. pub fn call_and_assert(&self, expected_result: Result<(), Error>) {
  217. let actual_result = TestWorkingGroup::update_role_account(
  218. self.origin.clone().into(),
  219. self.worker_id,
  220. self.new_role_account_id,
  221. );
  222. assert_eq!(actual_result, expected_result);
  223. if actual_result.is_ok() {
  224. let worker = TestWorkingGroup::worker_by_id(self.worker_id);
  225. assert_eq!(worker.role_account_id, self.new_role_account_id);
  226. }
  227. }
  228. }
  229. pub fn set_mint_id(mint_id: u64) {
  230. <crate::Mint<Test, TestWorkingGroupInstance>>::put(mint_id);
  231. }
  232. pub fn create_mint() -> u64 {
  233. <minting::Module<Test>>::add_mint(100, None).unwrap()
  234. }
  235. pub struct FillWorkerOpeningFixture {
  236. origin: RawOrigin<u64>,
  237. opening_id: u64,
  238. successful_application_ids: BTreeSet<u64>,
  239. role_account_id: u64,
  240. reward_policy: Option<RewardPolicy<u64, u64>>,
  241. }
  242. impl FillWorkerOpeningFixture {
  243. pub fn default_for_ids(opening_id: u64, application_ids: Vec<u64>) -> Self {
  244. let application_ids: BTreeSet<u64> = application_ids.iter().map(|x| *x).collect();
  245. Self {
  246. origin: RawOrigin::Signed(1),
  247. opening_id,
  248. successful_application_ids: application_ids,
  249. role_account_id: 1,
  250. reward_policy: None,
  251. }
  252. }
  253. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  254. Self { origin, ..self }
  255. }
  256. pub fn with_reward_policy(self, reward_policy: RewardPolicy<u64, u64>) -> Self {
  257. Self {
  258. reward_policy: Some(reward_policy),
  259. ..self
  260. }
  261. }
  262. pub fn call(&self) -> Result<u64, Error> {
  263. let saved_worker_next_id = TestWorkingGroup::next_worker_id();
  264. TestWorkingGroup::fill_opening(
  265. self.origin.clone().into(),
  266. self.opening_id,
  267. self.successful_application_ids.clone(),
  268. self.reward_policy.clone(),
  269. )?;
  270. Ok(saved_worker_next_id)
  271. }
  272. pub fn call_and_assert(&self, expected_result: Result<(), Error>) -> u64 {
  273. let saved_worker_next_id = TestWorkingGroup::next_worker_id();
  274. let actual_result = self.call().map(|_| ());
  275. assert_eq!(actual_result.clone(), expected_result);
  276. if actual_result.is_ok() {
  277. assert_eq!(TestWorkingGroup::next_worker_id(), saved_worker_next_id + 1);
  278. let worker_id = saved_worker_next_id;
  279. let opening = TestWorkingGroup::opening_by_id(self.opening_id);
  280. let role_stake_profile = if opening
  281. .policy_commitment
  282. .application_staking_policy
  283. .is_some()
  284. || opening.policy_commitment.role_staking_policy.is_some()
  285. {
  286. let stake_id = 0;
  287. Some(RoleStakeProfile::new(
  288. &stake_id,
  289. &opening
  290. .policy_commitment
  291. .terminate_role_stake_unstaking_period,
  292. &opening.policy_commitment.exit_role_stake_unstaking_period,
  293. ))
  294. } else {
  295. None
  296. };
  297. let reward_relationship = self.reward_policy.clone().map(|_| 0);
  298. let expected_worker = Worker {
  299. member_id: 1,
  300. role_account_id: self.role_account_id,
  301. reward_relationship,
  302. role_stake_profile,
  303. };
  304. let actual_worker = TestWorkingGroup::worker_by_id(worker_id);
  305. assert_eq!(actual_worker, expected_worker);
  306. }
  307. saved_worker_next_id
  308. }
  309. }
  310. pub struct BeginReviewWorkerApplicationsFixture {
  311. origin: RawOrigin<u64>,
  312. opening_id: u64,
  313. }
  314. impl BeginReviewWorkerApplicationsFixture {
  315. pub fn default_for_opening_id(opening_id: u64) -> Self {
  316. Self {
  317. origin: RawOrigin::Signed(1),
  318. opening_id,
  319. }
  320. }
  321. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  322. Self { origin, ..self }
  323. }
  324. pub fn call_and_assert(&self, expected_result: Result<(), Error>) {
  325. let actual_result =
  326. TestWorkingGroup::begin_applicant_review(self.origin.clone().into(), self.opening_id);
  327. assert_eq!(actual_result, expected_result);
  328. }
  329. }
  330. pub struct TerminateApplicationFixture {
  331. origin: RawOrigin<u64>,
  332. worker_application_id: u64,
  333. }
  334. impl TerminateApplicationFixture {
  335. pub fn with_signer(self, account_id: u64) -> Self {
  336. Self {
  337. origin: RawOrigin::Signed(account_id),
  338. ..self
  339. }
  340. }
  341. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  342. Self { origin, ..self }
  343. }
  344. pub fn default_for_application_id(application_id: u64) -> Self {
  345. Self {
  346. origin: RawOrigin::Signed(1),
  347. worker_application_id: application_id,
  348. }
  349. }
  350. pub fn call_and_assert(&self, expected_result: Result<(), Error>) {
  351. let actual_result = TestWorkingGroup::terminate_application(
  352. self.origin.clone().into(),
  353. self.worker_application_id,
  354. );
  355. assert_eq!(actual_result.clone(), expected_result);
  356. }
  357. }
  358. pub struct WithdrawApplicationFixture {
  359. origin: RawOrigin<u64>,
  360. worker_application_id: u64,
  361. }
  362. impl WithdrawApplicationFixture {
  363. pub fn with_signer(self, account_id: u64) -> Self {
  364. Self {
  365. origin: RawOrigin::Signed(account_id),
  366. ..self
  367. }
  368. }
  369. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  370. Self { origin, ..self }
  371. }
  372. pub fn default_for_application_id(application_id: u64) -> Self {
  373. Self {
  374. origin: RawOrigin::Signed(1),
  375. worker_application_id: application_id,
  376. }
  377. }
  378. pub fn call_and_assert(&self, expected_result: Result<(), Error>) {
  379. let actual_result = TestWorkingGroup::withdraw_application(
  380. self.origin.clone().into(),
  381. self.worker_application_id,
  382. );
  383. assert_eq!(actual_result.clone(), expected_result);
  384. }
  385. }
  386. pub fn increase_total_balance_issuance_using_account_id(account_id: u64, balance: u64) {
  387. let _ =
  388. <Balances as srml_support::traits::Currency<u64>>::deposit_creating(&account_id, balance);
  389. }
  390. pub fn get_balance(account_id: u64) -> u64 {
  391. <super::mock::Balances as srml_support::traits::Currency<u64>>::total_balance(&account_id)
  392. }
  393. pub fn setup_members(count: u8) {
  394. let authority_account_id = 1;
  395. Membership::set_screening_authority(RawOrigin::Root.into(), authority_account_id).unwrap();
  396. for i in 0..count {
  397. let account_id: u64 = i as u64;
  398. let handle: [u8; 20] = [i; 20];
  399. Membership::add_screened_member(
  400. RawOrigin::Signed(authority_account_id).into(),
  401. account_id,
  402. Some(handle.to_vec()),
  403. None,
  404. None,
  405. )
  406. .unwrap();
  407. }
  408. }
  409. pub struct ApplyOnWorkerOpeningFixture {
  410. origin: RawOrigin<u64>,
  411. member_id: u64,
  412. worker_opening_id: u64,
  413. role_account_id: u64,
  414. opt_role_stake_balance: Option<u64>,
  415. opt_application_stake_balance: Option<u64>,
  416. human_readable_text: Vec<u8>,
  417. }
  418. impl ApplyOnWorkerOpeningFixture {
  419. pub fn with_text(self, text: Vec<u8>) -> Self {
  420. Self {
  421. human_readable_text: text,
  422. ..self
  423. }
  424. }
  425. pub fn with_origin(self, origin: RawOrigin<u64>, member_id: u64) -> Self {
  426. Self {
  427. origin,
  428. member_id,
  429. ..self
  430. }
  431. }
  432. pub fn with_role_stake(self, stake: Option<u64>) -> Self {
  433. Self {
  434. opt_role_stake_balance: stake,
  435. ..self
  436. }
  437. }
  438. pub fn with_application_stake(self, stake: u64) -> Self {
  439. Self {
  440. opt_application_stake_balance: Some(stake),
  441. ..self
  442. }
  443. }
  444. pub fn default_for_opening_id(opening_id: u64) -> Self {
  445. Self {
  446. origin: RawOrigin::Signed(1),
  447. member_id: 1,
  448. worker_opening_id: opening_id,
  449. role_account_id: 1,
  450. opt_role_stake_balance: None,
  451. opt_application_stake_balance: None,
  452. human_readable_text: b"human_text".to_vec(),
  453. }
  454. }
  455. pub fn call(&self) -> Result<u64, Error> {
  456. let saved_application_next_id = TestWorkingGroup::next_application_id();
  457. TestWorkingGroup::apply_on_opening(
  458. self.origin.clone().into(),
  459. self.member_id,
  460. self.worker_opening_id,
  461. self.role_account_id,
  462. self.opt_role_stake_balance,
  463. self.opt_application_stake_balance,
  464. self.human_readable_text.clone(),
  465. )?;
  466. Ok(saved_application_next_id)
  467. }
  468. pub fn call_and_assert(&self, expected_result: Result<(), Error>) -> u64 {
  469. let saved_application_next_id = TestWorkingGroup::next_application_id();
  470. let actual_result = self.call().map(|_| ());
  471. assert_eq!(actual_result.clone(), expected_result);
  472. if actual_result.is_ok() {
  473. assert_eq!(
  474. TestWorkingGroup::next_application_id(),
  475. saved_application_next_id + 1
  476. );
  477. let application_id = saved_application_next_id;
  478. let actual_application = TestWorkingGroup::application_by_id(application_id);
  479. let expected_application = Application {
  480. role_account_id: self.role_account_id,
  481. opening_id: self.worker_opening_id,
  482. member_id: self.member_id,
  483. hiring_application_id: application_id,
  484. };
  485. assert_eq!(actual_application, expected_application);
  486. let current_opening = TestWorkingGroup::opening_by_id(self.worker_opening_id);
  487. assert!(current_opening.applications.contains(&application_id));
  488. }
  489. saved_application_next_id
  490. }
  491. }
  492. pub struct AcceptWorkerApplicationsFixture {
  493. origin: RawOrigin<u64>,
  494. opening_id: u64,
  495. }
  496. impl AcceptWorkerApplicationsFixture {
  497. pub fn default_for_opening_id(opening_id: u64) -> Self {
  498. Self {
  499. origin: RawOrigin::Signed(1),
  500. opening_id,
  501. }
  502. }
  503. pub fn call_and_assert(&self, expected_result: Result<(), Error>) {
  504. let actual_result =
  505. TestWorkingGroup::accept_applications(self.origin.clone().into(), self.opening_id);
  506. assert_eq!(actual_result, expected_result);
  507. }
  508. }
  509. pub struct SetLeadFixture {
  510. pub member_id: u64,
  511. pub role_account_id: u64,
  512. pub worker_id: u64,
  513. }
  514. impl Default for SetLeadFixture {
  515. fn default() -> Self {
  516. SetLeadFixture {
  517. member_id: 1,
  518. role_account_id: 1,
  519. worker_id: 1,
  520. }
  521. }
  522. }
  523. impl SetLeadFixture {
  524. pub fn unset_lead() {
  525. TestWorkingGroup::unset_lead();
  526. }
  527. pub fn set_lead(self) {
  528. TestWorkingGroup::set_lead(self.worker_id);
  529. }
  530. pub fn set_lead_with_ids(member_id: u64, role_account_id: u64, worker_id: u64) {
  531. Self {
  532. member_id,
  533. role_account_id,
  534. worker_id,
  535. }
  536. .set_lead();
  537. }
  538. }
  539. pub struct HireLeadFixture {
  540. setup_environment: bool,
  541. stake: Option<u64>,
  542. reward_policy: Option<RewardPolicy<u64, u64>>,
  543. }
  544. impl Default for HireLeadFixture {
  545. fn default() -> Self {
  546. Self {
  547. setup_environment: true,
  548. stake: None,
  549. reward_policy: None,
  550. }
  551. }
  552. }
  553. impl HireLeadFixture {
  554. pub fn with_stake(self, stake: u64) -> Self {
  555. Self {
  556. stake: Some(stake),
  557. ..self
  558. }
  559. }
  560. pub fn with_reward_policy(self, reward_policy: RewardPolicy<u64, u64>) -> Self {
  561. Self {
  562. reward_policy: Some(reward_policy),
  563. ..self
  564. }
  565. }
  566. pub fn hire_lead(self) -> u64 {
  567. fill_worker_position(
  568. self.reward_policy,
  569. self.stake,
  570. self.setup_environment,
  571. OpeningType::Leader,
  572. Some(b"leader".to_vec()),
  573. )
  574. }
  575. }
  576. pub fn get_worker_by_id(worker_id: u64) -> Worker<u64, u64, u64, u64, u64> {
  577. TestWorkingGroup::worker_by_id(worker_id)
  578. }
  579. pub struct AddWorkerOpeningFixture {
  580. origin: RawOrigin<u64>,
  581. activate_at: hiring::ActivateOpeningAt<u64>,
  582. commitment: OpeningPolicyCommitment<u64, u64>,
  583. human_readable_text: Vec<u8>,
  584. opening_type: OpeningType,
  585. }
  586. impl Default for AddWorkerOpeningFixture {
  587. fn default() -> Self {
  588. Self {
  589. origin: RawOrigin::Signed(1),
  590. activate_at: hiring::ActivateOpeningAt::CurrentBlock,
  591. commitment: <OpeningPolicyCommitment<u64, u64>>::default(),
  592. human_readable_text: b"human_text".to_vec(),
  593. opening_type: OpeningType::Worker,
  594. }
  595. }
  596. }
  597. impl AddWorkerOpeningFixture {
  598. pub fn with_policy_commitment(
  599. self,
  600. policy_commitment: OpeningPolicyCommitment<u64, u64>,
  601. ) -> Self {
  602. Self {
  603. commitment: policy_commitment,
  604. ..self
  605. }
  606. }
  607. pub fn call_and_assert(&self, expected_result: Result<(), Error>) -> u64 {
  608. let saved_opening_next_id = TestWorkingGroup::next_opening_id();
  609. let actual_result = self.call().map(|_| ());
  610. assert_eq!(actual_result.clone(), expected_result);
  611. if actual_result.is_ok() {
  612. assert_eq!(
  613. TestWorkingGroup::next_opening_id(),
  614. saved_opening_next_id + 1
  615. );
  616. let opening_id = saved_opening_next_id;
  617. let actual_opening = TestWorkingGroup::opening_by_id(opening_id);
  618. let expected_opening = Opening::<u64, u64, u64, u64> {
  619. hiring_opening_id: opening_id,
  620. applications: BTreeSet::new(),
  621. policy_commitment: self.commitment.clone(),
  622. opening_type: self.opening_type,
  623. };
  624. assert_eq!(actual_opening, expected_opening);
  625. }
  626. saved_opening_next_id
  627. }
  628. pub fn call(&self) -> Result<u64, Error> {
  629. let saved_opening_next_id = TestWorkingGroup::next_opening_id();
  630. TestWorkingGroup::add_opening(
  631. self.origin.clone().into(),
  632. self.activate_at.clone(),
  633. self.commitment.clone(),
  634. self.human_readable_text.clone(),
  635. self.opening_type,
  636. )?;
  637. Ok(saved_opening_next_id)
  638. }
  639. pub fn with_text(self, text: Vec<u8>) -> Self {
  640. Self {
  641. human_readable_text: text,
  642. ..self
  643. }
  644. }
  645. pub fn with_opening_type(self, opening_type: OpeningType) -> Self {
  646. Self {
  647. opening_type,
  648. ..self
  649. }
  650. }
  651. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  652. Self { origin, ..self }
  653. }
  654. pub fn with_activate_at(self, activate_at: hiring::ActivateOpeningAt<u64>) -> Self {
  655. Self {
  656. activate_at,
  657. ..self
  658. }
  659. }
  660. }
  661. pub struct EventFixture;
  662. impl EventFixture {
  663. pub fn assert_last_crate_event(
  664. expected_raw_event: RawEvent<
  665. u64,
  666. u64,
  667. u64,
  668. u64,
  669. std::collections::BTreeMap<u64, u64>,
  670. Vec<u8>,
  671. u64,
  672. u64,
  673. TestWorkingGroupInstance,
  674. >,
  675. ) {
  676. let converted_event = TestEvent::working_group_TestWorkingGroupInstance(expected_raw_event);
  677. Self::assert_last_global_event(converted_event)
  678. }
  679. pub fn assert_last_global_event(expected_event: TestEvent) {
  680. let expected_event = EventRecord {
  681. phase: Phase::ApplyExtrinsic(0),
  682. event: expected_event,
  683. topics: vec![],
  684. };
  685. assert_eq!(System::events().pop().unwrap(), expected_event);
  686. }
  687. }
  688. pub struct DecreaseWorkerStakeFixture {
  689. origin: RawOrigin<u64>,
  690. worker_id: u64,
  691. balance: u64,
  692. account_id: u64,
  693. }
  694. impl DecreaseWorkerStakeFixture {
  695. pub fn default_for_worker_id(worker_id: u64) -> Self {
  696. let account_id = 1;
  697. let lead_account_id = get_current_lead_account_id();
  698. Self {
  699. origin: RawOrigin::Signed(lead_account_id),
  700. worker_id,
  701. balance: 10,
  702. account_id,
  703. }
  704. }
  705. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  706. Self { origin, ..self }
  707. }
  708. pub fn with_balance(self, balance: u64) -> Self {
  709. Self { balance, ..self }
  710. }
  711. pub fn call_and_assert(&self, expected_result: Result<(), Error>) {
  712. let stake_id = 0;
  713. let old_balance = Balances::free_balance(&self.account_id);
  714. let old_stake = <stake::Module<Test>>::stakes(stake_id);
  715. let actual_result = TestWorkingGroup::decrease_stake(
  716. self.origin.clone().into(),
  717. self.worker_id,
  718. self.balance,
  719. );
  720. assert_eq!(actual_result, expected_result);
  721. if actual_result.is_ok() {
  722. let new_stake = <stake::Module<Test>>::stakes(stake_id);
  723. // stake decreased
  724. assert_eq!(
  725. get_stake_balance(new_stake),
  726. get_stake_balance(old_stake) - self.balance
  727. );
  728. let new_balance = Balances::free_balance(&self.account_id);
  729. // worker balance increased
  730. assert_eq!(new_balance, old_balance + self.balance,);
  731. }
  732. }
  733. }
  734. pub(crate) fn get_stake_balance(stake: stake::Stake<u64, u64, u64>) -> u64 {
  735. if let stake::StakingStatus::Staked(stake) = stake.staking_status {
  736. return stake.staked_amount;
  737. }
  738. panic!("Not staked.");
  739. }
  740. fn get_current_lead_account_id() -> u64 {
  741. let leader_worker_id = TestWorkingGroup::current_lead();
  742. if let Some(leader_worker_id) = leader_worker_id {
  743. let leader = TestWorkingGroup::worker_by_id(leader_worker_id);
  744. leader.role_account_id
  745. } else {
  746. 0 // return invalid lead_account_id for testing
  747. }
  748. }
  749. pub struct SlashWorkerStakeFixture {
  750. origin: RawOrigin<u64>,
  751. worker_id: u64,
  752. balance: u64,
  753. account_id: u64,
  754. }
  755. impl SlashWorkerStakeFixture {
  756. pub fn default_for_worker_id(worker_id: u64) -> Self {
  757. let account_id = 1;
  758. let lead_account_id = get_current_lead_account_id();
  759. Self {
  760. origin: RawOrigin::Signed(lead_account_id),
  761. worker_id,
  762. balance: 10,
  763. account_id,
  764. }
  765. }
  766. pub fn with_origin(self, origin: RawOrigin<u64>) -> Self {
  767. Self { origin, ..self }
  768. }
  769. pub fn with_balance(self, balance: u64) -> Self {
  770. Self { balance, ..self }
  771. }
  772. pub fn call_and_assert(&self, expected_result: Result<(), Error>) {
  773. let stake_id = 0;
  774. let old_balance = Balances::free_balance(&self.account_id);
  775. let old_stake = <stake::Module<Test>>::stakes(stake_id);
  776. let actual_result =
  777. TestWorkingGroup::slash_stake(self.origin.clone().into(), self.worker_id, self.balance);
  778. assert_eq!(actual_result, expected_result);
  779. if actual_result.is_ok() {
  780. let new_stake = <stake::Module<Test>>::stakes(stake_id);
  781. // stake decreased
  782. assert_eq!(
  783. get_stake_balance(new_stake),
  784. get_stake_balance(old_stake) - self.balance
  785. );
  786. let new_balance = Balances::free_balance(&self.account_id);
  787. // worker balance unchanged
  788. assert_eq!(new_balance, old_balance,);
  789. }
  790. }
  791. }