Skip to content

Commit

Permalink
Merge pull request #47 from alphatwirl/dev
Browse files Browse the repository at this point in the history
Replace sys.modules with the import statement
  • Loading branch information
TaiSakuma authored May 26, 2024
2 parents 8f7edcd + e4c9224 commit cc5d640
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 54 deletions.
24 changes: 13 additions & 11 deletions tests/presentation/detect/test_is_jupyter_notebook.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from collections.abc import Iterator
import sys
import unittest.mock as mock
from collections.abc import Iterator

import pytest

Expand All @@ -9,22 +8,26 @@

@pytest.fixture()
def mock_widgets_module(monkeypatch: pytest.MonkeyPatch) -> Iterator[mock.Mock]:
from atpbar.presentation.detect import jupy

ret = mock.Mock()
module = sys.modules['atpbar.presentation.detect.jupy']
monkeypatch.setattr(module, 'widgets', ret)
monkeypatch.setattr(jupy, 'widgets', ret)
yield ret


@pytest.fixture()
def mock_display_module(monkeypatch: pytest.MonkeyPatch) -> Iterator[mock.Mock]:
from atpbar.presentation.detect import jupy

ret = mock.Mock()
module = sys.modules['atpbar.presentation.detect.jupy']
monkeypatch.setattr(module, 'display', ret)
monkeypatch.setattr(jupy, 'display', ret)
yield ret


@pytest.fixture()
def mock_get_ipython(monkeypatch: pytest.MonkeyPatch) -> Iterator[mock.Mock]:
from atpbar.presentation.detect import jupy

