# coding=utf-8 import unittest import inspect import arcpy from functions import * from sys_config import SysConfig from logger import Logger from sde_sync import SdeSynchronous class TestSdeSynchronous(unittest.TestCase): def setUp(self): # 获取当前路径 caller_file = inspect.getfile(inspect.currentframe()) work_pathname = os.path.abspath(os.path.dirname(caller_file)) # 同步开始时间 curr_sync_date = time.strftime("%Y-%m-%d", time.localtime()) curr_sync_time = time.strftime("%H-%M-%S", time.localtime()) # 设置当前工作路径 config = SysConfig() config.pathname = work_pathname config.curr_sync_datetime = curr_sync_date + " " + curr_sync_time # 日志记录 create_dir(work_pathname + "\\log") config.logfile_name = work_pathname + "\\log\\" + config.curr_sync_datetime + "-SdeSynchronous.txt" logger = Logger(config.logfile_name) logger.log("==SdeSynchronous Started==") logger.log("Process begin at " + str(curr_sync_date) + " " + str(curr_sync_time) + ".") self.sde_db_sync = SdeSynchronous(config, logger) self.sde_db_sync.src_sde = self.sde_db_sync.get_connection(str(u"源库SDE连接").encode('gb2312')) arcpy.env.workspace = self.sde_db_sync.src_sde self.sde_db_sync.config.last_sync_datetime = self.sde_db_sync.get_last_sync_datetime() self.edit = self.sde_db_sync.start_edit() def tearDown(self): self.sde_db_sync.stop_edit(self.edit, False) self.sde_db_sync.logger.close() # 删除log日志文件 os.remove(self.sde_db_sync.config.logfile_name) def test_get_last_max_object_id(self): self.assertEqual(self.sde_db_sync.get_last_max_object_id("Building_A"), 11) def test_get_last_sync_datetime(self): print(self.sde_db_sync.get_last_sync_datetime()) def test_get_projects_info(self): self.sde_db_sync.config.last_sync_datetime = "2022-10-01 00:00:00" print(self.sde_db_sync.get_projects_info()) def test_create_gdb_files(self): self.sde_db_sync.create_gdb_files() self.assertTrue(os.path.exists(self.sde_db_sync.out_gdb_path)) self.assertTrue(os.path.exists(self.sde_db_sync.out_gdb_path + "\\" + self.sde_db_sync.out_gdb_files["Add"])) self.assertTrue(os.path.exists(self.sde_db_sync.out_gdb_path + "\\" + self.sde_db_sync.out_gdb_files["Edit"])) self.assertTrue(os.path.exists(self.sde_db_sync.out_gdb_path + "\\" + self.sde_db_sync.out_gdb_files["Delete"])) arcpy.Delete_management(self.sde_db_sync.out_gdb_path + "\\" + self.sde_db_sync.out_gdb_files["Add"]) arcpy.Delete_management(self.sde_db_sync.out_gdb_path + "\\" + self.sde_db_sync.out_gdb_files["Edit"]) arcpy.Delete_management(self.sde_db_sync.out_gdb_path + "\\" + self.sde_db_sync.out_gdb_files["Delete"]) def test_get_max_object_id(self): self.assertEqual(self.sde_db_sync.get_max_object_id("Building_A"), 1) def test_set_last_max_object_id(self): self.sde_db_sync.set_last_max_object_id("Building_A", 11) def test_get_sys_time_condition(self): ld = self.sde_db_sync.config.last_sync_datetime cd = self.sde_db_sync.config.curr_sync_datetime str_cond1 = "UpdateTime>=TO_DATE('" + ld + "','YYYY-MM-DD HH24:MI:SS') and UpdateTime0") if __name__ == '__main__': unittest.main()