mod.rs (8264B)
1 pub mod decode; 2 pub mod encode; 3 4 #[cfg(test)] 5 mod tests { 6 use crate::error::EventEncodeError; 7 use crate::plot::encode::{plot_address, plot_build_tags}; 8 use radroots_event::{ 9 farm::FarmRef, 10 farm::change_set::{GcsLocation, GeoJsonPoint, GeoJsonPolygon}, 11 farm::plot::{Plot, PlotLocation}, 12 }; 13 14 #[test] 15 fn plot_tags_include_farm_address() { 16 let plot = Plot { 17 d_tag: "AAAAAAAAAAAAAAAAAAAAAQ".to_string(), 18 farm: FarmRef { 19 pubkey: "farm_pubkey".to_string(), 20 d_tag: "AAAAAAAAAAAAAAAAAAAAAA".to_string(), 21 }, 22 name: "Orchard".to_string(), 23 about: None, 24 location: Some(PlotLocation { 25 primary: None, 26 city: None, 27 region: None, 28 country: None, 29 gcs: GcsLocation { 30 lat: 37.0, 31 lng: -122.0, 32 geohash: "9q8yy".to_string(), 33 point: GeoJsonPoint { 34 r#type: "Point".to_string(), 35 coordinates: [-122.0, 37.0], 36 }, 37 polygon: GeoJsonPolygon { 38 r#type: "Polygon".to_string(), 39 coordinates: vec![vec![ 40 [-122.0, 37.0], 41 [-122.0, 37.0001], 42 [-122.0001, 37.0001], 43 [-122.0, 37.0], 44 ]], 45 }, 46 accuracy: None, 47 altitude: None, 48 tag_0: None, 49 label: None, 50 area: None, 51 elevation: None, 52 soil: None, 53 climate: None, 54 gc_id: None, 55 gc_name: None, 56 gc_admin1_id: None, 57 gc_admin1_name: None, 58 gc_country_id: None, 59 gc_country_name: None, 60 }, 61 }), 62 tags: Some(vec!["orchard".to_string()]), 63 }; 64 65 let tags = plot_build_tags(&plot).expect("tags"); 66 let has_a = tags 67 .iter() 68 .any(|tag| tag.first().map(|v| v.as_str()) == Some("a")); 69 let has_p = tags 70 .iter() 71 .any(|tag| tag.first().map(|v| v.as_str()) == Some("p")); 72 assert!(has_a); 73 assert!(has_p); 74 } 75 76 #[test] 77 fn plot_tags_allow_missing_optional_fields() { 78 let plot = Plot { 79 d_tag: "AAAAAAAAAAAAAAAAAAAAAQ".to_string(), 80 farm: FarmRef { 81 pubkey: "farm_pubkey".to_string(), 82 d_tag: "AAAAAAAAAAAAAAAAAAAAAA".to_string(), 83 }, 84 name: "Orchard".to_string(), 85 about: None, 86 location: None, 87 tags: None, 88 }; 89 90 let tags = plot_build_tags(&plot).expect("tags without optional fields"); 91 assert!( 92 tags.iter() 93 .any(|tag| tag.first().map(|v| v.as_str()) == Some("d")) 94 ); 95 assert!( 96 tags.iter() 97 .any(|tag| tag.first().map(|v| v.as_str()) == Some("a")) 98 ); 99 assert!( 100 tags.iter() 101 .any(|tag| tag.first().map(|v| v.as_str()) == Some("p")) 102 ); 103 assert!( 104 !tags 105 .iter() 106 .any(|tag| tag.first().map(|v| v.as_str()) == Some("t")) 107 ); 108 assert!( 109 !tags 110 .iter() 111 .any(|tag| tag.first().map(|v| v.as_str()) == Some("g")) 112 ); 113 } 114 115 #[test] 116 fn plot_build_tags_rejects_invalid_plot_and_farm_d_tag() { 117 let mut plot = Plot { 118 d_tag: "AAAAAAAAAAAAAAAAAAAAAQ".to_string(), 119 farm: FarmRef { 120 pubkey: "farm_pubkey".to_string(), 121 d_tag: "AAAAAAAAAAAAAAAAAAAAAA".to_string(), 122 }, 123 name: "Orchard".to_string(), 124 about: None, 125 location: None, 126 tags: None, 127 }; 128 129 plot.d_tag = "invalid".to_string(); 130 let err = plot_build_tags(&plot).expect_err("invalid plot d_tag"); 131 assert!(matches!(err, EventEncodeError::InvalidField("d_tag"))); 132 133 plot.d_tag = "AAAAAAAAAAAAAAAAAAAAAQ".to_string(); 134 plot.farm.d_tag = "invalid".to_string(); 135 let err = plot_build_tags(&plot).expect_err("invalid farm d_tag"); 136 assert!(matches!(err, EventEncodeError::InvalidField("farm.d_tag"))); 137 } 138 139 #[test] 140 fn plot_encode_rejects_empty_required_fields() { 141 let mut plot = Plot { 142 d_tag: "AAAAAAAAAAAAAAAAAAAAAQ".to_string(), 143 farm: FarmRef { 144 pubkey: "farm_pubkey".to_string(), 145 d_tag: "AAAAAAAAAAAAAAAAAAAAAA".to_string(), 146 }, 147 name: "Orchard".to_string(), 148 about: None, 149 location: Some(PlotLocation { 150 primary: None, 151 city: None, 152 region: None, 153 country: None, 154 gcs: GcsLocation { 155 lat: 37.0, 156 lng: -122.0, 157 geohash: "9q8yy".to_string(), 158 point: GeoJsonPoint { 159 r#type: "Point".to_string(), 160 coordinates: [-122.0, 37.0], 161 }, 162 polygon: GeoJsonPolygon { 163 r#type: "Polygon".to_string(), 164 coordinates: vec![vec![ 165 [-122.0, 37.0], 166 [-122.0, 37.0001], 167 [-122.0001, 37.0001], 168 [-122.0, 37.0], 169 ]], 170 }, 171 accuracy: None, 172 altitude: None, 173 tag_0: None, 174 label: None, 175 area: None, 176 elevation: None, 177 soil: None, 178 climate: None, 179 gc_id: None, 180 gc_name: None, 181 gc_admin1_id: None, 182 gc_admin1_name: None, 183 gc_country_id: None, 184 gc_country_name: None, 185 }, 186 }), 187 tags: None, 188 }; 189 190 let err = 191 plot_address(" ", "AAAAAAAAAAAAAAAAAAAAAQ").expect_err("expected empty author pubkey"); 192 assert!(matches!( 193 err, 194 EventEncodeError::EmptyRequiredField("plot.author_pubkey") 195 )); 196 197 let err = plot_address("farm_pubkey", " ").expect_err("expected empty plot d_tag"); 198 assert!(matches!( 199 err, 200 EventEncodeError::EmptyRequiredField("plot.d_tag") 201 )); 202 203 plot.d_tag = " ".to_string(); 204 let err = plot_build_tags(&plot).expect_err("expected empty d_tag"); 205 assert!(matches!(err, EventEncodeError::EmptyRequiredField("d_tag"))); 206 207 plot.d_tag = "AAAAAAAAAAAAAAAAAAAAAQ".to_string(); 208 plot.name = " ".to_string(); 209 let err = plot_build_tags(&plot).expect_err("expected empty name"); 210 assert!(matches!(err, EventEncodeError::EmptyRequiredField("name"))); 211 212 plot.name = "Orchard".to_string(); 213 plot.farm.pubkey = " ".to_string(); 214 let err = plot_build_tags(&plot).expect_err("expected empty farm pubkey"); 215 assert!(matches!( 216 err, 217 EventEncodeError::EmptyRequiredField("farm.pubkey") 218 )); 219 220 plot.farm.pubkey = "farm_pubkey".to_string(); 221 plot.farm.d_tag = " ".to_string(); 222 let err = plot_build_tags(&plot).expect_err("expected empty farm d_tag"); 223 assert!(matches!( 224 err, 225 EventEncodeError::EmptyRequiredField("farm.d_tag") 226 )); 227 228 plot.farm.d_tag = "AAAAAAAAAAAAAAAAAAAAAA".to_string(); 229 plot.location.as_mut().expect("location").gcs.geohash = " ".to_string(); 230 let err = plot_build_tags(&plot).expect_err("expected empty geohash"); 231 assert!(matches!( 232 err, 233 EventEncodeError::EmptyRequiredField("location.gcs.geohash") 234 )); 235 } 236 }