Skip to content

Instantly share code, notes, and snippets.

@briggleman
Last active January 9, 2024 19:05
Show Gist options
  • Save briggleman/eadab91f790f7aa74568e650fad19eab to your computer and use it in GitHub Desktop.
Save briggleman/eadab91f790f7aa74568e650fad19eab to your computer and use it in GitHub Desktop.
Mocking a PeeWee model: To mock, you must chain all of the calls and set a return value for the last call
from unittest import mock
from box import Box
@pytest.fixture(autouse=True)
def cursor(records):
_cursor = []
for record in records:
_cursor.append(Box(record))
return _cursor
@pytest.fixture(autouse=True)
def items(cursor):
with mock.patch("spd.lib.calculate.Item") as mocked:
mocked.select().join().where().order_by.return_value = cursor
yield mocked
@pytest.fixture(autouse=True)
def item_attributes():
with mock.patch("spd.lib.price_deciles.ItemAttributes") as mocked:
mocked.insert().on_conflict().execute.return_value = True
yield mocked
@pytest.fixture(autouse=True)
def records():
return [
{
"retailer_id": "dc405c7e62e86c987e4ec3da96357f9f",
"item_id": "115681",
"cat_name": "bed",
"itemattributes": {"price": "110.00"}
},
{
"retailer_id": "dc405c7e62e86c987e4ec3da96357f9f",
"item_id": "115682",
"cat_name": "bed",
"itemattributes": {"price": "120.00"}
},
{
"retailer_id": "dc405c7e62e86c987e4ec3da96357f9f",
"item_id": "115683",
"cat_name": "bed",
"itemattributes": {"price": "130.00"}
},
{
"retailer_id": "dc405c7e62e86c987e4ec3da96357f9f",
"item_id": "115684",
"cat_name": "bed",
"itemattributes": {"price": "140.00"}
},
{
"retailer_id": "dc405c7e62e86c987e4ec3da96357f9f",
"item_id": "115685",
"cat_name": "bed",
"itemattributes": {"price": "150.00"}
},
{
"retailer_id": "dc405c7e62e86c987e4ec3da96357f9f",
"item_id": "115686",
"cat_name": "bed",
"itemattributes": {"price": "160.00"}
},
{
"retailer_id": "dc405c7e62e86c987e4ec3da96357f9f",
"item_id": "115687",
"cat_name": "bed",
"itemattributes": {"price": "170.00"}
},
{
"retailer_id": "dc405c7e62e86c987e4ec3da96357f9f",
"item_id": "115688",
"cat_name": "bed",
"itemattributes": {"price": "180.00"}
},
{
"retailer_id": "dc405c7e62e86c987e4ec3da96357f9f",
"item_id": "115689",
"cat_name": "bed",
"itemattributes": {"price": "190.00"}
},
{
"retailer_id": "dc405c7e62e86c987e4ec3da96357f9f",
"item_id": "115690",
"cat_name": "bed",
"itemattributes": {"price": "200.00"}
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment