Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8.4.0: test suite uses random2 #1454

Closed
kloczek opened this issue Jun 3, 2021 · 7 comments
Closed

8.4.0: test suite uses random2 #1454

kloczek opened this issue Jun 3, 2021 · 7 comments
Labels

Comments

@kloczek
Copy link
Contributor

kloczek commented Jun 3, 2021

Looks like pytest based tewst suite is trying to use random2 which is not maintained since 2013.

+ /usr/bin/python3 -Bm pytest -ra
=========================================================================== test session starts ============================================================================
platform linux -- Python 3.8.9, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- /usr/bin/python3
cachedir: .pytest_cache
hypothesis profile 'default' -> database=DirectoryBasedExampleDatabase('/home/tkloczko/rpmbuild/BUILD/faker-8.4.0/.hypothesis/examples')
rootdir: /home/tkloczko/rpmbuild/BUILD/faker-8.4.0, configfile: setup.cfg
plugins: Faker-8.4.0, forked-1.3.0, shutil-1.7.0, virtualenv-1.7.0, expect-1.1.0, cov-2.11.1, httpbin-1.0.0, xdist-2.2.1, flake8-1.0.7, timeout-1.4.2, betamax-0.8.1, pyfakefs-4.4.0, freezegun-0.4.2, cases-3.4.6, case-1.5.3, isort-1.3.0, aspectlib-1.5.2, asyncio-0.15.1, toolbox-0.5, xprocess-0.17.1, flaky-3.7.0, aiohttp-0.3.0, checkdocs-2.7.0, mock-3.6.1, rerunfailures-9.1.1, requests-mock-1.9.3, hypothesis-6.13.7
collected 1135 items / 1 error / 1134 selected

================================================================================== ERRORS ==================================================================================
_______________________________________________________________ ERROR collecting tests/providers/test_ssn.py _______________________________________________________________
ImportError while importing test module '/home/tkloczko/rpmbuild/BUILD/faker-8.4.0/tests/providers/test_ssn.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib64/python3.8/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/providers/test_ssn.py:10: in <module>
    import random2
E   ModuleNotFoundError: No module named 'random2'
============================================================================= warnings summary =============================================================================
faker/providers/person/fr_QC/__init__.py:8
  /home/tkloczko/rpmbuild/BUILD/faker-8.4.0/faker/providers/person/fr_QC/__init__.py:8: UserWarning: fr_QC locale is deprecated. Please use fr_CA.
    warnings.warn("fr_QC locale is deprecated. Please use fr_CA.")

-- Docs: https://docs.pytest.org/en/stable/warnings.html
========================================================================= short test summary info ==========================================================================
ERROR tests/providers/test_ssn.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
======================================================================= 1 warning, 1 error in 7.61s =======================================================================
@fcurella
Copy link
Collaborator

fcurella commented Jun 3, 2021

@kloczek That is correct and expected. We had to use random2 when we were supporting both Python 2 and Python 3.

In order to remove random2, we'll have to update a lot of test cases in the suite. It's doable, but it's very tedious.

@kloczek
Copy link
Contributor Author

kloczek commented Jun 3, 2021

python 2 is already 1.5 year EOSed https://www.python.org/doc/sunset-python-2/

In order to remove random2, we'll have to update a lot of test cases in the suite. It's doable, but it's very tedious.

On python 3.8.9 only that one unit is failing.

@fcurella
Copy link
Collaborator

fcurella commented Jun 3, 2021

@kloczek Feel free to send a Pull Request, I'll be happy to review and remove the dependency :)

@ktdreyer
Copy link

ktdreyer commented Oct 4, 2021

I looked into this issue today.

tests/providers/test_ssn.py is the only test code that uses random2. I naively tried changing all random2 references to random in that file, like so:

diff --git a/tests/providers/test_ssn.py b/tests/providers/test_ssn.py
index acb82caa..8f4fb03f 100644
--- a/tests/providers/test_ssn.py
+++ b/tests/providers/test_ssn.py
@@ -7,7 +7,7 @@ from unittest import mock
 
 import freezegun
 import pytest
-import random2
+import random
 
 from validators.i18n.es import es_cif as is_cif
 from validators.i18n.es import es_nie as is_nie
@@ -275,7 +275,7 @@ class TestEnUS(unittest.TestCase):
             assert area != '666'
 
     def test_invalid_ssn(self):
-        self.fake.random = random2.Random()
+        self.fake.random = random.Random()
         # Magic Numbers below generate '666-92-7944', '000-54-2963', '956-GG-9478', '436-00-1386',
         # and 134-76-0000 respectively. The "group" (GG) returned for '956-GG-9478 will be a random
         # number, and that random number is not in the "itin_group_numbers" List. The random GG occurs
@@ -603,7 +603,7 @@ class TestEtEE(unittest.TestCase):
 
     @freezegun.freeze_time('2019-03-11')
     def test_ssn(self):
-        self.fake.random = random2.Random()
+        self.fake.random = random.Random()
 
         self.fake.seed_instance(0)
         value = self.fake.ssn()
