hiring_workflow.rs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. use crate::tests::fixtures::{
  2. create_mint, increase_total_balance_issuance_using_account_id, set_mint_id, setup_members,
  3. AddWorkerOpeningFixture, ApplyOnWorkerOpeningFixture, BeginReviewWorkerApplicationsFixture,
  4. FillWorkerOpeningFixture, SetLeadFixture,
  5. };
  6. use crate::tests::mock::TestWorkingGroup;
  7. use crate::Error;
  8. use crate::{OpeningPolicyCommitment, OpeningType, RewardPolicy};
  9. use system::RawOrigin;
  10. #[derive(Clone)]
  11. struct HiringWorkflowApplication {
  12. stake: Option<u64>,
  13. worker_handle: Vec<u8>,
  14. origin: RawOrigin<u64>,
  15. member_id: u64,
  16. }
  17. pub struct HiringWorkflow {
  18. opening_type: OpeningType,
  19. expected_result: Result<(), Error>,
  20. role_stake: Option<u64>,
  21. applications: Vec<HiringWorkflowApplication>,
  22. setup_environment: bool,
  23. reward_policy: Option<RewardPolicy<u64, u64>>,
  24. }
  25. impl Default for HiringWorkflow {
  26. fn default() -> Self {
  27. Self {
  28. opening_type: OpeningType::Worker,
  29. expected_result: Ok(()),
  30. role_stake: None,
  31. applications: Vec::new(),
  32. setup_environment: true,
  33. reward_policy: None,
  34. }
  35. }
  36. }
  37. impl HiringWorkflow {
  38. pub fn expect(self, result: Result<(), Error>) -> Self {
  39. Self {
  40. expected_result: result,
  41. ..self
  42. }
  43. }
  44. pub fn disable_setup_environment(self) -> Self {
  45. Self {
  46. setup_environment: false,
  47. ..self
  48. }
  49. }
  50. pub fn with_setup_environment(self, setup_environment: bool) -> Self {
  51. Self {
  52. setup_environment,
  53. ..self
  54. }
  55. }
  56. pub fn with_opening_type(self, opening_type: OpeningType) -> Self {
  57. Self {
  58. opening_type,
  59. ..self
  60. }
  61. }
  62. pub fn with_role_stake(self, role_stake: Option<u64>) -> Self {
  63. Self { role_stake, ..self }
  64. }
  65. pub fn with_reward_policy(self, reward_policy: Option<RewardPolicy<u64, u64>>) -> Self {
  66. Self {
  67. reward_policy,
  68. ..self
  69. }
  70. }
  71. pub fn add_default_application(self) -> Self {
  72. let worker_handle = b"default worker handle".to_vec();
  73. self.add_application(worker_handle)
  74. }
  75. pub fn add_application(self, worker_handle: Vec<u8>) -> Self {
  76. self.add_application_with_origin(worker_handle, RawOrigin::Signed(1), 1)
  77. }
  78. pub fn add_application_with_origin(
  79. self,
  80. worker_handle: Vec<u8>,
  81. origin: RawOrigin<u64>,
  82. member_id: u64,
  83. ) -> Self {
  84. let mut applications = self.applications;
  85. applications.push(HiringWorkflowApplication {
  86. worker_handle,
  87. stake: self.role_stake.clone(),
  88. origin,
  89. member_id,
  90. });
  91. Self {
  92. applications,
  93. ..self
  94. }
  95. }
  96. fn setup_environment(&self) {
  97. if matches!(self.opening_type, OpeningType::Worker) {
  98. SetLeadFixture::default().set_lead();
  99. }
  100. increase_total_balance_issuance_using_account_id(1, 10000);
  101. setup_members(4);
  102. set_mint_id(create_mint());
  103. }
  104. pub fn execute(&self) -> Option<u64> {
  105. if self.setup_environment {
  106. self.setup_environment()
  107. }
  108. let result = self.fill_worker_position();
  109. let check_result = result.clone().map(|_| ());
  110. assert_eq!(check_result, self.expected_result);
  111. result.ok()
  112. }
  113. fn fill_worker_position(&self) -> Result<u64, Error> {
  114. let origin = match self.opening_type {
  115. OpeningType::Leader => RawOrigin::Root,
  116. OpeningType::Worker => {
  117. let leader_worker_id = TestWorkingGroup::current_lead().unwrap();
  118. let leader = TestWorkingGroup::worker_by_id(leader_worker_id);
  119. let lead_account_id = leader.role_account_id;
  120. RawOrigin::Signed(lead_account_id)
  121. }
  122. };
  123. // create the opening
  124. let mut add_worker_opening_fixture = AddWorkerOpeningFixture::default()
  125. .with_opening_type(self.opening_type)
  126. .with_origin(origin.clone());
  127. if let Some(stake) = self.role_stake.clone() {
  128. add_worker_opening_fixture =
  129. add_worker_opening_fixture.with_policy_commitment(OpeningPolicyCommitment {
  130. role_staking_policy: Some(hiring::StakingPolicy {
  131. amount: stake,
  132. amount_mode: hiring::StakingAmountLimitMode::AtLeast,
  133. crowded_out_unstaking_period_length: None,
  134. review_period_expired_unstaking_period_length: None,
  135. }),
  136. ..OpeningPolicyCommitment::default()
  137. });
  138. }
  139. let opening_id = add_worker_opening_fixture.call()?;
  140. // Fill applications.
  141. let mut application_ids = Vec::new();
  142. for application in self.applications.clone() {
  143. let apply_on_worker_opening_fixture =
  144. ApplyOnWorkerOpeningFixture::default_for_opening_id(opening_id)
  145. .with_text(application.worker_handle)
  146. .with_origin(application.origin, application.member_id)
  147. .with_role_stake(self.role_stake);
  148. let application_id = apply_on_worker_opening_fixture.call()?;
  149. application_ids.push(application_id);
  150. }
  151. // begin application review
  152. let begin_review_worker_applications_fixture =
  153. BeginReviewWorkerApplicationsFixture::default_for_opening_id(opening_id)
  154. .with_origin(origin.clone());
  155. begin_review_worker_applications_fixture.call_and_assert(Ok(()));
  156. // fill opening
  157. let mut fill_worker_opening_fixture =
  158. FillWorkerOpeningFixture::default_for_ids(opening_id, application_ids)
  159. .with_origin(origin.clone());
  160. if let Some(reward_policy) = self.reward_policy.clone() {
  161. fill_worker_opening_fixture =
  162. fill_worker_opening_fixture.with_reward_policy(reward_policy);
  163. }
  164. let worker_id = fill_worker_opening_fixture.call()?;
  165. Ok(worker_id)
  166. }
  167. }