Path: blob/master/tests/controller/test_session_store.py
896 views
# -*- coding: utf-8 -*-1# This program is free software; you can redistribute it and/or modify2# it under the terms of the GNU General Public License as published by3# the Free Software Foundation; either version 2 of the License, or4# (at your option) any later version.5#6# This program is distributed in the hope that it will be useful,7# but WITHOUT ANY WARRANTY; without even the implied warranty of8# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9# GNU General Public License for more details.10#11# You should have received a copy of the GNU General Public License12# along with this program; if not, write to the Free Software13# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,14# MA 02110-1301, USA.15#16# Author: Mauro Soria1718from __future__ import annotations1920import json21import os22import tempfile23from unittest import TestCase2425from lib.controller.session import SessionStore262728class TestSessionStore(TestCase):29def _write_json(self, path: str, payload: dict) -> None:30with open(path, "w", encoding="utf-8") as handle:31json.dump(payload, handle)3233def _write_session_dir(self, session_dir: str, url: str) -> None:34os.makedirs(session_dir, exist_ok=True)35self._write_json(36os.path.join(session_dir, SessionStore.FILES["meta"]),37{"version": SessionStore.SESSION_VERSION},38)39self._write_json(40os.path.join(session_dir, SessionStore.FILES["controller"]),41{"url": url, "directories": [], "jobs_processed": 1, "errors": 0},42)43self._write_json(44os.path.join(session_dir, SessionStore.FILES["options"]),45{"urls": ["https://example.com"]},46)4748def _write_session_file(self, session_file: str, url: str) -> None:49payload = {50"version": SessionStore.SESSION_VERSION,51"controller": {"url": url, "directories": [], "jobs_processed": 2, "errors": 0},52"dictionary": {"items": [], "index": 0, "extra": [], "extra_index": 0},53"options": {"urls": ["https://example.com"]},54}55self._write_json(session_file, payload)5657def test_list_sessions_recurses_and_includes_root_files(self):58with tempfile.TemporaryDirectory() as tmpdir:59nested_dir = os.path.join(tmpdir, "2024-01-01", "session_01")60self._write_session_dir(nested_dir, "https://nested.example.com")6162root_file = os.path.join(tmpdir, "session_root.json")63self._write_session_file(root_file, "https://root.example.com")6465sessions = SessionStore({}).list_sessions(tmpdir)6667self.assertEqual(len(sessions), 2)68self.assertEqual(69[session["path"] for session in sessions],70sorted([nested_dir, root_file]),71)727374