@@ -618,7 +618,7 @@ class TestEtEE(unittest.TestCase):
 
     @freezegun.freeze_time('2002-01-01')
     def test_ssn_2000(self):
-        self.fake.random = random2.Random()
+        self.fake.random = random.Random()
 
         self.fake.seed_instance(0)
         value = self.fake.ssn(min_age=0, max_age=1)
@@ -627,7 +627,7 @@ class TestEtEE(unittest.TestCase):
 
     @freezegun.freeze_time('2101-01-01')
     def test_ssn_2100(self):
-        self.fake.random = random2.Random()
+        self.fake.random = random.Random()
 
         self.fake.seed_instance(0)
         value = self.fake.ssn(min_age=0, max_age=1)

However, two tests fail with this change.

In TestEnUS::test_invalid_ssn, the

>       assert ssn.startswith('666')
E       AssertionError: assert False
E        +  where False = <built-in method startswith of str object at 0x7fb1e5744170>('666')
E        +    where <built-in method startswith of str object at 0x7fb1e5744170> = '682-80-0000'.startswith

and in TestEtEE::test_ssn:

>       assert not value.endswith('0')
E       AssertionError: assert not True
E        +  where True = <built-in method endswith of str object at 0x7fb1e5858630>('0')
E        +    where <built-in method endswith of str object at 0x7fb1e5858630> = '46808220410'.endswith

I don't know the faker project well enough to know how to resolve this. In particular, I don't understand the lines in the test suite like self.fake.seed_instance(1143). Would I need to change that seed value from 1143 to something else? How do I choose the right number?

@kloczek
Copy link
Contributor Author

kloczek commented Dec 27, 2021

I've updated that patch against 11.0.0

--- a/tests/providers/test_ssn.py~      2021-12-27 17:58:34.000000000 +0000
+++ b/tests/providers/test_ssn.py       2021-12-27 20:28:23.710907091 +0000
@@ -8,7 +8,7 @@

 import freezegun
 import pytest
-import random2
+import random

 from validators.i18n.es import es_cif as is_cif
 from validators.i18n.es import es_nie as is_nie
@@ -278,7 +278,7 @@
             assert area != "666"

     def test_invalid_ssn(self):
-        self.fake.random = random2.Random()
+        self.fake.random = random.Random()
         # Magic Numbers below generate '666-92-7944', '000-54-2963', '956-GG-9478', '436-00-1386',
         # and 134-76-0000 respectively. The "group" (GG) returned for '956-GG-9478 will be a random
         # number, and that random number is not in the "itin_group_numbers" List. The random GG occurs
@@ -659,7 +659,7 @@

     @freezegun.freeze_time("2019-03-11")
     def test_ssn(self):
-        self.fake.random = random2.Random()
+        self.fake.random = random.Random()

         self.fake.seed_instance(0)
         value = self.fake.ssn()
@@ -674,7 +674,7 @@

     @freezegun.freeze_time("2002-01-01")
     def test_ssn_2000(self):
-        self.fake.random = random2.Random()
+        self.fake.random = random.Random()

         self.fake.seed_instance(0)
         value = self.fake.ssn(min_age=0, max_age=1)
@@ -683,7 +683,7 @@

     @freezegun.freeze_time("2101-01-01")
     def test_ssn_2100(self):
-        self.fake.random = random2.Random()
+        self.fake.random = random.Random()

         self.fake.seed_instance(0)
         value = self.fake.ssn(min_age=0, max_age=1)

However as same as in your case pytaest fails with that patch

================================================================================= FAILURES =================================================================================
________________________________________________________________________ TestEnUS.test_invalid_ssn _________________________________________________________________________

self = <tests.providers.test_ssn.TestEnUS testMethod=test_invalid_ssn>

    def test_invalid_ssn(self):
        self.fake.random = random.Random()
        # Magic Numbers below generate '666-92-7944', '000-54-2963', '956-GG-9478', '436-00-1386',
        # and 134-76-0000 respectively. The "group" (GG) returned for '956-GG-9478 will be a random
        # number, and that random number is not in the "itin_group_numbers" List. The random GG occurs
        # even when using the same seed_instance() due to using random.choice() for GG to avoid valid
        # ITINs being returned as an invalid SSN:
        #
        # Ensure that generated SSNs are 11 characters long
        # including dashes, consist of dashes and digits only, and the tested number
        # violates the requirements below, ensuring an INVALID SSN is returned:
        #
        # A United States Social Security Number
        # (SSN) is a tax processing number issued by the Internal
        # Revenue Service with the format "AAA-GG-SSSS".  The
        # number is divided into three parts: the first three
        # digits, known as the area number because they were
        # formerly assigned by geographical region; the middle two
        # digits, known as the group number; and the final four
        # digits, known as the serial number. SSNs with the
        # following characteristics are not allocated:
        #
        # 1) Numbers with all zeros in any digit group
        # (000-##-####, ###-00-####, ###-##-0000).
        #
        # 2) Numbers with 666 or 900-999 in the first digit group.
        #
        # https://en.wikipedia.org/wiki/Social_Security_number
        #
        # ITIN explained:
        # https://www.irs.gov/individuals/international-taxpayers/general-itin-information

        itin_group_numbers = [
            70,
            71,
            72,
            73,
            74,
            75,
            76,
            77,
            78,
            79,
            80,
            81,
            82,
            83,
            84,
            85,
            86,
            87,
            88,
            90,
            91,
            92,
            94,
            95,
            96,
            97,
            98,
            99,
        ]

        self.fake.seed_instance(1143)
        ssn = self.fake.ssn(taxpayer_identification_number_type="INVALID_SSN")

        assert len(ssn) == 11
        assert ssn.replace("-", "").isdigit()
