def test_has_table(self): change_detection = ChangeDetection(['ChangeDetection'], test_fgdb, hash_table=hash_table) self.assertTrue( change_detection.has_table('UPDATE_TESTS.dbo.counties')) self.assertFalse(change_detection.has_table('bad table name'))
def test_invalid_data(test_gdb): hash_table = str(Path(test_gdb) / 'TableHashes') change_detection = ChangeDetection(['ChangeDetection'], test_gdb, hash_table=hash_table) table = 'update_tests.dbo.providers' crate = Crate(table, 'someWorkspace', arcpy.env.scratchGDB, Path(test_gdb).name) result = change_detection.update(crate) assert result[0] == Crate.INVALID_DATA
def test_has_changed(test_gdb): hash_table = str(Path(test_gdb) / 'TableHashes') change_detection = ChangeDetection(['ChangeDetection'], test_gdb, hash_table=hash_table) assert change_detection.has_changed('UPDATE_TESTS.dbo.counties') == False assert change_detection.has_changed('UPDATE_TESTS.dbo.providers') with raises(Exception): assert change_detection.has_table('bad table name')
def test_has_changed(self): change_detection = ChangeDetection(['ChangeDetection'], test_fgdb, hash_table=hash_table) self.assertFalse( change_detection.has_changed('UPDATE_TESTS.dbo.counties')) self.assertTrue( change_detection.has_changed('UPDATE_TESTS.dbo.providers')) with raises(Exception): assert change_detection.has_table('bad table name')
def test_update_new_dataset_with_change_detection(test_gdb): change_detection = ChangeDetection([], 'blah') change_detection.has_table = MagicMock(name='has_table', return_value=True) change_detection.has_changed = MagicMock(name='has_changed', return_value=False) crate = Crate('Counties', test_gdb, test_gdb, 'Counties_Destination') core.update(crate, lambda c: True, change_detection) source_count = arcpy.management.GetCount(str(Path(test_gdb) / 'Counties'))[0] destination_count = arcpy.management.GetCount( str(Path(test_gdb) / 'Counties_Destination'))[0] assert source_count == destination_count
def test_invalid_data(self): scratch_hash_table = path.join(arcpy.env.scratchGDB, path.basename(hash_table)) scratch_destination = path.join(arcpy.env.scratchGDB, 'Counties') temp_data = [scratch_hash_table, scratch_destination] for dataset in temp_data: if arcpy.Exists(dataset): arcpy.management.Delete(dataset) arcpy.management.Copy(hash_table, scratch_hash_table) change_detection = ChangeDetection(['ChangeDetection'], test_fgdb, hash_table=scratch_hash_table) table = 'update_tests.dbo.providers' crate = Crate(table, 'someWorkspace', arcpy.env.scratchGDB, path.basename(scratch_destination)) result = change_detection.update(crate) self.assertEqual(result[0], Crate.INVALID_DATA)
def test_can_handle_globalid_fields_without_index(test_gdb): hash_table = str(Path(test_gdb) / 'TableHashes') scratch_hash_table = str(Path(arcpy.env.scratchGDB) / Path(hash_table).name) scratch_destination = str(Path(arcpy.env.scratchGDB) / 'GlobalIds') temp_data = [scratch_hash_table, scratch_destination] for dataset in temp_data: if arcpy.Exists(dataset): arcpy.management.Delete(dataset) arcpy.management.Copy(hash_table, scratch_hash_table) test_sde = str(Path(test_data_folder) / 'UPDATE_TESTS.sde') change_detection = ChangeDetection(['ChangeDetection'], test_sde, hash_table=scratch_hash_table) table = 'GlobalIdsNoIndex' crate = Crate(table, test_sde, arcpy.env.scratchGDB, Path(scratch_destination).name) crate.result = (Crate.CREATED, None) core._create_destination_data(crate, skip_hash_field=True) change_detection.current_hashes[f'update_tests.dbo.{table.casefold()}'] = 'hash' result = change_detection.update(crate) assert result[0] == Crate.CREATED
def test_updates_data(test_gdb): hash_table = str(Path(test_gdb) / 'TableHashes') scratch_hash_table = str(Path(arcpy.env.scratchGDB) / Path(hash_table).name) scratch_destination = str(Path(arcpy.env.scratchGDB) / 'Counties') temp_data = [scratch_hash_table, scratch_destination] for dataset in temp_data: if arcpy.Exists(dataset): arcpy.management.Delete(dataset) arcpy.management.Copy(hash_table, scratch_hash_table) change_detection = ChangeDetection(['ChangeDetection'], test_gdb, hash_table=scratch_hash_table) table = 'counties' crate = Crate(table, test_gdb, arcpy.env.scratchGDB, Path(scratch_destination).name) crate.result = (Crate.CREATED, None) core._create_destination_data(crate, skip_hash_field=True) change_detection.current_hashes[table] = '8' result = change_detection.update(crate) where = f'{table_name_field} = \'{table}\'' with arcpy.da.SearchCursor(scratch_hash_table, [hash_field], where_clause=where) as cursor: assert next(cursor)[0] == '8' assert result[0] == Crate.CREATED change_detection.current_hashes[table] = '9' crate.result = (Crate.UNINITIALIZED, None) result = change_detection.update(crate) where = f'{table_name_field} = \'{table}\'' with arcpy.da.SearchCursor(scratch_hash_table, [hash_field], where_clause=where) as cursor: assert next(cursor)[0] == '9' assert result[0] == Crate.UPDATED
def test_preserves_globalids_table(test_gdb): hash_table = str(Path(test_gdb) / 'TableHashes') scratch_hash_table = str(Path(arcpy.env.scratchGDB) / Path(hash_table).name) scratch_destination = str(Path(arcpy.env.scratchGDB) / 'GlobalIds') temp_data = [scratch_hash_table, scratch_destination] for dataset in temp_data: if arcpy.Exists(dataset): arcpy.management.Delete(dataset) arcpy.management.Copy(hash_table, scratch_hash_table) test_sde = str(Path(test_data_folder) / 'UPDATE_TESTS.sde') change_detection = ChangeDetection(['ChangeDetection'], test_sde, hash_table=scratch_hash_table) table = 'GlobalIdsTable' crate = Crate(table, test_sde, str(Path(scratch_destination).parent), Path(scratch_destination).name) crate.result = (Crate.CREATED, None) core._create_destination_data(crate, skip_hash_field=True) change_detection.current_hashes[f'update_tests.dbo.{table.casefold()}'] = 'hash' result = change_detection.update(crate) assert result[0] == Crate.CREATED with arcpy.da.SearchCursor(scratch_destination, ['GlobalID', 'NAME'], 'NAME = \'JUAB\'') as cursor: assert next(cursor)[0] == '{D5868F73-B65A-4B11-B346-D00E7A5043F7}'
def test_updates_data(self): scratch_hash_table = path.join(arcpy.env.scratchGDB, path.basename(hash_table)) scratch_destination = path.join(arcpy.env.scratchGDB, 'Counties') temp_data = [scratch_hash_table, scratch_destination] for dataset in temp_data: if arcpy.Exists(dataset): arcpy.management.Delete(dataset) arcpy.management.Copy(hash_table, scratch_hash_table) change_detection = ChangeDetection(['ChangeDetection'], test_fgdb, hash_table=scratch_hash_table) table = 'counties' crate = Crate(table, test_fgdb, arcpy.env.scratchGDB, path.basename(scratch_destination)) crate.result = (Crate.CREATED, None) core._create_destination_data(crate, skip_hash_field=True) change_detection.current_hashes[table] = '8' result = change_detection.update(crate) where = f'{table_name_field} = \'{table}\'' with arcpy.da.SearchCursor(scratch_hash_table, [hash_field], where_clause=where) as cursor: self.assertEqual(next(cursor)[0], '8') self.assertEqual(result[0], Crate.CREATED) change_detection.current_hashes[table] = '9' crate.result = (Crate.UNINITIALIZED, None) result = change_detection.update(crate) where = f'{table_name_field} = \'{table}\'' with arcpy.da.SearchCursor(scratch_hash_table, [hash_field], where_clause=where) as cursor: self.assertEqual(next(cursor)[0], '9') self.assertEqual(result[0], Crate.UPDATED)
def test_has_table(test_gdb): hash_table = str(Path(test_gdb) / 'TableHashes') change_detection = ChangeDetection(['ChangeDetection'], test_gdb, hash_table=hash_table) assert change_detection.has_table('UPDATE_TESTS.dbo.counties') assert change_detection.has_table('bad table name') == False
try: arcpy.Delete_management(data) except: pass #: check for local sde HAS_LOCAL_SDE = arcpy.Exists(path.join(UPDATE_TESTS_SDE, 'ZipCodes')) def skip_if_no_local_sde(): if not HAS_LOCAL_SDE: raise pytest.skip('No test SDE detected, skipping test') CHANGE_DETECTION = ChangeDetection([], 'blah') @pytest.fixture(scope='function', autouse=True) def set_up_modules(): delete_if_arcpy_exists(TEMP_GDB) engine.init() core.init(engine.log) yield delete_if_arcpy_exists(TEMP_GDB) def test_update_no_existing_destination(test_gdb): crate = Crate('ZipCodes', test_gdb, TEMP_GDB, 'ImNotHere')