Python Clean code tip

Python clean code tip:
Use contract testing when you want to verify the same behavior for different implementations.
👇
 
import json import pathlib from dataclass import dataclass import pytest @dataclass class User: username: str class InMemoryUserRepository: def __init__(self): self.users = [] def add(self, user): self.users.append(user) def get_by_username(self, username): return next(user for user in self._users if user.username == username) class JSONUserRepository: def __init__(self, filepath): self.users = json.load(pathlib.Path(file_path).open()) def add(self, user): self.users.append(user) def get_by_username(self, username): return next(user for user in self._users if user.username == username) class UserRepositoryContract: @pytest.fixture def repository(self): raise NotImplementedError('Not Implemented Yet.') @pytest.fixture def username(self): return 'Johndoe'
 
notion imagenotion image