Skip to content

Commit b210975

Browse files
authored
Add commandline argument parsing (#14)
Added commandline argument parsing, in addition to testing of the CLI tool on Travis, and
1 parent b7d547f commit b210975

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

pymongoexplain/__main__.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,20 @@
1313
# limitations under the License.
1414

1515

16+
"""This script is intended to provide an easy method to run explain on all
17+
commands in a script. Find more documentation at
18+
https://github.com/mongodb-labs/pymongoexplain/"
19+
"""
20+
1621
from pymongo.collection import Collection
1722
from .explainable_collection import ExplainCollection
1823

1924
import sys
2025
import logging
26+
import argparse
27+
2128

2229

23-
'''This module allows for pymongo scripts to run with with pymongoexplain,
24-
explaining each command as it occurs.
25-
'''
2630

2731

2832
FORMAT = '%(asctime)s %(levelname)s %(module)s %(message)s'
@@ -48,6 +52,18 @@ def new_func(self: Collection, *args, **kwargs):
4852
setattr(Collection, old_func_name, make_func(old_func, old_func_name))
4953

5054
if __name__ == '__main__':
51-
for file in sys.argv[1:]:
52-
with open(file) as f:
53-
exec(f.read())
55+
parser = argparse.ArgumentParser(description=__doc__)
56+
parser.add_argument(
57+
"input_script", nargs=1,help="The script that you "
58+
"wish to run explain on.")
59+
parser.add_argument(
60+
"arguments", metavar="script_arguments", help="add arguments to "
61+
"explained script",
62+
nargs="?")
63+
64+
args = parser.parse_args()
65+
file = args.input_script[0]
66+
with open(file) as f:
67+
sys.argv = [file]+args.arguments if args.arguments is not None else\
68+
[file]
69+
exec(f.read())

test/test_cli_tool_script.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""Script to test CLI functionality of pymongoexplain"""
2+
3+
4+
import pymongo
5+
6+
import sys
7+
8+
9+
client = pymongo.MongoClient(serverSelectionTimeoutMS=1000)
10+
collection = client.db.get_collection("products")
11+
collection.update_one({"quantity": 1057, "category": "apparel"},{"$set": {"reorder": True}})

test/test_collection.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
1516
import unittest
17+
import subprocess
18+
import os
1619

1720
from pymongo import MongoClient
1821
from pymongo import monitoring
@@ -182,6 +185,18 @@ def test_estimated_document_count(self):
182185
last_cmd_payload = self.explain.last_cmd_payload
183186
self._compare_command_dicts(last_cmd_payload, last_logger_payload)
184187

188+
def test_cli_tool(self):
189+
script_path = os.path.join(os.path.dirname(os.path.realpath(
190+
__file__)), "test_cli_tool_script.py")
191+
res = subprocess.run(["python3", "-m", "pymongoexplain",
192+
script_path], stdout = subprocess.PIPE)
193+
self.assertTrue(res.returncode == 0)
194+
self.assertNotEqual(res.stdout, "")
195+
196+
res = subprocess.run(["python3", "-m", "pymongoexplain",
197+
script_path, "-h"], stdout = subprocess.PIPE)
198+
self.assertNotEqual(res.stdout, "")
199+
self.assertTrue(res.returncode == 0)
185200

186201
if __name__ == '__main__':
187202
unittest.main()

0 commit comments

Comments
 (0)