'Testing Data Source Layers.'

def test03a_layers(self):

    for source in ds_list:
        ds = DataSource(source.ds)
        for layer in ds:
            self.assertEqual(len(layer), source.nfeat)
            self.assertEqual(source.nfld, layer.num_fields)
            self.assertEqual(source.nfld, len(layer.fields))
            self.assertIsInstance(layer.extent, Envelope)
            self.assertAlmostEqual(source.extent[0], layer.extent.min_x, 5)
            self.assertAlmostEqual(source.extent[1], layer.extent.min_y, 5)
            self.assertAlmostEqual(source.extent[2], layer.extent.max_x, 5)
            self.assertAlmostEqual(source.extent[3], layer.extent.max_y, 5)
            flds = layer.fields
            for f in flds:
                self.assertIn(f, source.fields)
            with self.assertRaises(OGRIndexError):
                layer.__getitem__((-1))
            with self.assertRaises(OGRIndexError):
                layer.__getitem__(50000)
            if hasattr(source, 'field_values'):
                for (fld_name, fld_value) in source.field_values.items():
                    self.assertEqual(fld_value, layer.get_fields(fld_name))
                for (i, fid) in enumerate(source.fids):
                    feat = layer[fid]
                    self.assertEqual(fid, feat.fid)
                    for (fld_name, fld_value) in source.field_values.items():
                        self.assertEqual(fld_value[i], feat.get(fld_name))
