Add tests. Setup coverage.

This commit is contained in:
Norman Jäckel 2013-10-30 17:48:09 +01:00
parent c5827e0612
commit a35d31aaa2
5 changed files with 111 additions and 6 deletions

2
fabfile.py vendored
View File

@ -31,7 +31,7 @@ def coverage_report_plain():
Runs all tests and prints the coverage report.
"""
test()
local('coverage report -m')
local('coverage report -m --fail-under=73')
def coverage():

View File

@ -13,6 +13,7 @@ from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
from django.dispatch import receiver
from django.test.client import Client
from mock import patch
from openslides.config.api import (config, ConfigGroup, ConfigGroupedPage,
ConfigPage, ConfigVariable)
@ -210,6 +211,18 @@ class ConfigFormTest(TestCase):
text = '<script src="/static/javascript/test-config-djg4dFGVslk4209f.js" type="text/javascript"></script>'
self.assertContains(response=response, text=text, status_code=200)
@patch('openslides.config.views.FormView.get_context_data')
def test_extra_stylefiles_other_context(self, mock_get_context_data):
"""
Tests the view with empty context data at the beginning.
"""
mock_get_context_data.return_value = {}
response = self.client_manager.get('/config/testgroupedpage1/')
text1 = '<link href="/static/styles/test-config-sjNN56dFGDrg2.css" type="text/css" rel="stylesheet" />'
text2 = '<script src="/static/javascript/test-config-djg4dFGVslk4209f.js" type="text/javascript"></script>'
self.assertContains(response=response, text=text1, status_code=200)
self.assertContains(response=response, text=text2, status_code=200)
class ConfigWeightTest(TestCase):

View File

@ -9,16 +9,53 @@
"""
from django.test.client import Client
from mock import MagicMock, patch
from openslides import get_version
from openslides.agenda.models import Item
from openslides.participant.models import User
from openslides.utils.test import TestCase
class VersionViewTest(TestCase):
def test_get(self):
def setUp(self):
User.objects.create_user('CoreMaximilian', 'xxx@xx.xx', 'default')
client = Client()
client.login(username='CoreMaximilian', password='default')
response = client.get('/version/')
self.client = Client()
self.client.login(username='CoreMaximilian', password='default')
def test_get(self):
response = self.client.get('/version/')
self.assertContains(response, get_version(), status_code=200)
@patch('openslides.core.views.settings')
def test_with_missing_plugin(self, mock_settings):
"""
Tests that an not existing app does not appear on the version view.
"""
mock_settings.INSTALLED_PLUGINS = ('unexisting_app_nvhbkdfgmnsd',)
response = self.client.get('/version/')
self.assertNotContains(response, 'unexisting_app_nvhbkdfgmnsd', status_code=200)
@patch('openslides.core.views.settings')
@patch('openslides.core.views.import_module')
def test_with_plugin_without_version(self, mock_import_module, mock_settings):
"""
Tests that an exisiting app does not appear in the version view if
there are no version data.
"""
mock_settings.INSTALLED_PLUGINS = ('existing_app_without_version',)
mock_module = MagicMock(spec=['some_useless_attribute_ghbnckj756j36'])
mock_import_module.configure_mock(return_value=mock_module)
response = self.client.get('/version/')
self.assertNotContains(response, 'unexisting_app_nvhbkdfgmnsd', status_code=200)
class SearchViewTest(TestCase):
def test_simple_search(self):
Item.objects.create(title='agenda_item_bnghfdjkgndkjdfg')
User.objects.create_user('CoreMaximilian', 'xxx@xx.xx', 'default')
self.client = Client()
self.client.login(username='CoreMaximilian', password='default')
response = self.client.get('/search/?q=agenda_item_bnghfd')
text = '<span class="highlighted">agenda_item_bnghfd</span>jkgndkjdfg'
self.assertContains(response, text)

View File

@ -7,6 +7,10 @@
:license: GNU GPL, see LICENSE for more details.
"""
import tempfile
from mock import MagicMock, patch
from openslides import get_git_commit_id, get_version
from openslides.utils.test import TestCase
@ -24,10 +28,28 @@ class InitTest(TestCase):
self.assertEqual(get_version(version=(2, 5, 3, 'alpha', 0), release=True), '2.5.3a0')
self.assertEqual(get_version(version=(2, 5, 3, 'final', 0), release=True), '2.5.3')
def test_get_git_commit_id(self):
def test_get_git_commit_id_general(self):
"""
Tests the lenght of the git commit id.
"""
git_commit_id = get_git_commit_id()
if not git_commit_id == 'unknown':
self.assertEqual(len(git_commit_id), 40)
@patch('__builtin__.open', MagicMock(side_effect=IOError))
def test_get_commit_id_unknown(self):
"""
Tests unknown git commit id.
"""
self.assertEqual(get_git_commit_id(), 'unknown')
@patch('__builtin__.open')
def test_get_commit_id_without_ref(self, mock):
"""
Tests reading the content of the git_commit_id file.
"""
with tempfile.TemporaryFile() as git_file:
git_file.write('test_id_ahyuGo7yefai7Nai')
git_file.seek(0)
mock.return_value = git_file
self.assertEqual(get_git_commit_id(), 'test_id_ahyuGo7yefai7Nai')

View File

@ -12,13 +12,18 @@ import os
import sys
from django.core.exceptions import ImproperlyConfigured
from mock import MagicMock, patch
from openslides.utils.main import (
get_browser_url,
get_database_path_from_settings,
get_default_settings_path,
get_default_user_data_path,
get_port,
get_portable_paths,
get_user_data_path_values,
setup_django_settings_module,
start_browser,
UNIX_VERSION,
WINDOWS_PORTABLE_VERSION)
from openslides.utils.test import TestCase
@ -64,3 +69,31 @@ class TestFunctions(TestCase):
self.assertEqual(get_browser_url('123.456.789.365', 80), 'http://123.456.789.365')
self.assertEqual(get_browser_url('0.0.0.0', 6789), 'http://localhost:6789')
self.assertEqual(get_browser_url('0.0.0.0', 80), 'http://localhost')
def test_get_port(self):
class MyException(Exception):
pass
self.assertEqual(get_port('localhost', 8234), 8234)
with patch('openslides.utils.main.socket') as mock_socket:
mock_socket.error = MyException
mock_socket.socket().listen = MagicMock(side_effect=MyException)
self.assertEqual(get_port('localhost', 80), 8000)
@patch('openslides.utils.main.time')
@patch('openslides.utils.main.webbrowser')
def test_start_browser(self, mock_webbrowser, mock_time):
browser_mock = MagicMock()
mock_webbrowser.get.return_value = browser_mock
start_browser('http://localhost:8234')
browser_mock.open.assert_called_with('http://localhost:8234')
def test_get_database_path_from_settings_memory(self):
self.assertEqual(get_database_path_from_settings(), ':memory:')
@patch('openslides.utils.main.get_win32_portable_path')
def test_get_portable_paths(self, mock_get_win32_portable_path):
mock_get_win32_portable_path.return_value = '/test_path_AhgheeGee1eixaeYe1ra'
self.assertEqual(get_portable_paths('database'), '/test_path_AhgheeGee1eixaeYe1ra/openslides/database.sqlite')
self.assertEqual(get_portable_paths('media'), '/test_path_AhgheeGee1eixaeYe1ra/openslides/media/')
self.assertEqual(get_portable_paths('whoosh_index'), '/test_path_AhgheeGee1eixaeYe1ra/openslides/whoosh_index/')
self.assertRaisesMessage(TypeError, 'Unknown type unknown_string', get_portable_paths, 'unknown_string')