Configs can be saved in background using switches.

This commit is contained in:
Roland 2024-09-15 15:33:07 -06:00
parent 44d4921c84
commit d54735c4bb
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
53 changed files with 967 additions and 574 deletions

View file

@ -180,7 +180,7 @@ def save_inventories_file():
config.inventories = inventories
config.version = version
config.save()
config.background_save.activate_switch()
@before_startup

View file

@ -76,33 +76,33 @@ def test_item_api(test_session, init_test_inventory, test_client):
@pytest.mark.asyncio
async def test_inventory_api(test_session, test_async_client):
async def test_inventory_api(test_session, async_client):
"""An Inventory can be created."""
inventory = dict(name='foo')
request, response = await test_async_client.post('/api/inventory', content=json.dumps(inventory))
request, response = await async_client.post('/api/inventory', content=json.dumps(inventory))
assert response.status_code == HTTPStatus.CREATED, response.status_code
request, response = await test_async_client.get('/api/inventory')
request, response = await async_client.get('/api/inventory')
assert response.status_code == HTTPStatus.OK
inventory = response.json['inventories'][0]
inventory_id = inventory['id']
request, response = await test_async_client.get(f'/api/inventory/{inventory_id}')
request, response = await async_client.get(f'/api/inventory/{inventory_id}')
assert response.status_code == HTTPStatus.OK
request, response = await test_async_client.get('/api/inventory/categories')
request, response = await async_client.get('/api/inventory/categories')
assert response.status_code == HTTPStatus.OK
request, response = await test_async_client.get('/api/inventory/brands')
request, response = await async_client.get('/api/inventory/brands')
assert response.status_code == HTTPStatus.OK
inventory = dict(name='new name')
request, response = await test_async_client.put(f'/api/inventory/{inventory_id}', content=json.dumps(inventory))
request, response = await async_client.put(f'/api/inventory/{inventory_id}', content=json.dumps(inventory))
assert response.status_code == HTTPStatus.NO_CONTENT, response.status_code
request, response = await test_async_client.delete(f'/api/inventory/{inventory_id}')
request, response = await async_client.delete(f'/api/inventory/{inventory_id}')
assert response.status_code == HTTPStatus.NO_CONTENT, response.status_code
request, response = await test_async_client.get('/api/inventory')
request, response = await async_client.get('/api/inventory')
assert response.status_code == HTTPStatus.OK
assert len(response.json['inventories']) == 0

View file

@ -9,6 +9,7 @@ from pint import Quantity
from wrolpi.common import Base
from wrolpi.db import get_db_session
from wrolpi.switches import await_switches
from wrolpi.test.common import PytestCase
from .. import init
from ..common import sum_by_key, get_inventory_by_category, get_inventory_by_subcategory, get_inventory_by_name, \
@ -273,7 +274,8 @@ def test_no_inventories(test_session, test_directory):
pass
def test_inventories_version(test_async_client, test_session, test_directory, init_test_inventory):
@pytest.mark.asyncio
async def test_inventories_version(async_client, test_session, test_directory, init_test_inventory):
"""You can't save over a newer version of an inventory."""
config = get_inventories_config()
@ -284,10 +286,12 @@ def test_inventories_version(test_async_client, test_session, test_directory, in
# Version is set to 1 on first save.
save_inventories_file()
await await_switches()
assert config.version == 1
# Version is incremented when saving.
save_inventories_file()
await await_switches()
assert config.version == 2
# Version is greater than what will be saved.
@ -299,7 +303,7 @@ def test_inventories_version(test_async_client, test_session, test_directory, in
@pytest.mark.asyncio
async def test_inventories_config(test_async_client, test_session, test_directory, init_test_inventory):
async def test_inventories_config(async_client, test_session, test_directory, init_test_inventory):
for item in TEST_ITEMS:
item = Item(**item)
test_session.add(item)