1
0

__init__.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # -*- coding: utf-8 -*-
  2. #
  3. # This file is part of Radicale Server - Calendar Server
  4. # Copyright © 2012-2013 Guillaume Ayoub
  5. #
  6. # This library is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This library is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. Tests for Radicale.
  20. """
  21. import os
  22. import shutil
  23. import sys
  24. import tempfile
  25. from dulwich.repo import Repo
  26. from io import BytesIO
  27. sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
  28. import radicale
  29. from radicale import config
  30. from radicale.storage import filesystem, database
  31. from .helpers import get_file_content
  32. from sqlalchemy.orm import sessionmaker
  33. from sqlalchemy import create_engine
  34. class BaseTest(object):
  35. """Base class for tests."""
  36. def request(self, method, path, data=None, **args):
  37. """Send a request."""
  38. self.application._status = None
  39. self.application._headers = None
  40. self.application._answer = None
  41. for key in args:
  42. args[key.upper()] = args[key]
  43. args["REQUEST_METHOD"] = method.upper()
  44. args["PATH_INFO"] = path
  45. if data:
  46. args["wsgi.input"] = BytesIO(data)
  47. args["CONTENT_LENGTH"] = str(len(data))
  48. self.application._answer = self.application(args, self.start_response)
  49. return (
  50. int(self.application._status.split()[0]),
  51. dict(self.application._headers),
  52. self.application._answer[0].decode("utf-8")
  53. if self.application._answer else None)
  54. def start_response(self, status, headers):
  55. """Put the response values into the current application."""
  56. self.application._status = status
  57. self.application._headers = headers
  58. class FileSystem(BaseTest):
  59. """Base class for filesystem tests."""
  60. storage_type = "filesystem"
  61. def setup(self):
  62. """Setup function for each test."""
  63. self.colpath = tempfile.mkdtemp()
  64. config.set("storage", "type", self.storage_type)
  65. filesystem.FOLDER = self.colpath
  66. filesystem.GIT_REPOSITORY = None
  67. self.application = radicale.Application()
  68. def teardown(self):
  69. """Teardown function for each test."""
  70. shutil.rmtree(self.colpath)
  71. class MultiFileSystem(FileSystem):
  72. """Base class for multifilesystem tests."""
  73. storage_type = "multifilesystem"
  74. class DataBaseSystem(BaseTest):
  75. """Base class for database tests"""
  76. def setup(self):
  77. config.set("storage", "type", "database")
  78. config.set("storage", "database_url", "sqlite://")
  79. database.Session = sessionmaker()
  80. database.Session.configure(bind=create_engine("sqlite://"))
  81. session = database.Session()
  82. # session.execute(get_file_content("schema.sql"))
  83. for st in get_file_content("schema.sql").split(";"):
  84. session.execute(st)
  85. session.commit()
  86. self.application = radicale.Application()
  87. class GitFileSystem(FileSystem):
  88. """Base class for filesystem tests using Git"""
  89. def setup(self):
  90. super(GitFileSystem, self).setup()
  91. Repo.init(self.colpath)
  92. filesystem.GIT_REPOSITORY = Repo(self.colpath)
  93. class GitMultiFileSystem(GitFileSystem, MultiFileSystem):
  94. """Base class for multifilesystem tests using Git"""