Repository: pulp/pulpcore

Issue:
As a developer, I can have pytest run the unit tests
Author: @bmbouter (bmbouter)


Redmine Issue: 9643, https://pulp.plan.io/issues/9643

---

As part of the testing effort, it would be nice to have pytest run the unittests in addition to our functional tests.





Discussion:

Resolving patch:
diff --git a/pulpcore/app/settings.py b/pulpcore/app/settings.py
--- a/pulpcore/app/settings.py
+++ b/pulpcore/app/settings.py
@@ -375,7 +375,8 @@
 
 
 if not (
-    Path(sys.argv[0]).name == "sphinx-build"
+    Path(sys.argv[0]).name == "pytest"
+    or Path(sys.argv[0]).name == "sphinx-build"
     or (len(sys.argv) >= 2 and sys.argv[1] == "collectstatic")
 ):
     try:


Test patch:
diff --git a/pulpcore/tests/conftest.py b/pulpcore/tests/functional/conftest.py
similarity index 100%
rename from pulpcore/tests/conftest.py
rename to pulpcore/tests/functional/conftest.py
diff --git a/pulpcore/tests/conftest_pulp_file.py b/pulpcore/tests/functional/conftest_pulp_file.py
similarity index 100%
rename from pulpcore/tests/conftest_pulp_file.py
rename to pulpcore/tests/functional/conftest_pulp_file.py
diff --git a/pulpcore/tests/fixtures/basic/1.iso b/pulpcore/tests/functional/fixtures/basic/1.iso
similarity index 100%
rename from pulpcore/tests/fixtures/basic/1.iso
rename to pulpcore/tests/functional/fixtures/basic/1.iso
diff --git a/pulpcore/tests/fixtures/basic/2.iso b/pulpcore/tests/functional/fixtures/basic/2.iso
similarity index 100%
rename from pulpcore/tests/fixtures/basic/2.iso
rename to pulpcore/tests/functional/fixtures/basic/2.iso
diff --git a/pulpcore/tests/fixtures/basic/3.iso b/pulpcore/tests/functional/fixtures/basic/3.iso
similarity index 100%
rename from pulpcore/tests/fixtures/basic/3.iso
rename to pulpcore/tests/functional/fixtures/basic/3.iso
diff --git a/pulpcore/tests/fixtures/basic/PULP_MANIFEST b/pulpcore/tests/functional/fixtures/basic/PULP_MANIFEST
similarity index 100%
rename from pulpcore/tests/fixtures/basic/PULP_MANIFEST
rename to pulpcore/tests/functional/fixtures/basic/PULP_MANIFEST
diff --git a/pulpcore/tests/unit/serializers/test_content.py b/pulpcore/tests/unit/serializers/test_content.py
deleted file mode 100644
--- a/pulpcore/tests/unit/serializers/test_content.py
+++ /dev/null
@@ -1,37 +0,0 @@
-from unittest import TestCase
-
-import mock
-from pulpcore.app.models import Artifact
-from pulpcore.app.serializers import ArtifactSerializer
-from pulpcore.constants import ALL_KNOWN_CONTENT_CHECKSUMS
-from rest_framework import serializers
-
-
-class TestArtifactSerializer(TestCase):
-    def test_validate_file_checksum(self):
-        mock_file = mock.MagicMock(size=42)
-        mock_file.hashers.__getitem__.return_value.hexdigest.return_value = "asdf"
-
-        data = {"file": mock_file}
-        serializer = ArtifactSerializer(data=data)
-        self.assertTrue(serializer.is_valid())
-        new_data = serializer.validated_data
-        self.assertEqual(new_data["file"], mock_file)
-        self.assertEqual(new_data["size"], 42)
-        for csum in Artifact.DIGEST_FIELDS:
-            self.assertEqual(new_data[csum], "asdf")
-
-        for csum in ALL_KNOWN_CONTENT_CHECKSUMS.difference(Artifact.DIGEST_FIELDS):
-            self.assertFalse(csum in new_data, f"Found forbidden checksum {csum}")
-
-        # This part of the test will only fire if the system-under-test has forbidden
-        # use of 'md5'
-        if "md5" not in Artifact.DIGEST_FIELDS:
-            data = {"file": mock_file, "md5": "asdf"}
-            with self.assertRaises(serializers.ValidationError) as cm:  # noqa
-                serializer.validate(data)
-
-    def test_emtpy_data(self):
-        data = {}
-        serializer = ArtifactSerializer(data=data)
-        self.assertFalse(serializer.is_valid())
diff --git a/pulpcore/tests/unit/serializers/test_repository.py b/pulpcore/tests/unit/serializers/test_repository.py
--- a/pulpcore/tests/unit/serializers/test_repository.py
+++ b/pulpcore/tests/unit/serializers/test_repository.py
@@ -4,9 +4,7 @@
 import mock
 from rest_framework import serializers
 
