From 675b82d61a447d6ac0341f2f16760169a66d6737 Mon Sep 17 00:00:00 2001
From: Martin Curlej <mcurlej@redhat.com>
Date: Jan 10 2018 09:05:23 +0000
Subject: Fixed parameter bug, added cli unit tests, updated requirements


---

diff --git a/bin/module_diff b/bin/module_diff
index ec01496..5be7b69 100755
--- a/bin/module_diff
+++ b/bin/module_diff
@@ -4,8 +4,9 @@ import argparse
 from module_diff import module_diff
 
 
-def main(org_nsv, diff_nsv, json=False, dist=False, reused=False):
-    module_diff(org_nsv, diff_nsv, json=json)
+def main():
+    args = parse_arguments()
+    module_diff(args.org_nsv, args.diff_nsv, json=args.json, dist=args.dist, reused=args.reused)
 
 
 def parse_arguments():
@@ -24,5 +25,4 @@ def parse_arguments():
     return parser.parse_args()
 
 if __name__ == "__main__":
-    args = parse_arguments()
-    main(args.org_nsv, args.diff_nsv, json=args.json, dist=args.dist, reused=args.reused)
+    main()
diff --git a/module_diff/__init__.py b/module_diff/__init__.py
index 3c17ed2..2f71060 100644
--- a/module_diff/__init__.py
+++ b/module_diff/__init__.py
@@ -15,7 +15,6 @@ class CliColors:
 
 def module_diff(org_nsv, diff_nsv, json=False, dist=False, reused=False):
     pdc = PDC()
-    # TODO is check for not found module needed?
     org_module = pdc.get_module(org_nsv)
     diff_module = pdc.get_module(diff_nsv)
 
@@ -31,7 +30,8 @@ def human_readable_diff(diff, reused=False, dist=False):
     stats = diff["stats"]
     print("Compared modules:")
     print("NSV: %s, # of RPMs: %s" % (stats["org_module"]["nsv"], stats["org_module"]["num_rpms"]))
-    print("NSV: %s, # of RPMs: %s" % (stats["diff_module"]["nsv"], stats["diff_module"]["num_rpms"]))
+    print("NSV: %s, # of RPMs: %s" % (stats["diff_module"]["nsv"],
+                                      stats["diff_module"]["num_rpms"]))
     print("# of RPMs added: %s" % stats["num_added"])
     print("# of RPMs removed: %s" % stats["num_removed"])
     print("# of RPMs upgraded: %s" % stats["num_upgraded"])
@@ -41,9 +41,9 @@ def human_readable_diff(diff, reused=False, dist=False):
         print("# of RPMs reused: %s" % stats["num_reused"])
 
     if dist:
-        print("# of RPMs which dist tag changed: %s" % stats["changed_distag_rpms"])
+        print("# of RPMs which dist tag changed: %s" % stats["num_changed_dist"])
 
-    print("---- RPMs Added ----\n")
+    print("\n---- RPMs Added ----\n")
     for rpm in diff["added_rpms"]:
         print("%s+ %s%s" % (CliColors.OKGREEN, rpm, CliColors.ENDC))
     print("\n---- RPMs Removed ----\n")
@@ -65,5 +65,6 @@ def human_readable_diff(diff, reused=False, dist=False):
         for rpm in diff["changed_distag_rpms"]:
             print("C %s -> %s" % (rpm[0], rpm[1]))
 
+
 def jsonify_diff(diff):
     print(json.dumps(diff))
diff --git a/test-requirements.txt b/test-requirements.txt
index ed85ce0..43d12b4 100644
--- a/test-requirements.txt
+++ b/test-requirements.txt
@@ -1,2 +1,3 @@
+mock==2.0.0
 pytest==3.2.5
 requests-mock==1.3.0
