|
| 1 | +//! Public feature service query tests (no authentication required). |
| 2 | +//! |
| 3 | +//! These tests use ESRI's public World Cities sample service and run in CI. |
| 4 | +//! No API key or credentials needed. |
| 5 | +
|
| 6 | +use arcgis::{FeatureServiceClient, LayerId, ObjectId}; |
| 7 | + |
| 8 | +/// Public World Cities feature service (ESRI sample data). |
| 9 | +const WORLD_CITIES_SERVICE: &str = |
| 10 | + "https://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/World_Cities/FeatureServer"; |
| 11 | + |
| 12 | +/// Rate limiting helper to be polite to public services. |
| 13 | +async fn rate_limit() { |
| 14 | + tokio::time::sleep(tokio::time::Duration::from_millis(500)).await; |
| 15 | +} |
| 16 | + |
| 17 | +/// Create an unauthenticated client for public service access. |
| 18 | +fn create_public_client() -> arcgis::ArcGISClient { |
| 19 | + use arcgis::NoAuth; |
| 20 | + arcgis::ArcGISClient::new(NoAuth) |
| 21 | +} |
| 22 | + |
| 23 | +#[tokio::test] |
| 24 | +#[cfg(feature = "test-public")] |
| 25 | +async fn test_public_service_metadata() { |
| 26 | + // Verify we can access service metadata without authentication |
| 27 | + let client = reqwest::Client::new(); |
| 28 | + let url = format!("{}?f=json", WORLD_CITIES_SERVICE); |
| 29 | + |
| 30 | + rate_limit().await; |
| 31 | + |
| 32 | + let response = client.get(&url).send().await.expect("Request failed"); |
| 33 | + |
| 34 | + assert!( |
| 35 | + response.status().is_success(), |
| 36 | + "Public service should be accessible" |
| 37 | + ); |
| 38 | + |
| 39 | + let json: serde_json::Value = response.json().await.expect("Invalid JSON"); |
| 40 | + |
| 41 | + // Verify it's a feature service |
| 42 | + assert!( |
| 43 | + json.get("layers").is_some() || json.get("tables").is_some(), |
| 44 | + "Response should contain layers or tables" |
| 45 | + ); |
| 46 | + assert_eq!( |
| 47 | + json.get("serviceDescription").and_then(|v| v.as_str()), |
| 48 | + Some("World Cities"), |
| 49 | + "Service description should match" |
| 50 | + ); |
| 51 | +} |
| 52 | + |
| 53 | +#[tokio::test] |
| 54 | +#[cfg(feature = "test-public")] |
| 55 | +async fn test_query_where_clause() { |
| 56 | + let client = create_public_client(); |
| 57 | + let service = FeatureServiceClient::new(WORLD_CITIES_SERVICE, &client); |
| 58 | + |
| 59 | + rate_limit().await; |
| 60 | + |
| 61 | + // Query for large cities (population > 5 million) |
| 62 | + let result = service |
| 63 | + .query(LayerId::new(0)) |
| 64 | + .where_clause("POP > 5000000") |
| 65 | + .out_fields(&["CITY_NAME", "POP", "CNTRY_NAME"]) |
| 66 | + .return_geometry(true) |
| 67 | + .limit(10) |
| 68 | + .execute() |
| 69 | + .await |
| 70 | + .expect("Query failed"); |
| 71 | + |
| 72 | + // Should find major world cities |
| 73 | + assert!( |
| 74 | + !result.features().is_empty(), |
| 75 | + "Should find cities with population > 5 million" |
| 76 | + ); |
| 77 | + |
| 78 | + // Verify feature structure |
| 79 | + let first = &result.features()[0]; |
| 80 | + assert!( |
| 81 | + first.attributes().contains_key("CITY_NAME"), |
| 82 | + "Feature should have CITY_NAME" |
| 83 | + ); |
| 84 | + assert!( |
| 85 | + first.attributes().contains_key("POP"), |
| 86 | + "Feature should have POP" |
| 87 | + ); |
| 88 | + assert!( |
| 89 | + first.geometry().is_some(), |
| 90 | + "Feature should have geometry when requested" |
| 91 | + ); |
| 92 | +} |
| 93 | + |
| 94 | +#[tokio::test] |
| 95 | +#[cfg(feature = "test-public")] |
| 96 | +async fn test_query_count_only() { |
| 97 | + let client = create_public_client(); |
| 98 | + let service = FeatureServiceClient::new(WORLD_CITIES_SERVICE, &client); |
| 99 | + |
| 100 | + rate_limit().await; |
| 101 | + |
| 102 | + // Get count of all cities |
| 103 | + let result = service |
| 104 | + .query(LayerId::new(0)) |
| 105 | + .where_clause("1=1") |
| 106 | + .count_only(true) |
| 107 | + .execute() |
| 108 | + .await |
| 109 | + .expect("Count query failed"); |
| 110 | + |
| 111 | + // Should have a count |
| 112 | + assert!( |
| 113 | + result.count().is_some(), |
| 114 | + "Count-only query should return count" |
| 115 | + ); |
| 116 | + assert!( |
| 117 | + result.count().unwrap() > 1000, |
| 118 | + "World Cities dataset should have many cities" |
| 119 | + ); |
| 120 | + |
| 121 | + // Should not return features |
| 122 | + assert!( |
| 123 | + result.features().is_empty(), |
| 124 | + "Count-only query should not return features" |
| 125 | + ); |
| 126 | +} |
| 127 | + |
| 128 | +#[tokio::test] |
| 129 | +#[cfg(feature = "test-public")] |
| 130 | +async fn test_query_specific_object_ids() { |
| 131 | + let client = create_public_client(); |
| 132 | + let service = FeatureServiceClient::new(WORLD_CITIES_SERVICE, &client); |
| 133 | + |
| 134 | + rate_limit().await; |
| 135 | + |
| 136 | + // Query by specific object IDs (may or may not exist) |
| 137 | + let result = service |
| 138 | + .query(LayerId::new(0)) |
| 139 | + .object_ids(&[ObjectId::new(1), ObjectId::new(2), ObjectId::new(3)]) |
| 140 | + .out_fields(&["*"]) |
| 141 | + .return_geometry(false) |
| 142 | + .execute() |
| 143 | + .await |
| 144 | + .expect("Object ID query failed"); |
| 145 | + |
| 146 | + // Should return at most 3 features (if those IDs exist) |
| 147 | + assert!( |
| 148 | + result.features().len() <= 3, |
| 149 | + "Should return at most requested number of features" |
| 150 | + ); |
| 151 | +} |
| 152 | + |
| 153 | +#[tokio::test] |
| 154 | +#[cfg(feature = "test-public")] |
| 155 | +async fn test_query_with_field_filtering() { |
| 156 | + let client = create_public_client(); |
| 157 | + let service = FeatureServiceClient::new(WORLD_CITIES_SERVICE, &client); |
| 158 | + |
| 159 | + rate_limit().await; |
| 160 | + |
| 161 | + // Query with specific fields only |
| 162 | + let result = service |
| 163 | + .query(LayerId::new(0)) |
| 164 | + .where_clause("POP > 1000000") |
| 165 | + .out_fields(&["CITY_NAME", "POP"]) |
| 166 | + .return_geometry(false) |
| 167 | + .limit(5) |
| 168 | + .execute() |
| 169 | + .await |
| 170 | + .expect("Field filtering query failed"); |
| 171 | + |
| 172 | + assert!( |
| 173 | + !result.features().is_empty(), |
| 174 | + "Should find cities with population > 1 million" |
| 175 | + ); |
| 176 | + |
| 177 | + // Verify only requested fields are present |
| 178 | + let first = &result.features()[0]; |
| 179 | + assert!( |
| 180 | + first.attributes().contains_key("CITY_NAME"), |
| 181 | + "Should have requested CITY_NAME field" |
| 182 | + ); |
| 183 | + assert!( |
| 184 | + first.attributes().contains_key("POP"), |
| 185 | + "Should have requested POP field" |
| 186 | + ); |
| 187 | + assert!( |
| 188 | + first.geometry().is_none(), |
| 189 | + "Should not have geometry when not requested" |
| 190 | + ); |
| 191 | +} |
| 192 | + |
| 193 | +#[tokio::test] |
| 194 | +#[cfg(feature = "test-public")] |
| 195 | +async fn test_query_without_geometry() { |
| 196 | + let client = create_public_client(); |
| 197 | + let service = FeatureServiceClient::new(WORLD_CITIES_SERVICE, &client); |
| 198 | + |
| 199 | + rate_limit().await; |
| 200 | + |
| 201 | + // Query without geometry for faster response |
| 202 | + let result = service |
| 203 | + .query(LayerId::new(0)) |
| 204 | + .where_clause("1=1") |
| 205 | + .return_geometry(false) |
| 206 | + .limit(10) |
| 207 | + .execute() |
| 208 | + .await |
| 209 | + .expect("No-geometry query failed"); |
| 210 | + |
| 211 | + assert!(!result.features().is_empty(), "Should return features"); |
| 212 | + |
| 213 | + // Verify no geometry |
| 214 | + for feature in result.features() { |
| 215 | + assert!( |
| 216 | + feature.geometry().is_none(), |
| 217 | + "Features should not have geometry when not requested" |
| 218 | + ); |
| 219 | + } |
| 220 | +} |
| 221 | + |
| 222 | +#[tokio::test] |
| 223 | +#[cfg(feature = "test-public")] |
| 224 | +async fn test_query_with_limit() { |
| 225 | + let client = create_public_client(); |
| 226 | + let service = FeatureServiceClient::new(WORLD_CITIES_SERVICE, &client); |
| 227 | + |
| 228 | + rate_limit().await; |
| 229 | + |
| 230 | + // Query with small limit |
| 231 | + let limit = 3u32; |
| 232 | + let result = service |
| 233 | + .query(LayerId::new(0)) |
| 234 | + .where_clause("1=1") |
| 235 | + .limit(limit) |
| 236 | + .return_geometry(false) |
| 237 | + .execute() |
| 238 | + .await |
| 239 | + .expect("Limited query failed"); |
| 240 | + |
| 241 | + // Should respect limit |
| 242 | + assert!( |
| 243 | + result.features().len() <= limit as usize, |
| 244 | + "Should not exceed requested limit" |
| 245 | + ); |
| 246 | +} |
| 247 | + |
| 248 | +#[tokio::test] |
| 249 | +#[cfg(feature = "test-public")] |
| 250 | +async fn test_feature_count_method() { |
| 251 | + let client = create_public_client(); |
| 252 | + let service = FeatureServiceClient::new(WORLD_CITIES_SERVICE, &client); |
| 253 | + |
| 254 | + rate_limit().await; |
| 255 | + |
| 256 | + // Use the dedicated count method |
| 257 | + let count = service |
| 258 | + .query_feature_count(LayerId::new(0), "POP > 100000") |
| 259 | + .await |
| 260 | + .expect("Count method failed"); |
| 261 | + |
| 262 | + assert!( |
| 263 | + count > 0, |
| 264 | + "Should find cities with population > 100,000" |
| 265 | + ); |
| 266 | +} |
0 commit comments