| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# This program is free software; you can redistribute it and/or modify |
| 3 |
# it under the terms of the GNU General Public License as published by |
| 4 |
# the Free Software Foundation; either version 2 of the License, or |
| 5 |
# (at your option) any later version. |
| 6 |
# |
| 7 |
# This program is distributed in the hope that it will be useful, |
| 8 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 |
# GNU General Public License for more details. |
| 11 |
# |
| 12 |
# You should have received a copy of the GNU General Public License |
| 13 |
# along with this program; if not, write to the Free Software |
| 14 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 15 |
# MA 02110-1301, USA. |
| 16 |
# |
| 17 |
# Author: Mauro Soria |
| 18 |
|
| 19 |
from __future__ import annotations |
| 20 |
|
| 21 |
import json |
| 22 |
import os |
| 23 |
import tempfile |
| 24 |
from unittest import TestCase |
| 25 |
|
| 26 |
from lib.controller.session import SessionStore |
| 27 |
|
| 28 |
|
| 29 |
class TestSessionStore(TestCase): |
| 30 |
def _write_json(self, path: str, payload: dict) -> None: |
| 31 |
with open(path, "w", encoding="utf-8") as handle: |
| 32 |
json.dump(payload, handle) |
| 33 |
|
| 34 |
def _write_session_dir(self, session_dir: str, url: str) -> None: |
| 35 |
os.makedirs(session_dir, exist_ok=True) |
| 36 |
self._write_json( |
| 37 |
os.path.join(session_dir, SessionStore.FILES["meta"]), |
| 38 |
{"version": SessionStore.SESSION_VERSION}, |
| 39 |
) |
| 40 |
self._write_json( |
| 41 |
os.path.join(session_dir, SessionStore.FILES["controller"]), |
| 42 |
{"url": url, "directories": [], "jobs_processed": 1, "errors": 0}, |
| 43 |
) |
| 44 |
self._write_json( |
| 45 |
os.path.join(session_dir, SessionStore.FILES["options"]), |
| 46 |
{"urls": ["https://example.com"]}, |
| 47 |
) |
| 48 |
|
| 49 |
def _write_session_file(self, session_file: str, url: str) -> None: |
| 50 |
payload = { |
| 51 |
"version": SessionStore.SESSION_VERSION, |
| 52 |
"controller": {"url": url, "directories": [], "jobs_processed": 2, "errors": 0}, |
| 53 |
"dictionary": {"items": [], "index": 0, "extra": [], "extra_index": 0}, |
| 54 |
"options": {"urls": ["https://example.com"]}, |
| 55 |
} |
| 56 |
self._write_json(session_file, payload) |
| 57 |
|
| 58 |
def test_list_sessions_recurses_and_includes_root_files(self): |
| 59 |
with tempfile.TemporaryDirectory() as tmpdir: |
| 60 |
nested_dir = os.path.join(tmpdir, "2024-01-01", "session_01") |
| 61 |
self._write_session_dir(nested_dir, "https://nested.example.com") |
| 62 |
|
| 63 |
root_file = os.path.join(tmpdir, "session_root.json") |
| 64 |
self._write_session_file(root_file, "https://root.example.com") |
| 65 |
|
| 66 |
sessions = SessionStore({}).list_sessions(tmpdir) |
| 67 |
|
| 68 |
self.assertEqual(len(sessions), 2) |
| 69 |
self.assertEqual( |
| 70 |
[session["path"] for session in sessions], |
| 71 |
sorted([nested_dir, root_file]), |
| 72 |
) |