def test_check_schema_match(self): with self.assertRaises(ValidationException): core.check_schema(Crate('FieldLength', check_for_changes_gdb, check_for_changes_gdb, 'FieldLength2')) with self.assertRaises(ValidationException): core.check_schema(Crate('FieldType', check_for_changes_gdb, check_for_changes_gdb, 'FieldType2')) self.assertEqual(core.check_schema(Crate('ZipCodes', check_for_changes_gdb, check_for_changes_gdb2, 'ZipCodes')), True)
def test_schema_changes(self): arcpy.Copy_management(check_for_changes_gdb, test_gdb) with self.assertRaises(ValidationException): core.check_schema(Crate('ZipCodes', test_gdb, check_for_changes_gdb, 'FieldLength')) result = core.check_schema(Crate('ZipCodes', test_gdb, check_for_changes_gdb, 'ZipCodes')) self.assertEqual(result, True)
def test_schema_changes(test_gdb): with pytest.raises(ValidationException): core.check_schema(Crate('ZipCodes', test_gdb, test_gdb, 'FieldLength')) result = core.check_schema( Crate('ZipCodes', test_gdb, test_gdb, 'ZipCodes')) assert result == True
def test_crates_with_schema_changes_returns_false(self): updated = Crate('', '', '', '') updated.result = (Crate.UPDATED, None) schema_change = Crate('', '', '', '') schema_change.result = (Crate.INVALID_DATA, None) self.patient._crates = [schema_change, updated, updated] self.assertFalse(self.patient.requires_processing())
def test_crates_with_updates_and_changes_returns_true(self): updated = Crate('', '', '', '') updated.result = (Crate.UPDATED, None) no_changes = Crate('', '', '', '') no_changes.result = (Crate.NO_CHANGES, None) self.patient._crates = [updated, no_changes, no_changes] self.assertTrue(self.patient.requires_processing())
def test_crates_with_unhandled_exception_returns_false(self): updated = Crate('', '', '', '') updated.result = (Crate.UPDATED, None) unhandled_exception = Crate('', '', '', '') unhandled_exception.result = (Crate.UNHANDLED_EXCEPTION, None) self.patient._crates = [updated, updated, unhandled_exception] self.assertFalse(self.patient.requires_processing())
def test_check_schema_match(test_gdb): with pytest.raises(ValidationException): core.check_schema( Crate('FieldLength', test_gdb, test_gdb, 'FieldLength2')) with pytest.raises(ValidationException): core.check_schema(Crate('FieldType', test_gdb, test_gdb, 'FieldType2')) assert core.check_schema(Crate('ZipCodes', test_gdb, test_gdb, 'ZipCodes2')) == True
def test_is_ready_to_ship_crates_with_updates_and_no_changes_returns_true( self): updated = Crate('', '', '', '') updated.result = (Crate.UPDATED, None) no_changes = Crate('', '', '', '') no_changes.result = (Crate.NO_CHANGES, None) self.patient._crates = [no_changes, updated, no_changes] self.assertTrue(self.patient.is_ready_to_ship())
def test_process_crate_doesnt_call_update_def_on_duplicate_crates(self): crate1 = Crate('DNROilGasWells', test_gdb, test_gdb, 'a') crate2 = Crate('DNROilGasWells', test_gdb, test_gdb, 'a') pallet = Pallet() pallet._crates = [crate1, crate2] update_def = Mock(return_value=(Crate.UPDATED, 'message')) lift.process_crates_for([pallet], update_def) self.assertEqual(update_def.call_count, 1) self.assertEqual(crate1.result[0], Crate.UPDATED) self.assertEqual(crate2.result[0], Crate.UPDATED)
def test_hash_custom_source_key_text(self): skip_if_no_local_sde() tbl = 'NO_OBJECTID_TEST' #: has changes crate = Crate('UPDATE_TESTS.dbo.{}'.format(tbl), update_tests_sde, check_for_changes_gdb, tbl, source_primary_key='TEST') self.assertEqual(len(core._hash(crate).adds), 1) #: no changes crate = Crate('UPDATE_TESTS.dbo.{}'.format(tbl), update_tests_sde, check_for_changes_gdb, '{}_NO_CHANGES'.format(tbl), source_primary_key='TEST') self.assertEqual(len(core._hash(crate).adds), 0)
def test_is_ready_to_ship_crates_with_any_exception_returns_false(self): updated = Crate('', '', '', '') updated.result = (Crate.UPDATED, None) no_changes = Crate('', '', '', '') no_changes.result = (Crate.NO_CHANGES, None) unhandled_exception = Crate('', '', '', '') unhandled_exception.result = (Crate.UNHANDLED_EXCEPTION, None) self.patient._crates = [updated, no_changes, unhandled_exception] self.assertFalse(self.patient.is_ready_to_ship())
def test_process_crate_for_set_results(self): crate1 = Crate('DNROilGasWells', check_for_changes_gdb, check_for_changes_gdb, 'a') crate2 = Crate('DNROilGasWells', check_for_changes_gdb, check_for_changes_gdb, 'b') pallet = Pallet() pallet._crates = [crate1, crate2] update_def = Mock(return_value=(Crate.UPDATED, 'message')) lift.process_crates_for([pallet], update_def) self.assertEqual(update_def.call_count, 2) self.assertEqual(crate1.result[0], Crate.UPDATED) self.assertEqual(crate2.result[0], Crate.UPDATED)
def test_is_ready_to_ship_crates_with_any_schema_changed_returns_false( self): updated = Crate('', '', '', '') updated.result = (Crate.UPDATED, None) no_changes = Crate('', '', '', '') no_changes.result = (Crate.NO_CHANGES, None) schema_change = Crate('', '', '', '') schema_change.result = (Crate.INVALID_DATA, None) self.patient._crates = [updated, no_changes, schema_change] self.assertFalse(self.patient.is_ready_to_ship())
def test_hash_custom_source_key_text(test_gdb): skip_if_no_local_sde() tbl = 'NO_OBJECTID_TEST' #: has changes crate = Crate('UPDATE_TESTS.dbo.{}'.format(tbl), UPDATE_TESTS_SDE, test_gdb, tbl) assert len(core._hash(crate).adds) == 1 #: no changes crate = Crate('UPDATE_TESTS.dbo.{}'.format(tbl), UPDATE_TESTS_SDE, test_gdb, '{}_NO_CHANGES'.format(tbl)) assert len(core._hash(crate).adds) == 0
def test_try_to_find_data_source_by_name_returns_None_if_not_sde(self): crate = Crate(source_name='something.shp', source_workspace='c:\\temp', destination_workspace='c:\\something.gdb', destination_name='Counties') self.assertIsNone(crate._try_to_find_data_source_by_name()[0])
def test_check_schema_no_objectid_in_source(test_gdb): skip_if_no_local_sde() result = core.check_schema( Crate('UPDATE_TESTS.dbo.NO_OBJECTID_TEST', UPDATE_TESTS_SDE, test_gdb, r'NO_OBJECTID_TEST')) assert result == True
def test_is_ready_to_ship_crates_with_no_changes_returns_true(self): no_changes = Crate('', '', '', '') no_changes.result = (Crate.NO_CHANGES, None) self.patient._crates = [no_changes, no_changes, no_changes] self.assertTrue(self.patient.is_ready_to_ship())
def test_is_ready_to_ship_crates_with_updates_returns_true(self): updated = Crate('', '', '', '') updated.result = (Crate.UPDATED, None) self.patient._crates = [updated, updated, updated] self.assertTrue(self.patient.is_ready_to_ship())
def test_update_duplicate_features(self): arcpy.Copy_management(duplicates_gdb, duplicates_gdb_copy) crate = Crate('Duplicates', duplicates_gdb_copy, test_gdb, 'DuplicatesDest') core.update(crate, lambda x: True) self.assertEqual(arcpy.GetCount_management(crate.destination).getOutput(0), '4') #: remove feature with arcpy.da.UpdateCursor(crate.source, '*') as delete_cursor: for row in delete_cursor: delete_cursor.deleteRow() break core.update(crate, lambda x: True) self.assertEqual(arcpy.GetCount_management(crate.destination).getOutput(0), '3') #: change feature with arcpy.da.UpdateCursor(crate.source, ['TEST']) as update_cursor: for row in update_cursor: row[0] = 'change' update_cursor.updateRow(row) break self.assertEqual(core.update(crate, lambda x: True)[0], Crate.WARNING) self.assertEqual(arcpy.GetCount_management(crate.destination).getOutput(0), '3')
def test_create_destination_data_workspace(create_mock, test_gdb): #: file geodatabase crate = Crate('DNROilGasWells', test_gdb, TEMP_GDB) core._create_destination_data(crate) create_mock.assert_called_once()
def test_create_destination_data_raises(test_gdb, tmpdir): #: non-file geodatabase crate = Crate('DNROilGasWells', test_gdb, str(tmpdir), 'test.shp') with pytest.raises(Exception): core._create_destination_data(crate)
def test_update_duplicate_features(test_gdb): arcpy.management.Copy(test_gdb, TEMP_GDB) crate = Crate('Duplicates', TEMP_GDB, TEMP_GDB, 'DuplicatesDest') core.update(crate, lambda x: True, CHANGE_DETECTION) assert arcpy.GetCount_management(crate.destination).getOutput(0) == '4' #: remove feature with arcpy.da.UpdateCursor(crate.source, '*') as delete_cursor: for row in delete_cursor: delete_cursor.deleteRow() break core.update(crate, lambda x: True, CHANGE_DETECTION) assert arcpy.GetCount_management(crate.destination).getOutput(0) == '3' #: change feature with arcpy.da.UpdateCursor(crate.source, ['TEST']) as update_cursor: for row in update_cursor: row[0] = 'change' update_cursor.updateRow(row) break assert core.update( crate, lambda x: True, CHANGE_DETECTION)[0] == Crate.UPDATED_OR_CREATED_WITH_WARNINGS assert arcpy.GetCount_management(crate.destination).getOutput(0) == '3'
def test_init_with_coordinate_system_as_number_becomes_spatial_reference(self): crate = Crate('foo', 'bar', 'baz', 'qux', 26912) self.assertEqual(crate.source_name, 'foo') self.assertEqual(crate.source_workspace, 'bar') self.assertEqual(crate.destination_workspace, 'baz') self.assertEqual(crate.destination_name, 'qux') self.assertIsInstance(crate.destination_coordinate_system, SpatialReference)
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_set_source_name_updates_source_if_not_none(self): crate = Crate('foo', 'bar', 'baz', 'goo') crate.set_source_name(None) self.assertEqual(crate.source_name, 'foo') self.assertEqual(crate.source, path.join('bar', 'foo'))
def test_set_result_with_invalid_result_returns_result(self): crate = Crate('foo', 'bar', 'baz', 'goo') self.assertEqual( crate.set_result(('wat?', 'some crazy message'))[0], 'unknown result') self.assertEqual(crate.result[0], 'unknown result')
def test_init_with_coordinate_system_does_not_change(self): crate = Crate('foo', 'bar', 'baz', 'qux', SpatialReference(26921)) self.assertEqual(crate.source_name, 'foo') self.assertEqual(crate.source_workspace, 'bar') self.assertEqual(crate.destination_workspace, 'baz') self.assertEqual(crate.destination_name, 'qux') self.assertIsInstance(crate.destination_coordinate_system, SpatialReference)
def test_process_on_fail(self): updated = Crate('', '', '', '') updated.result = (Crate.UPDATED, None) no_changes = Crate('', '', '', '') no_changes.result = (Crate.NO_CHANGES, None) unhandled_exception = Crate('', '', '', '') unhandled_exception.result = (Crate.UNHANDLED_EXCEPTION, None) self.patient._crates = [updated, no_changes, unhandled_exception] self.patient.success = (False, None) self.patient.process_on_fail = True self.assertTrue(self.patient.requires_processing())
def test_requires_processing_crates_result_created_returns_true(self): updated = Crate('', '', '', '') updated.result = (Crate.CREATED, None) self.patient._crates = [updated] self.assertTrue(self.patient.requires_processing())
def test_bad_destination_name(self): crate = Crate('DNROilGasWells', check_for_changes_fgdb, 'destination_workspace', 'destination.Name') self.assertEqual(crate.result, ( Crate.INVALID_DATA, 'Validation error with destination_name: destination.Name != destination_Name' ))