-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 修复未开启认证的mongo执行上线失败问题 * 优化是否存在账号密码的逻辑,封装buildcmd函数 * _build_cmd逻辑优化,提取公共参数,根据不同情况判断进行其他参数拼接 * reformat mongo.py * refomart mongo by black * 添加单元测试
- Loading branch information
Showing
2 changed files
with
150 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import pytest | ||
from unittest.mock import patch, MagicMock | ||
from sql.engines.mongo import MongoEngine | ||
|
||
|
||
@pytest.fixture | ||
def mongo_engine(): | ||
engine = MongoEngine() | ||
engine.host = "localhost" | ||
engine.port = 27017 | ||
engine.user = "test_user" | ||
engine.password = "test_password" | ||
engine.instance = MagicMock() | ||
engine.instance.db_name = "test_db" | ||
return engine | ||
|
||
|
||
def test_build_cmd_with_load(mongo_engine): | ||
# Call the method with is_load=True | ||
cmd = mongo_engine._build_cmd( | ||
db_name="test_db", | ||
auth_db="admin", | ||
slave_ok="rs.slaveOk();", | ||
tempfile_="/tmp/test.js", | ||
is_load=True, | ||
) | ||
|
||
# Expected command template | ||
expected_cmd = ( | ||
"mongo --quiet -u test_user -p 'test_password' localhost:27017/admin <<\\EOF\n" | ||
"db=db.getSiblingDB('test_db');rs.slaveOk();load('/tmp/test.js')\nEOF" | ||
) | ||
|
||
# Assertions | ||
assert cmd == expected_cmd | ||
|
||
|
||
def test_build_cmd_without_load(mongo_engine): | ||
# Call the method with is_load=False | ||
cmd = mongo_engine._build_cmd( | ||
db_name="test_db", | ||
auth_db="admin", | ||
slave_ok="rs.slaveOk();", | ||
sql="db.test_collection.find()", | ||
is_load=False, | ||
) | ||
|
||
# Expected command template | ||
expected_cmd = ( | ||
"mongo --quiet -u test_user -p 'test_password' localhost:27017/admin <<\\EOF\n" | ||
"db=db.getSiblingDB('test_db');rs.slaveOk();printjson(db.test_collection.find())\nEOF" | ||
) | ||
|
||
# Assertions | ||
assert cmd == expected_cmd | ||
|
||
|
||
def test_build_cmd_without_auth(mongo_engine): | ||
# Set user and password to None | ||
mongo_engine.user = None | ||
mongo_engine.password = None | ||
|
||
# Call the method with is_load=False | ||
cmd = mongo_engine._build_cmd( | ||
db_name="test_db", | ||
auth_db="admin", | ||
slave_ok="rs.slaveOk();", | ||
sql="db.test_collection.find()", | ||
is_load=False, | ||
) | ||
|
||
# Expected command template | ||
expected_cmd = ( | ||
"mongo --quiet localhost:27017/admin <<\\EOF\n" | ||
"db=db.getSiblingDB('test_db');rs.slaveOk();printjson(db.test_collection.find())\nEOF" | ||
) | ||
|
||
# Assertions | ||
assert cmd == expected_cmd | ||
|
||
|
||
def test_build_cmd_with_load_without_auth(mongo_engine): | ||
# Set user and password to None | ||
mongo_engine.user = None | ||
mongo_engine.password = None | ||
|
||
# Call the method with is_load=True | ||
cmd = mongo_engine._build_cmd( | ||
db_name="test_db", | ||
auth_db="admin", | ||
slave_ok="rs.slaveOk();", | ||
tempfile_="/tmp/test.js", | ||
is_load=True, | ||
) | ||
|
||
# Expected command template | ||
expected_cmd = ( | ||
"mongo --quiet localhost:27017/admin <<\\EOF\n" | ||
"db=db.getSiblingDB('test_db');rs.slaveOk();load('/tmp/test.js')\nEOF" | ||
) | ||
|
||
# Assertions | ||
assert cmd == expected_cmd |