Skip to content

Commit

Permalink
补充单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
nick2wang committed Dec 12, 2023
1 parent 2a66bcd commit bd32df1
Showing 1 changed file with 55 additions and 2 deletions.
57 changes: 55 additions & 2 deletions sql/engines/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1606,20 +1606,73 @@ def test_execute_check_include_dot(self, mock_get_all_tables):
)

@patch("sql.engines.mongo.MongoEngine.get_all_tables")
def test_execute_check_on_dml(self, mock_get_all_tables):
def test_execute_check_on_dml_without_real_row_count(self, mock_get_all_tables):
sql = """db.job.insert([{"orderCode":1001},{"orderCode":1002}]);"""
mock_get_all_tables.return_value.rows = "job"
check_result = self.engine.execute_check("some_db", sql)
self.assertEqual(check_result.rows[0].__dict__["affected_rows"], 0)

@patch("sql.engines.mongo.MongoEngine.get_all_tables")
def test_execute_check_on_dml_with_real_row_count(self, mock_get_all_tables):
def test_execute_check_on_insert_one(self, mock_get_all_tables):
self.sys_config.set("real_row_count", True)
sql = """db.job.insertOne({"orderCode":1001});"""
mock_get_all_tables.return_value.rows = "job"
check_result = self.engine.execute_check("some_db", sql)
self.assertEqual(check_result.rows[0].__dict__["affected_rows"], 1)

@patch("sql.engines.mongo.MongoEngine.get_all_tables")
def test_execute_check_on_insert_single(self, mock_get_all_tables):
self.sys_config.set("real_row_count", True)
sql = """db.job.insert({"orderCode":1001});"""
mock_get_all_tables.return_value.rows = "job"
check_result = self.engine.execute_check("some_db", sql)
self.assertEqual(check_result.rows[0].__dict__["affected_rows"], 1)

@patch("sql.engines.mongo.MongoEngine.get_all_tables")
def test_execute_check_on_insert_multiple(self, mock_get_all_tables):
self.sys_config.set("real_row_count", True)
sql = """db.job.insert([{"orderCode":1001},{"orderCode":1002}]);"""
mock_get_all_tables.return_value.rows = "job"
check_result = self.engine.execute_check("some_db", sql)
self.assertEqual(check_result.rows[0].__dict__["affected_rows"], 2)

@patch("sql.engines.mongo.MongoEngine.get_all_tables")
def test_execute_check_on_insert_except(self, mock_get_all_tables):
self.sys_config.set("real_row_count", True)
sql = """db.job.insert(("orderCode":1001));"""
mock_get_all_tables.return_value.rows = "job"
check_result = self.engine.execute_check("some_db", sql)
self.assertEqual(check_result.rows[0].__dict__["affected_rows"], 0)

@patch("sql.engines.mongo.MongoEngine.get_all_tables")
@patch("sql.engines.mongo.MongoEngine.query")
def test_execute_check_on_update_with_find(self, mock_get_all_tables, mock_query):
self.sys_config.set("real_row_count", True)
sql = """db.job.find({"orderCode":1001}).update(({"orderCode":1002}));"""
mock_get_all_tables.return_value.rows = "job"
mock_query.return_value.rows = (('{"count": 0}',),)
check_result = self.engine.execute_check("some_db", sql)
self.assertEqual(check_result.rows[0].__dict__["affected_rows"], 0)

@patch("sql.engines.mongo.MongoEngine.get_all_tables")
@patch("sql.engines.mongo.MongoEngine.query")
def test_execute_check_on_update_without_find(
self, mock_get_all_tables, mock_query
):
self.sys_config.set("real_row_count", True)
sql = """db.job.update({"orderCode":1001},{$set:{"orderCode":1002}}));"""
mock_get_all_tables.return_value.rows = "job"
mock_query.return_value.rows = (('{"count": 0}',),)
check_result = self.engine.execute_check("some_db", sql)
self.assertEqual(check_result.rows[0].__dict__["affected_rows"], 0)

@patch("sql.engines.mongo.MongoEngine.get_all_tables")
def test_execute_check_with_syntax_error(self, mock_get_all_tables):
sql = """db.job.insert({"orderCode":1001);"""
mock_get_all_tables.return_value.rows = "job"
check_result = self.engine.execute_check("some_db", sql)
self.assertEqual(check_result.rows[0].__dict__["stagestatus"], "语法错误")

@patch("sql.engines.mongo.MongoEngine.exec_cmd")
@patch("sql.engines.mongo.MongoEngine.get_master")
def test_execute(self, mock_get_master, mock_exec_cmd):
Expand Down

0 comments on commit bd32df1

Please sign in to comment.