mock_ipython = mock.Mock()
mock_ipython.config = {
'IPKernelApp': {
Expand All @@ -33,17 +36,16 @@ def mock_get_ipython(monkeypatch: pytest.MonkeyPatch) -> Iterator[mock.Mock]:
}
ret = mock.Mock()
ret.return_value = mock_ipython

module = sys.modules['atpbar.presentation.detect.jupy']
monkeypatch.setattr(module, 'get_ipython', ret)
monkeypatch.setattr(jupy, 'get_ipython', ret)
yield ret


@pytest.fixture()
def mock_is_spyder_ide(monkeypatch: pytest.MonkeyPatch) -> Iterator[mock.Mock]:
from atpbar.presentation.detect import jupy

ret = mock.Mock()
module = sys.modules['atpbar.presentation.detect.jupy']
monkeypatch.setattr(module, 'is_spyder_ide', ret)
monkeypatch.setattr(jupy, 'is_spyder_ide', ret)
yield ret


Expand Down
11 changes: 6 additions & 5 deletions tests/presentation/detect/test_is_spyder_ide.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import sys
import unittest.mock as mock
from collections.abc import Iterator

Expand All @@ -10,14 +9,17 @@

@pytest.fixture()
def mock_spyder_module(monkeypatch: pytest.MonkeyPatch) -> Iterator[mock.Mock]:
from atpbar.presentation.detect import spy

ret = mock.Mock()
module = sys.modules['atpbar.presentation.detect.spy']
monkeypatch.setattr(module, 'spyder', ret)
monkeypatch.setattr(spy, 'spyder', ret)
yield ret


@pytest.fixture()
def mock_get_ipython(monkeypatch: pytest.MonkeyPatch) -> Iterator[mock.Mock]:
from atpbar.presentation.detect import spy

mock_ipython = mock.Mock()
mock_ipython.config = {
'IPKernelApp': {
Expand All @@ -27,8 +29,7 @@ def mock_get_ipython(monkeypatch: pytest.MonkeyPatch) -> Iterator[mock.Mock]:
ret = mock.Mock()
ret.return_value = mock_ipython

module = sys.modules['atpbar.presentation.detect.spy']
monkeypatch.setattr(module, 'get_ipython', ret)
monkeypatch.setattr(spy, 'get_ipython', ret)
yield ret


Expand Down
13 changes: 7 additions & 6 deletions tests/presentation/test_base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import sys
import time
import unittest.mock as mock
import uuid

Expand All @@ -15,11 +15,12 @@ def _present(self, report: Report) -> None:

@pytest.fixture()
def mock_time(monkeypatch: pytest.MonkeyPatch) -> mock.Mock:
ret = mock.Mock()
module = sys.modules['atpbar.presentation.base']
monkeypatch.setattr(module, 'time', ret)
ret.time.return_value = 1533374055.904203
return ret
from atpbar.presentation import base

m = mock.Mock(wraps=time)
m.time.return_value = 1533374055.904203
monkeypatch.setattr(base, 'time', m)
return m


@pytest.fixture()
Expand Down
34 changes: 17 additions & 17 deletions tests/presentation/test_create_presentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

has_jupyter_notebook = False
try:
import ipywidgets as widgets
from IPython.display import display
import ipywidgets as widgets # noqa: F401
from IPython.display import display # noqa: F401

has_jupyter_notebook = True
except ImportError:
Expand All @@ -17,16 +17,14 @@

@pytest.fixture(params=[True, False])
def isatty(request: pytest.FixtureRequest, monkeypatch: pytest.MonkeyPatch) -> bool:
from atpbar.presentation import create

ret = request.param
org_stdout = sys.stdout
f = mock.Mock(
**{
'stdout.isatty.return_value': ret,
'stdout.write.side_effect': lambda x: org_stdout.write(x),
}
)
module = sys.modules['atpbar.presentation.create']
monkeypatch.setattr(module, 'sys', f)
m = mock.Mock(wraps=sys)
m.stdout.isatty.return_value = ret
m.stdout.write.side_effect = lambda x: org_stdout.write(x)
monkeypatch.setattr(create, 'sys', m)
return ret


Expand All @@ -40,26 +38,28 @@ def isatty(request: pytest.FixtureRequest, monkeypatch: pytest.MonkeyPatch) -> b
def is_jupyter_notebook(
request: pytest.FixtureRequest, monkeypatch: pytest.MonkeyPatch
) -> bool:
from atpbar.presentation import create

ret = request.param
f = mock.Mock()
f.return_value = ret
module = sys.modules['atpbar.presentation.create']
monkeypatch.setattr(module, 'is_jupyter_notebook', f)
m = mock.Mock(wraps=create.is_jupyter_notebook)
m.return_value = ret
monkeypatch.setattr(create, 'is_jupyter_notebook', m)
return ret


@pytest.fixture(params=[True, False])
def del_ProgressBarJupyter(
request: pytest.FixtureRequest, monkeypatch: pytest.MonkeyPatch
) -> bool:
from atpbar.presentation import create

ret = request.param
module = sys.modules['atpbar.presentation.create']
if ret:
monkeypatch.delattr(module, 'ProgressBarJupyter', raising=False)
monkeypatch.delattr(create, 'ProgressBarJupyter', raising=False)
else:
m = mock.Mock()
m().__class__.__name__ = 'ProgressBarJupyter'
monkeypatch.setattr(module, 'ProgressBarJupyter', m, raising=False)
monkeypatch.setattr(create, 'ProgressBarJupyter', m, raising=False)
return ret


Expand Down
11 changes: 6 additions & 5 deletions tests/progress_report/test_reporter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import sys
import time
import unittest.mock as mock
import uuid
from typing import Any
Expand All @@ -15,10 +15,11 @@ def mock_queue() -> mock.Mock:

@pytest.fixture()
def mock_time(monkeypatch: pytest.MonkeyPatch) -> mock.Mock:
ret = mock.Mock()
module = sys.modules['atpbar.progress_report.reporter']
monkeypatch.setattr(module, 'time', ret)
return ret
from atpbar.progress_report import reporter

m = mock.Mock(wraps=time)
monkeypatch.setattr(reporter, 'time', m)
return m


@pytest.fixture()
Expand Down
24 changes: 14 additions & 10 deletions tests/scenarios/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import sys
from collections.abc import Iterator
from uuid import UUID

Expand Down Expand Up @@ -66,23 +65,28 @@ def __call__(self) -> MockProgressBar:

@pytest.fixture(autouse=True)
def mock_create_presentation(monkeypatch: pytest.MonkeyPatch) -> MockCreatePresentation:
from atpbar import machine

ret = MockCreatePresentation()
module = sys.modules['atpbar.machine']
monkeypatch.setattr(module, 'create_presentation', ret)
monkeypatch.setattr(machine, 'create_presentation', ret)
return ret


@pytest.fixture(autouse=True)
def machine(monkeypatch: pytest.MonkeyPatch) -> Iterator[None]:
module = sys.modules['atpbar.funcs']
y = module.StateMachine()
monkeypatch.setattr(module, '_machine', y)
yield
shutdown()
from atpbar import funcs

y = funcs.StateMachine()
monkeypatch.setattr(funcs, '_machine', y)
try:
yield
finally:
shutdown()


@pytest.fixture(autouse=True)
def reporter_interval(monkeypatch: pytest.MonkeyPatch) -> Iterator[None]:
module = sys.modules['atpbar.progress_report.reporter']
monkeypatch.setattr(module, 'DEFAULT_INTERVAL', 0)
from atpbar.progress_report import reporter

monkeypatch.setattr(reporter, 'DEFAULT_INTERVAL', 0)
yield

0 comments on commit cc5d640

Please sign in to comment.