-from pulpcore.app.models import Distribution
 from pulpcore.app.serializers import (
-    DistributionSerializer,
     PublicationSerializer,
     RemoteSerializer,
 )
@@ -15,38 +13,6 @@
 class TestRemoteSerializer(TestCase):
     minimal_data = {"name": "test", "url": "http://whatever"}
 
-    def test_minimal_data(self):
-        data = {}
-        data.update(self.minimal_data)
-        serializer = RemoteSerializer(data=data)
-        serializer.is_valid(raise_exception=True)
-
-    def test_validate_proxy(self):
-        data = {"proxy_url": "http://whatever"}
-        data.update(self.minimal_data)
-        serializer = RemoteSerializer(data=data)
-        serializer.is_valid(raise_exception=True)
-
-    def test_validate_proxy_invalid(self):
-        data = {"proxy_url": "http://user:pass@whatever"}
-        data.update(self.minimal_data)
-        serializer = RemoteSerializer(data=data)
-        with self.assertRaises(serializers.ValidationError):
-            serializer.is_valid(raise_exception=True)
-
-    def test_validate_proxy_creds(self):
-        data = {"proxy_url": "http://whatever", "proxy_username": "user", "proxy_password": "pass"}
-        data.update(self.minimal_data)
-        serializer = RemoteSerializer(data=data)
-        serializer.is_valid(raise_exception=True)
-
-    def test_validate_proxy_creds_invalid(self):
-        data = {"proxy_url": "http://whatever", "proxy_username": "user"}
-        data.update(self.minimal_data)
-        serializer = RemoteSerializer(data=data)
-        with self.assertRaises(serializers.ValidationError):
-            serializer.is_valid(raise_exception=True)
-
     def test_validate_proxy_creds_update(self):
         Remote = SimpleNamespace(
             proxy_url="http://whatever",
@@ -115,58 +81,3 @@ def test_validate_repository_version_only_unknown_field(self):
         serializer = PublicationSerializer(data=data)
         with self.assertRaises(serializers.ValidationError):
             serializer.validate(data)
-
-
-class TestDistributionPath(TestCase):
-    def test_overlap(self):
-        Distribution.objects.create(base_path="foo/bar", name="foobar")
-        overlap_errors = {"base_path": ["Overlaps with existing distribution 'foobar'"]}
-
-        # test that the new distribution cannot be nested in an existing path
-        data = {"name": "foobarbaz", "base_path": "foo/bar/baz"}
-        serializer = DistributionSerializer(data=data)
-        self.assertFalse(serializer.is_valid())
-        self.assertDictEqual(overlap_errors, serializer.errors)
-
-        # test that the new distribution cannot nest an existing path
-        data = {"name": "foo", "base_path": "foo"}
-        serializer = DistributionSerializer(data=data)
-        self.assertFalse(serializer.is_valid())
-        self.assertDictEqual(overlap_errors, serializer.errors)
-
-    def test_no_overlap(self):
-        Distribution.objects.create(base_path="fu/bar", name="fubar")
-
-        # different path
-        data = {"name": "fufu", "base_path": "fubar"}
-        serializer = DistributionSerializer(data=data)
-        self.assertTrue(serializer.is_valid())
-        self.assertDictEqual({}, serializer.errors)
-
-        # common base path but different path
-        data = {"name": "fufu", "base_path": "fu/baz"}
-        serializer = DistributionSerializer(data=data)
-        self.assertTrue(serializer.is_valid())
-        self.assertDictEqual({}, serializer.errors)
-
-    def test_slashes(self):
-        overlap_errors = {"base_path": ["Relative path cannot begin or end with slashes."]}
-
-        data = {"name": "fefe", "base_path": "fefe/"}
-        serializer = DistributionSerializer(data=data)
-        self.assertFalse(serializer.is_valid())
-        self.assertDictEqual(overlap_errors, serializer.errors)
-
-        data = {"name": "fefe", "base_path": "/fefe/foo"}
-        serializer = DistributionSerializer(data=data)
-        self.assertFalse(serializer.is_valid())
-        self.assertDictEqual(overlap_errors, serializer.errors)
-
-    def test_uniqueness(self):
-        Distribution.objects.create(base_path="fizz/buzz", name="fizzbuzz")
-        data = {"name": "feefee", "base_path": "fizz/buzz"}
-        overlap_errors = {"base_path": ["This field must be unique."]}
-
-        serializer = DistributionSerializer(data=data)
-        self.assertFalse(serializer.is_valid())
-        self.assertDictEqual(overlap_errors, serializer.errors)