Skip to main content

A plugin to show lint errors for IW

Project description

flake8_iw

IW01: Use of patch

Lint check to prevent the use of patch directly. Recommendation: Use PatchingTestCase / PatchingTransactionTestCase instead

Correct

from instawork.tests import PatchingTestCase


class SignUpUpdatedTests(PatchingTestCase):
    def setUp(self):
        self.mock_call = self.patch("apps.auth.signals.task_send_email.delay")

    def test_email(self):
        expect(self.mock_call).to(have_been_called_once)

    def test_sms(self):
        mock_sms = self.patch("apps.auth.signals.task_send_sms.delay")
        expect(mock_sms).to(have_been_called_once)

Wrong

from unittest.mock import patch


class SignUpUpdatedTests(TestCase):
    def setUp(self):
        self.patcher = patch("apps.auth.signals.task_send_email.delay")
        self.mock_email = self.patcher.start()

    def tearDown(self):
        self.patcher.stop()

    def test_email(self):
        ...
        expect(self.mock_email).to(have_been_called_once)

    @patch("apps.auth.signals.task_send_sms.delay")
    def test_sms(self, mock_sms):
        ...
        expect(mock_sms).to(have_been_called_once)

IW02: Use of patch for time freeze

Lint check to prevent the use of patch to freeze time. Recommendation: Use freeze_time from PatchingTestCase / PatchingTransactionTestCase or use freeze_time decorator or context manager from freezegun package.

Correct

from django.utils import timezone
from instawork.tests import PatchingTestCase


class UserFeatureViewTests(PatchingTestCase):
    def setUp(self):
        self.now = timezone.now()

    def test_feature_view(self):
        ufv = None

        # Option 1
        with freeze_time(self.now):
            ufv = UserFeatureView.objects.get_or_create(
                user=self.shift.worker, feature=UserFeatureView.FEATURE_1
            )

        # Option 2
        self.freeze_time(self.now)
        ufv = UserFeatureView.objects.get_or_create(
            user=self.shift.worker, feature=UserFeatureView.FEATURE_1
        )

        ...

        expect(ufv.date_created).to(equal(self.now))

Wrong

from django.utils import timezone
from instawork.tests import PatchingTestCase


class UserFeatureViewTests(PatchingTestCase):
    def setUp(self):
        self.now = timezone.now()
        self.mock_call = self.patch("django.utils.timezone.now", return_value=self.now)

    def test_feature_view(self):
        ufv = UserFeatureView.objects.get_or_create(
            user=self.shift.worker, feature=UserFeatureView.FEATURE_1
        )

        ...

        expect(ufv.date_created).to(equal(self.now))

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flake8_iw-0.0.8.tar.gz (2.5 kB view details)

Uploaded Source

Built Distribution

flake8_iw-0.0.8-py2.py3-none-any.whl (2.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file flake8_iw-0.0.8.tar.gz.

File metadata

  • Download URL: flake8_iw-0.0.8.tar.gz
  • Upload date:
  • Size: 2.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.15

File hashes

Hashes for flake8_iw-0.0.8.tar.gz
Algorithm Hash digest
SHA256 a3cf38ca8a531a37910908a7250edcf5decb956f163ecd349314c186c52ddb33
MD5 69e31439de8bc3dc0c617ad98c6f190d
BLAKE2b-256 b08dd764ec8fc3145bc8fbf808e82ef21452099e732c3b5f745da6b66e0d913c

See more details on using hashes here.

File details

Details for the file flake8_iw-0.0.8-py2.py3-none-any.whl.

File metadata

  • Download URL: flake8_iw-0.0.8-py2.py3-none-any.whl
  • Upload date:
  • Size: 2.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.15

File hashes

Hashes for flake8_iw-0.0.8-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 fb8191062e72cfc7b83e7fe9e5fec9a049e6be3a9f7f2838b7cd139c11b41eeb
MD5 ef57422c76866495894105342ea1e7eb
BLAKE2b-256 0e8ab285ae4cb42e5638d6dd0d7d4afccf6d8a562db927e927747f86b5978638

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page