|
| 1 | +use crate::{ |
| 2 | + models::jobposting::{JobPostingSearchParams, SearchJobPostingResponse}, |
| 3 | + PDLClient, PDLError, |
| 4 | +}; |
| 5 | + |
| 6 | +pub(crate) static JOB_POSTING_SEARCH_PATH: &str = "/job_posting/search"; |
| 7 | + |
| 8 | +pub struct JobPosting { |
| 9 | + pub client: PDLClient, |
| 10 | +} |
| 11 | + |
| 12 | +impl JobPosting { |
| 13 | + /// Searches PDL's job_posting dataset. |
| 14 | + /// docs: https://docs.peopledatalabs.com/docs/job-posting-search-api |
| 15 | + pub fn search( |
| 16 | + &self, |
| 17 | + params: JobPostingSearchParams, |
| 18 | + ) -> Result<SearchJobPostingResponse, PDLError> { |
| 19 | + params.validate()?; |
| 20 | + self.client |
| 21 | + .post::<SearchJobPostingResponse, JobPostingSearchParams>( |
| 22 | + JOB_POSTING_SEARCH_PATH, |
| 23 | + params, |
| 24 | + ) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +#[cfg(test)] |
| 29 | +mod tests { |
| 30 | + use crate::{ |
| 31 | + client::PDLClient, |
| 32 | + models::jobposting::{ |
| 33 | + JobPostingSearchBaseParams, JobPostingSearchParams, RemoteWorkPolicy, |
| 34 | + }, |
| 35 | + BaseParams, |
| 36 | + }; |
| 37 | + |
| 38 | + use super::JobPosting; |
| 39 | + |
| 40 | + #[test] |
| 41 | + fn test_job_posting_search_query() { |
| 42 | + let api_key = std::env::var("PDL_API_KEY").unwrap(); |
| 43 | + let client = PDLClient::new(&api_key).build(); |
| 44 | + |
| 45 | + let job_posting = JobPosting { client }; |
| 46 | + |
| 47 | + let mut base_params = BaseParams::default(); |
| 48 | + base_params.size = Some(3); |
| 49 | + |
| 50 | + let query = serde_json::json!({ |
| 51 | + "query": { |
| 52 | + "bool": { |
| 53 | + "must": [ |
| 54 | + { "term": { "title_role": "engineering" } }, |
| 55 | + ] |
| 56 | + } |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + let search_base_params = JobPostingSearchBaseParams { |
| 61 | + query: Some(query), |
| 62 | + ..JobPostingSearchBaseParams::default() |
| 63 | + }; |
| 64 | + |
| 65 | + let params = JobPostingSearchParams { |
| 66 | + base_params: Some(base_params), |
| 67 | + search_base_params, |
| 68 | + }; |
| 69 | + |
| 70 | + let resp = job_posting.search(params).expect("ERROR"); |
| 71 | + |
| 72 | + assert_eq!(resp.status, 200); |
| 73 | + assert!(resp.data.is_some()); |
| 74 | + } |
| 75 | + |
| 76 | + #[test] |
| 77 | + fn test_job_posting_search_params() { |
| 78 | + let api_key = std::env::var("PDL_API_KEY").unwrap(); |
| 79 | + let client = PDLClient::new(&api_key).build(); |
| 80 | + |
| 81 | + let job_posting = JobPosting { client }; |
| 82 | + |
| 83 | + let mut base_params = BaseParams::default(); |
| 84 | + base_params.size = Some(3); |
| 85 | + |
| 86 | + let search_base_params = JobPostingSearchBaseParams { |
| 87 | + title_role: Some("engineering".to_string()), |
| 88 | + remote_work_policy: Some(RemoteWorkPolicy::Remote), |
| 89 | + ..JobPostingSearchBaseParams::default() |
| 90 | + }; |
| 91 | + |
| 92 | + let params = JobPostingSearchParams { |
| 93 | + base_params: Some(base_params), |
| 94 | + search_base_params, |
| 95 | + }; |
| 96 | + |
| 97 | + let resp = job_posting.search(params).expect("ERROR"); |
| 98 | + |
| 99 | + assert_eq!(resp.status, 200); |
| 100 | + assert!(resp.data.is_some()); |
| 101 | + } |
| 102 | + |
| 103 | + #[test] |
| 104 | + fn test_job_posting_search_size_out_of_range() { |
| 105 | + let api_key = std::env::var("PDL_API_KEY").unwrap(); |
| 106 | + let client = PDLClient::new(&api_key).build(); |
| 107 | + |
| 108 | + let job_posting = JobPosting { client }; |
| 109 | + |
| 110 | + let mut base_params = BaseParams::default(); |
| 111 | + base_params.size = Some(101); |
| 112 | + |
| 113 | + let search_base_params = JobPostingSearchBaseParams { |
| 114 | + title: Some("engineer".to_string()), |
| 115 | + ..JobPostingSearchBaseParams::default() |
| 116 | + }; |
| 117 | + |
| 118 | + let params = JobPostingSearchParams { |
| 119 | + base_params: Some(base_params), |
| 120 | + search_base_params, |
| 121 | + }; |
| 122 | + |
| 123 | + let resp = job_posting.search(params); |
| 124 | + assert!(resp.is_err()); |
| 125 | + } |
| 126 | + |
| 127 | + #[test] |
| 128 | + fn test_job_posting_is_active_serialization_opt_in() { |
| 129 | + // None must be omitted entirely; Some(false) must round-trip. |
| 130 | + let mut params = JobPostingSearchBaseParams::default(); |
| 131 | + params.title = Some("engineer".to_string()); |
| 132 | + let body = serde_json::to_string(¶ms).unwrap(); |
| 133 | + assert!(!body.contains("is_active")); |
| 134 | + |
| 135 | + params.is_active = Some(false); |
| 136 | + let body = serde_json::to_string(¶ms).unwrap(); |
| 137 | + assert!(body.contains("\"is_active\":false")); |
| 138 | + } |
| 139 | + |
| 140 | + #[test] |
| 141 | + fn test_job_posting_scroll_token_round_trips_verbatim() { |
| 142 | + let token = "eyJhIjogMX0=".to_string(); |
| 143 | + let mut params = JobPostingSearchBaseParams::default(); |
| 144 | + params.scroll_token = Some(token.clone()); |
| 145 | + let body = serde_json::to_string(¶ms).unwrap(); |
| 146 | + assert!(body.contains(&format!("\"scroll_token\":\"{}\"", token))); |
| 147 | + } |
| 148 | +} |
0 commit comments