Simplified tests structure

This commit is contained in:
Sergey Fursov 2013-12-28 14:40:29 +04:00
parent 3b0328ca1e
commit 017df0ddcf
3 changed files with 86 additions and 101 deletions

View File

@ -22,23 +22,15 @@ Tests for Radicale.
""" """
import os import os
import shutil
import sys import sys
import tempfile
from dulwich.repo import Repo
from io import BytesIO from io import BytesIO
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__))) sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
import radicale
os.environ["RADICALE_CONFIG"] = os.path.join(os.path.dirname( os.environ["RADICALE_CONFIG"] = os.path.join(os.path.dirname(
os.path.dirname(__file__)), "config") os.path.dirname(__file__)), "config")
from radicale import config
from .helpers import get_file_content from .helpers import get_file_content
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
class BaseTest(object): class BaseTest(object):
@ -70,83 +62,3 @@ class BaseTest(object):
"""Put the response values into the current application.""" """Put the response values into the current application."""
self.application._status = status self.application._status = status
self.application._headers = headers 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

View File

@ -27,18 +27,25 @@ import hashlib
import os import os
import radicale import radicale
import tempfile import tempfile
from tests import AuthSystem
from radicale import config from radicale import config
from radicale.auth import htpasswd from radicale.auth import htpasswd
from tests import BaseTest
class TestBaseAuthRequests(AuthSystem): class TestBaseAuthRequests(BaseTest):
""" """
Tests basic requests with auth. Tests basic requests with auth.
We should setup auth for each type before create Application object 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): def test_root(self):
self.colpath = tempfile.mkdtemp() self.colpath = tempfile.mkdtemp()
htpasswd_file_path = os.path.join(self.colpath, ".htpasswd") htpasswd_file_path = os.path.join(self.colpath, ".htpasswd")

View File

@ -21,11 +21,15 @@ Radicale tests with simple requests.
""" """
from . import (FileSystem, MultiFileSystem, DataBaseSystem,
GitFileSystem, GitMultiFileSystem)
from .helpers import get_file_content from .helpers import get_file_content
import sys import radicale
from tests import CustomStorageSystem 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): class BaseRequests(object):
@ -82,10 +86,72 @@ class BaseRequests(object):
status, headers, answer = self.request("GET", "/calendar.ics/") status, headers, answer = self.request("GET", "/calendar.ics/")
assert "VEVENT" not in answer assert "VEVENT" not in answer
# Generate classes with different configs
cl_list = [FileSystem, MultiFileSystem, DataBaseSystem, class TestFileSystem(BaseRequests, BaseTest):
GitFileSystem, GitMultiFileSystem, CustomStorageSystem] """Base class for filesystem tests."""
for cl in cl_list: storage_type = "filesystem"
classname = "Test%s" % cl.__name__
setattr(sys.modules[__name__], def setup(self):
classname, type(classname, (BaseRequests, cl), {})) """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)