This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import pytest | |
REDIRECT_REGEX = r"(^/([A-Za-z0-9#&?=]+|$)|^https://my.domain\.com/[A-Za-z0-9#&?=/]*$)" | |
def is_valid_success_url(url: str) -> bool: | |
return bool(re.match(REDIRECT_REGEX, url)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from unittest.mock import MagicMock, create_autospec | |
class MyCRMService: | |
def sync_user(self, *, first_name: str, last_name: str, email: str) -> None: | |
print("Calling MyCRM's REST API to send data there. That's slow and unreliable.") | |
class CreateUser: | |
def __init__(self, *, crm_service: MyCRMService) -> None: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from unittest.mock import MagicMock, create_autospec | |
class SentimentClassifier: | |
def predict(self, text: str) -> str: | |
# Imagine that prediction is taking a long time | |
time.sleep(5) | |
return "positive" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pytest | |
from email_validator import is_valid_email | |
@pytest.mark.parametrize( | |
"email,is_valid", | |
[ | |
("[email protected]", True), | |
("gqehtjwyeumi,", False), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def is_valid_email(email: str) -> bool: | |
return bool(re.search(r"^[\w\.\-\+']+@[\w\.\-]+\.\w+$", email)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from dataclasses import dataclass | |
from enum import Enum | |
class TaskStatus(str, Enum): | |
OPEN = "OPEN" | |
CLOSED = "CLOSED" | |
@dataclass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3 | |
import pytest | |
from models import Task, TaskStatus | |
from store import TaskStoreSQLite | |
@pytest.fixture | |
def connection(tmp_path) -> sqlite3.Connection: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from dataclasses import dataclass | |
from enum import Enum | |
class TaskStatus(str, Enum): | |
OPEN = "OPEN" | |
CLOSED = "CLOSED" | |
@dataclass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# behavior/tests.py | |
def test_add_task_(): | |
store = TaskStoreInMemory() | |
task = Task(title="Do the dishes", status=TaskStatus.OPEN) | |
store.add_task(task) | |
assert store.tasks[0].title == "Do the dishes" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# behavior/store.py | |
class TaskStoreInMemory(TaskStore): | |
def __init__(self): | |
self._tasks = [] | |
def add_task(self, task: Task) -> None: | |
self._tasks.append(task) | |
def list_tasks(self) -> list[Task]: |
NewerOlder