generated from ApeWorX/project-template
-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(runner): add BacktestRunner w/
silverback test
command
- Loading branch information
Showing
6 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import asyncio | ||
import os | ||
from pathlib import Path | ||
|
||
import pytest | ||
import yaml # type: ignore[import] | ||
from ape.utils import cached_property | ||
from uvicorn.importer import import_from_string | ||
|
||
from silverback.exceptions import SilverbackException | ||
from silverback.runner import BacktestRunner | ||
|
||
|
||
class AssertionViolation(SilverbackException): | ||
pass | ||
|
||
|
||
def pytest_collect_file(parent, file_path): | ||
if file_path.suffix == ".yaml" and file_path.name.startswith("backtest"): | ||
return BacktestFile.from_parent(parent, path=file_path) | ||
|
||
|
||
class BacktestFile(pytest.File): | ||
def collect(self): | ||
raw = yaml.safe_load(self.path.open()) | ||
yield BacktestItem.from_parent( | ||
self, | ||
name=self.name, | ||
file_path=self.path, | ||
app_path=raw.get("app", os.environ.get("SILVERBACK_APP")), | ||
network_triple=raw.get("network", ""), | ||
start_block=raw.get("start_block", 0), | ||
stop_block=raw.get("stop_block", -1), | ||
assertion_checks=raw.get("assertions", {}), | ||
) | ||
|
||
|
||
class BacktestItem(pytest.Item): | ||
def __init__( | ||
self, | ||
*, | ||
file_path, | ||
app_path, | ||
network_triple, | ||
start_block, | ||
stop_block, | ||
assertion_checks, | ||
**kwargs, | ||
): | ||
super().__init__(**kwargs) | ||
self.file_path = file_path | ||
self.app_path = app_path or "app.py:app" | ||
self.network_triple = network_triple | ||
self.start_block = start_block | ||
self.stop_block = stop_block | ||
self.assertion_checks = assertion_checks | ||
|
||
self.assertion_failures = 0 | ||
self.overruns = 0 | ||
|
||
@cached_property | ||
def runner(self): | ||
app_path, app_name = self.app_path.split(":") | ||
app_path = Path(app_path) | ||
os.environ["SILVERBACK_NETWORK_CHOICE"] = self.network_triple | ||
os.environ["PYTHONPATH"] = str(app_path.parent) | ||
app = import_from_string(f"{app_path.stem}:{app_name}") | ||
return BacktestRunner(app, start_block=self.start_block, stop_block=self.stop_block) | ||
|
||
def check_assertions(self, result: dict): | ||
pass | ||
|
||
def runtest(self): | ||
asyncio.run(self.runner.run()) | ||
self.raise_run_status() | ||
|
||
def raise_run_status(self): | ||
if self.overruns > 0 or self.assertion_failures > 0: | ||
raise AssertionViolation() |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
bot: "example" | ||
network: "ethereum:mainnet-fork" | ||
start_block: 15_338_009 | ||
stop_block: 15_338_018 | ||
something_else: blah |