From 017df0ddcf2298568d0945fa1b2ece4117e77bea Mon Sep 17 00:00:00 2001 From: Sergey Fursov Date: Sat, 28 Dec 2013 14:40:29 +0400 Subject: [PATCH] Simplified tests structure --- tests/__init__.py | 88 ---------------------------------------------- tests/test_auth.py | 11 ++++-- tests/test_base.py | 88 ++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 86 insertions(+), 101 deletions(-) diff --git a/tests/__init__.py b/tests/__init__.py index 1621bae..6c4dd9b 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -22,23 +22,15 @@ Tests for Radicale. """ import os -import shutil import sys -import tempfile -from dulwich.repo import Repo from io import BytesIO sys.path.insert(0, os.path.dirname(os.path.dirname(__file__))) -import radicale - os.environ["RADICALE_CONFIG"] = os.path.join(os.path.dirname( os.path.dirname(__file__)), "config") -from radicale import config from .helpers import get_file_content -from sqlalchemy.orm import sessionmaker -from sqlalchemy import create_engine class BaseTest(object): @@ -70,83 +62,3 @@ class BaseTest(object): """Put the response values into the current application.""" self.application._status = status self.application._headers = headers - - -class FileSystem(BaseTest): - """Base class for filesystem tests.""" - storage_type = "filesystem" - - def setup(self): - """Setup function for each test.""" - self.colpath = tempfile.mkdtemp() - config.set("storage", "type", self.storage_type) - from radicale.storage import filesystem - filesystem.FOLDER = self.colpath - filesystem.GIT_REPOSITORY = None - self.application = radicale.Application() - - def teardown(self): - """Teardown function for each test.""" - shutil.rmtree(self.colpath) - - -class MultiFileSystem(FileSystem): - """Base class for multifilesystem tests.""" - storage_type = "multifilesystem" - - -class DataBaseSystem(BaseTest): - """Base class for database tests""" - def setup(self): - config.set("storage", "type", "database") - config.set("storage", "database_url", "sqlite://") - from radicale.storage import database - database.Session = sessionmaker() - database.Session.configure(bind=create_engine("sqlite://")) - session = database.Session() - for st in get_file_content("schema.sql").split(";"): - session.execute(st) - session.commit() - self.application = radicale.Application() - - -class GitFileSystem(FileSystem): - """Base class for filesystem tests using Git""" - def setup(self): - super(GitFileSystem, self).setup() - Repo.init(self.colpath) - from radicale.storage import filesystem - filesystem.GIT_REPOSITORY = Repo(self.colpath) - - -class GitMultiFileSystem(GitFileSystem, MultiFileSystem): - """Base class for multifilesystem tests using Git""" - - -class CustomStorageSystem(BaseTest): - """Base class for custom backend tests.""" - storage_type = "custom" - - def setup(self): - """Setup function for each test.""" - self.colpath = tempfile.mkdtemp() - config.set("storage", "type", self.storage_type) - config.set("storage", "custom_handler", "tests.custom.storage") - from tests.custom import storage - storage.FOLDER = self.colpath - storage.GIT_REPOSITORY = None - self.application = radicale.Application() - - def teardown(self): - """Teardown function for each test.""" - shutil.rmtree(self.colpath) - - -class AuthSystem(BaseTest): - """Base class to test Radicale with Htpasswd authentication""" - def setup(self): - self.userpass = "dG1wOmJlcG8=" - - def teardown(self): - config.set("auth", "type", "None") - radicale.auth.is_authenticated = lambda *_: True diff --git a/tests/test_auth.py b/tests/test_auth.py index fb31464..0a1f61e 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -27,18 +27,25 @@ import hashlib import os import radicale import tempfile -from tests import AuthSystem from radicale import config from radicale.auth import htpasswd +from tests import BaseTest -class TestBaseAuthRequests(AuthSystem): +class TestBaseAuthRequests(BaseTest): """ Tests basic requests with auth. We should setup auth for each type before create Application object """ + def setup(self): + self.userpass = "dG1wOmJlcG8=" + + def teardown(self): + config.set("auth", "type", "None") + radicale.auth.is_authenticated = lambda *_: True + def test_root(self): self.colpath = tempfile.mkdtemp() htpasswd_file_path = os.path.join(self.colpath, ".htpasswd") diff --git a/tests/test_base.py b/tests/test_base.py index d2c030c..cc1fc3d 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -21,11 +21,15 @@ Radicale tests with simple requests. """ -from . import (FileSystem, MultiFileSystem, DataBaseSystem, - GitFileSystem, GitMultiFileSystem) from .helpers import get_file_content -import sys -from tests import CustomStorageSystem +import radicale +import shutil +import tempfile +from dulwich.repo import Repo +from radicale import config +from sqlalchemy.orm import sessionmaker +from sqlalchemy import create_engine +from tests import BaseTest class BaseRequests(object): @@ -82,10 +86,72 @@ class BaseRequests(object): status, headers, answer = self.request("GET", "/calendar.ics/") assert "VEVENT" not in answer -# Generate classes with different configs -cl_list = [FileSystem, MultiFileSystem, DataBaseSystem, - GitFileSystem, GitMultiFileSystem, CustomStorageSystem] -for cl in cl_list: - classname = "Test%s" % cl.__name__ - setattr(sys.modules[__name__], - classname, type(classname, (BaseRequests, cl), {})) + +class TestFileSystem(BaseRequests, BaseTest): + """Base class for filesystem tests.""" + storage_type = "filesystem" + + def setup(self): + """Setup function for each test.""" + self.colpath = tempfile.mkdtemp() + config.set("storage", "type", self.storage_type) + from radicale.storage import filesystem + filesystem.FOLDER = self.colpath + filesystem.GIT_REPOSITORY = None + self.application = radicale.Application() + + def teardown(self): + """Teardown function for each test.""" + shutil.rmtree(self.colpath) + + +class TestMultiFileSystem(TestFileSystem): + """Base class for multifilesystem tests.""" + storage_type = "multifilesystem" + + +class TestDataBaseSystem(BaseRequests, BaseTest): + """Base class for database tests""" + def setup(self): + config.set("storage", "type", "database") + config.set("storage", "database_url", "sqlite://") + from radicale.storage import database + database.Session = sessionmaker() + database.Session.configure(bind=create_engine("sqlite://")) + session = database.Session() + for st in get_file_content("schema.sql").split(";"): + session.execute(st) + session.commit() + self.application = radicale.Application() + + +class TestGitFileSystem(TestFileSystem): + """Base class for filesystem tests using Git""" + def setup(self): + super(TestGitFileSystem, self).setup() + Repo.init(self.colpath) + from radicale.storage import filesystem + filesystem.GIT_REPOSITORY = Repo(self.colpath) + + +class TestGitMultiFileSystem(TestGitFileSystem, TestMultiFileSystem): + """Base class for multifilesystem tests using Git""" + + +class TestCustomStorageSystem(BaseRequests, BaseTest): + """Base class for custom backend tests.""" + storage_type = "custom" + + def setup(self): + """Setup function for each test.""" + self.colpath = tempfile.mkdtemp() + config.set("storage", "type", self.storage_type) + config.set("storage", "custom_handler", "tests.custom.storage") + from tests.custom import storage + storage.FOLDER = self.colpath + storage.GIT_REPOSITORY = None + self.application = radicale.Application() + + def teardown(self): + """Teardown function for each test.""" + shutil.rmtree(self.colpath) \ No newline at end of file