Testing Data Source Layers.
(self)
| 150 | DataSource(source.ds) |
| 151 | |
| 152 | def test03a_layers(self): |
| 153 | "Testing Data Source Layers." |
| 154 | for source in ds_list: |
| 155 | ds = DataSource(source.ds) |
| 156 | |
| 157 | # Incrementing through each layer, this tests DataSource.__iter__ |
| 158 | for layer in ds: |
| 159 | self.assertEqual(layer.name, source.name) |
| 160 | self.assertEqual(str(layer), source.name) |
| 161 | # Making sure we get the number of features we expect |
| 162 | self.assertEqual(len(layer), source.nfeat) |
| 163 | |
| 164 | # Making sure we get the number of fields we expect |
| 165 | self.assertEqual(source.nfld, layer.num_fields) |
| 166 | self.assertEqual(source.nfld, len(layer.fields)) |
| 167 | |
| 168 | # Testing the layer's extent (an Envelope), and its properties |
| 169 | self.assertIsInstance(layer.extent, Envelope) |
| 170 | self.assertAlmostEqual(source.extent[0], layer.extent.min_x, 5) |
| 171 | self.assertAlmostEqual(source.extent[1], layer.extent.min_y, 5) |
| 172 | self.assertAlmostEqual(source.extent[2], layer.extent.max_x, 5) |
| 173 | self.assertAlmostEqual(source.extent[3], layer.extent.max_y, 5) |
| 174 | |
| 175 | # Now checking the field names. |
| 176 | flds = layer.fields |
| 177 | for f in flds: |
| 178 | self.assertIn(f, source.fields) |
| 179 | |
| 180 | # Negative FIDs are not allowed. |
| 181 | with self.assertRaisesMessage( |
| 182 | IndexError, "Negative indices are not allowed on OGR Layers." |
| 183 | ): |
| 184 | layer.__getitem__(-1) |
| 185 | with self.assertRaisesMessage(IndexError, "Invalid feature id: 50000."): |
| 186 | layer.__getitem__(50000) |
| 187 | |
| 188 | if hasattr(source, "field_values"): |
| 189 | # Testing `Layer.get_fields` (which uses Layer.__iter__) |
| 190 | for fld_name, fld_value in source.field_values.items(): |
| 191 | self.assertEqual(fld_value, layer.get_fields(fld_name)) |
| 192 | |
| 193 | # Testing `Layer.__getitem__`. |
| 194 | for i, fid in enumerate(source.fids): |
| 195 | feat = layer[fid] |
| 196 | self.assertEqual(fid, feat.fid) |
| 197 | # Maybe this should be in the test below, but we might |
| 198 | # as well test the feature values here while in this |
| 199 | # loop. |
| 200 | for fld_name, fld_value in source.field_values.items(): |
| 201 | self.assertEqual(fld_value[i], feat.get(fld_name)) |
| 202 | |
| 203 | msg = ( |
| 204 | "Index out of range when accessing field in a feature: %s." |
| 205 | ) |
| 206 | with self.assertRaisesMessage(IndexError, msg % len(feat)): |
| 207 | feat.__getitem__(len(feat)) |
| 208 | |
| 209 | with self.assertRaisesMessage( |
nothing calls this directly
no test coverage detected