>       assert ssn.startswith("666")
E       AssertionError: assert False
E        +  where False = <built-in method startswith of str object at 0x7f8a58411e30>('666')
E        +    where <built-in method startswith of str object at 0x7f8a58411e30> = '682-80-0000'.startswith

tests/providers/test_ssn.py:348: AssertionError
____________________________________________________________________________ TestEtEE.test_ssn _____________________________________________________________________________

self = <tests.providers.test_ssn.TestEtEE testMethod=test_ssn>

    @freezegun.freeze_time("2019-03-11")
    def test_ssn(self):
        self.fake.random = random.Random()

        self.fake.seed_instance(0)
        value = self.fake.ssn()
        assert re.search(r"^\d{11}$", value)
>       assert not value.endswith("0")
E       AssertionError: assert not True
E        +  where True = <built-in method endswith of str object at 0x7f8a57eab030>('0')
E        +    where <built-in method endswith of str object at 0x7f8a57eab030> = '46808220410'.endswith

tests/providers/test_ssn.py:667: AssertionError
============================================================================= warnings summary =============================================================================
tests/test_factory.py::FactoryTestCase::test_documentor
  /home/tkloczko/rpmbuild/BUILDROOT/python-faker-11.0.0-2.fc35.x86_64/usr/lib/python3.8/site-packages/faker/documentor.py:107: UserWarning: No appropriate country for country code AA
    warnings.warn(str(e))

tests/test_factory.py::FactoryTestCase::test_documentor
  /usr/lib/python3.8/site-packages/dateutil/zoneinfo/__init__.py:26: UserWarning: I/O error(2): No such file or directory
    warnings.warn("I/O error({0}): {1}".format(e.errno, e.strerror))

tests/test_factory.py::FactoryTestCase::test_documentor
tests/test_providers_formats.py::test_no_invalid_formats[fr_QC]
  /home/tkloczko/rpmbuild/BUILDROOT/python-faker-11.0.0-2.fc35.x86_64/usr/lib/python3.8/site-packages/faker/providers/person/fr_QC/__init__.py:10: UserWarning: fr_QC locale is deprecated. Please use fr_CA.
    warnings.warn("fr_QC locale is deprecated. Please use fr_CA.")

tests/test_factory.py::FactoryTestCase::test_documentor
  /home/tkloczko/rpmbuild/BUILDROOT/python-faker-11.0.0-2.fc35.x86_64/usr/lib/python3.8/site-packages/faker/documentor.py:107: UserWarning: No appropriate country for country code QC
    warnings.warn(str(e))

tests/test_factory.py::FactoryTestCase::test_documentor
  /home/tkloczko/rpmbuild/BUILDROOT/python-faker-11.0.0-2.fc35.x86_64/usr/lib/python3.8/site-packages/faker/documentor.py:107: UserWarning: Country code cannot be determined from locale
    warnings.warn(str(e))

-- Docs: https://docs.pytest.org/en/stable/warnings.html
========================================================================= short test summary info ==========================================================================
SKIPPED [1] tests/providers/test_address.py:428: could not import 'ukpostcodeparser.parser': No module named 'ukpostcodeparser'
SKIPPED [2] tests/pytest/session_overrides/session_locale/test_autouse_faker_locale.py: This test is skipped by default since it depends on changes in the behavior of session-scoped fixtures. Use a separate pytest run for tests like this with the "--exclusive-faker-session" flag specified.
SKIPPED [2] tests/pytest/session_overrides/session_locale/test_autouse_faker_seed.py: This test is skipped by default since it depends on changes in the behavior of session-scoped fixtures. Use a separate pytest run for tests like this with the "--exclusive-faker-session" flag specified.
SKIPPED [4] tests/pytest/session_overrides/session_locale/test_manual_injection.py: This test is skipped by default since it depends on changes in the behavior of session-scoped fixtures. Use a separate pytest run for tests like this with the "--exclusive-faker-session" flag specified.
FAILED tests/providers/test_ssn.py::TestEnUS::test_invalid_ssn - AssertionError: assert False
FAILED tests/providers/test_ssn.py::TestEtEE::test_ssn - AssertionError: assert not True
===================================================== 2 failed, 1309 passed, 9 skipped, 6 warnings in 65.39s (0:01:05) =====================================================

@github-actions
Copy link

This issue is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale label Mar 29, 2022
@github-actions
Copy link

This issue was closed because it has been inactive for 14 days since being marked as stale.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants