Skip to content

Commit

Permalink
Use stdlib unittest mock.
Browse files Browse the repository at this point in the history
  • Loading branch information
akornatskyy committed Jan 28, 2021
1 parent 0589d3b commit 015affd
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 144 deletions.
1 change: 0 additions & 1 deletion requirements/dev.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
mock
pytest
pytest-cov
pip-tools
1 change: 0 additions & 1 deletion requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ click==7.1.2 # via pip-tools
colorama==0.4.4 # via pytest
coverage==5.3 # via pytest-cov
iniconfig==1.1.1 # via pytest
mock==4.0.3 # via -r requirements/dev.in
packaging==20.8 # via pytest
pip-tools==5.4.0 # via -r requirements/dev.in
pluggy==0.13.1 # via pytest
Expand Down
15 changes: 2 additions & 13 deletions src/wheezy/core/tests/test_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
"""

import unittest
from unittest.mock import Mock, PropertyMock

from mock import Mock
from wheezy.core.benchmark import Benchmark, Timer


class BenchmarkTestCase(unittest.TestCase):
def test_run(self):
"""Ensure targets are called."""
from wheezy.core.benchmark import Benchmark

t1 = Mock()
t1.__name__ = "t1"
t2 = Mock()
Expand All @@ -29,10 +28,6 @@ def test_run(self):

def test_run_timer(self):
"""Ensure timer is used."""
from mock import PropertyMock

from wheezy.core.benchmark import Benchmark

t1 = Mock()
t1.__name__ = "t1"
mock_timer = Mock()
Expand All @@ -51,8 +46,6 @@ def test_run_timer(self):

def test_zero_division_error(self):
"""ZeroDivisionError is not raised when timing is 0."""
from wheezy.core.benchmark import Benchmark

t1 = Mock()
t1.__name__ = "t1"
mock_timer = Mock()
Expand All @@ -62,8 +55,6 @@ def test_zero_division_error(self):

def test_report(self):
"""Ensure report is printed."""
from wheezy.core.benchmark import Benchmark

t1 = Mock()
t1.__name__ = "t1"
mock_timer = Mock()
Expand All @@ -75,8 +66,6 @@ def test_report(self):
class TimerTestCase(unittest.TestCase):
def test_start_stop(self):
"""Ensure a call is intercepted."""
from wheezy.core.benchmark import Timer

mock_target = Mock()
mock_name = Mock()
mock_target.name = mock_name
Expand Down
31 changes: 9 additions & 22 deletions src/wheezy/core/tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@
"""

import unittest
import warnings
from unittest.mock import Mock

from mock import Mock
from wheezy.core.db import ( # isort:skip
NullSession,
NullTPCSession,
SESSION_STATUS_ACTIVE,
Session,
TPCSession,
)


class SessionTestCase(unittest.TestCase):
def setUp(self):
from wheezy.core.db import Session

self.mock_pool = Mock()
self.session = Session(self.mock_pool)

Expand All @@ -32,7 +38,6 @@ def test_connection(self):

def test_on_active(self):
"""Ensure on_active is called once."""
from wheezy.core.db import Session

class MockSession(Session):
pass
Expand Down Expand Up @@ -121,8 +126,6 @@ def test_exit_connection_error(self):

class TPCSessionTestCase(unittest.TestCase):
def setUp(self):
from wheezy.core.db import TPCSession

self.mock_pool = Mock()
self.session = TPCSession(self.mock_pool)

Expand Down Expand Up @@ -170,8 +173,6 @@ def test_commit_no_enlisted(self):

def test_commit_prepare_error(self):
"""An error is raised while working with connection."""
from wheezy.core.db import SESSION_STATUS_ACTIVE

self.session.__enter__()
session = Mock()
session.__enter__ = Mock()
Expand All @@ -186,8 +187,6 @@ def test_commit_prepare_error(self):

def test_commit_error(self):
"""An error is raised while working with connection."""
from wheezy.core.db import SESSION_STATUS_ACTIVE

self.session.__enter__()
session = Mock()
session.__enter__ = Mock()
Expand All @@ -202,8 +201,6 @@ def test_commit_error(self):

def test_commit(self):
"""Enlisted sessions are exited."""
from wheezy.core.db import SESSION_STATUS_ACTIVE

self.session.__enter__()
session = Mock()
session.__enter__ = Mock()
Expand Down Expand Up @@ -234,8 +231,6 @@ def test_exit_no_active(self):

def test_exit_active(self):
"""There are active sessions enlisted."""
from wheezy.core.db import SESSION_STATUS_ACTIVE

self.session.__enter__()
session = Mock()
session.__enter__ = Mock()
Expand All @@ -250,10 +245,6 @@ def test_exit_active_on_error(self):
"""There are active sessions enlisted and error is raised
while working with connection.
"""
import warnings

from wheezy.core.db import SESSION_STATUS_ACTIVE

self.session.__enter__()
session = Mock()
session.__enter__ = Mock()
Expand All @@ -270,8 +261,6 @@ def test_exit_active_on_error(self):

class NullSessionTestCase(unittest.TestCase):
def setUp(self):
from wheezy.core.db import NullSession

self.session = NullSession()

def test_enter(self):
Expand Down Expand Up @@ -314,8 +303,6 @@ def test_exit(self):

class NullTPCSessionTestCase(unittest.TestCase):
def setUp(self):
from wheezy.core.db import NullTPCSession

self.session = NullTPCSession()

def test_enter(self):
Expand Down
4 changes: 2 additions & 2 deletions src/wheezy/core/tests/test_gzip.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

import unittest

from wheezy.core.gzip import compress, decompress


class GzipTestCase(unittest.TestCase):
def test_compress_decompress(self):
"""Ensure decompress is a reverse function of compress."""
from wheezy.core.gzip import compress, decompress

c = compress("test".encode("utf-8"))
assert "test" == decompress(c).decode("utf-8")
10 changes: 3 additions & 7 deletions src/wheezy/core/tests/test_httpclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
"""

import unittest
from unittest.mock import Mock, patch

from mock import Mock, patch
from wheezy.core import __version__, httpclient
from wheezy.core.gzip import compress


class HTTPClientTestCase(unittest.TestCase):
def setUp(self):
from wheezy.core import __version__, httpclient

self.patcher = patch.object(httpclient, "HTTPConnection")
self.mock_c_class = self.patcher.start()
self.headers = [("date", "Sat, 12 Oct 2013 18:29:13 GMT")]
Expand Down Expand Up @@ -117,8 +117,6 @@ def test_assert_json(self):

def test_json(self):
"""json response."""
from wheezy.core import httpclient

patcher = patch.object(httpclient, "json_loads")
mock_json_loads = patcher.start()
mock_json_loads.return_value = {}
Expand All @@ -132,8 +130,6 @@ def test_json(self):

def test_gzip(self):
"""Ensure gzip decompression."""
from wheezy.core.gzip import compress

self.headers.append(("content-encoding", "gzip"))
self.mock_response.read.return_value = compress("test".encode("utf-8"))
self.client.get("auth/token")
Expand Down
15 changes: 3 additions & 12 deletions src/wheezy/core/tests/test_introspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

import unittest

from wheezy.core.descriptors import attribute
from wheezy.core.introspection import looks


class LooksLike(object):
def assert_warning(self, msg):
Expand All @@ -12,7 +15,6 @@ def assert_warning(self, msg):

def test_func(self):
"""Tests if there is any function missing."""
from wheezy.core.introspection import looks

class IFoo(object):
def foo(self):
Expand All @@ -27,7 +29,6 @@ def bar(self):

def test_ignore_func(self):
"""Tests if function is ignored."""
from wheezy.core.introspection import looks

class IFoo(object):
def foo(self):
Expand All @@ -41,7 +42,6 @@ def bar(self):

def test_redundant_ignore(self):
"""Tests function is set to be ignored but it is not found."""
from wheezy.core.introspection import looks

class IFoo(object):
def bar(self):
Expand All @@ -56,7 +56,6 @@ def bar(self):

def test_args(self):
"""Tests if there any function args corresponds."""
from wheezy.core.introspection import looks

class IFoo(object):
def foo(self, a, b=1):
Expand All @@ -73,7 +72,6 @@ def foo(self, a, b):

def test_ignore_args(self):
"""Tests if function args ignored."""
from wheezy.core.introspection import looks

class IFoo(object):
def foo(self, a, b=1):
Expand All @@ -87,7 +85,6 @@ def foo(self, a, b):

def test_property(self):
"""Tests if there any @property corresponds."""
from wheezy.core.introspection import looks

class IFoo(object):
@property
Expand All @@ -103,7 +100,6 @@ def foo(self):

def test_inheritance(self):
"""Test inheritance use case."""
from wheezy.core.introspection import looks

class IFoo(object):
def foo(self):
Expand All @@ -125,7 +121,6 @@ def bar(self):

def test_special_method(self):
"""Test if __?__ are checked"""
from wheezy.core.introspection import looks

class IFoo(object):
def __len__(self):
Expand All @@ -147,7 +142,6 @@ def __len__(self):

def test_decorator(self):
"""Decorator argspec doesn't match."""
from wheezy.core.introspection import looks

def bar():
def decorate(m):
Expand All @@ -174,8 +168,6 @@ def foo(self, a):

def test_type(self):
"""Test if decorator types do not match."""
from wheezy.core.descriptors import attribute
from wheezy.core.introspection import looks

class IFoo(object):
@attribute
Expand All @@ -192,7 +184,6 @@ def foo(self):

def test_various(self):
"""Tests if there are no errors."""
from wheezy.core.introspection import looks

class IFoo(object):
def foo(self, a, b=1):
Expand Down
Loading

0 comments on commit 015affd

Please sign in to comment.