Skip to content

Commit

Permalink
update and extend tests for Py3 and Py2
Browse files Browse the repository at this point in the history
  • Loading branch information
gdherbert committed Jun 26, 2021
1 parent c1e4b6b commit 38b3a5d
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 41 deletions.
38 changes: 23 additions & 15 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,42 @@

testfolder = 'tests'

@pytest.fixture(scope='module')
def testdata(pytestconfig):
gdb = "arc_utils_test.gdb"
fc_names = ['test_fc', 'test_fc2']
print('folder {}, gdb {}, fc {}'.format(testfolder, gdb, fc_names))
result = collections.namedtuple('testdata', 'gdb, fc1, fc2')
result.gdb = str(pytestconfig.rootdir.join(testfolder).join(gdb))
result.fc1 = str(pytestconfig.rootdir.join(testfolder).join(gdb).join(fc_names[0]))
result.fc2 = str(pytestconfig.rootdir.join(testfolder).join(gdb).join(fc_names[1]))
print(result)
return result
#@pytest.fixture(scope='module')
#def testdata(pytestconfig):
# gdb = "arc_utils_test.gdb"
# fc_names = ['test_fc', 'test_fc2']
# print('folder {}, gdb {}, fc {}'.format(testfolder, gdb, fc_names))
# result = collections.namedtuple('testdata', 'gdb, test_fc, test_fc2')
# result.gdb = str(pytestconfig.rootdir.join(testfolder).join(gdb))
# result.fc1 = str(pytestconfig.rootdir.join(testfolder).join(gdb).join(fc_names[0]))
# result.fc2 = str(pytestconfig.rootdir.join(testfolder).join(gdb).join(fc_names[1]))
# print(result)
# return result

@pytest.fixture(scope='module')
def testgdb(pytestconfig):
gdb_name = "arc_utils_test_tmp.gdb"
return str(pytestconfig.rootdir.join(testfolder).join(gdb_name))

@pytest.fixture(scope='module')
def testmxd(pytestconfig):
mxd_name = "arc_utils_test.mxd"
return str(pytestconfig.rootdir.join(testfolder).join(mxd_name))

@pytest.fixture(scope='module')
def testaprx(pytestconfig):
aprx_name = "arc_utils_test_pro.aprx"
return str(pytestconfig.rootdir.join(testfolder).join(aprx_name))

@pytest.fixture(scope='module')
def testdata2(request):
def testdatabase(request, testgdb):
# create a testing gdb
print("Creating test geodatabase")
testgdbpath = os.path.join(arcpy.env.scratchFolder, "arc_utils_test.gdb")
testgdbpath = testgdb # os.path.join(arcpy.env.scratchFolder, "arc_utils_test.gdb")
if arcpy.Exists(testgdbpath):
print("Deleting " + testgdbpath)
arcpy.Delete_management(testgdbpath)
arcpy.CreateFileGDB_management(arcpy.env.scratchFolder, "arc_utils_test.gdb")
arcpy.CreateFileGDB_management(os.path.dirname(testgdbpath), os.path.basename(testgdbpath))
# add a domain for each field in testfc
ftext_dom_name = "ftext_coded"
fint_dom_name = "fint_range"
Expand Down Expand Up @@ -72,7 +80,7 @@ def testdata2(request):
lat = 47.6 + float(random.randint(-9, 9)) / 100
cursor.insertRow([key, val, (lon, lat)])

result = collections.namedtuple('testdata', 'gdb, fc1, fc2')
result = collections.namedtuple('testdata', 'gdb, test_fc1, test_fc2')
result.gdb = testgdbpath
result.fc1 = fc_paths[0]
result.fc2 = fc_paths[1]
Expand Down
23 changes: 23 additions & 0 deletions tests/test_aprx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import sys
if sys.version_info.major == 3:
from arc_utils import aprx

def test_aprxobj_properties(testaprx):
aprxobj = aprx.AprxObj(testaprx)
assert aprxobj.path == testaprx
assert aprxobj.maps[0].name == 'Map'
assert aprxobj.layouts[0].name == 'MyLayout'


def test_aprxobj_layer_names(testaprx):
aprxobj = aprx.AprxObj(testaprx)
assert aprxobj.get_layer_names_as_array(aprxobj.maps[0]) == ['test_fc', 'Topographic']


def test_aprxobj_layer_obj(testaprx):
aprxobj = aprx.AprxObj(testaprx)
map_layer_obj = aprxobj.get_layer_obj_as_array(aprxobj.maps[0])
assert isinstance(map_layer_obj, list)
assert map_layer_obj[0].name == 'test_fc'


13 changes: 12 additions & 1 deletion tests/test_gdb.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
from arc_utils import gdb

