Example #1
0
    def test_dictToTable_method(self):

        for dataset, fields in [(DATASETS[i], FIELDS[i]) for i in [0, 2]]:
            try:
                input = os.path.join(TEST_GDB, dataset)
                output = input + '_output'

                if arctools.arcpy.Exists(output):
                    arctools.arcpy.Delete_management(output)

                data = arctools.tableToDict(input, fields=fields)
                self.assertTrue(data)

                # INSERT METHOD:
                method = 'insert'
                arctools.dictToTable(data, output)  # default to method = 'insert'

                # Test fail when writing to existing table or feature class:
                try:
                    arctools.dictToTable(data, output, method='insert', makeTable=True)  # makeTable should be false when writing to an existing table. This should therefore fail.
                    self.fail('arctools.dictToTable overwrote output when is should have failed.')
                except:
                    self.assertTrue(True)  # Test is a success if the above line fails.

                # Assert feature type and spatial reference:
                in_desc = arctools.arcpy.Describe(input)
                out_desc = arctools.arcpy.Describe(output)

                self.assertTrue(in_desc.dataType == out_desc.dataType)
                if hasattr(in_desc, 'shapeType') and hasattr(out_desc, 'shapeType'):
                    self.assertTrue(in_desc.shapeType == out_desc.shapeType)
                    self.assertTrue(in_desc.spatialReference.name == out_desc.spatialReference.name)
                else:
                    self.assertFalse(hasattr(in_desc, 'shapeType') and hasattr(out_desc, 'shapeType'))

                # Test insert when writing data to table a second time (append):
                arctools.dictToTable(data,output,method = 'insert', makeTable = False) #Duplicate contents of output table.
                test = arctools.tableToDict(output,fields = fields)
                self.assertTrue(test == data + data)

                # UPDATE METHOD:
                # All utems with ID=1 should have name = 'test_name'
                update_data = [{'id':1,'name':'test_name'}]
                arctools.dictToTable(update_data, output, method = 'update', dictionaryKey = 'id')

                response = arctools.tableToDict(output, groupBy = 'id')
                for id in response:
                    for item in response[id]:
                        if id == update_data[0]['id']:
                            self.assertTrue(item['name'] == update_data[0]['name'])
                        else:
                            self.assertFalse(item['name'] == update_data[0]['name'])

                # Test different kinds of input data structures.

            finally:
                if arctools.arcpy.Exists(output):
                    arctools.arcpy.Delete_management(output)
Example #2
0
    def test_tableToDict_method(self):

        for dataset in DATASETS:
            fullpath = os.path.join(TEST_GDB,dataset)

            data = arctools.tableToDict(fullpath)
            self.assertTrue(data)