mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-25 17:36:18 -05:00
Testing that nearly-empty configs are ignored on import. Adding setting to disable automatic upgrade checking.
This commit is contained in:
parent
3e3ab04f4d
commit
5b29144d70
16 changed files with 1180 additions and 13 deletions
|
|
@ -296,6 +296,7 @@ export async function getSettings() {
|
|||
hotspot_password: content.hotspot_password,
|
||||
hotspot_ssid: content.hotspot_ssid,
|
||||
hotspot_status: content.hotspot_status,
|
||||
check_for_upgrades: content.check_for_upgrades,
|
||||
ignore_outdated_zims: content.ignore_outdated_zims,
|
||||
ignored_directories: content.ignored_directories,
|
||||
log_level: content.log_level,
|
||||
|
|
|
|||
|
|
@ -186,9 +186,9 @@ export function NavBar() {
|
|||
/>
|
||||
}
|
||||
|
||||
// Upgrade available notification - only show on native (non-Docker) installs
|
||||
// Upgrade available notification - only show on native (non-Docker) installs and when WROL Mode is disabled
|
||||
let upgradeIcon;
|
||||
if (status?.update_available && !status?.dockerized) {
|
||||
if (status?.update_available && !status?.dockerized && !wrolModeEnabled) {
|
||||
const commitsBehind = status.commits_behind || 0;
|
||||
const branch = status.git_branch || 'unknown';
|
||||
const icon = <Link to='/admin/settings'>
|
||||
|
|
|
|||
|
|
@ -318,6 +318,7 @@ export function SettingsPage() {
|
|||
hotspot_on_startup: settings.hotspot_on_startup,
|
||||
hotspot_password: settings.hotspot_password,
|
||||
hotspot_ssid: settings.hotspot_ssid,
|
||||
check_for_upgrades: settings.check_for_upgrades,
|
||||
ignore_outdated_zims: settings.ignore_outdated_zims,
|
||||
log_level: fromApiLogLevel(settings.log_level),
|
||||
map_destination: settings.map_destination,
|
||||
|
|
@ -398,6 +399,15 @@ export function SettingsPage() {
|
|||
/>
|
||||
</div>
|
||||
|
||||
<div style={{margin: '0.5em'}}>
|
||||
<Toggle
|
||||
label='Check for upgrades hourly'
|
||||
disabled={disabled || state.check_for_upgrades === null}
|
||||
checked={state.check_for_upgrades === true}
|
||||
onChange={checked => handleInputChange(null, 'check_for_upgrades', checked)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<FormGroup inline>
|
||||
<FormInput
|
||||
label={<>
|
||||
|
|
|
|||
4
main.py
4
main.py
|
|
@ -379,6 +379,10 @@ async def perpetual_check_for_updates():
|
|||
if DOCKERIZED:
|
||||
return
|
||||
|
||||
# Don't check if setting is disabled
|
||||
if not get_wrolpi_config().check_for_upgrades:
|
||||
return
|
||||
|
||||
# Only check when we have internet
|
||||
if not flags.have_internet.is_set():
|
||||
return
|
||||
|
|
|
|||
|
|
@ -88,13 +88,22 @@ class DomainsConfig(ConfigFile):
|
|||
|
||||
def import_config(self, file: pathlib.Path = None, send_events=False):
|
||||
"""Import domain collections from config file into database using batch operations."""
|
||||
file = file or self.get_file()
|
||||
file_str = str(get_relative_to_media_directory(file))
|
||||
|
||||
# If config file doesn't exist, mark import as successful (nothing to import)
|
||||
if not file.is_file():
|
||||
logger.info('No domains config file, skipping import')
|
||||
self.successful_import = True
|
||||
return
|
||||
|
||||
ConfigFile.import_config(self, file, send_events)
|
||||
|
||||
file_str = str(self.get_relative_file())
|
||||
collections_data = self._config.get('collections', [])
|
||||
|
||||
# Empty collections list = never delete DB records
|
||||
if not collections_data:
|
||||
logger.info(f'No domain collections to import from {file_str}')
|
||||
logger.info(f'No domain collections in config, preserving existing DB domain collections')
|
||||
self.successful_import = True
|
||||
return
|
||||
|
||||
|
|
|
|||
290
modules/archive/test/test_domains_config.py
Normal file
290
modules/archive/test/test_domains_config.py
Normal file
|
|
@ -0,0 +1,290 @@
|
|||
"""
|
||||
Tests for DomainsConfig import/upgrade scenarios.
|
||||
|
||||
Tests cover:
|
||||
1. Onboarding (new install) - no config file exists
|
||||
2. Upgrade - config file exists with data
|
||||
3. Edge cases - empty config, missing config with DB data
|
||||
"""
|
||||
import pytest
|
||||
import yaml
|
||||
|
||||
from modules.archive.lib import get_domains_config
|
||||
from wrolpi.collections import Collection
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
class TestDomainsConfigOnboarding:
|
||||
"""Tests for new install scenarios where no config file exists."""
|
||||
|
||||
async def test_new_install_no_db_data(self, test_session, test_directory, async_client):
|
||||
"""
|
||||
New install with empty DB and no config file.
|
||||
Should succeed with empty config.
|
||||
"""
|
||||
config = get_domains_config()
|
||||
config_path = config.get_file()
|
||||
|
||||
# Ensure no config file exists
|
||||
if config_path.is_file():
|
||||
config_path.unlink()
|
||||
|
||||
# Ensure DB has no domain collections
|
||||
assert test_session.query(Collection).filter_by(kind='domain').count() == 0
|
||||
|
||||
# Import should succeed
|
||||
config.import_config()
|
||||
assert config.successful_import is True
|
||||
|
||||
async def test_new_install_with_db_data(self, test_session, test_directory, async_client):
|
||||
"""
|
||||
Config file missing but DB has data.
|
||||
Import should succeed (nothing to import), DB data preserved.
|
||||
"""
|
||||
config = get_domains_config()
|
||||
config_path = config.get_file()
|
||||
|
||||
# Create a domain collection in DB
|
||||
collection = Collection(
|
||||
name='example.com',
|
||||
kind='domain',
|
||||
directory=None,
|
||||
)
|
||||
test_session.add(collection)
|
||||
test_session.commit()
|
||||
|
||||
# Remove config file to simulate missing config
|
||||
if config_path.is_file():
|
||||
config_path.unlink()
|
||||
|
||||
# Import should succeed (nothing to import)
|
||||
config.import_config()
|
||||
assert config.successful_import is True
|
||||
|
||||
# DB data should be preserved
|
||||
collections = test_session.query(Collection).filter_by(kind='domain').all()
|
||||
assert len(collections) == 1
|
||||
assert collections[0].name == 'example.com'
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
class TestDomainsConfigUpgrade:
|
||||
"""Tests for upgrade scenarios where config file exists."""
|
||||
|
||||
async def test_upgrade_config_exists_with_data(self, test_session, test_directory, async_client):
|
||||
"""
|
||||
Upgrade with existing config containing collection data.
|
||||
Should import and sync DB with config.
|
||||
"""
|
||||
config = get_domains_config()
|
||||
config_path = config.get_file()
|
||||
|
||||
# Create a collection in DB
|
||||
collection = Collection(
|
||||
name='test.org',
|
||||
kind='domain',
|
||||
directory=None,
|
||||
)
|
||||
test_session.add(collection)
|
||||
test_session.commit()
|
||||
|
||||
# Dump to create config file
|
||||
config.dump_config()
|
||||
assert config_path.is_file()
|
||||
|
||||
# Now import should succeed and preserve the collection
|
||||
config.import_config()
|
||||
assert config.successful_import is True
|
||||
|
||||
# Collection should still exist
|
||||
collections = test_session.query(Collection).filter_by(kind='domain').all()
|
||||
assert len(collections) == 1
|
||||
assert collections[0].name == 'test.org'
|
||||
|
||||
async def test_upgrade_config_exists_empty_list(self, test_session, test_directory, async_client):
|
||||
"""
|
||||
Config exists but has empty collections list.
|
||||
Should NOT delete existing DB collections (never delete on empty).
|
||||
"""
|
||||
config = get_domains_config()
|
||||
config_path = config.get_file()
|
||||
|
||||
# Create a collection in DB
|
||||
collection = Collection(
|
||||
name='preserve.me',
|
||||
kind='domain',
|
||||
directory=None,
|
||||
)
|
||||
test_session.add(collection)
|
||||
test_session.commit()
|
||||
|
||||
# Write empty config
|
||||
config_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with open(config_path, 'w') as f:
|
||||
yaml.dump({'version': 0, 'collections': []}, f)
|
||||
|
||||
# Reinitialize config to read the empty file
|
||||
config.initialize()
|
||||
|
||||
# Import should succeed but NOT delete the collection
|
||||
config.import_config()
|
||||
assert config.successful_import is True
|
||||
|
||||
# Collection should still exist (never delete on empty config)
|
||||
collections = test_session.query(Collection).filter_by(kind='domain').all()
|
||||
assert len(collections) == 1
|
||||
assert collections[0].name == 'preserve.me'
|
||||
|
||||
async def test_upgrade_config_missing_db_has_data(self, test_session, test_directory, async_client):
|
||||
"""
|
||||
Config file missing but DB has domain collections.
|
||||
Import should succeed (nothing to import), DB data preserved.
|
||||
"""
|
||||
config = get_domains_config()
|
||||
config_path = config.get_file()
|
||||
|
||||
# Create collections in DB
|
||||
for name in ['domain1.com', 'domain2.org']:
|
||||
collection = Collection(
|
||||
name=name,
|
||||
kind='domain',
|
||||
directory=None,
|
||||
)
|
||||
test_session.add(collection)
|
||||
test_session.commit()
|
||||
|
||||
# Remove config file
|
||||
if config_path.is_file():
|
||||
config_path.unlink()
|
||||
|
||||
# Import should succeed (nothing to import)
|
||||
config.import_config()
|
||||
assert config.successful_import is True
|
||||
|
||||
# DB data should be preserved
|
||||
collections = test_session.query(Collection).filter_by(kind='domain').all()
|
||||
assert len(collections) == 2
|
||||
collection_names = {c.name for c in collections}
|
||||
assert 'domain1.com' in collection_names
|
||||
assert 'domain2.org' in collection_names
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
class TestDomainsConfigEdgeCases:
|
||||
"""Tests for edge cases in config import."""
|
||||
|
||||
async def test_config_deletes_removed_items(self, test_session, test_directory, async_client):
|
||||
"""
|
||||
Config removes a collection that exists in DB.
|
||||
Should delete the collection from DB.
|
||||
"""
|
||||
config = get_domains_config()
|
||||
config_path = config.get_file()
|
||||
|
||||
# Create two collections
|
||||
for name in ['keep.this', 'delete.this']:
|
||||
collection = Collection(
|
||||
name=name,
|
||||
kind='domain',
|
||||
directory=None,
|
||||
)
|
||||
test_session.add(collection)
|
||||
test_session.commit()
|
||||
|
||||
# Dump to config
|
||||
config.dump_config()
|
||||
|
||||
# Modify config to remove one collection
|
||||
with open(config_path) as f:
|
||||
config_data = yaml.safe_load(f)
|
||||
|
||||
config_data['collections'] = [c for c in config_data['collections'] if c['name'] == 'keep.this']
|
||||
|
||||
with open(config_path, 'w') as f:
|
||||
yaml.dump(config_data, f)
|
||||
|
||||
# Reinitialize and import
|
||||
config.initialize()
|
||||
config.import_config()
|
||||
assert config.successful_import is True
|
||||
|
||||
# Only one collection should remain
|
||||
collections = test_session.query(Collection).filter_by(kind='domain').all()
|
||||
assert len(collections) == 1
|
||||
assert collections[0].name == 'keep.this'
|
||||
|
||||
async def test_config_adds_new_items(self, test_session, test_directory, async_client):
|
||||
"""
|
||||
Config has a new collection that doesn't exist in DB.
|
||||
Should create the collection in DB.
|
||||
"""
|
||||
config = get_domains_config()
|
||||
config_path = config.get_file()
|
||||
|
||||
# Ensure no collections in DB
|
||||
assert test_session.query(Collection).filter_by(kind='domain').count() == 0
|
||||
|
||||
# Write config with a new collection
|
||||
config_data = {
|
||||
'version': 0,
|
||||
'collections': [{
|
||||
'name': 'new.domain',
|
||||
'kind': 'domain',
|
||||
}]
|
||||
}
|
||||
|
||||
config_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with open(config_path, 'w') as f:
|
||||
yaml.dump(config_data, f)
|
||||
|
||||
# Reinitialize and import
|
||||
config.initialize()
|
||||
config.import_config()
|
||||
assert config.successful_import is True
|
||||
|
||||
# Collection should be created
|
||||
collections = test_session.query(Collection).filter_by(kind='domain').all()
|
||||
assert len(collections) == 1
|
||||
assert collections[0].name == 'new.domain'
|
||||
|
||||
async def test_config_updates_existing_items(self, test_session, test_directory, async_client):
|
||||
"""
|
||||
Config updates an existing collection's description.
|
||||
Should update the collection in DB.
|
||||
"""
|
||||
config = get_domains_config()
|
||||
config_path = config.get_file()
|
||||
|
||||
# Create a collection
|
||||
collection = Collection(
|
||||
name='update.me',
|
||||
kind='domain',
|
||||
directory=None,
|
||||
description='Old description',
|
||||
)
|
||||
test_session.add(collection)
|
||||
test_session.commit()
|
||||
|
||||
# Dump to config
|
||||
config.dump_config()
|
||||
|
||||
# Modify config to change description
|
||||
with open(config_path) as f:
|
||||
config_data = yaml.safe_load(f)
|
||||
|
||||
for c in config_data['collections']:
|
||||
if c['name'] == 'update.me':
|
||||
c['description'] = 'New description'
|
||||
|
||||
with open(config_path, 'w') as f:
|
||||
yaml.dump(config_data, f)
|
||||
|
||||
# Reinitialize and import
|
||||
config.initialize()
|
||||
config.import_config()
|
||||
assert config.successful_import is True
|
||||
|
||||
# Collection should be updated
|
||||
collection = test_session.query(Collection).filter_by(name='update.me').first()
|
||||
assert collection is not None
|
||||
assert collection.description == 'New description'
|
||||
|
|
@ -152,14 +152,29 @@ class InventoriesConfig(ConfigFile):
|
|||
super().dump_config(file, send_events, overwrite)
|
||||
|
||||
def import_config(self, file: pathlib.Path = None, send_events=False):
|
||||
file = file or self.get_file()
|
||||
|
||||
# If config file doesn't exist, mark import as successful (nothing to import)
|
||||
if not file.is_file():
|
||||
logger.info('No inventories config file, skipping import')
|
||||
self.successful_import = True
|
||||
return
|
||||
|
||||
super().import_config(file)
|
||||
|
||||
inventories_data = self.inventories
|
||||
# Empty inventories list = never delete DB records
|
||||
if not inventories_data:
|
||||
logger.info(f'No inventories in config, preserving existing DB inventories')
|
||||
self.successful_import = True
|
||||
return
|
||||
|
||||
try:
|
||||
with get_db_session(commit=True) as session:
|
||||
db_inventories = session.query(Inventory).all()
|
||||
db_inventories_by_name = {i.name: i for i in db_inventories}
|
||||
config_inventories = [i['name'] for i in self.inventories]
|
||||
for inventory_dict in self.inventories:
|
||||
config_inventories = [i['name'] for i in inventories_data]
|
||||
for inventory_dict in inventories_data:
|
||||
if inventory_dict['name'] not in db_inventories_by_name:
|
||||
inventory = Inventory(
|
||||
name=inventory_dict['name'],
|
||||
|
|
@ -176,7 +191,7 @@ class InventoriesConfig(ConfigFile):
|
|||
if name not in config_inventories:
|
||||
session.delete(inventory)
|
||||
|
||||
for inventory_dict in self.inventories:
|
||||
for inventory_dict in inventories_data:
|
||||
# Items are complex and do not have useful primary keys. Delete all items, create new items.
|
||||
session.query(Item).filter(Item.inventory_id == inventory_dict['id']).delete()
|
||||
inventory = db_inventories_by_name[inventory_dict['name']]
|
||||
|
|
|
|||
219
modules/inventory/test/test_inventories_config.py
Normal file
219
modules/inventory/test/test_inventories_config.py
Normal file
|
|
@ -0,0 +1,219 @@
|
|||
"""
|
||||
Tests for InventoriesConfig import/upgrade scenarios.
|
||||
|
||||
Tests cover:
|
||||
1. Onboarding (new install) - no config file exists
|
||||
2. Upgrade - config file exists with data
|
||||
3. Edge cases - empty config, missing config with DB data
|
||||
"""
|
||||
import pytest
|
||||
import yaml
|
||||
|
||||
from modules.inventory import Inventory
|
||||
from modules.inventory.common import get_inventories_config
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
class TestInventoriesConfigOnboarding:
|
||||
"""Tests for new install scenarios where no config file exists."""
|
||||
|
||||
async def test_new_install_no_db_data(self, test_session, test_directory, async_client):
|
||||
"""
|
||||
New install with empty DB and no config file.
|
||||
Should succeed with empty config.
|
||||
"""
|
||||
config = get_inventories_config()
|
||||
config_path = config.get_file()
|
||||
|
||||
# Ensure no config file exists
|
||||
if config_path.is_file():
|
||||
config_path.unlink()
|
||||
|
||||
# Ensure DB has no inventories
|
||||
assert test_session.query(Inventory).count() == 0
|
||||
|
||||
# Import should succeed
|
||||
config.import_config()
|
||||
assert config.successful_import is True
|
||||
|
||||
async def test_new_install_with_db_data(self, test_session, test_directory, async_client, test_inventory):
|
||||
"""
|
||||
Config file missing but DB has data.
|
||||
Import should succeed (nothing to import), DB data preserved.
|
||||
"""
|
||||
config = get_inventories_config()
|
||||
config_path = config.get_file()
|
||||
|
||||
# Ensure we have an inventory from the fixture
|
||||
assert test_session.query(Inventory).count() == 1
|
||||
|
||||
# Remove config file to simulate missing config
|
||||
if config_path.is_file():
|
||||
config_path.unlink()
|
||||
|
||||
# Import should succeed (nothing to import)
|
||||
config.import_config()
|
||||
assert config.successful_import is True
|
||||
|
||||
# DB data should be preserved
|
||||
inventories = test_session.query(Inventory).all()
|
||||
assert len(inventories) == 1
|
||||
assert inventories[0].name == 'Test Inventory'
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
class TestInventoriesConfigUpgrade:
|
||||
"""Tests for upgrade scenarios where config file exists."""
|
||||
|
||||
async def test_upgrade_config_exists_with_data(self, test_session, test_directory, async_client, test_inventory):
|
||||
"""
|
||||
Upgrade with existing config containing inventory data.
|
||||
Should import and sync DB with config.
|
||||
"""
|
||||
config = get_inventories_config()
|
||||
config_path = config.get_file()
|
||||
|
||||
# Dump to create config file
|
||||
config.dump_config()
|
||||
assert config_path.is_file()
|
||||
|
||||
# Now import should succeed and preserve the inventory
|
||||
config.import_config()
|
||||
assert config.successful_import is True
|
||||
|
||||
# Inventory should still exist
|
||||
inventories = test_session.query(Inventory).all()
|
||||
assert len(inventories) == 1
|
||||
assert inventories[0].name == 'Test Inventory'
|
||||
|
||||
async def test_upgrade_config_exists_empty_list(self, test_session, test_directory, async_client, test_inventory):
|
||||
"""
|
||||
Config exists but has empty inventories list.
|
||||
Should NOT delete existing DB inventories (never delete on empty).
|
||||
"""
|
||||
config = get_inventories_config()
|
||||
config_path = config.get_file()
|
||||
|
||||
# Write empty config
|
||||
config_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with open(config_path, 'w') as f:
|
||||
yaml.dump({'version': 0, 'inventories': []}, f)
|
||||
|
||||
# Reinitialize config to read the empty file
|
||||
config.initialize()
|
||||
|
||||
# Import should succeed but NOT delete the inventory
|
||||
config.import_config()
|
||||
assert config.successful_import is True
|
||||
|
||||
# Inventory should still exist (never delete on empty config)
|
||||
inventories = test_session.query(Inventory).all()
|
||||
assert len(inventories) == 1
|
||||
assert inventories[0].name == 'Test Inventory'
|
||||
|
||||
async def test_upgrade_config_missing_db_has_data(self, test_session, test_directory, async_client):
|
||||
"""
|
||||
Config file missing but DB has inventories.
|
||||
Import should succeed (nothing to import), DB data preserved.
|
||||
"""
|
||||
config = get_inventories_config()
|
||||
config_path = config.get_file()
|
||||
|
||||
# Create inventories in DB
|
||||
for name in ['Inventory One', 'Inventory Two']:
|
||||
inventory = Inventory(name=name)
|
||||
test_session.add(inventory)
|
||||
test_session.commit()
|
||||
|
||||
# Remove config file
|
||||
if config_path.is_file():
|
||||
config_path.unlink()
|
||||
|
||||
# Import should succeed (nothing to import)
|
||||
config.import_config()
|
||||
assert config.successful_import is True
|
||||
|
||||
# DB data should be preserved
|
||||
inventories = test_session.query(Inventory).all()
|
||||
assert len(inventories) == 2
|
||||
inventory_names = {i.name for i in inventories}
|
||||
assert 'Inventory One' in inventory_names
|
||||
assert 'Inventory Two' in inventory_names
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
class TestInventoriesConfigEdgeCases:
|
||||
"""Tests for edge cases in config import."""
|
||||
|
||||
async def test_config_deletes_removed_items(self, test_session, test_directory, async_client):
|
||||
"""
|
||||
Config removes an inventory that exists in DB.
|
||||
Should delete the inventory from DB.
|
||||
"""
|
||||
config = get_inventories_config()
|
||||
config_path = config.get_file()
|
||||
|
||||
# Create two inventories
|
||||
for name in ['Keep This', 'Delete This']:
|
||||
inventory = Inventory(name=name)
|
||||
test_session.add(inventory)
|
||||
test_session.commit()
|
||||
|
||||
# Dump to config
|
||||
config.dump_config()
|
||||
|
||||
# Modify config to remove one inventory
|
||||
with open(config_path) as f:
|
||||
config_data = yaml.safe_load(f)
|
||||
|
||||
config_data['inventories'] = [i for i in config_data['inventories'] if i['name'] == 'Keep This']
|
||||
|
||||
with open(config_path, 'w') as f:
|
||||
yaml.dump(config_data, f)
|
||||
|
||||
# Reinitialize and import
|
||||
config.initialize()
|
||||
config.import_config()
|
||||
assert config.successful_import is True
|
||||
|
||||
# Only one inventory should remain
|
||||
inventories = test_session.query(Inventory).all()
|
||||
assert len(inventories) == 1
|
||||
assert inventories[0].name == 'Keep This'
|
||||
|
||||
async def test_config_adds_new_items(self, test_session, test_directory, async_client):
|
||||
"""
|
||||
Config has a new inventory that doesn't exist in DB.
|
||||
Should create the inventory in DB.
|
||||
"""
|
||||
config = get_inventories_config()
|
||||
config_path = config.get_file()
|
||||
|
||||
# Ensure no inventories in DB
|
||||
assert test_session.query(Inventory).count() == 0
|
||||
|
||||
# Write config with a new inventory
|
||||
config_data = {
|
||||
'version': 0,
|
||||
'inventories': [{
|
||||
'name': 'New Inventory',
|
||||
'id': 1,
|
||||
'created_at': None,
|
||||
'deleted_at': None,
|
||||
'items': [],
|
||||
}]
|
||||
}
|
||||
|
||||
config_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with open(config_path, 'w') as f:
|
||||
yaml.dump(config_data, f)
|
||||
|
||||
# Reinitialize and import
|
||||
config.initialize()
|
||||
config.import_config()
|
||||
assert config.successful_import is True
|
||||
|
||||
# Inventory should be created
|
||||
inventories = test_session.query(Inventory).all()
|
||||
assert len(inventories) == 1
|
||||
assert inventories[0].name == 'New Inventory'
|
||||
|
|
@ -220,7 +220,7 @@ def convert_or_generate_poster(video: Video) -> Tuple[Optional[pathlib.Path], Op
|
|||
class ChannelDictValidator:
|
||||
name: str
|
||||
directory: str
|
||||
download_frequency: int
|
||||
download_frequency: int = 604800 # Default to weekly
|
||||
url: str = None
|
||||
|
||||
|
||||
|
|
@ -253,11 +253,20 @@ class ChannelsConfig(ConfigFile):
|
|||
|
||||
def import_config(self, file: pathlib.Path = None, send_events=False):
|
||||
"""Import channels from config file into database using batch operations."""
|
||||
file = file or self.get_file()
|
||||
|
||||
# If config file doesn't exist, mark import as successful (nothing to import)
|
||||
if not file.is_file():
|
||||
channel_import_logger.info('No channels config file, skipping import')
|
||||
self.successful_import = True
|
||||
return
|
||||
|
||||
super().import_config()
|
||||
try:
|
||||
channels = self.channels
|
||||
# Empty channels list = never delete DB records
|
||||
if not channels:
|
||||
channel_import_logger.info('No channels to import')
|
||||
channel_import_logger.info('No channels in config, preserving existing DB channels')
|
||||
self.successful_import = True
|
||||
return
|
||||
|
||||
|
|
@ -375,6 +384,23 @@ class ChannelsConfig(ConfigFile):
|
|||
Events.send_config_import_failed(message)
|
||||
raise
|
||||
|
||||
def dump_config(self, file: pathlib.Path = None, send_events=False, overwrite=False):
|
||||
"""Dump all channels from database to config file."""
|
||||
from wrolpi.collections import Collection
|
||||
channel_import_logger.info('Dumping channels to config')
|
||||
|
||||
with get_db_session() as session:
|
||||
# Get all channels, ordered by collection name
|
||||
channels = session.query(Channel).join(Collection).order_by(Collection.name).all()
|
||||
|
||||
# Use config_view to export each channel
|
||||
channels_data = [channel.config_view() for channel in channels]
|
||||
|
||||
self._config['channels'] = channels_data
|
||||
|
||||
channel_import_logger.info(f'Dumping {len(channels_data)} channels to config')
|
||||
self.save(file, send_events, overwrite)
|
||||
|
||||
|
||||
CHANNELS_CONFIG: ChannelsConfig = ChannelsConfig()
|
||||
TEST_CHANNELS_CONFIG: ChannelsConfig = None
|
||||
|
|
|
|||
270
modules/videos/test/test_channels_config.py
Normal file
270
modules/videos/test/test_channels_config.py
Normal file
|
|
@ -0,0 +1,270 @@
|
|||
"""
|
||||
Tests for ChannelsConfig import/upgrade scenarios.
|
||||
|
||||
Tests cover:
|
||||
1. Onboarding (new install) - no config file exists
|
||||
2. Upgrade - config file exists with data
|
||||
3. Edge cases - empty config, missing config with DB data
|
||||
"""
|
||||
import pytest
|
||||
import yaml
|
||||
|
||||
from modules.videos.lib import get_channels_config
|
||||
from modules.videos.models import Channel
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
class TestChannelsConfigOnboarding:
|
||||
"""Tests for new install scenarios where no config file exists."""
|
||||
|
||||
async def test_new_install_no_db_data(self, test_session, test_directory, async_client, test_channels_config):
|
||||
"""
|
||||
New install with empty DB and no config file.
|
||||
Should succeed with empty config.
|
||||
"""
|
||||
config = get_channels_config()
|
||||
config_path = config.get_file()
|
||||
|
||||
# Ensure no config file exists
|
||||
if config_path.is_file():
|
||||
config_path.unlink()
|
||||
|
||||
# Ensure DB has no channels
|
||||
assert test_session.query(Channel).count() == 0
|
||||
|
||||
# Import should succeed
|
||||
config.import_config()
|
||||
assert config.successful_import is True
|
||||
|
||||
async def test_new_install_with_db_data(self, test_session, test_directory, async_client, test_channels_config,
|
||||
channel_factory):
|
||||
"""
|
||||
Config file missing but DB has data.
|
||||
Import should succeed (nothing to import), DB data preserved.
|
||||
"""
|
||||
config = get_channels_config()
|
||||
config_path = config.get_file()
|
||||
|
||||
# Create a channel in DB
|
||||
channel = channel_factory(name='Existing Channel')
|
||||
test_session.commit()
|
||||
|
||||
# Remove config file to simulate missing config
|
||||
if config_path.is_file():
|
||||
config_path.unlink()
|
||||
|
||||
# Import should succeed (nothing to import)
|
||||
config.import_config()
|
||||
assert config.successful_import is True
|
||||
|
||||
# DB data should be preserved
|
||||
channels = test_session.query(Channel).all()
|
||||
assert len(channels) == 1
|
||||
assert channels[0].name == 'Existing Channel'
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
class TestChannelsConfigUpgrade:
|
||||
"""Tests for upgrade scenarios where config file exists."""
|
||||
|
||||
async def test_upgrade_config_exists_with_data(self, test_session, test_directory, async_client,
|
||||
test_channels_config, channel_factory):
|
||||
"""
|
||||
Upgrade with existing config containing channel data.
|
||||
Should import and sync DB with config.
|
||||
"""
|
||||
config = get_channels_config()
|
||||
config_path = config.get_file()
|
||||
|
||||
# Create a channel in DB
|
||||
channel = channel_factory(name='Config Channel')
|
||||
test_session.commit()
|
||||
|
||||
# Dump to create config file
|
||||
config.dump_config()
|
||||
assert config_path.is_file()
|
||||
|
||||
# Now import should succeed and preserve the channel
|
||||
config.import_config()
|
||||
assert config.successful_import is True
|
||||
|
||||
# Channel should still exist
|
||||
channels = test_session.query(Channel).all()
|
||||
assert len(channels) == 1
|
||||
assert channels[0].name == 'Config Channel'
|
||||
|
||||
async def test_upgrade_config_exists_empty_list(self, test_session, test_directory, async_client,
|
||||
test_channels_config, channel_factory):
|
||||
"""
|
||||
Config exists but has empty channels list.
|
||||
Should NOT delete existing DB channels (never delete on empty).
|
||||
"""
|
||||
config = get_channels_config()
|
||||
config_path = config.get_file()
|
||||
|
||||
# Create a channel in DB
|
||||
channel = channel_factory(name='Should Not Delete')
|
||||
test_session.commit()
|
||||
|
||||
# Write empty config
|
||||
with open(config_path, 'w') as f:
|
||||
yaml.dump({'version': 0, 'channels': []}, f)
|
||||
|
||||
# Reinitialize config to read the empty file
|
||||
config.initialize()
|
||||
|
||||
# Import should succeed but NOT delete the channel
|
||||
config.import_config()
|
||||
assert config.successful_import is True
|
||||
|
||||
# Channel should still exist (never delete on empty config)
|
||||
channels = test_session.query(Channel).all()
|
||||
assert len(channels) == 1
|
||||
assert channels[0].name == 'Should Not Delete'
|
||||
|
||||
async def test_upgrade_config_missing_db_has_data(self, test_session, test_directory, async_client,
|
||||
test_channels_config, channel_factory):
|
||||
"""
|
||||
Config file missing but DB has channels.
|
||||
Import should succeed (nothing to import), DB data preserved.
|
||||
"""
|
||||
config = get_channels_config()
|
||||
config_path = config.get_file()
|
||||
|
||||
# Create channels in DB
|
||||
channel1 = channel_factory(name='Channel One')
|
||||
channel2 = channel_factory(name='Channel Two')
|
||||
test_session.commit()
|
||||
|
||||
# Remove config file
|
||||
if config_path.is_file():
|
||||
config_path.unlink()
|
||||
|
||||
# Import should succeed (nothing to import)
|
||||
config.import_config()
|
||||
assert config.successful_import is True
|
||||
|
||||
# DB data should be preserved
|
||||
channels = test_session.query(Channel).all()
|
||||
assert len(channels) == 2
|
||||
channel_names = {c.name for c in channels}
|
||||
assert 'Channel One' in channel_names
|
||||
assert 'Channel Two' in channel_names
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
class TestChannelsConfigEdgeCases:
|
||||
"""Tests for edge cases in config import."""
|
||||
|
||||
async def test_config_deletes_removed_items(self, test_session, test_directory, async_client, test_channels_config,
|
||||
channel_factory):
|
||||
"""
|
||||
Config removes a channel that exists in DB.
|
||||
Should delete the channel from DB.
|
||||
"""
|
||||
config = get_channels_config()
|
||||
config_path = config.get_file()
|
||||
|
||||
# Create two channels
|
||||
channel1 = channel_factory(name='Keep This')
|
||||
channel2 = channel_factory(name='Delete This')
|
||||
test_session.commit()
|
||||
|
||||
# Dump to config
|
||||
config.dump_config()
|
||||
|
||||
# Modify config to remove one channel
|
||||
with open(config_path) as f:
|
||||
config_data = yaml.safe_load(f)
|
||||
|
||||
config_data['channels'] = [c for c in config_data['channels'] if c['name'] == 'Keep This']
|
||||
|
||||
with open(config_path, 'w') as f:
|
||||
yaml.dump(config_data, f)
|
||||
|
||||
# Reinitialize and import
|
||||
config.initialize()
|
||||
config.import_config()
|
||||
assert config.successful_import is True
|
||||
|
||||
# Only one channel should remain
|
||||
channels = test_session.query(Channel).all()
|
||||
assert len(channels) == 1
|
||||
assert channels[0].name == 'Keep This'
|
||||
|
||||
async def test_config_adds_new_items(self, test_session, test_directory, async_client, test_channels_config):
|
||||
"""
|
||||
Config has a new channel that doesn't exist in DB.
|
||||
Should create the channel in DB.
|
||||
"""
|
||||
config = get_channels_config()
|
||||
config_path = config.get_file()
|
||||
|
||||
# Ensure no channels in DB
|
||||
assert test_session.query(Channel).count() == 0
|
||||
|
||||
# Write config with a new channel
|
||||
new_channel_dir = test_directory / 'videos' / 'New Channel'
|
||||
new_channel_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
config_data = {
|
||||
'version': 0,
|
||||
'channels': [{
|
||||
'name': 'New Channel',
|
||||
'directory': str(new_channel_dir),
|
||||
'url': 'https://example.com/new',
|
||||
'download_frequency': 604800,
|
||||
'downloads': [],
|
||||
}]
|
||||
}
|
||||
|
||||
with open(config_path, 'w') as f:
|
||||
yaml.dump(config_data, f)
|
||||
|
||||
# Reinitialize and import
|
||||
config.initialize()
|
||||
config.import_config()
|
||||
assert config.successful_import is True
|
||||
|
||||
# Channel should be created
|
||||
channels = test_session.query(Channel).all()
|
||||
assert len(channels) == 1
|
||||
assert channels[0].name == 'New Channel'
|
||||
assert channels[0].url == 'https://example.com/new'
|
||||
|
||||
async def test_config_updates_existing_items(self, test_session, test_directory, async_client, test_channels_config,
|
||||
channel_factory):
|
||||
"""
|
||||
Config updates an existing channel's URL.
|
||||
Should update the channel in DB.
|
||||
"""
|
||||
config = get_channels_config()
|
||||
config_path = config.get_file()
|
||||
|
||||
# Create a channel
|
||||
channel = channel_factory(name='Update Me', url='https://example.com/old')
|
||||
test_session.commit()
|
||||
|
||||
# Dump to config
|
||||
config.dump_config()
|
||||
|
||||
# Modify config to change URL
|
||||
with open(config_path) as f:
|
||||
config_data = yaml.safe_load(f)
|
||||
|
||||
for c in config_data['channels']:
|
||||
if c['name'] == 'Update Me':
|
||||
c['url'] = 'https://example.com/new'
|
||||
|
||||
with open(config_path, 'w') as f:
|
||||
yaml.dump(config_data, f)
|
||||
|
||||
# Reinitialize and import
|
||||
config.initialize()
|
||||
config.import_config()
|
||||
assert config.successful_import is True
|
||||
|
||||
# Channel should be updated
|
||||
channel = test_session.query(Channel).filter_by(url='https://example.com/new').first()
|
||||
assert channel is not None
|
||||
assert channel.name == 'Update Me'
|
||||
|
|
@ -9,7 +9,7 @@ from modules.zim.errors import UnknownZim, UnknownZimEntry
|
|||
from modules.zim.models import TagZimEntry, ZimSubscription
|
||||
from wrolpi import tags, flags
|
||||
from wrolpi.common import DownloadFileInfo, get_wrolpi_config
|
||||
from wrolpi.downloader import Download, import_downloads_config, save_downloads_config
|
||||
from wrolpi.downloader import Download, import_downloads_config, save_downloads_config, get_download_manager_config
|
||||
from wrolpi.files import lib as files_lib
|
||||
from wrolpi.files.models import FileGroup
|
||||
from wrolpi.test.common import skip_circleci
|
||||
|
|
@ -366,8 +366,10 @@ async def test_zim_subscription_download_import(async_client, test_session):
|
|||
download = test_session.query(Download).filter(Download.frequency.isnot(None)).one()
|
||||
assert download.url == 'https://download.kiwix.org/zim/wikisource/wikisource_en_all_maxi_'
|
||||
|
||||
# Save config file, delete all entries.
|
||||
save_downloads_config()
|
||||
# Save config file synchronously (dump_config() + save()), delete all entries.
|
||||
config = get_download_manager_config()
|
||||
config.dump_config()
|
||||
config.save()
|
||||
test_session.query(Download).delete()
|
||||
test_session.query(ZimSubscription).delete()
|
||||
test_session.commit()
|
||||
|
|
|
|||
|
|
@ -759,6 +759,7 @@ class WROLPiConfig(ConfigFile):
|
|||
hotspot_on_startup=True,
|
||||
hotspot_password='wrolpi hotspot',
|
||||
hotspot_ssid='WROLPi',
|
||||
check_for_upgrades=True,
|
||||
ignore_outdated_zims=False,
|
||||
ignored_directories=['config', 'tags'],
|
||||
map_destination='map',
|
||||
|
|
@ -869,6 +870,14 @@ class WROLPiConfig(ConfigFile):
|
|||
def ignore_outdated_zims(self, value: bool):
|
||||
self.update({'ignore_outdated_zims': value})
|
||||
|
||||
@property
|
||||
def check_for_upgrades(self) -> bool:
|
||||
return self._config.get('check_for_upgrades', True)
|
||||
|
||||
@check_for_upgrades.setter
|
||||
def check_for_upgrades(self, value: bool):
|
||||
self.update({'check_for_upgrades': value})
|
||||
|
||||
@property
|
||||
def throttle_on_startup(self) -> bool:
|
||||
return self._config['throttle_on_startup']
|
||||
|
|
|
|||
|
|
@ -1313,7 +1313,23 @@ class DownloadManagerConfig(ConfigFile):
|
|||
Events.send_config_save_failed(message)
|
||||
|
||||
def import_config(self, file: pathlib.Path = None, send_events=False):
|
||||
file = file or self.get_file()
|
||||
|
||||
# If config file doesn't exist, mark import as successful (nothing to import)
|
||||
if not file.is_file():
|
||||
logger.info('No downloads config file, skipping import')
|
||||
self.successful_import = True
|
||||
return
|
||||
|
||||
super().import_config(file)
|
||||
|
||||
downloads_data = self.downloads
|
||||
# Empty downloads list = never delete DB records
|
||||
if not downloads_data:
|
||||
logger.info(f'No downloads in config, preserving existing DB downloads')
|
||||
self.successful_import = True
|
||||
return
|
||||
|
||||
with get_db_session(commit=True) as session:
|
||||
from modules.zim.lib import zim_download_url_to_name
|
||||
from modules.zim.models import ZimSubscription
|
||||
|
|
@ -1321,7 +1337,7 @@ class DownloadManagerConfig(ConfigFile):
|
|||
try:
|
||||
logger.warning('Importing downloads in config')
|
||||
|
||||
downloads_by_url = {i['url']: i for i in get_download_manager_config().downloads}
|
||||
downloads_by_url = {i['url']: i for i in downloads_data}
|
||||
existing_downloads: List[Download] = list(session.query(Download))
|
||||
for existing in existing_downloads:
|
||||
download = downloads_by_url.pop(existing.url, None)
|
||||
|
|
|
|||
|
|
@ -163,6 +163,7 @@ def get_settings(_: Request):
|
|||
'hotspot_password': wrolpi_config.hotspot_password,
|
||||
'hotspot_ssid': wrolpi_config.hotspot_ssid,
|
||||
'hotspot_status': admin.hotspot_status().name,
|
||||
'check_for_upgrades': wrolpi_config.check_for_upgrades,
|
||||
'ignore_outdated_zims': wrolpi_config.ignore_outdated_zims,
|
||||
'ignored_directories': ignored_directories,
|
||||
'log_level': api_app.shared_ctx.log_level.value,
|
||||
|
|
|
|||
|
|
@ -63,6 +63,7 @@ class SettingsRequest:
|
|||
hotspot_password: Optional[str] = None
|
||||
hotspot_ssid: Optional[str] = None
|
||||
hotspot_status: Optional[bool] = None
|
||||
check_for_upgrades: Optional[bool] = None
|
||||
ignore_outdated_zims: Optional[bool] = None
|
||||
log_level: Optional[int] = None
|
||||
map_destination: Optional[str] = None
|
||||
|
|
|
|||
294
wrolpi/test/test_downloads_config.py
Normal file
294
wrolpi/test/test_downloads_config.py
Normal file
|
|
@ -0,0 +1,294 @@
|
|||
"""
|
||||
Tests for DownloadManagerConfig import/upgrade scenarios.
|
||||
|
||||
Tests cover:
|
||||
1. Onboarding (new install) - no config file exists
|
||||
2. Upgrade - config file exists with data
|
||||
3. Edge cases - empty config, missing config with DB data
|
||||
"""
|
||||
import pytest
|
||||
import yaml
|
||||
|
||||
from wrolpi.downloader import Download, get_download_manager_config
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
class TestDownloadManagerConfigOnboarding:
|
||||
"""Tests for new install scenarios where no config file exists."""
|
||||
|
||||
async def test_new_install_no_db_data(self, test_session, test_directory, async_client, test_download_manager_config):
|
||||
"""
|
||||
New install with empty DB and no config file.
|
||||
Should succeed with empty config.
|
||||
"""
|
||||
config = get_download_manager_config()
|
||||
config_path = config.get_file()
|
||||
|
||||
# Ensure no config file exists
|
||||
if config_path.is_file():
|
||||
config_path.unlink()
|
||||
|
||||
# Ensure DB has no downloads
|
||||
assert test_session.query(Download).count() == 0
|
||||
|
||||
# Import should succeed
|
||||
config.import_config()
|
||||
assert config.successful_import is True
|
||||
|
||||
async def test_new_install_with_db_data(self, test_session, test_directory, async_client,
|
||||
test_download_manager_config):
|
||||
"""
|
||||
Config file missing but DB has data.
|
||||
Import should succeed (nothing to import), DB data preserved.
|
||||
"""
|
||||
config = get_download_manager_config()
|
||||
config_path = config.get_file()
|
||||
|
||||
# Create a download in DB
|
||||
download = Download(
|
||||
url='https://example.com/video',
|
||||
downloader='video',
|
||||
frequency=604800,
|
||||
status='new',
|
||||
)
|
||||
test_session.add(download)
|
||||
test_session.commit()
|
||||
|
||||
# Remove config file to simulate missing config
|
||||
if config_path.is_file():
|
||||
config_path.unlink()
|
||||
|
||||
# Import should succeed (nothing to import)
|
||||
config.import_config()
|
||||
assert config.successful_import is True
|
||||
|
||||
# DB data should be preserved
|
||||
downloads = test_session.query(Download).all()
|
||||
assert len(downloads) == 1
|
||||
assert downloads[0].url == 'https://example.com/video'
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
class TestDownloadManagerConfigUpgrade:
|
||||
"""Tests for upgrade scenarios where config file exists."""
|
||||
|
||||
async def test_upgrade_config_exists_with_data(self, test_session, test_directory, async_client,
|
||||
test_download_manager_config):
|
||||
"""
|
||||
Upgrade with existing config containing download data.
|
||||
Should import and sync DB with config.
|
||||
"""
|
||||
config = get_download_manager_config()
|
||||
config_path = config.get_file()
|
||||
|
||||
# Create a download in DB
|
||||
download = Download(
|
||||
url='https://example.com/download',
|
||||
downloader='video',
|
||||
frequency=604800,
|
||||
status='new',
|
||||
)
|
||||
test_session.add(download)
|
||||
test_session.commit()
|
||||
|
||||
# Write config file directly (dump_config uses async background switches)
|
||||
config_data = {
|
||||
'version': 0,
|
||||
'skip_urls': [],
|
||||
'downloads': [{
|
||||
'url': 'https://example.com/download',
|
||||
'downloader': 'video',
|
||||
'destination': None,
|
||||
'frequency': 604800,
|
||||
'last_successful_download': None,
|
||||
'next_download': None,
|
||||
'status': 'new',
|
||||
'sub_downloader': None,
|
||||
'settings': None,
|
||||
'tag_names': [],
|
||||
}]
|
||||
}
|
||||
config_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with open(config_path, 'w') as f:
|
||||
yaml.dump(config_data, f)
|
||||
assert config_path.is_file()
|
||||
|
||||
# Now import should succeed and preserve the download
|
||||
config.import_config()
|
||||
assert config.successful_import is True
|
||||
|
||||
# Download should still exist
|
||||
downloads = test_session.query(Download).all()
|
||||
assert len(downloads) == 1
|
||||
assert downloads[0].url == 'https://example.com/download'
|
||||
|
||||
async def test_upgrade_config_exists_empty_list(self, test_session, test_directory, async_client,
|
||||
test_download_manager_config):
|
||||
"""
|
||||
Config exists but has empty downloads list.
|
||||
Should NOT delete existing DB downloads (never delete on empty).
|
||||
"""
|
||||
config = get_download_manager_config()
|
||||
config_path = config.get_file()
|
||||
|
||||
# Create a download in DB
|
||||
download = Download(
|
||||
url='https://example.com/preserve',
|
||||
downloader='video',
|
||||
frequency=604800,
|
||||
status='new',
|
||||
)
|
||||
test_session.add(download)
|
||||
test_session.commit()
|
||||
|
||||
# Write empty config
|
||||
config_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with open(config_path, 'w') as f:
|
||||
yaml.dump({'version': 0, 'downloads': [], 'skip_urls': []}, f)
|
||||
|
||||
# Reinitialize config to read the empty file
|
||||
config.initialize()
|
||||
|
||||
# Import should succeed but NOT delete the download
|
||||
config.import_config()
|
||||
assert config.successful_import is True
|
||||
|
||||
# Download should still exist (never delete on empty config)
|
||||
downloads = test_session.query(Download).all()
|
||||
assert len(downloads) == 1
|
||||
assert downloads[0].url == 'https://example.com/preserve'
|
||||
|
||||
async def test_upgrade_config_missing_db_has_data(self, test_session, test_directory, async_client,
|
||||
test_download_manager_config):
|
||||
"""
|
||||
Config file missing but DB has downloads.
|
||||
Import should succeed (nothing to import), DB data preserved.
|
||||
"""
|
||||
config = get_download_manager_config()
|
||||
config_path = config.get_file()
|
||||
|
||||
# Create downloads in DB
|
||||
for url in ['https://example.com/one', 'https://example.com/two']:
|
||||
download = Download(
|
||||
url=url,
|
||||
downloader='video',
|
||||
frequency=604800,
|
||||
status='new',
|
||||
)
|
||||
test_session.add(download)
|
||||
test_session.commit()
|
||||
|
||||
# Remove config file
|
||||
if config_path.is_file():
|
||||
config_path.unlink()
|
||||
|
||||
# Import should succeed (nothing to import)
|
||||
config.import_config()
|
||||
assert config.successful_import is True
|
||||
|
||||
# DB data should be preserved
|
||||
downloads = test_session.query(Download).all()
|
||||
assert len(downloads) == 2
|
||||
download_urls = {d.url for d in downloads}
|
||||
assert 'https://example.com/one' in download_urls
|
||||
assert 'https://example.com/two' in download_urls
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
class TestDownloadManagerConfigEdgeCases:
|
||||
"""Tests for edge cases in config import."""
|
||||
|
||||
async def test_config_deletes_removed_items(self, test_session, test_directory, async_client,
|
||||
test_download_manager_config):
|
||||
"""
|
||||
Config removes a download that exists in DB.
|
||||
Should delete the download from DB.
|
||||
"""
|
||||
config = get_download_manager_config()
|
||||
config_path = config.get_file()
|
||||
|
||||
# Create two downloads
|
||||
for url in ['https://example.com/keep', 'https://example.com/delete']:
|
||||
download = Download(
|
||||
url=url,
|
||||
downloader='video',
|
||||
frequency=604800,
|
||||
status='new',
|
||||
)
|
||||
test_session.add(download)
|
||||
test_session.commit()
|
||||
|
||||
# Write config file directly with only one download (dump_config uses async background switches)
|
||||
config_data = {
|
||||
'version': 0,
|
||||
'skip_urls': [],
|
||||
'downloads': [{
|
||||
'url': 'https://example.com/keep',
|
||||
'downloader': 'video',
|
||||
'destination': None,
|
||||
'frequency': 604800,
|
||||
'last_successful_download': None,
|
||||
'next_download': None,
|
||||
'status': 'new',
|
||||
'sub_downloader': None,
|
||||
'settings': None,
|
||||
'tag_names': [],
|
||||
}]
|
||||
}
|
||||
config_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with open(config_path, 'w') as f:
|
||||
yaml.dump(config_data, f)
|
||||
|
||||
# Reinitialize and import
|
||||
config.initialize()
|
||||
config.import_config()
|
||||
assert config.successful_import is True
|
||||
|
||||
# Only one download should remain
|
||||
downloads = test_session.query(Download).all()
|
||||
assert len(downloads) == 1
|
||||
assert downloads[0].url == 'https://example.com/keep'
|
||||
|
||||
async def test_config_adds_new_items(self, test_session, test_directory, async_client,
|
||||
test_download_manager_config):
|
||||
"""
|
||||
Config has a new download that doesn't exist in DB.
|
||||
Should create the download in DB.
|
||||
"""
|
||||
config = get_download_manager_config()
|
||||
config_path = config.get_file()
|
||||
|
||||
# Ensure no downloads in DB
|
||||
assert test_session.query(Download).count() == 0
|
||||
|
||||
# Write config with a new download
|
||||
config_data = {
|
||||
'version': 0,
|
||||
'skip_urls': [],
|
||||
'downloads': [{
|
||||
'url': 'https://example.com/new',
|
||||
'downloader': 'video',
|
||||
'destination': None,
|
||||
'frequency': 604800,
|
||||
'last_successful_download': None,
|
||||
'next_download': None,
|
||||
'status': 'new',
|
||||
'sub_downloader': None,
|
||||
'settings': None,
|
||||
'tag_names': [],
|
||||
}]
|
||||
}
|
||||
|
||||
config_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with open(config_path, 'w') as f:
|
||||
yaml.dump(config_data, f)
|
||||
|
||||
# Reinitialize and import
|
||||
config.initialize()
|
||||
config.import_config()
|
||||
assert config.successful_import is True
|
||||
|
||||
# Download should be created
|
||||
downloads = test_session.query(Download).all()
|
||||
assert len(downloads) == 1
|
||||
assert downloads[0].url == 'https://example.com/new'
|
||||
Loading…
Add table
Add a link
Reference in a new issue