testgdb = r"C:\Temp\scriptTesting\domain_test.gdb"
def test_gdb_properties(testdatabase):
gdbpath = testdatabase.gdb
gdbobj = gdb.GDBObj(gdbpath)
assert gdbobj.path == gdbpath
assert isinstance(gdbobj.feature_classes, list)
assert isinstance(gdbobj.domain_names, list)

def test_gdb_content(testdatabase):
gdbpath = testdatabase.gdb
gdbobj = gdb.GDBObj(gdbpath)
assert sorted(gdbobj.get_feature_class_names()) == ['test_fc', 'test_fc2']
assert sorted(gdbobj.get_all_domain_names()) == ['fint_range', 'ftext_coded']
18 changes: 10 additions & 8 deletions tests/test_mxd.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from arc_utils import mxd
import sys
if sys.version_info.major == 2:
from arc_utils import mxd


def test_mxdobj_properties(testmxd):
mxdpath = testmxd
mxdobj = mxd.MxdObj(mxdpath)
assert mxdobj.path == mxdpath
#assert mxdobj.mxd is object
assert mxdobj.layer_names_array == [u'test_fc']
assert isinstance(mxdobj.layer_obj_array, list)
def test_mxdobj_properties(testmxd):
mxdpath = testmxd
mxdobj = mxd.MxdObj(mxdpath)
assert mxdobj.path == mxdpath
#assert mxdobj.mxd is object
assert mxdobj.layer_names_array == [u'test_fc']
assert isinstance(mxdobj.layer_obj_array, list)

52 changes: 35 additions & 17 deletions tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import collections
import os

def test_tableobj_properties(testdata2):
def test_tableobj_properties(testdatabase):
# test table object properties
testdata = testdata2
testdata = testdatabase
print(testdata.gdb)
print(testdata.fc1)
tbl = table.TableObj(testdata.fc1)
Expand All @@ -19,35 +19,53 @@ def test_tableobj_properties(testdata2):
assert tbl.field_dict['fint']['type']== u'SmallInteger'


def test_tableobj_methods(testdata2):
def test_tableobj_single_text_field_methods(testdatabase):
# test table object methods
testdata = testdata2
tbl = table.TableObj(testdata.fc1)
tbl = table.TableObj(testdatabase.fc1)
ftext_set = tbl.get_field_value_set('ftext')

assert isinstance(ftext_set, set)
assert sorted(ftext_set) == [u'NULL', 'val02', 'val1', 'val2']
assert tbl.get_max_field_value('ftext') == 'val2'
assert tbl.get_max_field_value('ftext', True) == 'val02'
assert tbl.get_max_field_value_length('ftext') == 4
assert tbl.get_max_field_value_length('ftext') == 5


def test_tableobj_single_text_field_methods(testdatabase):
# test table object methods
tbl = table.TableObj(testdatabase.fc1)
fint_set = tbl.get_field_value_set('fint')
assert isinstance(fint_set, set)
assert tbl.get_field_value_set('fint') == set([4, 5, 7, 10, u'NULL'])
assert tbl.get_max_field_value('fint') == 10
assert tbl.get_max_field_value_length('fint') == 2


def test_tableobj_multi_field_methods(testdatabase):
# test table object methods
tbl = table.TableObj(testdatabase.fc1)
multi_field = tbl.get_multiple_field_value_set(['ftext', 'fint'])
assert isinstance(multi_field, set)
assert sorted(multi_field) == [u'NULL:5', u'val1:0', u'val1:10', u'val1:4', u'val1:5', u'val2:5', u'val2:7']
assert tbl.fields == [u'OBJECTID', u'Shape', u'ftext', u'fint']
assert tbl.fields2 == [u'ftext', u'fint']
result = tbl.compare_field_values_to_domain('ftext', testdata.gdb, "ftext_coded")
assert result.match == ['val2', 'val1']
assert result.unmatched == ['NULL']
result = tbl.compare_field_values_to_domain('fint', testdata.gdb, "fint_range")
assert sorted(multi_field) == [u'NULL:5', u'val02:7', u'val1:0', u'val1:10', u'val1:4', u'val1:5', u'val2:5', u'val2:7']



def test_tableobj_compare_methods(testdatabase):
# test table object compare methods
tbl = table.TableObj(testdatabase.fc1)
result = tbl.compare_field_values_to_domain('fint', testdatabase.gdb, "fint_range")
result.match.sort()
assert result.match == [4, 5, 7, 10]
assert result.unmatched == [u'NULL']
assert result.unmatched == []

result = tbl.compare_field_values_to_domain('ftext', testdatabase.gdb, "ftext_coded")
result.match.sort()
result.unmatched.sort()
assert result.match == ['val1', 'val2']
assert result.unmatched == ['NULL', 'val02']


def test_table_fc_methods(testdata):
def test_table_fc_methods(testdatabase):
# test non-object table methods
#table.compare_schema(testdata.fc, testdata.fc2)
pass
assert True

0 comments on commit 38b3a5d

Please sign in to comment.