From 00b999d6501d8b5ab86a0b55c9c37639e52c910f Mon Sep 17 00:00:00 2001 From: Daniel Baston Date: Tue, 15 Aug 2023 13:30:35 -0400 Subject: [PATCH 1/2] autotest: ogr_rfc35_shape.py: make tests independent --- autotest/ogr/ogr_rfc35_shape.py | 112 ++++++++++++++++---------------- 1 file changed, 57 insertions(+), 55 deletions(-) diff --git a/autotest/ogr/ogr_rfc35_shape.py b/autotest/ogr/ogr_rfc35_shape.py index f694540b1954..e48b366e14d2 100755 --- a/autotest/ogr/ogr_rfc35_shape.py +++ b/autotest/ogr/ogr_rfc35_shape.py @@ -30,6 +30,8 @@ ############################################################################### +import tempfile + import gdaltest import pytest @@ -47,26 +49,27 @@ def module_disable_exceptions(): # -def CheckFileSize(src_filename): +def CheckFileSize(src_filename, tmpdir): import test_py_scripts + dst_filename = tempfile.mktemp(suffix=".dbf", dir=tmpdir) + script_path = test_py_scripts.get_py_script("ogr2ogr") if script_path is None: pytest.skip() test_py_scripts.run_py_script( - script_path, "ogr2ogr", "tmp/CheckFileSize.dbf " + src_filename + script_path, "ogr2ogr", f"{dst_filename} {src_filename}" ) statBufSrc = gdal.VSIStatL( src_filename, gdal.VSI_STAT_EXISTS_FLAG | gdal.VSI_STAT_NATURE_FLAG | gdal.VSI_STAT_SIZE_FLAG, ) statBufDst = gdal.VSIStatL( - "tmp/CheckFileSize.dbf", + dst_filename, gdal.VSI_STAT_EXISTS_FLAG | gdal.VSI_STAT_NATURE_FLAG | gdal.VSI_STAT_SIZE_FLAG, ) - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/CheckFileSize.dbf") assert statBufSrc.size == statBufDst.size, ( "src_size = %d, dst_size = %d", @@ -79,9 +82,12 @@ def CheckFileSize(src_filename): # Initiate the test file -def test_ogr_rfc35_shape_1(): +@pytest.fixture() +def rfc35_test_dbf(tmp_path): - ds = ogr.GetDriverByName("ESRI Shapefile").CreateDataSource("tmp/rfc35_test.dbf") + fname = str(tmp_path / "rfc35_test.dbf") + + ds = ogr.GetDriverByName("ESRI Shapefile").CreateDataSource(fname) lyr = ds.CreateLayer("rfc35_test") lyr.ReorderFields([]) @@ -120,6 +126,8 @@ def test_ogr_rfc35_shape_1(): fd.SetWidth(20) lyr.CreateField(fd) + return fname + ############################################################################### # Test ReorderField() @@ -146,6 +154,7 @@ def CheckFeatures(lyr, field1="foo5", field2="bar10", field3="baz15", field4="ba lyr.ResetReading() feat = lyr.GetNextFeature() i = 0 + while feat is not None: if ( ( @@ -177,18 +186,20 @@ def CheckFeatures(lyr, field1="foo5", field2="bar10", field3="baz15", field4="ba def CheckColumnOrder(lyr, expected_order): + __tracebackhide__ = True + lyr_defn = lyr.GetLayerDefn() for i, exp_order in enumerate(expected_order): assert lyr_defn.GetFieldDefn(i).GetName() == exp_order -def Check(lyr, expected_order): +def Check(ds, lyr, expected_order): CheckColumnOrder(lyr, expected_order) CheckFeatures(lyr) - ds = ogr.Open("tmp/rfc35_test.dbf", update=1) + ds = ogr.Open(ds.GetDescription(), update=1) lyr_reopen = ds.GetLayer(0) CheckColumnOrder(lyr_reopen, expected_order) @@ -196,9 +207,9 @@ def Check(lyr, expected_order): CheckFeatures(lyr_reopen) -def test_ogr_rfc35_shape_2(): +def test_ogr_rfc35_shape_2(rfc35_test_dbf): - ds = ogr.Open("tmp/rfc35_test.dbf", update=1) + ds = ogr.Open(rfc35_test_dbf, update=1) lyr = ds.GetLayer(0) assert lyr.TestCapability(ogr.OLCReorderFields) == 1 @@ -212,28 +223,28 @@ def test_ogr_rfc35_shape_2(): feat = None assert lyr.ReorderField(1, 3) == 0 - Check(lyr, ["foo5", "baz15", "baw20", "bar10"]) + Check(ds, lyr, ["foo5", "baz15", "baw20", "bar10"]) lyr.ReorderField(3, 1) - Check(lyr, ["foo5", "bar10", "baz15", "baw20"]) + Check(ds, lyr, ["foo5", "bar10", "baz15", "baw20"]) lyr.ReorderField(0, 2) - Check(lyr, ["bar10", "baz15", "foo5", "baw20"]) + Check(ds, lyr, ["bar10", "baz15", "foo5", "baw20"]) lyr.ReorderField(2, 0) - Check(lyr, ["foo5", "bar10", "baz15", "baw20"]) + Check(ds, lyr, ["foo5", "bar10", "baz15", "baw20"]) lyr.ReorderField(0, 1) - Check(lyr, ["bar10", "foo5", "baz15", "baw20"]) + Check(ds, lyr, ["bar10", "foo5", "baz15", "baw20"]) lyr.ReorderField(1, 0) - Check(lyr, ["foo5", "bar10", "baz15", "baw20"]) + Check(ds, lyr, ["foo5", "bar10", "baz15", "baw20"]) lyr.ReorderFields([3, 2, 1, 0]) - Check(lyr, ["baw20", "baz15", "bar10", "foo5"]) + Check(ds, lyr, ["baw20", "baz15", "bar10", "foo5"]) lyr.ReorderFields([3, 2, 1, 0]) - Check(lyr, ["foo5", "bar10", "baz15", "baw20"]) + Check(ds, lyr, ["foo5", "bar10", "baz15", "baw20"]) with gdaltest.error_handler(): ret = lyr.ReorderFields([0, 0, 0, 0]) @@ -241,7 +252,7 @@ def test_ogr_rfc35_shape_2(): ds = None - ds = ogr.Open("tmp/rfc35_test.dbf", update=1) + ds = ogr.Open(rfc35_test_dbf, update=1) lyr = ds.GetLayer(0) CheckColumnOrder(lyr, ["foo5", "bar10", "baz15", "baw20"]) @@ -253,9 +264,9 @@ def test_ogr_rfc35_shape_2(): # Test AlterFieldDefn() for change of name and width -def test_ogr_rfc35_shape_3(): +def test_ogr_rfc35_shape_3(rfc35_test_dbf): - ds = ogr.Open("tmp/rfc35_test.dbf", update=1) + ds = ogr.Open(rfc35_test_dbf, update=1) lyr = ds.GetLayer(0) fd = ogr.FieldDefn("baz25", ogr.OFTString) @@ -285,7 +296,7 @@ def test_ogr_rfc35_shape_3(): ds = None - ds = ogr.Open("tmp/rfc35_test.dbf", update=1) + ds = ogr.Open(rfc35_test_dbf, update=1) lyr = ds.GetLayer(0) lyr_defn = lyr.GetLayerDefn() fld_defn = lyr_defn.GetFieldDefn(lyr_defn.GetFieldIndex("baz5")) @@ -298,9 +309,9 @@ def test_ogr_rfc35_shape_3(): # Test AlterFieldDefn() for change of type -def test_ogr_rfc35_shape_4(): +def test_ogr_rfc35_shape_4(rfc35_test_dbf, tmp_path): - ds = ogr.Open("tmp/rfc35_test.dbf", update=1) + ds = ogr.Open(rfc35_test_dbf, update=1) lyr = ds.GetLayer(0) lyr_defn = lyr.GetLayerDefn() @@ -325,7 +336,7 @@ def test_ogr_rfc35_shape_4(): assert feat.GetField("intfield") == 12345 feat = None - CheckFeatures(lyr, field3="baz5") + CheckFeatures(lyr) fd.SetWidth(5) lyr.AlterFieldDefn(lyr_defn.GetFieldIndex("intfield"), fd, ogr.ALTER_ALL_FLAG) @@ -335,11 +346,11 @@ def test_ogr_rfc35_shape_4(): assert feat.GetField("intfield") == 12345 feat = None - CheckFeatures(lyr, field3="baz5") + CheckFeatures(lyr) ds = None - ds = ogr.Open("tmp/rfc35_test.dbf", update=1) + ds = ogr.Open(rfc35_test_dbf, update=1) lyr = ds.GetLayer(0) lyr_defn = lyr.GetLayerDefn() @@ -351,14 +362,14 @@ def test_ogr_rfc35_shape_4(): assert feat.GetField("intfield") == 1234 feat = None - CheckFeatures(lyr, field3="baz5") + CheckFeatures(lyr) ds = None # Check that the file size has decreased after column shrinking - CheckFileSize("tmp/rfc35_test.dbf") + CheckFileSize(rfc35_test_dbf, tmp_path) - ds = ogr.Open("tmp/rfc35_test.dbf", update=1) + ds = ogr.Open(rfc35_test_dbf, update=1) lyr = ds.GetLayer(0) lyr_defn = lyr.GetLayerDefn() @@ -371,11 +382,11 @@ def test_ogr_rfc35_shape_4(): assert feat.GetField("oldintfld") == "1234" feat = None - CheckFeatures(lyr, field3="baz5") + CheckFeatures(lyr) ds = None - ds = ogr.Open("tmp/rfc35_test.dbf", update=1) + ds = ogr.Open(rfc35_test_dbf, update=1) lyr = ds.GetLayer(0) lyr_defn = lyr.GetLayerDefn() @@ -384,7 +395,7 @@ def test_ogr_rfc35_shape_4(): assert feat.GetField("oldintfld") == "1234" feat = None - CheckFeatures(lyr, field3="baz5") + CheckFeatures(lyr) lyr.DeleteField(lyr_defn.GetFieldIndex("oldintfld")) @@ -409,11 +420,11 @@ def test_ogr_rfc35_shape_4(): assert feat.GetField("oldintfld") == "98765" feat = None - CheckFeatures(lyr, field3="baz5") + CheckFeatures(lyr) ds = None - ds = ogr.Open("tmp/rfc35_test.dbf", update=1) + ds = ogr.Open(rfc35_test_dbf, update=1) lyr = ds.GetLayer(0) lyr_defn = lyr.GetLayerDefn() @@ -422,23 +433,21 @@ def test_ogr_rfc35_shape_4(): assert feat.GetField("oldintfld") == "98765" feat = None - CheckFeatures(lyr, field3="baz5") + CheckFeatures(lyr) ############################################################################### # Test DeleteField() -def test_ogr_rfc35_shape_5(): +def test_ogr_rfc35_shape_5(rfc35_test_dbf, tmp_path): - ds = ogr.Open("tmp/rfc35_test.dbf", update=1) + ds = ogr.Open(rfc35_test_dbf, update=1) lyr = ds.GetLayer(0) lyr_defn = lyr.GetLayerDefn() assert lyr.TestCapability(ogr.OLCDeleteField) == 1 - assert lyr.DeleteField(0) == 0 - with gdaltest.error_handler(): ret = lyr.DeleteField(-1) assert ret != 0 @@ -447,44 +456,37 @@ def test_ogr_rfc35_shape_5(): ret = lyr.DeleteField(lyr.GetLayerDefn().GetFieldCount()) assert ret != 0 - CheckFeatures(lyr, field3="baz5") + CheckFeatures(lyr) assert lyr.DeleteField(lyr_defn.GetFieldIndex("baw20")) == 0 ds = None # Check that the file size has decreased after column removing - CheckFileSize("tmp/rfc35_test.dbf") + CheckFileSize(rfc35_test_dbf, tmp_path) - ds = ogr.Open("tmp/rfc35_test.dbf", update=1) + ds = ogr.Open(rfc35_test_dbf, update=1) lyr = ds.GetLayer(0) lyr_defn = lyr.GetLayerDefn() - CheckFeatures(lyr, field3="baz5", field4=None) + CheckFeatures(lyr, field4=None) - assert lyr.DeleteField(lyr_defn.GetFieldIndex("baz5")) == 0 + assert lyr.DeleteField(lyr_defn.GetFieldIndex("baz15")) == 0 CheckFeatures(lyr, field3=None, field4=None) assert lyr.DeleteField(lyr_defn.GetFieldIndex("foo5")) == 0 + CheckFeatures(lyr, field1=None, field3=None, field4=None) + assert lyr.DeleteField(lyr_defn.GetFieldIndex("bar10")) == 0 CheckFeatures(lyr, field1=None, field2=None, field3=None, field4=None) ds = None - ds = ogr.Open("tmp/rfc35_test.dbf", update=1) + ds = ogr.Open(rfc35_test_dbf, update=1) lyr = ds.GetLayer(0) lyr_defn = lyr.GetLayerDefn() CheckFeatures(lyr, field1=None, field2=None, field3=None, field4=None) - - -############################################################################### -# Initiate the test file - - -def test_ogr_rfc35_shape_cleanup(): - - ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/rfc35_test.dbf") From 2719c452026fe85c84daec7090534f7abcab261a Mon Sep 17 00:00:00 2001 From: Daniel Baston Date: Tue, 15 Aug 2023 21:22:44 -0400 Subject: [PATCH 2/2] autotest: combine ogr_rfc_35 tests --- .../ogr/{ogr_rfc35_shape.py => ogr_rfc35.py} | 302 ++++++---- autotest/ogr/ogr_rfc35_mem.py | 381 ------------- autotest/ogr/ogr_rfc35_mitab.py | 520 ------------------ autotest/ogr/ogr_rfc35_sqlite.py | 426 -------------- 4 files changed, 191 insertions(+), 1438 deletions(-) rename autotest/ogr/{ogr_rfc35_shape.py => ogr_rfc35.py} (59%) delete mode 100755 autotest/ogr/ogr_rfc35_mem.py delete mode 100755 autotest/ogr/ogr_rfc35_mitab.py delete mode 100755 autotest/ogr/ogr_rfc35_sqlite.py diff --git a/autotest/ogr/ogr_rfc35_shape.py b/autotest/ogr/ogr_rfc35.py similarity index 59% rename from autotest/ogr/ogr_rfc35_shape.py rename to autotest/ogr/ogr_rfc35.py index e48b366e14d2..c41216dfd8ea 100755 --- a/autotest/ogr/ogr_rfc35_shape.py +++ b/autotest/ogr/ogr_rfc35.py @@ -4,11 +4,11 @@ # $Id$ # # Project: GDAL/OGR Test Suite -# Purpose: Test RFC35 for shape driver +# Purpose: Test RFC35 for several drivers # Author: Even Rouault # ############################################################################### -# Copyright (c) 2011-2013, Even Rouault +# Copyright (c) 2011-2014, Even Rouault # # Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the "Software"), @@ -30,7 +30,7 @@ ############################################################################### -import tempfile +import os import gdaltest import pytest @@ -45,6 +45,19 @@ def module_disable_exceptions(): yield +driver_extensions = { + "ESRI Shapefile": "dbf", + "MapInfo File": "tab", + "SQLite": "sqlite", + "Memory": None, +} + + +@pytest.fixture(autouse=True, params=driver_extensions.keys()) +def driver_name(request): + return request.param + + ############################################################################### # @@ -53,15 +66,22 @@ def CheckFileSize(src_filename, tmpdir): import test_py_scripts - dst_filename = tempfile.mktemp(suffix=".dbf", dir=tmpdir) + src_ext = os.path.splitext(src_filename)[1] + + for driver_name, ext in driver_extensions.items(): + if ext is not None and src_ext.endswith(ext): + driver = driver_name + + dst_filename = os.path.join(tmpdir, "checkfilesize." + src_ext) script_path = test_py_scripts.get_py_script("ogr2ogr") if script_path is None: pytest.skip() test_py_scripts.run_py_script( - script_path, "ogr2ogr", f"{dst_filename} {src_filename}" + script_path, "ogr2ogr", f'-f "{driver}" {dst_filename} {src_filename}' ) + statBufSrc = gdal.VSIStatL( src_filename, gdal.VSI_STAT_EXISTS_FLAG | gdal.VSI_STAT_NATURE_FLAG | gdal.VSI_STAT_SIZE_FLAG, @@ -83,11 +103,17 @@ def CheckFileSize(src_filename, tmpdir): @pytest.fixture() -def rfc35_test_dbf(tmp_path): +def rfc35_test_input(driver_name, tmp_path): + + ext = driver_extensions[driver_name] + fname = str(tmp_path / f"rfc35_test.{ext}") + + drv = ogr.GetDriverByName(driver_name) - fname = str(tmp_path / "rfc35_test.dbf") + if drv is None: + pytest.skip(f"Driver {driver_name} not available") - ds = ogr.GetDriverByName("ESRI Shapefile").CreateDataSource(fname) + ds = drv.CreateDataSource(fname) lyr = ds.CreateLayer("rfc35_test") lyr.ReorderFields([]) @@ -126,11 +152,10 @@ def rfc35_test_dbf(tmp_path): fd.SetWidth(20) lyr.CreateField(fd) - return fname - - -############################################################################### -# Test ReorderField() + if driver_name == "Memory": + return ds + else: + return fname def Truncate(val, lyr_defn, fieldname): @@ -140,7 +165,15 @@ def Truncate(val, lyr_defn, fieldname): return val[0 : lyr_defn.GetFieldDefn(lyr_defn.GetFieldIndex(fieldname)).GetWidth()] -def CheckFeatures(lyr, field1="foo5", field2="bar10", field3="baz15", field4="baw20"): +def Identity(val, *args): + return val + + +def CheckFeatures( + ds, lyr, field1="foo5", field2="bar10", field3="baz15", field4="baw20" +): + + driver_name = ds.GetDriver().GetName() expected_values = [ ["foo0", None, None, None], @@ -149,39 +182,35 @@ def CheckFeatures(lyr, field1="foo5", field2="bar10", field3="baz15", field4="ba ["foo3", "bar3_01234", "baz3_0123456789", "baw3_012345678901234"], ] + if driver_name == "MapInfo File": + for i in range(len(expected_values)): + expected_values[i] = [ + x if x is not None else "" for x in expected_values[i] + ] + + truncate_fn = Identity if driver_name in ("SQLite", "Memory") else Truncate + lyr_defn = lyr.GetLayerDefn() lyr.ResetReading() - feat = lyr.GetNextFeature() - i = 0 - - while feat is not None: - if ( - ( - field1 is not None - and feat.GetField(field1) - != Truncate(expected_values[i][0], lyr_defn, field1) + + for i, feat in enumerate(lyr): + if field1 is not None: + assert feat.GetField(field1) == truncate_fn( + expected_values[i][0], lyr_defn, field1 ) - or ( - field2 is not None - and feat.GetField(field2) - != Truncate(expected_values[i][1], lyr_defn, field2) + if field2 is not None: + assert feat.GetField(field2) == truncate_fn( + expected_values[i][1], lyr_defn, field2 ) - or ( - field3 is not None - and feat.GetField(field3) - != Truncate(expected_values[i][2], lyr_defn, field3) + if field3 is not None: + assert feat.GetField(field3) == truncate_fn( + expected_values[i][2], lyr_defn, field3 ) - or ( - field4 is not None - and feat.GetField(field4) - != Truncate(expected_values[i][3], lyr_defn, field4) + if field4 is not None: + assert feat.GetField(field4) == truncate_fn( + expected_values[i][3], lyr_defn, field4 ) - ): - feat.DumpReadable() - pytest.fail() - feat = lyr.GetNextFeature() - i = i + 1 def CheckColumnOrder(lyr, expected_order): @@ -197,19 +226,30 @@ def Check(ds, lyr, expected_order): CheckColumnOrder(lyr, expected_order) - CheckFeatures(lyr) + CheckFeatures(ds, lyr) + + if ds.GetDriver().GetName() == "Memory": + return ds = ogr.Open(ds.GetDescription(), update=1) lyr_reopen = ds.GetLayer(0) CheckColumnOrder(lyr_reopen, expected_order) - CheckFeatures(lyr_reopen) + CheckFeatures(ds, lyr_reopen) + + +############################################################################### +# Test ReorderField() + +def test_ogr_rfc35_2(rfc35_test_input, driver_name): -def test_ogr_rfc35_shape_2(rfc35_test_dbf): + if driver_name == "Memory": + ds = rfc35_test_input + else: + ds = ogr.Open(rfc35_test_input, update=1) - ds = ogr.Open(rfc35_test_dbf, update=1) lyr = ds.GetLayer(0) assert lyr.TestCapability(ogr.OLCReorderFields) == 1 @@ -250,23 +290,30 @@ def test_ogr_rfc35_shape_2(rfc35_test_dbf): ret = lyr.ReorderFields([0, 0, 0, 0]) assert ret != 0 + if driver_name == "Memory": + return + ds = None - ds = ogr.Open(rfc35_test_dbf, update=1) + ds = ogr.Open(rfc35_test_input, update=1) lyr = ds.GetLayer(0) CheckColumnOrder(lyr, ["foo5", "bar10", "baz15", "baw20"]) - CheckFeatures(lyr) + CheckFeatures(ds, lyr) ############################################################################### # Test AlterFieldDefn() for change of name and width -def test_ogr_rfc35_shape_3(rfc35_test_dbf): +def test_ogr_rfc35_3(rfc35_test_input, driver_name): + + if driver_name == "Memory": + ds = rfc35_test_input + else: + ds = ogr.Open(rfc35_test_input, update=1) - ds = ogr.Open(rfc35_test_dbf, update=1) lyr = ds.GetLayer(0) fd = ogr.FieldDefn("baz25", ogr.OFTString) @@ -284,7 +331,7 @@ def test_ogr_rfc35_shape_3(rfc35_test_dbf): lyr.AlterFieldDefn(lyr_defn.GetFieldIndex("baz15"), fd, ogr.ALTER_ALL_FLAG) - CheckFeatures(lyr, field3="baz25") + CheckFeatures(ds, lyr, field3="baz25") fd = ogr.FieldDefn("baz5", ogr.OFTString) fd.SetWidth(5) @@ -292,26 +339,43 @@ def test_ogr_rfc35_shape_3(rfc35_test_dbf): lyr_defn = lyr.GetLayerDefn() lyr.AlterFieldDefn(lyr_defn.GetFieldIndex("baz25"), fd, ogr.ALTER_ALL_FLAG) - CheckFeatures(lyr, field3="baz5") + CheckFeatures(ds, lyr, field3="baz5") - ds = None + if driver_name not in ("SQLite", "Memory"): + ds = None + ds = ogr.Open(rfc35_test_input, update=1) - ds = ogr.Open(rfc35_test_dbf, update=1) lyr = ds.GetLayer(0) lyr_defn = lyr.GetLayerDefn() fld_defn = lyr_defn.GetFieldDefn(lyr_defn.GetFieldIndex("baz5")) assert fld_defn.GetWidth() == 5 - CheckFeatures(lyr, field3="baz5") + CheckFeatures(ds, lyr, field3="baz5") + + # Change only name + if driver_name == "SQLite": + fd = ogr.FieldDefn("baz5_2", ogr.OFTString) + fd.SetWidth(5) + lyr.AlterFieldDefn(lyr_defn.GetFieldIndex("baz5"), fd, ogr.ALTER_ALL_FLAG) + assert fld_defn.GetWidth() == 5 ############################################################################### # Test AlterFieldDefn() for change of type -def test_ogr_rfc35_shape_4(rfc35_test_dbf, tmp_path): +def test_ogr_rfc35_4(rfc35_test_input, driver_name, tmp_path): + + if driver_name == "ESRI Shapefile": + int_resizing_supported = True + else: + int_resizing_supported = False + + if driver_name == "Memory": + ds = rfc35_test_input + else: + ds = ogr.Open(rfc35_test_input, update=1) - ds = ogr.Open(rfc35_test_dbf, update=1) lyr = ds.GetLayer(0) lyr_defn = lyr.GetLayerDefn() @@ -329,49 +393,53 @@ def test_ogr_rfc35_shape_4(rfc35_test_dbf, tmp_path): feat = None fd.SetWidth(10) - lyr.AlterFieldDefn(lyr_defn.GetFieldIndex("intfield"), fd, ogr.ALTER_ALL_FLAG) + ret = lyr.AlterFieldDefn(lyr_defn.GetFieldIndex("intfield"), fd, ogr.ALTER_ALL_FLAG) + + if driver_name == "MapInfo File": + assert ret != 0 lyr.ResetReading() feat = lyr.GetNextFeature() assert feat.GetField("intfield") == 12345 feat = None - CheckFeatures(lyr) - - fd.SetWidth(5) - lyr.AlterFieldDefn(lyr_defn.GetFieldIndex("intfield"), fd, ogr.ALTER_ALL_FLAG) + CheckFeatures(ds, lyr) - lyr.ResetReading() - feat = lyr.GetNextFeature() - assert feat.GetField("intfield") == 12345 - feat = None + if int_resizing_supported: + fd.SetWidth(5) + lyr.AlterFieldDefn(lyr_defn.GetFieldIndex("intfield"), fd, ogr.ALTER_ALL_FLAG) - CheckFeatures(lyr) + lyr.ResetReading() + feat = lyr.GetNextFeature() + assert feat.GetField("intfield") == 12345 + feat = None - ds = None + CheckFeatures(ds, lyr) - ds = ogr.Open(rfc35_test_dbf, update=1) - lyr = ds.GetLayer(0) - lyr_defn = lyr.GetLayerDefn() + ds = ogr.Open(rfc35_test_input, update=1) + lyr = ds.GetLayer(0) + lyr_defn = lyr.GetLayerDefn() - fd.SetWidth(4) - lyr.AlterFieldDefn(lyr_defn.GetFieldIndex("intfield"), fd, ogr.ALTER_ALL_FLAG) + fd.SetWidth(4) + lyr.AlterFieldDefn(lyr_defn.GetFieldIndex("intfield"), fd, ogr.ALTER_ALL_FLAG) - lyr.ResetReading() - feat = lyr.GetNextFeature() - assert feat.GetField("intfield") == 1234 - feat = None + lyr.ResetReading() + feat = lyr.GetNextFeature() + assert feat.GetField("intfield") == 1234 + feat = None - CheckFeatures(lyr) + CheckFeatures(ds, lyr) - ds = None + ds = None - # Check that the file size has decreased after column shrinking - CheckFileSize(rfc35_test_dbf, tmp_path) + # Check that the file size has decreased after column shrinking + CheckFileSize(rfc35_test_input, tmp_path) - ds = ogr.Open(rfc35_test_dbf, update=1) - lyr = ds.GetLayer(0) - lyr_defn = lyr.GetLayerDefn() + if driver_name != "Memory": + ds = None + ds = ogr.Open(rfc35_test_input, update=1) + lyr = ds.GetLayer(0) + lyr_defn = lyr.GetLayerDefn() fd = ogr.FieldDefn("oldintfld", ogr.OFTString) fd.SetWidth(15) @@ -379,23 +447,23 @@ def test_ogr_rfc35_shape_4(rfc35_test_dbf, tmp_path): lyr.ResetReading() feat = lyr.GetNextFeature() - assert feat.GetField("oldintfld") == "1234" + assert feat.GetField("oldintfld") == "1234" if int_resizing_supported else "12345" feat = None - CheckFeatures(lyr) - - ds = None + CheckFeatures(ds, lyr) - ds = ogr.Open(rfc35_test_dbf, update=1) - lyr = ds.GetLayer(0) - lyr_defn = lyr.GetLayerDefn() + if driver_name != "Memory": + ds = None + ds = ogr.Open(rfc35_test_input, update=1) + lyr = ds.GetLayer(0) + lyr_defn = lyr.GetLayerDefn() lyr.ResetReading() feat = lyr.GetNextFeature() - assert feat.GetField("oldintfld") == "1234" + assert feat.GetField("oldintfld") == "1234" if int_resizing_supported else "12345" feat = None - CheckFeatures(lyr) + CheckFeatures(ds, lyr) lyr.DeleteField(lyr_defn.GetFieldIndex("oldintfld")) @@ -420,11 +488,14 @@ def test_ogr_rfc35_shape_4(rfc35_test_dbf, tmp_path): assert feat.GetField("oldintfld") == "98765" feat = None - CheckFeatures(lyr) + CheckFeatures(ds, lyr) + + if driver_name == "Memory": + return ds = None - ds = ogr.Open(rfc35_test_dbf, update=1) + ds = ogr.Open(rfc35_test_input, update=1) lyr = ds.GetLayer(0) lyr_defn = lyr.GetLayerDefn() @@ -433,16 +504,20 @@ def test_ogr_rfc35_shape_4(rfc35_test_dbf, tmp_path): assert feat.GetField("oldintfld") == "98765" feat = None - CheckFeatures(lyr) + CheckFeatures(ds, lyr) ############################################################################### # Test DeleteField() -def test_ogr_rfc35_shape_5(rfc35_test_dbf, tmp_path): +def test_ogr_rfc35_5(rfc35_test_input, driver_name, tmp_path): + + if driver_name == "Memory": + ds = rfc35_test_input + else: + ds = ogr.Open(rfc35_test_input, update=1) - ds = ogr.Open(rfc35_test_dbf, update=1) lyr = ds.GetLayer(0) lyr_defn = lyr.GetLayerDefn() @@ -456,37 +531,42 @@ def test_ogr_rfc35_shape_5(rfc35_test_dbf, tmp_path): ret = lyr.DeleteField(lyr.GetLayerDefn().GetFieldCount()) assert ret != 0 - CheckFeatures(lyr) + CheckFeatures(ds, lyr) assert lyr.DeleteField(lyr_defn.GetFieldIndex("baw20")) == 0 - ds = None - - # Check that the file size has decreased after column removing - CheckFileSize(rfc35_test_dbf, tmp_path) + if driver_name not in ("Memory", "SQLite"): + ds = None + # Check that the file size has decreased after column removing + CheckFileSize(rfc35_test_input, tmp_path) - ds = ogr.Open(rfc35_test_dbf, update=1) - lyr = ds.GetLayer(0) - lyr_defn = lyr.GetLayerDefn() + ds = ogr.Open(rfc35_test_input, update=1) + lyr = ds.GetLayer(0) + lyr_defn = lyr.GetLayerDefn() - CheckFeatures(lyr, field4=None) + CheckFeatures(ds, lyr, field4=None) assert lyr.DeleteField(lyr_defn.GetFieldIndex("baz15")) == 0 - CheckFeatures(lyr, field3=None, field4=None) + CheckFeatures(ds, lyr, field3=None, field4=None) assert lyr.DeleteField(lyr_defn.GetFieldIndex("foo5")) == 0 - CheckFeatures(lyr, field1=None, field3=None, field4=None) + CheckFeatures(ds, lyr, field1=None, field3=None, field4=None) + + if driver_name != "MapInfo File": + # MapInfo does not allow removing the last field + assert lyr.DeleteField(lyr_defn.GetFieldIndex("bar10")) == 0 - assert lyr.DeleteField(lyr_defn.GetFieldIndex("bar10")) == 0 + CheckFeatures(ds, lyr, field1=None, field2=None, field3=None, field4=None) - CheckFeatures(lyr, field1=None, field2=None, field3=None, field4=None) + if driver_name == "Memory": + return ds = None - ds = ogr.Open(rfc35_test_dbf, update=1) + ds = ogr.Open(rfc35_test_input, update=1) lyr = ds.GetLayer(0) lyr_defn = lyr.GetLayerDefn() - CheckFeatures(lyr, field1=None, field2=None, field3=None, field4=None) + CheckFeatures(ds, lyr, field1=None, field2=None, field3=None, field4=None) diff --git a/autotest/ogr/ogr_rfc35_mem.py b/autotest/ogr/ogr_rfc35_mem.py deleted file mode 100755 index 5f1f041b94e5..000000000000 --- a/autotest/ogr/ogr_rfc35_mem.py +++ /dev/null @@ -1,381 +0,0 @@ -#!/usr/bin/env pytest -############################################################################### -# $Id$ -# -# Project: GDAL/OGR Test Suite -# Purpose: Test RFC35 for Memory driver -# Author: Even Rouault -# -############################################################################### -# Copyright (c) 2011, Even Rouault -# -# Permission is hereby granted, free of charge, to any person obtaining a -# copy of this software and associated documentation files (the "Software"), -# to deal in the Software without restriction, including without limitation -# the rights to use, copy, modify, merge, publish, distribute, sublicense, -# and/or sell copies of the Software, and to permit persons to whom the -# Software is furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included -# in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -# DEALINGS IN THE SOFTWARE. -############################################################################### - - -import gdaltest -import pytest - -from osgeo import ogr - - -############################################################################### -@pytest.fixture(autouse=True, scope="module") -def module_disable_exceptions(): - with gdaltest.disable_exceptions(): - yield - - -############################################################################### -# Initiate the test file - - -def test_ogr_rfc35_mem_1(): - - gdaltest.rfc35_mem_ds = ogr.GetDriverByName("Memory").CreateDataSource("rfc35_test") - lyr = gdaltest.rfc35_mem_ds.CreateLayer("rfc35_test") - - lyr.ReorderFields([]) - - fd = ogr.FieldDefn("foo5", ogr.OFTString) - fd.SetWidth(5) - lyr.CreateField(fd) - - feat = ogr.Feature(lyr.GetLayerDefn()) - feat.SetField(0, "foo0") - lyr.CreateFeature(feat) - feat = None - - fd = ogr.FieldDefn("bar10", ogr.OFTString) - fd.SetWidth(10) - lyr.CreateField(fd) - - feat = ogr.Feature(lyr.GetLayerDefn()) - feat.SetField(0, "foo1") - feat.SetField(1, "bar1") - lyr.CreateFeature(feat) - feat = None - - fd = ogr.FieldDefn("baz15", ogr.OFTString) - fd.SetWidth(15) - lyr.CreateField(fd) - - feat = ogr.Feature(lyr.GetLayerDefn()) - feat.SetField(0, "foo2") - feat.SetField(1, "bar2_01234") - feat.SetField(2, "baz2_0123456789") - lyr.CreateFeature(feat) - feat = None - - fd = ogr.FieldDefn("baw20", ogr.OFTString) - fd.SetWidth(20) - lyr.CreateField(fd) - - -############################################################################### -# Test ReorderField() - - -def Truncate(val, lyr_defn, fieldname): - # pylint: disable=unused-argument - - # if val is None: - # return val - - # return val[0:lyr_defn.GetFieldDefn(lyr_defn.GetFieldIndex(fieldname)).GetWidth()] - # Mem driver doesn't actually truncate - return val - - -def CheckFeatures(lyr, field1="foo5", field2="bar10", field3="baz15", field4="baw20"): - - expected_values = [ - ["foo0", None, None, None], - ["foo1", "bar1", None, None], - ["foo2", "bar2_01234", "baz2_0123456789", None], - ["foo3", "bar3_01234", "baz3_0123456789", "baw3_012345678901234"], - ] - - lyr_defn = lyr.GetLayerDefn() - - lyr.ResetReading() - feat = lyr.GetNextFeature() - i = 0 - while feat is not None: - if ( - ( - field1 is not None - and feat.GetField(field1) - != Truncate(expected_values[i][0], lyr_defn, field1) - ) - or ( - field2 is not None - and feat.GetField(field2) - != Truncate(expected_values[i][1], lyr_defn, field2) - ) - or ( - field3 is not None - and feat.GetField(field3) - != Truncate(expected_values[i][2], lyr_defn, field3) - ) - or ( - field4 is not None - and feat.GetField(field4) - != Truncate(expected_values[i][3], lyr_defn, field4) - ) - ): - feat.DumpReadable() - pytest.fail() - feat = lyr.GetNextFeature() - i = i + 1 - - -def CheckColumnOrder(lyr, expected_order): - - lyr_defn = lyr.GetLayerDefn() - for i, exp_order in enumerate(expected_order): - assert lyr_defn.GetFieldDefn(i).GetName() == exp_order - - -def Check(lyr, expected_order): - - CheckColumnOrder(lyr, expected_order) - - CheckFeatures(lyr) - - -def test_ogr_rfc35_mem_2(): - - lyr = gdaltest.rfc35_mem_ds.GetLayer(0) - - assert lyr.TestCapability(ogr.OLCReorderFields) == 1 - - feat = ogr.Feature(lyr.GetLayerDefn()) - feat.SetField(0, "foo3") - feat.SetField(1, "bar3_01234") - feat.SetField(2, "baz3_0123456789") - feat.SetField(3, "baw3_012345678901234") - lyr.CreateFeature(feat) - feat = None - - assert lyr.ReorderField(1, 3) == 0 - Check(lyr, ["foo5", "baz15", "baw20", "bar10"]) - - lyr.ReorderField(3, 1) - Check(lyr, ["foo5", "bar10", "baz15", "baw20"]) - - lyr.ReorderField(0, 2) - Check(lyr, ["bar10", "baz15", "foo5", "baw20"]) - - lyr.ReorderField(2, 0) - Check(lyr, ["foo5", "bar10", "baz15", "baw20"]) - - lyr.ReorderField(0, 1) - Check(lyr, ["bar10", "foo5", "baz15", "baw20"]) - - lyr.ReorderField(1, 0) - Check(lyr, ["foo5", "bar10", "baz15", "baw20"]) - - lyr.ReorderFields([3, 2, 1, 0]) - Check(lyr, ["baw20", "baz15", "bar10", "foo5"]) - - lyr.ReorderFields([3, 2, 1, 0]) - Check(lyr, ["foo5", "bar10", "baz15", "baw20"]) - - with gdaltest.error_handler(): - ret = lyr.ReorderFields([0, 0, 0, 0]) - assert ret != 0 - - -############################################################################### -# Test AlterFieldDefn() for change of name and width - - -def test_ogr_rfc35_mem_3(): - - lyr = gdaltest.rfc35_mem_ds.GetLayer(0) - - fd = ogr.FieldDefn("baz25", ogr.OFTString) - fd.SetWidth(25) - - lyr_defn = lyr.GetLayerDefn() - - with gdaltest.error_handler(): - ret = lyr.AlterFieldDefn(-1, fd, ogr.ALTER_ALL_FLAG) - assert ret != 0 - - with gdaltest.error_handler(): - ret = lyr.AlterFieldDefn(lyr_defn.GetFieldCount(), fd, ogr.ALTER_ALL_FLAG) - assert ret != 0 - - lyr.AlterFieldDefn(lyr_defn.GetFieldIndex("baz15"), fd, ogr.ALTER_ALL_FLAG) - - CheckFeatures(lyr, field3="baz25") - - fd = ogr.FieldDefn("baz5", ogr.OFTString) - fd.SetWidth(5) - - lyr_defn = lyr.GetLayerDefn() - lyr.AlterFieldDefn(lyr_defn.GetFieldIndex("baz25"), fd, ogr.ALTER_ALL_FLAG) - - CheckFeatures(lyr, field3="baz5") - - lyr_defn = lyr.GetLayerDefn() - fld_defn = lyr_defn.GetFieldDefn(lyr_defn.GetFieldIndex("baz5")) - assert fld_defn.GetWidth() == 5 - - CheckFeatures(lyr, field3="baz5") - - -############################################################################### -# Test AlterFieldDefn() for change of type - - -def test_ogr_rfc35_mem_4(): - - lyr = gdaltest.rfc35_mem_ds.GetLayer(0) - lyr_defn = lyr.GetLayerDefn() - - assert lyr.TestCapability(ogr.OLCAlterFieldDefn) == 1 - - fd = ogr.FieldDefn("intfield", ogr.OFTInteger) - lyr.CreateField(fd) - - lyr.ReorderField(lyr_defn.GetFieldIndex("intfield"), 0) - - lyr.ResetReading() - feat = lyr.GetNextFeature() - feat.SetField("intfield", 12345) - lyr.SetFeature(feat) - feat = None - - fd.SetWidth(10) - lyr.AlterFieldDefn(lyr_defn.GetFieldIndex("intfield"), fd, ogr.ALTER_ALL_FLAG) - - lyr.ResetReading() - feat = lyr.GetNextFeature() - assert feat.GetField("intfield") == 12345 - feat = None - - CheckFeatures(lyr, field3="baz5") - - fd.SetWidth(5) - lyr.AlterFieldDefn(lyr_defn.GetFieldIndex("intfield"), fd, ogr.ALTER_ALL_FLAG) - - lyr.ResetReading() - feat = lyr.GetNextFeature() - assert feat.GetField("intfield") == 12345 - feat = None - - CheckFeatures(lyr, field3="baz5") - - fd.SetWidth(4) - lyr.AlterFieldDefn(lyr_defn.GetFieldIndex("intfield"), fd, ogr.ALTER_ALL_FLAG) - - lyr.ResetReading() - feat = lyr.GetNextFeature() - # if feat.GetField("intfield") != 1234: - assert feat.GetField("intfield") == 12345 - feat = None - - CheckFeatures(lyr, field3="baz5") - - fd = ogr.FieldDefn("oldintfld", ogr.OFTString) - fd.SetWidth(15) - lyr.AlterFieldDefn(lyr_defn.GetFieldIndex("intfield"), fd, ogr.ALTER_ALL_FLAG) - - lyr.ResetReading() - feat = lyr.GetNextFeature() - # if feat.GetField("oldintfld") != '1234': - assert feat.GetField("oldintfld") == "12345" - feat = None - - CheckFeatures(lyr, field3="baz5") - - lyr.DeleteField(lyr_defn.GetFieldIndex("oldintfld")) - - fd = ogr.FieldDefn("intfield", ogr.OFTInteger) - fd.SetWidth(10) - assert lyr.CreateField(fd) == 0 - - assert lyr.ReorderField(lyr_defn.GetFieldIndex("intfield"), 0) == 0 - - lyr.ResetReading() - feat = lyr.GetNextFeature() - feat.SetField("intfield", 98765) - assert lyr.SetFeature(feat) == 0 - feat = None - - fd = ogr.FieldDefn("oldintfld", ogr.OFTString) - fd.SetWidth(6) - lyr.AlterFieldDefn(lyr_defn.GetFieldIndex("intfield"), fd, ogr.ALTER_ALL_FLAG) - - lyr.ResetReading() - feat = lyr.GetNextFeature() - assert feat.GetField("oldintfld") == "98765" - feat = None - - CheckFeatures(lyr, field3="baz5") - - -############################################################################### -# Test DeleteField() - - -def test_ogr_rfc35_mem_5(): - - lyr = gdaltest.rfc35_mem_ds.GetLayer(0) - lyr_defn = lyr.GetLayerDefn() - - assert lyr.TestCapability(ogr.OLCDeleteField) == 1 - - with gdaltest.error_handler(): - ret = lyr.DeleteField(-1) - assert ret != 0 - - with gdaltest.error_handler(): - ret = lyr.DeleteField(lyr.GetLayerDefn().GetFieldCount()) - assert ret != 0 - - assert lyr.DeleteField(0) == 0 - - CheckFeatures(lyr, field3="baz5") - - assert lyr.DeleteField(lyr_defn.GetFieldIndex("baw20")) == 0 - - CheckFeatures(lyr, field3="baz5", field4=None) - - assert lyr.DeleteField(lyr_defn.GetFieldIndex("baz5")) == 0 - - CheckFeatures(lyr, field3=None, field4=None) - - assert lyr.DeleteField(lyr_defn.GetFieldIndex("foo5")) == 0 - - assert lyr.DeleteField(lyr_defn.GetFieldIndex("bar10")) == 0 - - CheckFeatures(lyr, field1=None, field2=None, field3=None, field4=None) - - -############################################################################### -# Initiate the test file - - -def test_ogr_rfc35_mem_cleanup(): - - gdaltest.rfc35_mem_ds = None diff --git a/autotest/ogr/ogr_rfc35_mitab.py b/autotest/ogr/ogr_rfc35_mitab.py deleted file mode 100755 index 9460e723b41c..000000000000 --- a/autotest/ogr/ogr_rfc35_mitab.py +++ /dev/null @@ -1,520 +0,0 @@ -#!/usr/bin/env pytest -# -*- coding: utf-8 -*- -############################################################################### -# $Id$ -# -# Project: GDAL/OGR Test Suite -# Purpose: Test RFC35 for MITAB driver -# Author: Even Rouault -# -############################################################################### -# Copyright (c) 2014, Even Rouault -# -# Permission is hereby granted, free of charge, to any person obtaining a -# copy of this software and associated documentation files (the "Software"), -# to deal in the Software without restriction, including without limitation -# the rights to use, copy, modify, merge, publish, distribute, sublicense, -# and/or sell copies of the Software, and to permit persons to whom the -# Software is furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included -# in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -# DEALINGS IN THE SOFTWARE. -############################################################################### - - -import gdaltest -import pytest - -from osgeo import gdal, ogr - - -############################################################################### -@pytest.fixture(autouse=True, scope="module") -def module_disable_exceptions(): - with gdaltest.disable_exceptions(): - yield - - -############################################################################### -# - - -def CheckFileSize(src_filename): - import test_py_scripts - - script_path = test_py_scripts.get_py_script("ogr2ogr") - if script_path is None: - pytest.skip() - - test_py_scripts.run_py_script( - script_path, - "ogr2ogr", - '-f "MapInfo File" tmp/CheckFileSize.tab ' + src_filename, - ) - statBufSrc = gdal.VSIStatL( - src_filename[0:-3] + "dat", - gdal.VSI_STAT_EXISTS_FLAG | gdal.VSI_STAT_NATURE_FLAG | gdal.VSI_STAT_SIZE_FLAG, - ) - statBufDst = gdal.VSIStatL( - "tmp/CheckFileSize.dat", - gdal.VSI_STAT_EXISTS_FLAG | gdal.VSI_STAT_NATURE_FLAG | gdal.VSI_STAT_SIZE_FLAG, - ) - ogr.GetDriverByName("MapInfo File").DeleteDataSource("tmp/CheckFileSize.tab") - - assert statBufSrc.size == statBufDst.size - - -############################################################################### -# Initiate the test file - - -def test_ogr_rfc35_mitab_1(): - - ds = ogr.GetDriverByName("MapInfo File").CreateDataSource("tmp/rfc35_test.tab") - lyr = ds.CreateLayer("rfc35_test") - - lyr.ReorderFields([]) - - fd = ogr.FieldDefn("foo5", ogr.OFTString) - fd.SetWidth(5) - lyr.CreateField(fd) - - feat = ogr.Feature(lyr.GetLayerDefn()) - feat.SetField(0, "foo0") - lyr.CreateFeature(feat) - feat = None - - fd = ogr.FieldDefn("bar10", ogr.OFTString) - fd.SetWidth(10) - lyr.CreateField(fd) - - feat = ogr.Feature(lyr.GetLayerDefn()) - feat.SetField(0, "foo1") - feat.SetField(1, "bar1") - lyr.CreateFeature(feat) - feat = None - - fd = ogr.FieldDefn("baz15", ogr.OFTString) - fd.SetWidth(15) - lyr.CreateField(fd) - - feat = ogr.Feature(lyr.GetLayerDefn()) - feat.SetField(0, "foo2") - feat.SetField(1, "bar2_01234") - feat.SetField(2, "baz2_0123456789") - lyr.CreateFeature(feat) - feat = None - - fd = ogr.FieldDefn("baw20", ogr.OFTString) - fd.SetWidth(20) - lyr.CreateField(fd) - - -############################################################################### -# Test ReorderField() - - -def Truncate(val, lyr_defn, fieldname): - if val is None: - return val - - return val[0 : lyr_defn.GetFieldDefn(lyr_defn.GetFieldIndex(fieldname)).GetWidth()] - - -def CheckFeatures(lyr, field1="foo5", field2="bar10", field3="baz15", field4="baw20"): - - expected_values = [ - ["foo0", "", "", ""], - ["foo1", "bar1", "", ""], - ["foo2", "bar2_01234", "baz2_0123456789", ""], - ["foo3", "bar3_01234", "baz3_0123456789", "baw3_012345678901234"], - ] - - lyr_defn = lyr.GetLayerDefn() - - lyr.ResetReading() - feat = lyr.GetNextFeature() - i = 0 - while feat is not None: - if ( - ( - field1 is not None - and feat.GetField(field1) - != Truncate(expected_values[i][0], lyr_defn, field1) - ) - or ( - field2 is not None - and feat.GetField(field2) - != Truncate(expected_values[i][1], lyr_defn, field2) - ) - or ( - field3 is not None - and feat.GetField(field3) - != Truncate(expected_values[i][2], lyr_defn, field3) - ) - or ( - field4 is not None - and feat.GetField(field4) - != Truncate(expected_values[i][3], lyr_defn, field4) - ) - ): - feat.DumpReadable() - pytest.fail() - feat = lyr.GetNextFeature() - i = i + 1 - - -def CheckColumnOrder(lyr, expected_order): - - lyr_defn = lyr.GetLayerDefn() - for i, exp_order in enumerate(expected_order): - assert lyr_defn.GetFieldDefn(i).GetName() == exp_order - - -def Check(lyr, expected_order): - - CheckColumnOrder(lyr, expected_order) - - CheckFeatures(lyr) - - ds = ogr.Open("tmp/rfc35_test.tab", update=1) - lyr_reopen = ds.GetLayer(0) - - CheckColumnOrder(lyr_reopen, expected_order) - - CheckFeatures(lyr_reopen) - - -def test_ogr_rfc35_mitab_2(): - - ds = ogr.Open("tmp/rfc35_test.tab", update=1) - lyr = ds.GetLayer(0) - - assert lyr.TestCapability(ogr.OLCReorderFields) == 1 - - feat = ogr.Feature(lyr.GetLayerDefn()) - feat.SetField(0, "foo3") - feat.SetField(1, "bar3_01234") - feat.SetField(2, "baz3_0123456789") - feat.SetField(3, "baw3_012345678901234") - lyr.CreateFeature(feat) - feat = None - - assert lyr.ReorderField(1, 3) == 0 - # ds = None - # ds = ogr.Open('tmp/rfc35_test.tab', update = 1) - # lyr = ds.GetLayer(0) - Check(lyr, ["foo5", "baz15", "baw20", "bar10"]) - - lyr.ReorderField(3, 1) - # ds = None - # ds = ogr.Open('tmp/rfc35_test.tab', update = 1) - # lyr = ds.GetLayer(0) - Check(lyr, ["foo5", "bar10", "baz15", "baw20"]) - - lyr.ReorderField(0, 2) - # ds = None - # ds = ogr.Open('tmp/rfc35_test.tab', update = 1) - # lyr = ds.GetLayer(0) - Check(lyr, ["bar10", "baz15", "foo5", "baw20"]) - - lyr.ReorderField(2, 0) - # ds = None - # ds = ogr.Open('tmp/rfc35_test.tab', update = 1) - # lyr = ds.GetLayer(0) - Check(lyr, ["foo5", "bar10", "baz15", "baw20"]) - - lyr.ReorderField(0, 1) - # ds = None - # ds = ogr.Open('tmp/rfc35_test.tab', update = 1) - # lyr = ds.GetLayer(0) - Check(lyr, ["bar10", "foo5", "baz15", "baw20"]) - - lyr.ReorderField(1, 0) - # ds = None - # ds = ogr.Open('tmp/rfc35_test.tab', update = 1) - # lyr = ds.GetLayer(0) - Check(lyr, ["foo5", "bar10", "baz15", "baw20"]) - - lyr.ReorderFields([3, 2, 1, 0]) - # ds = None - # ds = ogr.Open('tmp/rfc35_test.tab', update = 1) - # lyr = ds.GetLayer(0) - Check(lyr, ["baw20", "baz15", "bar10", "foo5"]) - - lyr.ReorderFields([3, 2, 1, 0]) - # ds = None - # ds = ogr.Open('tmp/rfc35_test.tab', update = 1) - # lyr = ds.GetLayer(0) - Check(lyr, ["foo5", "bar10", "baz15", "baw20"]) - - with gdaltest.error_handler(): - ret = lyr.ReorderFields([0, 0, 0, 0]) - assert ret != 0 - - # ds = None - - # ds = ogr.Open('tmp/rfc35_test.tab', update = 1) - lyr = ds.GetLayer(0) - - CheckColumnOrder(lyr, ["foo5", "bar10", "baz15", "baw20"]) - - CheckFeatures(lyr) - - -############################################################################### -# Test AlterFieldDefn() for change of name and width - - -def test_ogr_rfc35_mitab_3(): - - ds = ogr.Open("tmp/rfc35_test.tab", update=1) - lyr = ds.GetLayer(0) - - fd = ogr.FieldDefn("baz25", ogr.OFTString) - fd.SetWidth(25) - - lyr_defn = lyr.GetLayerDefn() - - with gdaltest.error_handler(): - ret = lyr.AlterFieldDefn(-1, fd, ogr.ALTER_ALL_FLAG) - assert ret != 0 - - with gdaltest.error_handler(): - ret = lyr.AlterFieldDefn(lyr_defn.GetFieldCount(), fd, ogr.ALTER_ALL_FLAG) - assert ret != 0 - - lyr.AlterFieldDefn(lyr_defn.GetFieldIndex("baz15"), fd, ogr.ALTER_ALL_FLAG) - - CheckFeatures(lyr, field3="baz25") - - fd = ogr.FieldDefn("baz5", ogr.OFTString) - fd.SetWidth(5) - - lyr_defn = lyr.GetLayerDefn() - lyr.AlterFieldDefn(lyr_defn.GetFieldIndex("baz25"), fd, ogr.ALTER_ALL_FLAG) - - CheckFeatures(lyr, field3="baz5") - - ds = None - - ds = ogr.Open("tmp/rfc35_test.tab", update=1) - lyr = ds.GetLayer(0) - lyr_defn = lyr.GetLayerDefn() - fld_defn = lyr_defn.GetFieldDefn(lyr_defn.GetFieldIndex("baz5")) - assert fld_defn.GetWidth() == 5 - - CheckFeatures(lyr, field3="baz5") - - -############################################################################### -# Test AlterFieldDefn() for change of type - - -def test_ogr_rfc35_mitab_4(): - - ds = ogr.Open("tmp/rfc35_test.tab", update=1) - lyr = ds.GetLayer(0) - lyr_defn = lyr.GetLayerDefn() - - assert lyr.TestCapability(ogr.OLCAlterFieldDefn) == 1 - - fd = ogr.FieldDefn("intfield", ogr.OFTInteger) - lyr.CreateField(fd) - - lyr.ReorderField(lyr_defn.GetFieldIndex("intfield"), 0) - - lyr.ResetReading() - feat = lyr.GetNextFeature() - feat.SetField("intfield", 12345) - lyr.SetFeature(feat) - feat = None - - fd.SetWidth(10) - with gdaltest.error_handler(): - ret = lyr.AlterFieldDefn( - lyr_defn.GetFieldIndex("intfield"), fd, ogr.ALTER_ALL_FLAG - ) - assert ret != 0 - - lyr.ResetReading() - feat = lyr.GetNextFeature() - assert feat.GetField("intfield") == 12345 - feat = None - - CheckFeatures(lyr, field3="baz5") - - if False: # pylint: disable=using-constant-test - fd.SetWidth(5) - lyr.AlterFieldDefn(lyr_defn.GetFieldIndex("intfield"), fd, ogr.ALTER_ALL_FLAG) - - lyr.ResetReading() - feat = lyr.GetNextFeature() - assert feat.GetField("intfield") == 12345 - feat = None - - CheckFeatures(lyr, field3="baz5") - - ds = None - - if False: # pylint: disable=using-constant-test - ds = ogr.Open("tmp/rfc35_test.tab", update=1) - lyr = ds.GetLayer(0) - lyr_defn = lyr.GetLayerDefn() - - fd.SetWidth(4) - lyr.AlterFieldDefn(lyr_defn.GetFieldIndex("intfield"), fd, ogr.ALTER_ALL_FLAG) - - lyr.ResetReading() - feat = lyr.GetNextFeature() - assert feat.GetField("intfield") == 1234 - feat = None - - CheckFeatures(lyr, field3="baz5") - - ds = None - - # Check that the file size has decreased after column shrinking - CheckFileSize("tmp/rfc35_test.tab") - - ds = ogr.Open("tmp/rfc35_test.tab", update=1) - lyr = ds.GetLayer(0) - lyr_defn = lyr.GetLayerDefn() - - fd = ogr.FieldDefn("oldintfld", ogr.OFTString) - fd.SetWidth(15) - lyr.AlterFieldDefn(lyr_defn.GetFieldIndex("intfield"), fd, ogr.ALTER_ALL_FLAG) - - lyr.ResetReading() - feat = lyr.GetNextFeature() - assert feat.GetField("oldintfld") == "12345" - feat = None - - CheckFeatures(lyr, field3="baz5") - - ds = None - - ds = ogr.Open("tmp/rfc35_test.tab", update=1) - lyr = ds.GetLayer(0) - lyr_defn = lyr.GetLayerDefn() - - lyr.ResetReading() - feat = lyr.GetNextFeature() - assert feat.GetField("oldintfld") == "12345" - feat = None - - CheckFeatures(lyr, field3="baz5") - - lyr.DeleteField(lyr_defn.GetFieldIndex("oldintfld")) - - fd = ogr.FieldDefn("intfield", ogr.OFTInteger) - fd.SetWidth(10) - assert lyr.CreateField(fd) == 0 - - assert lyr.ReorderField(lyr_defn.GetFieldIndex("intfield"), 0) == 0 - - lyr.ResetReading() - feat = lyr.GetNextFeature() - feat.SetField("intfield", 98765) - assert lyr.SetFeature(feat) == 0 - feat = None - - fd = ogr.FieldDefn("oldintfld", ogr.OFTString) - fd.SetWidth(6) - lyr.AlterFieldDefn(lyr_defn.GetFieldIndex("intfield"), fd, ogr.ALTER_ALL_FLAG) - - lyr.ResetReading() - feat = lyr.GetNextFeature() - assert feat.GetField("oldintfld") == "98765" - feat = None - - CheckFeatures(lyr, field3="baz5") - - ds = None - - ds = ogr.Open("tmp/rfc35_test.tab", update=1) - lyr = ds.GetLayer(0) - lyr_defn = lyr.GetLayerDefn() - - lyr.ResetReading() - feat = lyr.GetNextFeature() - assert feat.GetField("oldintfld") == "98765" - feat = None - - CheckFeatures(lyr, field3="baz5") - - -############################################################################### -# Test DeleteField() - - -def test_ogr_rfc35_mitab_5(): - - ds = ogr.Open("tmp/rfc35_test.tab", update=1) - lyr = ds.GetLayer(0) - lyr_defn = lyr.GetLayerDefn() - - assert lyr.TestCapability(ogr.OLCDeleteField) == 1 - - assert lyr.DeleteField(0) == 0 - - with gdaltest.error_handler(): - ret = lyr.DeleteField(-1) - assert ret != 0 - - with gdaltest.error_handler(): - ret = lyr.DeleteField(lyr.GetLayerDefn().GetFieldCount()) - assert ret != 0 - - CheckFeatures(lyr, field3="baz5") - - assert lyr.DeleteField(lyr_defn.GetFieldIndex("baw20")) == 0 - - ds = None - - # Check that the file size has decreased after column removing - CheckFileSize("tmp/rfc35_test.tab") - - ds = ogr.Open("tmp/rfc35_test.tab", update=1) - lyr = ds.GetLayer(0) - lyr_defn = lyr.GetLayerDefn() - - CheckFeatures(lyr, field3="baz5", field4=None) - - assert lyr.DeleteField(lyr_defn.GetFieldIndex("baz5")) == 0 - - CheckFeatures(lyr, field3=None, field4=None) - - assert lyr.DeleteField(lyr_defn.GetFieldIndex("foo5")) == 0 - - # We cannot delete the only one remaining field (well MapInfo prohibits that) - with gdaltest.error_handler(): - ret = lyr.DeleteField(lyr_defn.GetFieldIndex("bar10")) - assert ret != 0 - - CheckFeatures(lyr, field1=None, field2=None, field3=None, field4=None) - - ds = None - - ds = ogr.Open("tmp/rfc35_test.tab", update=1) - lyr = ds.GetLayer(0) - lyr_defn = lyr.GetLayerDefn() - - CheckFeatures(lyr, field1=None, field2=None, field3=None, field4=None) - - -############################################################################### -# Initiate the test file - - -def test_ogr_rfc35_mitab_cleanup(): - - ogr.GetDriverByName("MapInfo File").DeleteDataSource("tmp/rfc35_test.tab") diff --git a/autotest/ogr/ogr_rfc35_sqlite.py b/autotest/ogr/ogr_rfc35_sqlite.py deleted file mode 100755 index 35f5abcac10d..000000000000 --- a/autotest/ogr/ogr_rfc35_sqlite.py +++ /dev/null @@ -1,426 +0,0 @@ -#!/usr/bin/env pytest -############################################################################### -# $Id$ -# -# Project: GDAL/OGR Test Suite -# Purpose: Test RFC35 for SQLite driver -# Author: Even Rouault -# -############################################################################### -# Copyright (c) 2011, Even Rouault -# -# Permission is hereby granted, free of charge, to any person obtaining a -# copy of this software and associated documentation files (the "Software"), -# to deal in the Software without restriction, including without limitation -# the rights to use, copy, modify, merge, publish, distribute, sublicense, -# and/or sell copies of the Software, and to permit persons to whom the -# Software is furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included -# in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -# DEALINGS IN THE SOFTWARE. -############################################################################### - - -import gdaltest -import pytest - -from osgeo import gdal, ogr - - -############################################################################### -@pytest.fixture(autouse=True, scope="module") -def module_disable_exceptions(): - with gdaltest.disable_exceptions(): - yield - - -############################################################################### -@pytest.fixture(autouse=True, scope="module") -def setup_and_cleanup(): - # This is to speed-up the runtime of tests on EXT4 filesystems - # Do not use this for production environment if you care about data safety - # w.r.t system/OS crashes, unless you know what you are doing. - with gdal.config_option("OGR_SQLITE_SYNCHRONOUS", "OFF"): - yield - - -############################################################################### -# Initiate the test file - - -def test_ogr_rfc35_sqlite_1(): - - gdaltest.rfc35_sqlite_ds = None - gdaltest.rfc35_sqlite_ds_name = None - sqlite_dr = ogr.GetDriverByName("SQLite") - if sqlite_dr is None: - pytest.skip() - - gdal.Unlink("tmp/rfc35_test.sqlite") - - gdaltest.rfc35_sqlite_ds_name = "/vsimem/rfc35_test.sqlite" - with gdaltest.error_handler(): - gdaltest.rfc35_sqlite_ds = ogr.GetDriverByName("SQLite").CreateDataSource( - gdaltest.rfc35_sqlite_ds_name - ) - if gdaltest.rfc35_sqlite_ds is None: - gdaltest.rfc35_sqlite_ds_name = "tmp/rfc35_test.sqlite" - gdaltest.rfc35_sqlite_ds = ogr.GetDriverByName("SQLite").CreateDataSource( - gdaltest.rfc35_sqlite_ds_name - ) - lyr = gdaltest.rfc35_sqlite_ds.CreateLayer("rfc35_test") - - lyr.ReorderFields([]) - - fd = ogr.FieldDefn("foo5", ogr.OFTString) - fd.SetWidth(5) - lyr.CreateField(fd) - - feat = ogr.Feature(lyr.GetLayerDefn()) - feat.SetField(0, "foo0") - lyr.CreateFeature(feat) - feat = None - - fd = ogr.FieldDefn("bar10", ogr.OFTString) - fd.SetWidth(10) - lyr.CreateField(fd) - - feat = ogr.Feature(lyr.GetLayerDefn()) - feat.SetField(0, "foo1") - feat.SetField(1, "bar1") - lyr.CreateFeature(feat) - feat = None - - fd = ogr.FieldDefn("baz15", ogr.OFTString) - fd.SetWidth(15) - lyr.CreateField(fd) - - feat = ogr.Feature(lyr.GetLayerDefn()) - feat.SetField(0, "foo2") - feat.SetField(1, "bar2_01234") - feat.SetField(2, "baz2_0123456789") - lyr.CreateFeature(feat) - feat = None - - fd = ogr.FieldDefn("baw20", ogr.OFTString) - fd.SetWidth(20) - lyr.CreateField(fd) - - -############################################################################### -# Test ReorderField() - - -def Truncate(val, lyr_defn, fieldname): - # pylint: disable=unused-argument - # Mem driver doesn't actually truncate - return val - - -def CheckFeatures(lyr, field1="foo5", field2="bar10", field3="baz15", field4="baw20"): - - expected_values = [ - ["foo0", None, None, None], - ["foo1", "bar1", None, None], - ["foo2", "bar2_01234", "baz2_0123456789", None], - ["foo3", "bar3_01234", "baz3_0123456789", "baw3_012345678901234"], - ] - - lyr_defn = lyr.GetLayerDefn() - - lyr.ResetReading() - feat = lyr.GetNextFeature() - i = 0 - while feat is not None: - if ( - ( - field1 is not None - and feat.GetField(field1) - != Truncate(expected_values[i][0], lyr_defn, field1) - ) - or ( - field2 is not None - and feat.GetField(field2) - != Truncate(expected_values[i][1], lyr_defn, field2) - ) - or ( - field3 is not None - and feat.GetField(field3) - != Truncate(expected_values[i][2], lyr_defn, field3) - ) - or ( - field4 is not None - and feat.GetField(field4) - != Truncate(expected_values[i][3], lyr_defn, field4) - ) - ): - feat.DumpReadable() - pytest.fail() - feat = lyr.GetNextFeature() - i = i + 1 - - -def CheckColumnOrder(lyr, expected_order): - - lyr_defn = lyr.GetLayerDefn() - for i, exp_order in enumerate(expected_order): - assert lyr_defn.GetFieldDefn(i).GetName() == exp_order - - -def Check(lyr, expected_order): - CheckColumnOrder(lyr, expected_order) - CheckFeatures(lyr) - - -def test_ogr_rfc35_sqlite_2(): - - if gdaltest.rfc35_sqlite_ds is None: - pytest.skip() - - lyr = gdaltest.rfc35_sqlite_ds.GetLayer(0) - - assert lyr.TestCapability(ogr.OLCReorderFields) == 1 - - feat = ogr.Feature(lyr.GetLayerDefn()) - feat.SetField(0, "foo3") - feat.SetField(1, "bar3_01234") - feat.SetField(2, "baz3_0123456789") - feat.SetField(3, "baw3_012345678901234") - lyr.CreateFeature(feat) - feat = None - - assert lyr.ReorderField(1, 3) == 0 - - Check(lyr, ["foo5", "baz15", "baw20", "bar10"]) - - lyr.ReorderField(3, 1) - Check(lyr, ["foo5", "bar10", "baz15", "baw20"]) - - lyr.ReorderField(0, 2) - Check(lyr, ["bar10", "baz15", "foo5", "baw20"]) - - lyr.ReorderField(2, 0) - Check(lyr, ["foo5", "bar10", "baz15", "baw20"]) - - lyr.ReorderField(0, 1) - Check(lyr, ["bar10", "foo5", "baz15", "baw20"]) - - lyr.ReorderField(1, 0) - Check(lyr, ["foo5", "bar10", "baz15", "baw20"]) - - lyr.ReorderFields([3, 2, 1, 0]) - Check(lyr, ["baw20", "baz15", "bar10", "foo5"]) - - lyr.ReorderFields([3, 2, 1, 0]) - Check(lyr, ["foo5", "bar10", "baz15", "baw20"]) - - with gdaltest.error_handler(): - ret = lyr.ReorderFields([0, 0, 0, 0]) - assert ret != 0 - - -############################################################################### -# Test AlterFieldDefn() for change of name and width - - -def test_ogr_rfc35_sqlite_3(): - - if gdaltest.rfc35_sqlite_ds is None: - pytest.skip() - - lyr = gdaltest.rfc35_sqlite_ds.GetLayer(0) - - fd = ogr.FieldDefn("baz25", ogr.OFTString) - fd.SetWidth(25) - - lyr_defn = lyr.GetLayerDefn() - - with gdaltest.error_handler(): - ret = lyr.AlterFieldDefn(-1, fd, ogr.ALTER_ALL_FLAG) - assert ret != 0 - - with gdaltest.error_handler(): - ret = lyr.AlterFieldDefn(lyr_defn.GetFieldCount(), fd, ogr.ALTER_ALL_FLAG) - assert ret != 0 - - lyr.AlterFieldDefn(lyr_defn.GetFieldIndex("baz15"), fd, ogr.ALTER_ALL_FLAG) - - CheckFeatures(lyr, field3="baz25") - - fd = ogr.FieldDefn("baz5_tmp", ogr.OFTString) - fd.SetWidth(5) - - lyr_defn = lyr.GetLayerDefn() - lyr.AlterFieldDefn(lyr_defn.GetFieldIndex("baz25"), fd, ogr.ALTER_ALL_FLAG) - - CheckFeatures(lyr, field3="baz5_tmp") - - lyr_defn = lyr.GetLayerDefn() - fld_defn = lyr_defn.GetFieldDefn(lyr_defn.GetFieldIndex("baz5_tmp")) - assert fld_defn.GetWidth() == 5 - - CheckFeatures(lyr, field3="baz5_tmp") - - # Change only name - fd = ogr.FieldDefn("baz5", ogr.OFTString) - fd.SetWidth(5) - lyr.AlterFieldDefn(lyr_defn.GetFieldIndex("baz5_tmp"), fd, ogr.ALTER_ALL_FLAG) - - fld_defn = lyr_defn.GetFieldDefn(lyr_defn.GetFieldIndex("baz5")) - assert fld_defn.GetWidth() == 5 - - -############################################################################### -# Test AlterFieldDefn() for change of type - - -def test_ogr_rfc35_sqlite_4(): - - if gdaltest.rfc35_sqlite_ds is None: - pytest.skip() - - lyr = gdaltest.rfc35_sqlite_ds.GetLayer(0) - lyr_defn = lyr.GetLayerDefn() - - assert lyr.TestCapability(ogr.OLCAlterFieldDefn) == 1 - - fd = ogr.FieldDefn("intfield", ogr.OFTInteger) - lyr.CreateField(fd) - - lyr.ReorderField(lyr_defn.GetFieldIndex("intfield"), 0) - - lyr.ResetReading() - feat = lyr.GetNextFeature() - feat.SetField("intfield", 12345) - lyr.SetFeature(feat) - feat = None - - fd.SetWidth(10) - lyr.AlterFieldDefn(lyr_defn.GetFieldIndex("intfield"), fd, ogr.ALTER_ALL_FLAG) - - lyr.ResetReading() - feat = lyr.GetNextFeature() - assert feat.GetField("intfield") == 12345 - feat = None - - CheckFeatures(lyr, field3="baz5") - - fd.SetWidth(5) - lyr.AlterFieldDefn(lyr_defn.GetFieldIndex("intfield"), fd, ogr.ALTER_ALL_FLAG) - - lyr.ResetReading() - feat = lyr.GetNextFeature() - assert feat.GetField("intfield") == 12345 - feat = None - - CheckFeatures(lyr, field3="baz5") - - fd.SetWidth(4) - lyr.AlterFieldDefn(lyr_defn.GetFieldIndex("intfield"), fd, ogr.ALTER_ALL_FLAG) - - lyr.ResetReading() - feat = lyr.GetNextFeature() - # if feat.GetField("intfield") != 1234: - assert feat.GetField("intfield") == 12345 - feat = None - - CheckFeatures(lyr, field3="baz5") - - fd = ogr.FieldDefn("oldintfld", ogr.OFTString) - fd.SetWidth(15) - lyr.AlterFieldDefn(lyr_defn.GetFieldIndex("intfield"), fd, ogr.ALTER_ALL_FLAG) - - lyr.ResetReading() - feat = lyr.GetNextFeature() - # if feat.GetField("oldintfld") != '1234': - assert feat.GetField("oldintfld") == "12345" - feat = None - - CheckFeatures(lyr, field3="baz5") - - lyr.DeleteField(lyr_defn.GetFieldIndex("oldintfld")) - - fd = ogr.FieldDefn("intfield", ogr.OFTInteger) - fd.SetWidth(10) - assert lyr.CreateField(fd) == 0 - - assert lyr.ReorderField(lyr_defn.GetFieldIndex("intfield"), 0) == 0 - - lyr.ResetReading() - feat = lyr.GetNextFeature() - feat.SetField("intfield", 98765) - assert lyr.SetFeature(feat) == 0 - feat = None - - fd = ogr.FieldDefn("oldintfld", ogr.OFTString) - fd.SetWidth(6) - lyr.AlterFieldDefn(lyr_defn.GetFieldIndex("intfield"), fd, ogr.ALTER_ALL_FLAG) - - lyr.ResetReading() - feat = lyr.GetNextFeature() - assert feat.GetField("oldintfld") == "98765" - feat = None - - CheckFeatures(lyr, field3="baz5") - - -############################################################################### -# Test DeleteField() - - -def test_ogr_rfc35_sqlite_5(): - - if gdaltest.rfc35_sqlite_ds is None: - pytest.skip() - - lyr = gdaltest.rfc35_sqlite_ds.GetLayer(0) - lyr_defn = lyr.GetLayerDefn() - - assert lyr.TestCapability(ogr.OLCDeleteField) == 1 - - with gdaltest.error_handler(): - ret = lyr.DeleteField(-1) - assert ret != 0 - - with gdaltest.error_handler(): - ret = lyr.DeleteField(lyr.GetLayerDefn().GetFieldCount()) - assert ret != 0 - - assert lyr.DeleteField(0) == 0 - - CheckFeatures(lyr, field3="baz5") - - assert lyr.DeleteField(lyr_defn.GetFieldIndex("baw20")) == 0 - - CheckFeatures(lyr, field3="baz5", field4=None) - - assert lyr.DeleteField(lyr_defn.GetFieldIndex("baz5")) == 0 - - CheckFeatures(lyr, field3=None, field4=None) - - assert lyr.DeleteField(lyr_defn.GetFieldIndex("foo5")) == 0 - - assert lyr.DeleteField(lyr_defn.GetFieldIndex("bar10")) == 0 - - CheckFeatures(lyr, field1=None, field2=None, field3=None, field4=None) - - -############################################################################### -# Initiate the test file - - -def test_ogr_rfc35_sqlite_cleanup(): - - if gdaltest.rfc35_sqlite_ds_name is None: - pytest.skip() - - gdaltest.rfc35_sqlite_ds = None - ogr.GetDriverByName("SQLite").DeleteDataSource(gdaltest.rfc35_sqlite_ds_name)