[Libosinfo] [PATCH osinfo-db 06/15] tests: test_urls: Combine the test cases

Cole Robinson crobinso at redhat.com
Mon Mar 25 14:32:10 UTC 2019


They are all essentially doing the same thing: checking URL liveliness.
So let's scoop up all the URLs we want to test, then invoke the function
once per shortid

Signed-off-by: Cole Robinson <crobinso at redhat.com>
---
 tests/test_urls.py | 43 +++++++++++++++++++++----------------------
 tests/util.py      |  5 +++--
 2 files changed, 24 insertions(+), 24 deletions(-)

diff --git a/tests/test_urls.py b/tests/test_urls.py
index ea8c6f4..631cfc0 100644
--- a/tests/test_urls.py
+++ b/tests/test_urls.py
@@ -4,6 +4,7 @@
 import http
 import logging
 
+import pytest
 import requests
 
 from . import util
@@ -18,31 +19,29 @@ def _check_url(url):
     return response.ok
 
 
- at util.os_parametrize('osxml', filter_images=True)
-def test_images_url(osxml):
-    broken = []
-    for image in osxml.images:
-        if image.url:
-            if not _check_url(image.url):
-                broken.append(image.url)
-    assert broken == []
+def _collect_os_urls():
+    """
+    Iterate the OS list and return a list of pairs (shortid, [url list)
+    """
+    ret = []
 
+    for osxml in util.DataFiles.oses():
+        urls = []
+        urls.extend([i.url for i in osxml.images if i.url])
+        urls.extend([m.url for m in osxml.medias if m.url])
+        urls.extend([t.url for t in osxml.trees if t.url])
+        if urls:
+            ret.append((osxml.shortid, urls))
 
- at util.os_parametrize('osxml', filter_trees=True)
-def test_medias_url(osxml):
-    broken = []
-    for media in osxml.medias:
-        if media.url:
-            if not _check_url(media.url):
-                broken.append(media.url)
-    assert broken == []
+    return ret
 
 
- at util.os_parametrize('osxml', filter_media=True)
-def test_trees_url(osxml):
+ at pytest.mark.parametrize('testdata', _collect_os_urls(),
+        ids=lambda testdata: testdata[0])
+def test_urls(testdata):
+    urls = testdata[1]
     broken = []
-    for tree in osxml.trees:
-        if tree.url:
-            if not _check_url(tree.url):
-                broken.append(tree.url)
+    for url in urls:
+        if not _check_url(url):
+            broken.append(url)
     assert broken == []
diff --git a/tests/util.py b/tests/util.py
index ac90af9..bf04b74 100644
--- a/tests/util.py
+++ b/tests/util.py
@@ -111,8 +111,9 @@ def os_parametrize(argname, **kwargs):
     extra arguments to DataFiles.oses()
     """
     def ids_cb(osxml):
-        # pytest passes us a weird value when oses is empty, like for
-        # test_urls image testing at the time of this commit
+        # pytest passes us a weird value when oses is empty, which
+        # might happen depending on how agressively we filter. So
+        # we can't assume we are passed an Os instance
         return getattr(osxml, "shortid", str(osxml))
 
     oses = DataFiles.oses(**kwargs)
-- 
2.21.0




More information about the Libosinfo mailing list