diff --git a/tests/test_cli.py b/tests/test_cli.py
new file mode 100644
index 0000000..e17f49c
--- /dev/null
+++ b/tests/test_cli.py
@@ -0,0 +1,171 @@
+import json
+import mock
+import imp
+# importing the bin script as source through imp
+imp.load_source("module_diff_bin", "bin/module_diff") # noqa
+import requests_mock
+from module_diff_bin import main
+
+module1_url = ("https://pdc.fedoraproject.org/rest_api/v1"
+               "/unreleasedvariants/"
+               "?page_size=1&variant_uid=mariadb-10.2-20171019133930")
+
+module2_url = ("https://pdc.fedoraproject.org/rest_api/v1"
+               "/unreleasedvariants/"
+               "?page_size=1&variant_uid=mariadb-10.2-20171103103655")
+
+
+def generate_module_res(pdc_json_res):
+    module1_res = pdc_json_res.get()
+    module2_res = pdc_json_res.get()
+    module1_res["results"][0]["rpms"].remove(
+        "mariadb-connect-engine-3:10.2.9-3.module_629c669e.x86_64.rpm")
+    module2_res["results"][0]["koji_tag"] = "module-15d35d06c29b1848"
+    module2_res["results"][0]["rpms"] = [
+            "mariadb-debugsource-3:10.2.9-3.module_629c669e.x86_64.rpm",
+            "mariadb-connect-engine-debuginfo-3:10.2.9-3.module_629c669e.x86_64.rpm",
+            "mariadb-debuginfo-3:11.2.9-3.module_15d35d06.x86_64.rpm",
+            "mariadb-3:10.2.9-3.module_629c669e.x86_64.rpm",
+            "mariadb-devel-3:9.2.9-3.module_15d35d06.x86_64.rpm",
+            "mariadb-connect-engine-3:10.2.9-3.module_15d35d06.x86_64.rpm"
+    ]
+
+    return module1_res, module2_res
+
+
+class TestCli(object):
+    """
+    Tests for the cli of the module_diff tool
+    """
+
+    def test_cli_required_options(self, pdc_json_res, capsys):
+        """ Testing output with minimum required options """
+
+        module1_res, module2_res = generate_module_res(pdc_json_res)
+
+        with requests_mock.mock() as mock_http:
+            mock_http.register_uri("GET", module1_url, json=module1_res)
+            mock_http.register_uri("GET", module2_url, json=module2_res)
+
+            cli_cmd = [
+                'module_diff',
+                'mariadb-10.2-20171019133930',
+                'mariadb-10.2-20171103103655'
+            ]
+
+            with mock.patch("sys.argv", cli_cmd):
+                main()
+                output = capsys.readouterr()[0]
+
+            assert "Compared modules" in output
+            assert "NSV:" in output
+            assert "# of RPMs:" in output
+            assert "# of RPMs added:" in output
+            assert "# of RPMs removed:" in output
+            assert "# of RPMs upgraded:" in output
+            assert "# of RPMs downgraded:" in output
+            assert "RPMs Added" in output
+            assert "RPMs Added" in output
+            assert "RPMs Removed" in output
+            assert "RPMs Upgraded" in output
+            assert "RPMs Downgraded" in output
+            assert "RPMs which dist tag changed" not in output
+            assert "RPMs Reused" not in output
+
+    def test_cli_dist_option(self, pdc_json_res, capsys):
+        """
+        Testing option --dist which displays RPMs which changed their dist tag.
+        """
+
+        module1_res, module2_res = generate_module_res(pdc_json_res)
+
+        with requests_mock.mock() as mock_http:
+            mock_http.register_uri("GET", module1_url, json=module1_res)
+            mock_http.register_uri("GET", module2_url, json=module2_res)
+
+            cli_cmd = [
+                'module_diff',
+                'mariadb-10.2-20171019133930',
+                'mariadb-10.2-20171103103655',
+                '--dist'
+            ]
+
+            with mock.patch("sys.argv", cli_cmd):
+                main()
+                output = capsys.readouterr()[0]
+
+            assert "Compared modules" in output
+            assert "NSV:" in output
+            assert "# of RPMs:" in output
+            assert "# of RPMs added:" in output
+            assert "# of RPMs removed:" in output
+            assert "# of RPMs upgraded:" in output
+            assert "# of RPMs downgraded:" in output
+            assert "RPMs Added" in output
+            assert "RPMs Added" in output
+            assert "RPMs Removed" in output
+            assert "RPMs Upgraded" in output
+            assert "RPMs Downgraded" in output
+            assert "RPMs which dist tag changed" in output
+            assert "RPMs Reused" not in output
+
+    def test_cli_reused_option(self, pdc_json_res, capsys):
+        """
+        Testing option --reused which displays RPMs which where reused from previous build.
+        """
+        module1_res, module2_res = generate_module_res(pdc_json_res)
+
+        with requests_mock.mock() as mock_http:
+            mock_http.register_uri("GET", module1_url, json=module1_res)
+            mock_http.register_uri("GET", module2_url, json=module2_res)
+
+            cli_cmd = [
+                'module_diff',
+                'mariadb-10.2-20171019133930',
+                'mariadb-10.2-20171103103655',
+                '--reused'
+            ]
+
+            with mock.patch("sys.argv", cli_cmd):
+                main()
+                output = capsys.readouterr()[0]
+
+            assert "Compared modules" in output
+            assert "NSV:" in output
+            assert "# of RPMs:" in output
+            assert "# of RPMs added:" in output
+            assert "# of RPMs removed:" in output
+            assert "# of RPMs upgraded:" in output
+            assert "# of RPMs downgraded:" in output
+            assert "RPMs Added" in output
+            assert "RPMs Added" in output
+            assert "RPMs Removed" in output
+            assert "RPMs Upgraded" in output
+            assert "RPMs Downgraded" in output
+            assert "RPMs which dist tag changed" not in output
+            assert "RPMs Reused" in output
+
+    def test_cli_json_option(self, pdc_json_res, capsys):
+        """
+        Testing option --json which displays the output as a json string.
+        """
+        module1_res, module2_res = generate_module_res(pdc_json_res)
+
+        with requests_mock.mock() as mock_http:
+            mock_http.register_uri("GET", module1_url, json=module1_res)
+            mock_http.register_uri("GET", module2_url, json=module2_res)
+
+            cli_cmd = [
+                'module_diff',
+                'mariadb-10.2-20171019133930',
+                'mariadb-10.2-20171103103655',
+                '--json'
+            ]
+
+            with mock.patch("sys.argv", cli_cmd):
+                main()
+                output = capsys.readouterr()[0]
+                # json.loads will fire an exception if the json is not valid
+                json_dict = json.loads(output)
+                assert isinstance(json_dict, dict)
+                assert len(json_dict)
diff --git a/tests/test_module.py b/tests/test_module.py
index c9ef050..63342d6 100644
--- a/tests/test_module.py
+++ b/tests/test_module.py
@@ -20,7 +20,7 @@ module_diff_ns_url = ("https://pdc.fedoraproject.org/rest_api/v1"
 @pytest.mark.usefixtures("pdc_json_res")
 class TestModule(object):
     """
-    Test for the Module Class
+    Tests for the Module Class
     """
 
     def test_get_diff_no_diff(self, pdc_json_res):
@@ -211,11 +211,12 @@ class TestModule(object):
         md = module1.get_rpm_md(rpm)
 
         expected_md = {'arch': 'x86_64',
-            'dist_tag': 'module_80d712e7',
-            'epoch': '0',
-            'full_name': 'python3-hawkey-debuginfo-0:0.10.1-2.module_80d712e7.modularity.1.x86_64.rpm',
-            'name': 'python3-hawkey-debuginfo',
-            'release': '2.module_80d712e7.modularityx86_64rpm',
-            'version': '0.10.1'}
+                       'dist_tag': 'module_80d712e7',
+                       'epoch': '0',
+                       'full_name': ('python3-hawkey-debuginfo-0:0.10.1-2'
+                                     '.module_80d712e7.modularity.1.x86_64.rpm'),
+                       'name': 'python3-hawkey-debuginfo',
+                       'release': '2.module_80d712e7.modularityx86_64rpm',
+                       'version': '0.10.1'}
 
         assert md == expected_md

