Tests the `fid_range` keyword and the `step` keyword of .save().
(self)
| 239 | self.county_helper() |
| 240 | |
| 241 | def test_test_fid_range_step(self): |
| 242 | "Tests the `fid_range` keyword and the `step` keyword of .save()." |
| 243 | |
| 244 | # Function for clearing out all the counties before testing. |
| 245 | def clear_counties(): |
| 246 | County.objects.all().delete() |
| 247 | |
| 248 | State.objects.bulk_create( |
| 249 | [State(name="Colorado"), State(name="Hawaii"), State(name="Texas")] |
| 250 | ) |
| 251 | |
| 252 | # Initializing the LayerMapping object to use in these tests. |
| 253 | lm = LayerMapping(County, co_shp, co_mapping, transform=False, unique="name") |
| 254 | |
| 255 | # Bad feature id ranges should raise a type error. |
| 256 | bad_ranges = (5.0, "foo", co_shp) |
| 257 | for bad in bad_ranges: |
| 258 | with self.assertRaises(TypeError): |
| 259 | lm.save(fid_range=bad) |
| 260 | |
| 261 | # Step keyword should not be allowed w/`fid_range`. |
| 262 | fr = (3, 5) # layer[3:5] |
| 263 | with self.assertRaises(LayerMapError): |
| 264 | lm.save(fid_range=fr, step=10) |
| 265 | lm.save(fid_range=fr) |
| 266 | |
| 267 | # Features IDs 3 & 4 are for Galveston County, Texas -- only |
| 268 | # one model is returned because the `unique` keyword was set. |
| 269 | qs = County.objects.all() |
| 270 | self.assertEqual(1, qs.count()) |
| 271 | self.assertEqual("Galveston", qs[0].name) |
| 272 | |
| 273 | # Features IDs 5 and beyond for Honolulu County, Hawaii, and |
| 274 | # FID 0 is for Pueblo County, Colorado. |
| 275 | clear_counties() |
| 276 | lm.save(fid_range=slice(5, None), silent=True, strict=True) # layer[5:] |
| 277 | lm.save(fid_range=slice(None, 1), silent=True, strict=True) # layer[:1] |
| 278 | |
| 279 | # Only Pueblo & Honolulu counties should be present because of |
| 280 | # the `unique` keyword. Have to set `order_by` on this QuerySet |
| 281 | # or else MySQL will return a different ordering than the other dbs. |
| 282 | qs = County.objects.order_by("name") |
| 283 | self.assertEqual(2, qs.count()) |
| 284 | hi, co = tuple(qs) |
| 285 | hi_idx, co_idx = tuple(map(NAMES.index, ("Honolulu", "Pueblo"))) |
| 286 | self.assertEqual("Pueblo", co.name) |
| 287 | self.assertEqual(NUMS[co_idx], len(co.mpoly)) |
| 288 | self.assertEqual("Honolulu", hi.name) |
| 289 | self.assertEqual(NUMS[hi_idx], len(hi.mpoly)) |
| 290 | |
| 291 | # Testing the `step` keyword -- should get the same counties |
| 292 | # regardless of we use a step that divides equally, that is odd, |
| 293 | # or that is larger than the dataset. |
| 294 | for st in (4, 7, 1000): |
| 295 | clear_counties() |
| 296 | lm.save(step=st, strict=True) |
| 297 | self.county_helper(county_feat=False) |
| 298 |
nothing calls this directly
no test coverage detected