mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-02 20:59:56 -05:00
Configs can be saved in background using switches.
This commit is contained in:
parent
44d4921c84
commit
d54735c4bb
53 changed files with 967 additions and 574 deletions
|
|
@ -100,14 +100,14 @@ def zim_factory(test_session, zim_path_factory, test_directory):
|
|||
|
||||
|
||||
@pytest.fixture
|
||||
def assert_zim_search(test_async_client, test_directory):
|
||||
def assert_zim_search(async_client, test_directory):
|
||||
from http import HTTPStatus
|
||||
|
||||
async def _(search_str: str, zim_id: int, expected: Dict, tag_names: List[str] = [],
|
||||
expected_status_code: int = HTTPStatus.OK):
|
||||
content = dict(search_str=search_str, tag_names=tag_names)
|
||||
assert isinstance(zim_id, int), 'This fixture only supports Zim ids of type integer'
|
||||
request, response = await test_async_client.post(f'api/zim/search/{zim_id}', content=json.dumps(content))
|
||||
request, response = await async_client.post(f'api/zim/search/{zim_id}', content=json.dumps(content))
|
||||
assert response.status_code == expected_status_code
|
||||
|
||||
result = response.json['zim']
|
||||
|
|
|
|||
|
|
@ -192,7 +192,7 @@ class Zim(Base):
|
|||
else Tag.get_by_id(tag_id_or_name, session)
|
||||
tag_zim_entry = TagZimEntry(tag=tag, zim=self, zim_entry=zim_entry)
|
||||
session.add(tag_zim_entry)
|
||||
tags.save_tags_config()
|
||||
tags.save_tags_config.activate_switch()
|
||||
return tag_zim_entry
|
||||
|
||||
def untag_entry(self, tag_id_or_name: int | str, zim_entry: str):
|
||||
|
|
@ -207,7 +207,7 @@ class Zim(Base):
|
|||
session.delete(tag_zim_entry)
|
||||
else:
|
||||
raise UnknownZimTagEntry(f'Could not find at {repr(str(zim_entry))}')
|
||||
tags.save_tags_config()
|
||||
tags.save_tags_config.activate_switch()
|
||||
|
||||
@staticmethod
|
||||
def _entries_with_tags_process(limit: int, offset: int, params: dict, session: Session, tags_sub_select: str):
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ from wrolpi.downloader import Download
|
|||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_zim_search(test_async_client, test_session, test_zim, tag_factory, assert_zim_search):
|
||||
async def test_zim_search(async_client, test_session, test_zim, tag_factory, assert_zim_search):
|
||||
"""Entries can be tagged and retrieved using their containing Zim."""
|
||||
tag1, tag2 = await tag_factory(), await tag_factory()
|
||||
test_zim.tag_entry(tag1.name, 'one')
|
||||
|
|
@ -80,7 +80,7 @@ async def test_zim_search(test_async_client, test_session, test_zim, tag_factory
|
|||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_entries_by_tag(test_async_client, test_session, test_zim, tag_factory):
|
||||
async def test_entries_by_tag(async_client, test_session, test_zim, tag_factory):
|
||||
"""Entries can be tagged and retrieved using their containing Zim."""
|
||||
tag1, tag2 = await tag_factory(), await tag_factory()
|
||||
test_zim.tag_entry(tag1.name, 'one')
|
||||
|
|
@ -99,37 +99,37 @@ async def test_entries_by_tag(test_async_client, test_session, test_zim, tag_fac
|
|||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_zim_crud(test_async_client, test_session, test_directory, test_zim):
|
||||
request, response = await test_async_client.get('/api/zim')
|
||||
async def test_zim_crud(async_client, test_session, test_directory, test_zim):
|
||||
request, response = await async_client.get('/api/zim')
|
||||
assert response.status_code == HTTPStatus.OK
|
||||
assert len(response.json['zims']) == 1
|
||||
assert response.json['zims'][0]['path'] == test_zim.path.name
|
||||
|
||||
request, response = await test_async_client.delete('/api/zim/1')
|
||||
request, response = await async_client.delete('/api/zim/1')
|
||||
assert response.status_code == HTTPStatus.NO_CONTENT
|
||||
assert not test_zim.path.exists()
|
||||
assert test_session.query(Zim).count() == 0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_zim_tag_and_untag(test_async_client, test_session, test_zim, tag_factory):
|
||||
async def test_zim_tag_and_untag(async_client, test_session, test_zim, tag_factory):
|
||||
"""Zim entries can be Tagged and Untagged."""
|
||||
tag1, tag2 = await tag_factory(), await tag_factory()
|
||||
test_session.commit()
|
||||
|
||||
# Entries can be tagged in the API.
|
||||
content = dict(tag_name=tag1.name, zim_id=test_zim.id, zim_entry='home')
|
||||
request, response = await test_async_client.post('/api/zim/tag', content=json.dumps(content))
|
||||
request, response = await async_client.post('/api/zim/tag', content=json.dumps(content))
|
||||
assert response.status_code == HTTPStatus.CREATED
|
||||
|
||||
# Entries can be untagged in the API.
|
||||
content = dict(tag_name=tag1.name, zim_id=test_zim.id, zim_entry='home')
|
||||
request, response = await test_async_client.post('/api/zim/untag', content=json.dumps(content))
|
||||
request, response = await async_client.post('/api/zim/untag', content=json.dumps(content))
|
||||
assert response.status_code == HTTPStatus.NO_CONTENT
|
||||
|
||||
# Cannot untag twice.
|
||||
content = dict(tag_name=tag1.name, zim_id=test_zim.id, zim_entry='home')
|
||||
request, response = await test_async_client.post('/api/zim/untag', content=json.dumps(content))
|
||||
request, response = await async_client.post('/api/zim/untag', content=json.dumps(content))
|
||||
assert response.status_code == HTTPStatus.NOT_FOUND
|
||||
|
||||
|
||||
|
|
@ -199,7 +199,7 @@ def test_get_zim_entry(test_session, test_client, test_zim):
|
|||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_find_outdated_zim_files(test_session, test_directory, test_zim_bytes, test_async_client):
|
||||
async def test_find_outdated_zim_files(test_session, test_directory, test_zim_bytes, async_client):
|
||||
"""Outdated Zim files are found and reported. They can automatically be deleted."""
|
||||
# Can search empty directory.
|
||||
outdated, current = lib.find_outdated_zim_files()
|
||||
|
|
@ -217,11 +217,11 @@ async def test_find_outdated_zim_files(test_session, test_directory, test_zim_by
|
|||
asdf.write_bytes(test_zim_bytes)
|
||||
empty = test_directory / 'zims/wikipedia_en_all_maxi_2023-03.zim' # empty files should be ignored.
|
||||
empty.touch()
|
||||
request, response = await test_async_client.get('/api/zim/outdated')
|
||||
request, response = await async_client.get('/api/zim/outdated')
|
||||
assert response.status_code == HTTPStatus.OK
|
||||
assert response.json['outdated'] == [str(wikipedia_one.relative_to(test_directory)), ]
|
||||
|
||||
request, response = await test_async_client.delete('/api/zim/outdated')
|
||||
request, response = await async_client.delete('/api/zim/outdated')
|
||||
assert response.status_code == HTTPStatus.NO_CONTENT
|
||||
# Only the outdated was deleted.
|
||||
assert wikibooks_one.is_file()
|
||||
|
|
@ -232,12 +232,12 @@ async def test_find_outdated_zim_files(test_session, test_directory, test_zim_by
|
|||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_search_estimates(test_session, test_async_client, zim_factory):
|
||||
async def test_search_estimates(test_session, async_client, zim_factory):
|
||||
zim_factory('test zim')
|
||||
test_session.commit()
|
||||
|
||||
body = dict(search_str='one')
|
||||
request, response = await test_async_client.post('/api/zim/search_estimates', json=body)
|
||||
request, response = await async_client.post('/api/zim/search_estimates', json=body)
|
||||
assert response.status_code == HTTPStatus.OK
|
||||
assert len(response.json['zims_estimates']) == 1
|
||||
assert response.json['zims_estimates'][0]['estimate'] == 2
|
||||
|
|
|
|||
|
|
@ -12,10 +12,11 @@ from wrolpi.common import DownloadFileInfo, get_wrolpi_config
|
|||
from wrolpi.downloader import Download, import_downloads_config, save_downloads_config
|
||||
from wrolpi.files import lib as files_lib
|
||||
from wrolpi.files.models import FileGroup
|
||||
from wrolpi.switches import await_switches
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_zim(test_async_client, test_session, zim_path_factory):
|
||||
async def test_get_zim(async_client, test_session, zim_path_factory):
|
||||
zim_path_factory()
|
||||
await files_lib.refresh_files()
|
||||
|
||||
|
|
@ -26,7 +27,7 @@ async def test_get_zim(test_async_client, test_session, zim_path_factory):
|
|||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_zim_get_entry(test_async_client, test_session, zim_path_factory):
|
||||
async def test_zim_get_entry(async_client, test_session, zim_path_factory):
|
||||
zim_path_factory()
|
||||
await files_lib.refresh_files()
|
||||
|
||||
|
|
@ -50,14 +51,14 @@ async def test_zim_get_entry(test_async_client, test_session, zim_path_factory):
|
|||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_zim_bad_entry(test_async_client, test_session, test_zim, tag_factory):
|
||||
async def test_zim_bad_entry(async_client, test_session, test_zim, tag_factory):
|
||||
tag1 = await tag_factory()
|
||||
with pytest.raises(UnknownZimEntry):
|
||||
test_zim.tag_entry(tag1.name, 'path does not exist')
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_zim_get_entries_tags(test_async_client, test_session, test_zim, tag_factory):
|
||||
async def test_zim_get_entries_tags(async_client, test_session, test_zim, tag_factory):
|
||||
tag1, tag2 = await tag_factory('tag1'), await tag_factory('tag2')
|
||||
test_zim.tag_entry(tag1.name, 'one')
|
||||
test_zim.tag_entry(tag2.name, 'one')
|
||||
|
|
@ -69,7 +70,7 @@ async def test_zim_get_entries_tags(test_async_client, test_session, test_zim, t
|
|||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_tag_zim_entry_model(test_async_client, test_session, zim_factory, tag_factory):
|
||||
async def test_tag_zim_entry_model(async_client, test_session, zim_factory, tag_factory):
|
||||
tag = await tag_factory()
|
||||
tze = zim_factory('zim1').tag_entry(tag.name, 'one')
|
||||
# Test TagZimEntry.__repr__
|
||||
|
|
@ -77,7 +78,7 @@ async def test_tag_zim_entry_model(test_async_client, test_session, zim_factory,
|
|||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_zim_entries_with_tags(test_async_client, test_session, zim_factory, tag_factory):
|
||||
async def test_zim_entries_with_tags(async_client, test_session, zim_factory, tag_factory):
|
||||
"""Can fetch Zim Entries that have Tags. Can filter only those entries that are tagged on a specific Zim."""
|
||||
tag1, tag2 = await tag_factory(), await tag_factory()
|
||||
zim1, zim2 = zim_factory('zim1'), zim_factory('zim2')
|
||||
|
|
@ -126,7 +127,8 @@ def test_get_unique_paths():
|
|||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_zim_tags_config(test_session, test_directory, test_zim, tag_factory, test_tags_config, fake_now):
|
||||
async def test_zim_tags_config(async_client, test_session, test_directory, test_zim, tag_factory, test_tags_config,
|
||||
fake_now):
|
||||
fake_now(datetime.datetime(2000, 1, 1, 0, 0, 0, 1))
|
||||
config = tags.get_tags_config()
|
||||
assert not config.tag_zims
|
||||
|
|
@ -137,6 +139,7 @@ async def test_zim_tags_config(test_session, test_directory, test_zim, tag_facto
|
|||
test_zim.tag_entry(tag2.name, 'one')
|
||||
test_zim.tag_entry(tag1.name, 'two')
|
||||
test_session.commit()
|
||||
await await_switches()
|
||||
# Tags are created in empty database.
|
||||
assert {i[0] for i in test_session.query(tags.Tag.id)} == {1, 2}
|
||||
|
||||
|
|
@ -150,6 +153,7 @@ async def test_zim_tags_config(test_session, test_directory, test_zim, tag_facto
|
|||
# Remove a tag, the config should change.
|
||||
test_zim.untag_entry(tag1.name, 'one')
|
||||
test_session.commit()
|
||||
await await_switches()
|
||||
|
||||
config = tags.get_tags_config()
|
||||
assert config.tag_zims == [
|
||||
|
|
@ -246,7 +250,7 @@ async def test_zim_download(test_session, kiwix_download_zim, test_directory, te
|
|||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_zim_tag_migration(test_session, test_directory, zim_factory, tag_factory, test_tags_config):
|
||||
async def test_zim_tag_migration(async_client, test_session, test_directory, zim_factory, tag_factory, test_tags_config):
|
||||
"""When an outdated Zim file is deleted, the Tags should be moved to the latest Zim."""
|
||||
zim1 = zim_factory('wikipedia_en_all_maxi_2020-01.zim') # The outdated Zim.
|
||||
zim2 = zim_factory('wikipedia_en_all_maxi_2020-02.zim')
|
||||
|
|
@ -261,6 +265,7 @@ async def test_zim_tag_migration(test_session, test_directory, zim_factory, tag_
|
|||
zim3.tag_entry(tag2.name, 'one')
|
||||
test_session.commit()
|
||||
assert test_session.query(TagZimEntry).count() == 4
|
||||
await await_switches()
|
||||
|
||||
# All Zims have been tagged.
|
||||
tags_config = tags.get_tags_config()
|
||||
|
|
@ -352,7 +357,7 @@ async def test_zim_subscription_download_import(test_session, test_downloader_co
|
|||
assert download.url == 'https://download.kiwix.org/zim/wikisource/wikisource_en_all_maxi_'
|
||||
|
||||
# Save config file, delete all entries.
|
||||
await save_downloads_config()
|
||||
save_downloads_config()
|
||||
test_session.query(Download).delete()
|
||||
test_session.query(ZimSubscription).delete()
|
||||
test_session.commit()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue