Repository: lk-geimfari/mimesis

Issue:
Check tests for duplicates.
It is possible that some tests are repeated several times without any need. If so, then we need to fix it.


Discussion:
How does one run the tests?
@pzelnip `make test` or `py.test .`
I will work on this

Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -22,13 +22,8 @@
 
 here = abspath(dirname(__file__))
 
-tests_requirements = [
-    'pytest',
-    'flake8-builtins',
-    'flake8-commas',
-    'flake8-quotes',
-    'pytest-flake8',
-]
+with open('dev_requirements.txt') as f:
+    tests_requirements = f.read().splitlines()
 
 # Long description.
 with open('PYPI_README.rst', 'r', encoding='utf-8') as f:


Test patch:
diff --git a/tests/test_builtins/en/test_usa_spec.py b/tests/test_builtins/en/test_usa_spec.py
--- a/tests/test_builtins/en/test_usa_spec.py
+++ b/tests/test_builtins/en/test_usa_spec.py
@@ -39,9 +39,14 @@ def test_personality(usa):
 def test_ssn(usa):
     result = usa.ssn()
     assert result is not None
-    # todo fix so this actually checks that 666 prefix can never be returned
     assert '666' != result[:3]
     assert re.match('^\d{3}-\d{2}-\d{4}$', result)
 
     assert result.replace('-', '').isdigit()
     assert len(result.replace('-', '')) == 9
+
+
+def test_cpf_with_666_prefix(mocker, usa):
+    with mocker.patch.object(usa.random, 'randint', return_value=666):
+        result = usa.ssn()
+        assert '665' == result[:3]
diff --git a/tests/test_data/test_code.py b/tests/test_data/test_code.py
--- a/tests/test_data/test_code.py
+++ b/tests/test_data/test_code.py
@@ -14,15 +14,10 @@ def test_str(code):
 
 def test_custom_code(code):
     result = code.custom_code(
-        mask='@###', char='@', digit='#')
-    assert len(result) == 4
-
-
-def test_custom_code_args(code):
-    result = code.custom_code(
-        mask='@@@-###-@@@').split('-')
+        mask='@@@-###-@@@', char='@', digit='#')
+    assert len(result) == 11
 
-    a, b, c = result
+    a, b, c = result.split('-')
     assert a.isalpha()
     assert b.isdigit()
     assert c.isalpha()
diff --git a/tests/test_data/test_cryptographic.py b/tests/test_data/test_cryptographic.py
--- a/tests/test_data/test_cryptographic.py
+++ b/tests/test_data/test_cryptographic.py
@@ -22,21 +22,25 @@ def test_uuid(crypto):
     ],
 )
 def test_hash(crypto, algorithm, length):
-    assert len(crypto.hash(algorithm=algorithm)) == length
+    result = crypto.hash(algorithm=algorithm)
+    assert len(result) == length
 
     with pytest.raises(UnsupportedAlgorithm):
         crypto.hash(algorithm='mimesis')
 
 
 def test_bytes(crypto):
-    assert crypto.bytes(entropy=64) is not None
-    assert isinstance(crypto.bytes(entropy=64), bytes)
+    result = crypto.bytes(entropy=64)
+    assert result is not None
+    assert isinstance(result, bytes)
 
 
 def test_token(crypto):
+    result = crypto.token(entropy=16)
+
     # Each byte converted to two hex digits.
-    assert len(crypto.token(entropy=16)) == 32
-    assert isinstance(crypto.token(), str)
+    assert len(result) == 32
+    assert isinstance(result, str)
 
 
 def test_salt(crypto):
diff --git a/tests/test_data/test_development.py b/tests/test_data/test_development.py
--- a/tests/test_data/test_development.py
+++ b/tests/test_data/test_development.py
@@ -39,8 +39,8 @@ def test_database(dev):
     result = dev.database()
     assert result in SQL
 
-    _result = dev.database(nosql=True)
-    assert _result in NOSQL
+    result = dev.database(nosql=True)
+    assert result in NOSQL
 
 
 def test_other(dev):
diff --git a/tests/test_data/test_games.py b/tests/test_data/test_games.py
--- a/tests/test_data/test_games.py
+++ b/tests/test_data/test_games.py
@@ -7,30 +7,31 @@ def test_gaming_platform(games):
 
 
 def test_score(games):
-    score = games.score(minimum=5.5, maximum=10)
-    assert isinstance(score, float)
-    assert (score >= 5.5) and (score <= 10)
+    result = games.score(minimum=5.5, maximum=10)
+    assert isinstance(result, float)
+    assert (result >= 5.5) and (result <= 10)
 
 
 def test_pegi_rating(games):
-    rating = games.pegi_rating().split(' ')[1]
+    result = games.pegi_rating().split(' ')[1]
     standard = [3, 7, 12, 16, 18]
 
-    assert int(rating) in standard
+    assert int(result) in standard
 
-    rating_pt = games.pegi_rating(pt=True).split(' ')[1]
-    assert int(rating_pt) <= 18
+    result = games.pegi_rating(pt=True).split(' ')[1]
+    assert int(result) <= 18
 
 
 def test_genre(games):
-    genre = games.genre()
-    assert genre in GENRES
+    result = games.genre()
+    assert result in GENRES
 
 
 def test_score_phrase(games):
-    phrase = games.score_phrase()
-    assert phrase in SCORE_PHRASES
+    result = games.score_phrase()
+    assert result in SCORE_PHRASES
 
 
 def test_game(games):
-    assert games.game() in GAMES
+    result = games.game()
+    assert result in GAMES
diff --git a/tests/test_data/test_path.py b/tests/test_data/test_path.py
--- a/tests/test_data/test_path.py
+++ b/tests/test_data/test_path.py
@@ -4,11 +4,13 @@
 
 
 def test_root(path):
-    assert 'C:\\', '/' == path.root()
+    result = path.root()
+    assert 'C:\\', '/' == result
 
 
 def test_home(path):
-    assert 'С:\\Users\\', '/home/' == path.home()
+    result = path.home()
+    assert 'С:\\Users\\', '/home/' == result
 
 
 def test_user(path):
diff --git a/tests/test_data/test_personal.py b/tests/test_data/test_personal.py
--- a/tests/test_data/test_personal.py
+++ b/tests/test_data/test_personal.py
@@ -87,11 +87,11 @@ def test_paypal(_personal):
     ],
 )
 def test_password(_personal, algorithm, length):
-    plain_password = _personal.password(length=15)
-    assert len(plain_password) == 15
+    result = _personal.password(length=15)
+    assert len(result) == 15
 
-    encrypted_password = _personal.password(algorithm=algorithm)
-    assert len(encrypted_password) == length
+    result = _personal.password(algorithm=algorithm)
+    assert len(result) == length
 
     with pytest.raises(UnsupportedAlgorithm):
         _personal.password(algorithm='sha42')
@@ -109,8 +109,8 @@ def test_password(_personal, algorithm, length):
     ],
 )
 def test_username(_personal, template):
-    username = _personal.username(template=template)
-    assert re.match(USERNAME_REGEX, username)
+    result = _personal.username(template=template)
+    assert re.match(USERNAME_REGEX, result)
 
     with pytest.raises(WrongArgument):
         _personal.username(template=':D')
@@ -252,8 +252,8 @@ def test_telephone(personal):
     assert result is not None
 
     mask = '+5 (###)-###-##-##'
-    result2 = personal.telephone(mask=mask)
-    head = result2.split(' ')[0]
+    result = personal.telephone(mask=mask)
+    head = result.split(' ')[0]
     assert head == '+5'
 
 
@@ -303,8 +303,8 @@ def test_gender(personal):
     result = personal.gender()
     assert result in personal.data['gender']
 
-    symbol = personal.gender(symbol=True)
-    assert symbol in GENDER_SYMBOLS
+    result = personal.gender(symbol=True)
+    assert result in GENDER_SYMBOLS
 
     # The four codes specified in ISO/IEC 5218 are:
     # 0 = not known, 1 = male, 2 = female, 9 = not applicable.
@@ -377,8 +377,8 @@ def test_title(personal, gender, title_type):
     assert result is not None
     assert isinstance(result, str)
 
-    result_by_gender = personal.title(gender=gender)
-    assert result_by_gender is not None
+    result = personal.title(gender=gender)
+    assert result is not None
 
     with pytest.raises(WrongArgument):
         personal.title(gender='other', title_type='religious')
diff --git a/tests/test_data/test_structured.py b/tests/test_data/test_structured.py
--- a/tests/test_data/test_structured.py
+++ b/tests/test_data/test_structured.py
@@ -58,6 +58,6 @@ def test_json(structured):
 
     # Recursive returns python object, not JSON and
     # maximum depth of three elements
-    r = structured.json(items=3, max_depth=4, recursive=True)
-    assert isinstance(r, (dict, list))
-    assert depth(r) <= 4
+    result = structured.json(items=3, max_depth=4, recursive=True)
+    assert isinstance(result, (dict, list))
+    assert depth(result) <= 4
diff --git a/tests/test_generic.py b/tests/test_generic.py
--- a/tests/test_generic.py
+++ b/tests/test_generic.py
@@ -56,36 +56,6 @@ def test_bad_argument(generic):
         _ = generic.bad_argument  # noqa
 
 
-def test_add_provider(generic):
-    class CustomProvider:
-        class Meta:
-            name = 'custom_provider'
-
-        @staticmethod
-        def say():
-            return 'Custom'
-
-        @staticmethod
-        def number():
-            return 1
-
-    generic.add_provider(CustomProvider)
-    assert generic.custom_provider.say() is not None
-    assert generic.custom_provider.number() == 1
-    with pytest.raises(TypeError):
-        generic.add_provider(True)
-
-    class UnnamedProvider(object):
-        @staticmethod
-        def nothing():
-            return None
-
-    generic.add_provider(UnnamedProvider)
-    assert generic.unnamedprovider.nothing() is None
-
-    assert 'unnamedprovider' == UnnamedProvider.__name__.lower()
-
-
 def test_add_providers(generic):
     class Provider1(object):
         @staticmethod
@@ -93,6 +63,9 @@ def one():
             return 1
 
     class Provider2(object):
+        class Meta:
+            name = 'custom_provider'
+
         @staticmethod
         def two():
             return 2
@@ -104,8 +77,16 @@ def three():
 
     generic.add_providers(Provider1, Provider2, Provider3)
     assert generic.provider1.one() == 1
-    assert generic.provider2.two() == 2
+    assert generic.custom_provider.two() == 2
     assert generic.provider3.three() == 3
 
     with pytest.raises(TypeError):
         generic.add_providers(True)
+
+    class UnnamedProvider(object):
+        @staticmethod
+        def nothing():
+            return None
+
+    generic.add_provider(UnnamedProvider)
+    assert generic.unnamedprovider.nothing() is None
diff --git a/tests/test_helpers.py b/tests/test_helpers.py
--- a/tests/test_helpers.py
+++ b/tests/test_helpers.py
@@ -14,11 +14,11 @@ def test_randints(random):
     # Length of default list is 3
     assert len(result) == 3
 
-    result_custom = random.randints(25, 1, 1)
+    result = random.randints(25, 1, 1)
 
-    assert len(result_custom) == 25
+    assert len(result) == 25
     # All elements in result_custom equals to 1.
-    assert result_custom[0] == 1 and result_custom[-1] == 1
+    assert result[0] == 1 and result[-1] == 1
 
 
 @pytest.mark.parametrize(
diff --git a/tests/test_utils.py b/tests/test_utils.py
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -1,7 +1,6 @@
 # -*- coding: utf-8 -*-
 
 import os
-import socket
 import sys
 
 import pytest
@@ -11,16 +10,6 @@
                            luhn_checksum, pull, update_dict)
 
 
-def is_connected():
-    try:
-        host = socket.gethostbyname('https://github.com/')
-        socket.create_connection((host, 80), 2)
-        return True
-    except:
-        pass
-    return False
-
-
 def test_luhn_checksum():
     assert luhn_checksum('7992739871') == '3'
 
@@ -45,14 +34,20 @@ def test_pull():
     assert 'Melbourne' in data['city']
 
 
-def test_download_image():
+def test_download_image(mocker):
     result = download_image(url=None)
     assert result is None
 
     url = 'https://github.com/lk-geimfari/mimesis/' \
           'raw/master/media/mimesis.png'
 
-    if is_connected():
+    def create_image(*args, **kwargs):
+        image_name = 'mimesis.png'
+        with open(image_name, 'w') as f:
+            f.write('testing image')
+
+    with mocker.patch('mimesis.utils.request.urlretrieve',
+                      side_effect=create_image):
         verified = download_image(url=url)
         assert verified == 'mimesis.png'
         os.remove(verified)