class TestParseSourceArgs(unittest.TestCase): def setUp(self): self.patient = Seeder() self.all_sources = ['WQP', 'SDWIS', 'DOGM', 'DWR', 'UGS'] def test_gets_all_sources_when_given_none(self): self.assertEqual(self.patient._parse_source_args(None), self.all_sources) def test_gets_all_sources_when_given_empty_string(self): self.assertEqual(self.patient._parse_source_args(''), self.all_sources) def test_returns_none_when_given_bad_source(self): self.assertIsNone(self.patient._parse_source_args('not a source')) def test_gets_array_of_sources_when_given_csv(self): self.assertEqual(self.patient._parse_source_args('WQP, SDWIS'), ['WQP', 'SDWIS']) self.assertEqual(self.patient._parse_source_args(' WQP, SDWIS'), ['WQP', 'SDWIS']) self.assertEqual(self.patient._parse_source_args(' WQP , SDWIS '), ['WQP', 'SDWIS']) self.assertEqual(self.patient._parse_source_args('WQP, SDWIS, not a source'), ['WQP', 'SDWIS']) def test_gets_array_of_sources_when_given_one(self): self.assertEqual(self.patient._parse_source_args('WQP'), ['WQP']) self.assertEqual(self.patient._parse_source_args('WQP '), ['WQP']) self.assertEqual(self.patient._parse_source_args(' WQP'), ['WQP']) self.assertEqual(self.patient._parse_source_args(' WQP '), ['WQP'])
def setUp(self): self.parent_folder = os.path.join(os.getcwd(), 'dbseeder', 'tests') self.location = os.path.join(self.parent_folder, 'temp_tests') self.tearDown() if not os.path.exists(self.location): os.makedirs(self.location) self.patient = Seeder(self.location, self.gdb_name) self.patient.count = 41
def setUp(self): self.parent_folder = os.path.join(os.getcwd(), 'dbseeder', 'tests') self.location = os.path.join(self.parent_folder, 'temp_tests') self.gdb_name = 'sdwis.gdb' self.tearDown() if not os.path.exists(self.location): os.makedirs(self.location) self.folder = os.path.join(self.location, self.gdb_name) seed = Seeder(self.location, self.gdb_name) seed._create_gdb() seed._create_feature_classes(['Results', 'Stations']) self.patient = Sdwis(self.folder, InsertCursor)
def setUp(self): self.locations = { 'source': 'connections\WRI_Source on (local).sde', 'destination': 'connections\WRI on (local).sde' } self.patient = Seeder(self.locations, 'url template') model.Lookup.project_id = { 'Project_FK': 1, 'CompletedProject_FK': 2 }
def setUp(self): self.parent_folder = os.path.join(os.getcwd(), 'dbseeder', 'tests') self.location = os.path.join(self.parent_folder, 'temp_tests') self.gdb_name = 'dogm.gdb' self.analysisdate = datetime.datetime(2014, 11, 17, 0, 0) self.sampledate = datetime.datetime(2008, 11, 17, 0, 0) self.sampletime = datetime.datetime(1899, 12, 30, 11, 10) self.resultvalue = 10.0 self.tearDown() if not os.path.exists(self.location): os.makedirs(self.location) self.folder = os.path.join(self.location, self.gdb_name) seed = Seeder(self.location, self.gdb_name) seed._create_gdb() seed._create_feature_classes(['Results', 'Stations']) self.patient = Dogm(self.folder, SearchCursor, InsertCursor)
def setUp(self): self.patient = Seeder() self.all_sources = ['WQP', 'SDWIS', 'DOGM', 'DWR', 'UGS']
class TestDbSeeder(unittest.TestCase): #: thing being tested patient = None location = None parent_folder = None gdb_name = 'seed.gdb' def setUp(self): self.parent_folder = os.path.join(os.getcwd(), 'dbseeder', 'tests') self.location = os.path.join(self.parent_folder, 'temp_tests') self.tearDown() if not os.path.exists(self.location): os.makedirs(self.location) self.patient = Seeder(self.location, self.gdb_name) self.patient.count = 41 def test_sanity(self): self.assertIsNotNone(self.patient) def test_gdb_creation(self): self.patient._create_gdb() gdb = os.path.join(self.location, self.gdb_name) self.assertTrue(os.path.exists(gdb)) def test_fc_creation(self): self.patient._create_gdb() self.patient._create_feature_classes(['Stations', 'Results']) arcpy.env.workspace = self.patient.location self.assertEqual(len(arcpy.ListFeatureClasses()), 1) self.assertEqual(len(arcpy.ListTables()), 1) def _test_update(self): # self.patient.chemistry_query_url = self.chemistry_url # self.patient.update() pass def _test_seed(self): folder = os.path.join(os.getcwd(), 'dbseeder', 'tests', 'data') self.patient.seed(folder, ['Results', 'Stations']) arcpy.env.workspace = self.patient.location self.assertEqual( arcpy.GetCount_management('Stations').getOutput(0), '700') def tearDown(self): self.patient = None del self.patient limit = 5000 i = 0 while os.path.exists(self.location) and i < limit: try: rmtree(self.location) except: i += 1
class TestDbSeeder(unittest.TestCase): def setUp(self): self.locations = { 'source': 'connections\WRI_Source on (local).sde', 'destination': 'connections\WRI on (local).sde' } self.patient = Seeder(self.locations, 'url template') model.Lookup.project_id = { 'Project_FK': 1, 'CompletedProject_FK': 2 } def test_ensure_absolute_path(self): self.assertTrue(isfile(self.patient.locations['source'])) self.assertTrue(isfile(self.patient.locations['destination'])) def test_ensure_throws_if_not_found(self): self.assertRaises(Exception, Seeder, { 'source': 'not a file' }) def test_get_where_clause_int(self): cursor = Mock() cursor.execute = Mock(return_value=['\'2\'']) arcpy = Mock() arcpy.ArcSDESQLExecute = Mock(return_value=cursor) actual = self.patient._get_where_clause(arcpy, {}) expected = 'Project_FK in (\'2\')' self.assertEqual(actual, expected) def test_get_where_clause_array(self): cursor = Mock() cursor.execute = Mock(return_value=[['\'2\''], ['\'1\'']]) arcpy = Mock() arcpy.ArcSDESQLExecute = Mock(return_value=cursor) actual = self.patient._get_where_clause(arcpy, {}) expected = 'Project_FK in (\'2\',\'1\')' self.assertEqual(actual, expected) def test_get_where_clause_empty(self): cursor = Mock() cursor.execute = Mock(return_value=None) arcpy = Mock() arcpy.ArcSDESQLExecute = Mock(return_value=cursor) actual = self.patient._get_where_clause(arcpy, {}) self.assertIsNone(actual) def test_points_etl(self): patient = model.Points() self.patient.point_to_multipoint = Mock(return_value='point_to_multipoint') source_type = 1 type = self.get_type_pair_for('Trough', model.Lookup.other_points) type_code = self.get_type_pair_for(type[0], model.Lookup.other_points_code, 0) original_project_status = self.get_type_pair_for('Project', model.Lookup.original_status)[0] current_project_status = self.get_type_pair_for('Current', model.Lookup.new_status) row = ('shape', 'guid', 'Project_FK', source_type, source_type, ' description ', original_project_status, original_project_status, 'Project_FK') expected = [('SHAPE@', 'point_to_multipoint'), ('GUID', 'guid'), ('Project_FK', 'Project_FK'), ('TypeDescription', type[1]), ('TypeCode', type_code[1]), ('Description', 'description'), ('StatusDescription', current_project_status[1]), ('StatusCode', current_project_status[0]), ('Project_Id', 1)] actual = self.patient._etl_row(patient, row) self.assertEqual(actual, expected) def test_final_points_etl(self): patient = model.Points(final=True) self.patient.point_to_multipoint = Mock(return_value='point_to_multipoint') source_type = 2 type = self.get_type_pair_for('Water control structure', model.Lookup.other_points) type_code = self.get_type_pair_for(type[0], model.Lookup.other_points_code, 0) current_project_status = self.get_type_pair_for('Completed', model.Lookup.new_status) row = ('shape', 'guid', 'CompletedProject_FK', source_type, source_type, ' description ', 'CompletedProject_FK') expected = [('SHAPE@', 'point_to_multipoint'), ('GUID', 'guid'), ('Project_FK', 'CompletedProject_FK'), ('TypeDescription', type[1]), ('TypeCode', type_code[1]), ('Description', 'description'), ('StatusDescription', current_project_status[1]), ('StatusCode', current_project_status[0]), ('Project_Id', 2)] actual = self.patient._etl_row(patient, row) self.assertEqual(actual, expected) def test_guzzler_etl(self): patient = model.Guzzler() self.patient.point_to_multipoint = Mock(return_value='point_to_multipoint') type = self.get_feature_type_pair('Guzzler') action = 5 subtype = 1 original_project_status = 2 new_project_status = 3 row = ('shape', 'guid', 'Project_FK', subtype, subtype, action, action, original_project_status, original_project_status, 'Project_FK') expected = [('SHAPE@', 'point_to_multipoint'), ('GUID', 'guid'), ('Project_FK', 'Project_FK'), ('TypeDescription', type[1]), ('TypeCode', type[0]), ('FeatureSubTypeDescription', 'Big game'), ('FeatureSubTypeID', 14), ('ActionDescription', 'Removal'), ('ActionID', 27), ('StatusDescription', 'Current'), ('StatusCode', new_project_status), ('Project_Id', 1)] actual = self.patient._etl_row(patient, row) self.assertEqual(actual, expected) def test_final_guzzler_etl(self): patient = model.Guzzler(final=True) self.patient.point_to_multipoint = Mock(return_value='point_to_multipoint') type = self.get_feature_type_pair('Guzzler') action = 3 subtype = 2 row = ('shape', 'guid', 'CompletedProject_FK', subtype, subtype, action, action, 'CompletedProject_FK') expected = [('SHAPE@', 'point_to_multipoint'), ('GUID', 'guid'), ('Project_FK', 'CompletedProject_FK'), ('TypeDescription', type[1]), ('TypeCode', type[0]), ('FeatureSubTypeDescription', 'Other'), ('FeatureSubTypeID', 17), ('ActionDescription', 'Construction'), ('ActionID', 8), ('StatusDescription', 'Completed'), ('StatusCode', 5), ('Project_Id', 2)] actual = self.patient._etl_row(patient, row) self.assertEqual(actual, expected) def test_fence_etl(self): patient = model.Fence() type = self.get_feature_type_pair('Fence') sub_type = 5 #: 'Pole top' action = 4 #: 'Reconstruction' original_project_status = 2 new_project_status = 3 row = ('shape', 'guid', 'Project_FK', sub_type, sub_type, action, action, original_project_status, original_project_status, 'Project_FK') expected = [('SHAPE@', 'shape'), ('GUID', 'guid'), ('Project_FK', 'Project_FK'), ('TypeDescription', type[1]), ('TypeCode', type[0]), ('FeatureSubTypeDescription', 'Pole top'), ('FeatureSubTypeID', 5), ('ActionDescription', 'Reconstruction'), ('ActionID', 26), ('StatusDescription', 'Current'), ('StatusCode', new_project_status), ('Project_Id', 1)] actual = self.patient._etl_row(patient, row) self.assertEqual(actual, expected) def test_real_fence(self): patient = model.Fence() row = (495, #: shape '1E383419-8A37-4454-98EA-2E23E2DD6D30', #: guid 'Project_FK', #: pfk 2, #: fence type 2, #: fence type 3, #: fence action 3, #: fence action 2, #: status 2, #: status 'Project_FK') #: pfk expected = ([ ('SHAPE@', 495), #: shape ('GUID', '1E383419-8A37-4454-98EA-2E23E2DD6D30'), #: guid ('Project_FK', 'Project_FK'), #: pfk ('TypeDescription', 'Fence'), ('TypeCode', 10), ('FeatureSubTypeDescription', 'Buck pole'), ('FeatureSubTypeID', 2), ('ActionDescription', 'Construction'), ('ActionID', 8), ('StatusDescription', 'Current'), ('StatusCode', 3), ('Project_Id', 1)]) actual = self.patient._etl_row(patient, row) self.assertEqual(actual, expected) def test_final_fence_etl(self): patient = model.Fence(final=True) type = self.get_feature_type_pair('Fence') sub_type = 5 #: 'Pole top' action = 4 #: 'Reconstruction' row = ('shape', 'guid', 'CompletedProject_FK', sub_type, sub_type, action, action, 'CompletedProject_FK') expected = [('SHAPE@', 'shape'), ('GUID', 'guid'), ('Project_FK', 'CompletedProject_FK'), ('TypeDescription', type[1]), ('TypeCode', type[0]), ('FeatureSubTypeDescription', 'Pole top'), ('FeatureSubTypeID', 5), ('ActionDescription', 'Reconstruction'), ('ActionID', 26), ('StatusDescription', 'Completed'), ('StatusCode', 5), ('Project_Id', 2)] actual = self.patient._etl_row(patient, row) self.assertEqual(actual, expected) def test_pipeline_etl(self): patient = model.Pipeline() type = self.get_feature_type_pair('Pipeline') sub_type = 1 #: 'Above surface' action = 4 #: 'Reconstruction' original_project_status = 2 new_project_status = 3 row = ('shape', 'guid', 'Project_FK', sub_type, sub_type, action, action, original_project_status, original_project_status, 'Project_FK') expected = [('SHAPE@', 'shape'), ('GUID', 'guid'), ('Project_FK', 'Project_FK'), ('TypeDescription', type[1]), ('TypeCode', type[0]), ('FeatureSubTypeDescription', 'Above surface'), ('FeatureSubTypeID', 8), ('ActionDescription', 'Reconstruction'), ('ActionID', 26), ('StatusDescription', 'Current'), ('StatusCode', new_project_status), ('Project_Id', 1)] actual = self.patient._etl_row(patient, row) self.assertEqual(actual, expected) def test_final_pipeline_etl(self): patient = model.Pipeline(final=True) type = self.get_feature_type_pair('Pipeline') sub_type = 2 #: 'Below surface' action = 5 #: 'Removal' row = ('shape', 'guid', 'CompletedProject_FK', sub_type, sub_type, action, action, 'CompletedProject_FK') expected = [('SHAPE@', 'shape'), ('GUID', 'guid'), ('Project_FK', 'CompletedProject_FK'), ('TypeDescription', type[1]), ('TypeCode', type[0]), ('FeatureSubTypeDescription', 'Below surface'), ('FeatureSubTypeID', 9), ('ActionDescription', 'Removal'), ('ActionID', 27), ('StatusDescription', 'Completed'), ('StatusCode', 5), ('Project_Id', 2)] actual = self.patient._etl_row(patient, row) self.assertEqual(actual, expected) def test_dam_etl(self): patient = model.Dam() type = self.get_feature_type_pair('Dam') action = 4 #: 'Reconstruction' original_project_status = 2 current_project_status = 3 row = ('shape', 'guid', 'Project_FK', action, action, original_project_status, original_project_status, 'Project_FK') expected = [('SHAPE@', 'poly_to_line'), ('GUID', 'guid'), ('Project_FK', 'Project_FK'), ('TypeDescription', type[1]), ('TypeCode', type[0]), ('ActionDescription', 'Reconstruction'), ('ActionID', 26), ('StatusDescription', 'Current'), ('StatusCode', current_project_status), ('Project_Id', 1)] self.patient.polygon_to_line = Mock(return_value='poly_to_line') actual = self.patient._etl_row(patient, row) self.assertEqual(actual, expected) self.patient.polygon_to_line.assert_called_with('shape') def test_final_dam_etl(self): patient = model.Dam(final=True) type = self.get_feature_type_pair('Dam') action = 1 #: 'Maintenance' self.patient.polygon_to_line = Mock(return_value='poly_to_line') row = ('shape', 'guid', 'CompletedProject_FK', action, action, 'CompletedProject_FK') expected = [('SHAPE@', 'poly_to_line'), ('GUID', 'guid'), ('Project_FK', 'CompletedProject_FK'), ('TypeDescription', type[1]), ('TypeCode', type[0]), ('ActionDescription', 'Maintenance'), ('ActionID', 20), ('StatusDescription', 'Completed'), ('StatusCode', 5), ('Project_Id', 2)] actual = self.patient._etl_row(patient, row) self.assertEqual(actual, expected) self.patient.polygon_to_line.assert_called_with('shape') def test_affectedarea_etl(self): patient = model.AffectedArea() type = self.get_feature_type_pair('Affected Area') original_project_status = 2 current_project_status = 3 row = ('shape', 'guid', 'Project_FK', original_project_status, original_project_status, 'Project_FK') expected = [('SHAPE@', 'shape'), ('GUID', 'guid'), ('Project_FK', 'Project_FK'), ('TypeDescription', type[1]), ('TypeCode', type[0]), ('StatusDescription', 'Current'), ('StatusCode', current_project_status), ('Project_Id', 1)] actual = self.patient._etl_row(patient, row) self.assertEqual(actual, expected) def test_final_affectedarea_etl(self): patient = model.AffectedArea(final=True) type = self.get_feature_type_pair('Affected Area') row = ('shape', 'guid', 'CompletedProject_FK', 'CompletedProject_FK') expected = [('SHAPE@', 'shape'), ('GUID', 'guid'), ('Project_FK', 'CompletedProject_FK'), ('TypeDescription', type[1]), ('TypeCode', type[0]), ('StatusDescription', 'Completed'), ('StatusCode', 5), ('Project_Id', 2)] actual = self.patient._etl_row(patient, row) self.assertEqual(actual, expected) def test_easement_etl(self): patient = model.EasementAquisition() type = self.get_feature_type_pair('Easement/Acquisition') original_project_status = 2 current_project_status = 3 row = ('shape', 'guid', 'Project_FK', original_project_status, original_project_status, 'Project_FK') expected = [('SHAPE@', 'shape'), ('GUID', 'guid'), ('Project_FK', 'Project_FK'), ('TypeDescription', type[1]), ('TypeCode', type[0]), ('StatusDescription', 'Current'), ('StatusCode', current_project_status), ('Project_Id', 1)] actual = self.patient._etl_row(patient, row) self.assertEqual(actual, expected) def test_final_easement_etl(self): patient = model.EasementAquisition(final=True) type = self.get_feature_type_pair('Easement/Acquisition') current_project_status = self.get_type_pair_for('Completed', model.Lookup.new_status) row = ('shape', 'guid', 'CompletedProject_FK', 'CompletedProject_FK') expected = [('SHAPE@', 'shape'), ('GUID', 'guid'), ('Project_FK', 'CompletedProject_FK'), ('TypeDescription', type[1]), ('TypeCode', type[0]), ('StatusDescription', current_project_status[1]), ('StatusCode', current_project_status[0]), ('Project_Id', 2)] actual = self.patient._etl_row(patient, row) self.assertEqual(actual, expected) def test_aquatic_etl(self): patient = model.AquaticTreatmentArea() type = self.get_feature_type_pair('Aquatic/Riparian Treatment Area') source_type_code = 2 original_project_status = self.get_type_pair_for('Project', model.Lookup.original_status)[0] current_project_status = self.get_type_pair_for('Current', model.Lookup.new_status) row = ('shape', 'guid', 'Project_FK', source_type_code, original_project_status, original_project_status, 'Project_FK') expected = [('SHAPE@', 'shape'), ('GUID', 'guid'), ('Project_FK', 'Project_FK'), ('TypeDescription', type[1]), ('TypeCode', type[0]), ('StatusDescription', current_project_status[1]), ('StatusCode', current_project_status[0]), ('Project_Id', 1)] actual = self.patient._etl_row(patient, row) self.assertEqual(actual, expected) def test_final_aquatic_etl(self): patient = model.AquaticTreatmentArea(final=True) type = self.get_feature_type_pair('Aquatic/Riparian Treatment Area') source_type_code = 2 row = ('shape', 'guid', 'CompletedProject_FK', source_type_code, 'CompletedProject_FK',) expected = [('SHAPE@', 'shape'), ('GUID', 'guid'), ('Project_FK', 'CompletedProject_FK'), ('TypeDescription', type[1]), ('TypeCode', type[0]), ('StatusDescription', 'Completed'), ('StatusCode', 5), ('Project_Id', 2)] actual = self.patient._etl_row(patient, row) self.assertEqual(actual, expected) def test_research_etl(self): patient = model.Research() type = self.get_feature_type_pair('Affected Area') original_project_status = 2 new_project_status = 3 row = ('shape', 'guid', 'Project_FK', original_project_status, original_project_status, 'Project_FK') expected = [('SHAPE@', 'shape'), ('GUID', 'guid'), ('Project_FK', 'Project_FK'), ('TypeDescription', type[1]), ('TypeCode', type[0]), ('StatusDescription', 'Current'), ('StatusCode', new_project_status), ('Project_Id', 1)] actual = self.patient._etl_row(patient, row) self.assertEqual(actual, expected) def test_final_research_etl(self): patient = model.Research(final=True) type = self.get_feature_type_pair('Affected Area') row = ('shape', 'guid', 'CompletedProject_FK', 'CompletedProject_FK') expected = [('SHAPE@', 'shape'), ('GUID', 'guid'), ('Project_FK', 'CompletedProject_FK'), ('TypeDescription', type[1]), ('TypeCode', type[0]), ('StatusDescription', 'Completed'), ('StatusCode', 5), ('Project_Id', 2)] actual = self.patient._etl_row(patient, row) self.assertEqual(actual, expected) def test_fishpassage_etl(self): patient = model.FishPassage() self.patient.point_to_multipoint = Mock(return_value='point_to_multipoint') type = self.get_feature_type_pair('Fish passage structure') original_project_status = 2 new_project_status = 3 class TestDummy(object): pass poly = TestDummy() poly.centroid = 'centroid' row = (poly, 'guid', 'Project_FK', original_project_status, original_project_status, 'Project_FK') expected = [('SHAPE@', 'point_to_multipoint'), ('GUID', 'guid'), ('Project_FK', 'Project_FK'), ('TypeDescription', type[1]), ('TypeCode', type[0]), ('StatusDescription', 'Current'), ('StatusCode', new_project_status), ('Project_Id', 1)] actual = self.patient._etl_row(patient, row) self.assertEqual(actual, expected) def test_final_fishpassage_etl(self): patient = model.FishPassage(final=True) self.patient.point_to_multipoint = Mock(return_value='point_to_multipoint') type = self.get_feature_type_pair('Fish passage structure') class TestDummy(object): pass poly = TestDummy() poly.centroid = 'centroid' row = (poly, 'guid', 'CompletedProject_FK', 'CompletedProject_FK') expected = [('SHAPE@', 'point_to_multipoint'), ('GUID', 'guid'), ('Project_FK', 'CompletedProject_FK'), ('TypeDescription', type[1]), ('TypeCode', type[0]), ('StatusDescription', 'Completed'), ('StatusCode', 5), ('Project_Id', 2)] actual = self.patient._etl_row(patient, row) self.assertEqual(actual, expected) def test_terrestrial_etl(self): patient = model.TerrestrialTreatmentArea() type = self.get_feature_type_pair('Terrestrial Treatment Area') source_type_code = 1 project_status = 2 row = ('shape', 'guid', 'Project_FK', source_type_code, project_status, project_status, 'Project_FK') expected = [('SHAPE@', 'shape'), ('GUID', 'guid'), ('Project_FK', 'Project_FK'), ('TypeDescription', type[1]), ('TypeCode', type[0]), ('StatusDescription', 'Current'), ('StatusCode', 3), ('Project_Id', 1)] actual = self.patient._etl_row(patient, row) self.assertEqual(actual, expected) def test_final_terrestrial_etl(self): patient = model.TerrestrialTreatmentArea(final=True) type = self.get_feature_type_pair('Terrestrial Treatment Area') source_type_code = 1 row = ('shape', 'guid', 'CompletedProject_FK', source_type_code, 'CompletedProject_FK') expected = [('SHAPE@', 'shape'), ('GUID', 'guid'), ('Project_FK', 'CompletedProject_FK'), ('TypeDescription', type[1]), ('TypeCode', type[0]), ('StatusDescription', 'Completed'), ('StatusCode', 5), ('Project_Id', 2)] actual = self.patient._etl_row(patient, row) self.assertEqual(actual, expected) def get_feature_type_pair(self, type): for pair in model.Lookup.feature_type.iteritems(): if pair[1] == type: return pair def get_type_pair_for(self, type, lookup, index=1): for pair in lookup.iteritems(): if pair[index] == type: return pair