feat: 新增技能扩展N11一章相关示例源码
This commit is contained in:
79
code/newsletter/N11/tests/conftest.py
Normal file
79
code/newsletter/N11/tests/conftest.py
Normal file
@@ -0,0 +1,79 @@
|
||||
import dataclasses
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class Connection:
|
||||
def execute(self, sql: str):
|
||||
print(f"execute {sql}...")
|
||||
return True
|
||||
|
||||
def close(self):
|
||||
print(f"\ndisconnect {repr(self)} object...")
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class Database:
|
||||
dialect: str = "sqlite"
|
||||
|
||||
def connect(self):
|
||||
return Connection()
|
||||
|
||||
def close(self):
|
||||
print(f"\ndisconnect {repr(self)} object...")
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def sqlite():
|
||||
db = Database()
|
||||
conn = db.connect()
|
||||
try:
|
||||
yield conn
|
||||
finally:
|
||||
conn.close()
|
||||
db.close()
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def mysql():
|
||||
db = Database(dialect="mysql")
|
||||
conn = db.connect()
|
||||
try:
|
||||
yield conn
|
||||
finally:
|
||||
conn.close()
|
||||
db.close()
|
||||
|
||||
|
||||
@pytest.fixture(params=["sqlite", "mysql"])
|
||||
def database(request):
|
||||
db = Database(dialect=request.param)
|
||||
conn = db.connect()
|
||||
try:
|
||||
yield conn
|
||||
finally:
|
||||
conn.close()
|
||||
db.close()
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def gender():
|
||||
return ["male", "female"]
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def cpu():
|
||||
return "cpu"
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def device(cpu):
|
||||
return f"the device with {cpu}"
|
||||
|
||||
|
||||
@pytest.fixture(scope="class")
|
||||
def logger():
|
||||
print("\nstart recording...")
|
||||
yield
|
||||
print("\nend recording...")
|
||||
Reference in New Issue
Block a user