anki-sync-server/tests/test_collection_wrappers.py

61 lines
1.9 KiB
Python
Raw Permalink Normal View History

# -*- coding: utf-8 -*-
import os
import unittest
import configparser
from ankisyncd.collection import CollectionWrapper
from ankisyncd.collection import get_collection_wrapper
import helpers.server_utils
2022-10-15 03:07:21 +08:00
class FakeCollectionWrapper(CollectionWrapper):
def __init__(self, config, path, setup_new_collection=None):
2022-10-15 03:07:21 +08:00
self._CollectionWrapper__col = None
pass
2022-10-15 03:07:21 +08:00
class BadCollectionWrapper:
pass
2022-10-15 03:07:21 +08:00
class CollectionWrapperFactoryTest(unittest.TestCase):
def test_get_collection_wrapper(self):
# Get absolute path to development ini file.
script_dir = os.path.dirname(os.path.realpath(__file__))
2022-10-15 03:07:21 +08:00
ini_file_path = os.path.join(script_dir, "assets", "test.conf")
# Create temporary files and dirs the server will use.
server_paths = helpers.server_utils.create_server_paths()
config = configparser.ConfigParser()
config.read(ini_file_path)
2022-10-15 03:07:21 +08:00
path = os.path.realpath("fake/collection.anki2")
# Use custom files and dirs in settings. Should be CollectionWrapper
2022-10-15 03:07:21 +08:00
config["sync_app"].update(server_paths)
self.assertTrue(
type(get_collection_wrapper(config["sync_app"], path) == CollectionWrapper)
)
# A conf-specified CollectionWrapper is loaded
2022-10-15 03:07:21 +08:00
config.set(
"sync_app",
"collection_wrapper",
"test_collection_wrappers.FakeCollectionWrapper",
)
self.assertTrue(
type(get_collection_wrapper(config["sync_app"], path))
== FakeCollectionWrapper
)
# Should fail at load time if the class doesn't inherit from CollectionWrapper
2022-10-15 03:07:21 +08:00
config.set(
"sync_app",
"collection_wrapper",
"test_collection_wrappers.BadCollectionWrapper",
)
with self.assertRaises(TypeError):
2022-10-15 03:07:21 +08:00
pm = get_collection_wrapper(config["sync_app"], path)