177 lines
4.6 KiB
Python
177 lines
4.6 KiB
Python
import os
|
|
import pickle
|
|
import shutil
|
|
|
|
import pytest
|
|
|
|
from django.utils.six import BytesIO
|
|
from quixote import cleanup
|
|
|
|
from wcs.categories import Category, CardDefCategory
|
|
|
|
from utilities import create_temporary_pub
|
|
|
|
|
|
def setup_module(module):
|
|
cleanup()
|
|
|
|
global pub
|
|
|
|
pub = create_temporary_pub()
|
|
|
|
|
|
def teardown_module(module):
|
|
shutil.rmtree(pub.APP_DIR)
|
|
|
|
|
|
@pytest.mark.parametrize('category_class', [Category, CardDefCategory])
|
|
def test_store(category_class):
|
|
category_class.wipe()
|
|
test = category_class()
|
|
test.name = 'Test'
|
|
test.description = 'Hello world'
|
|
test.store()
|
|
test2 = category_class.get(1)
|
|
assert test.id == test2.id
|
|
assert test.name == test2.name
|
|
assert test.description == test2.description
|
|
|
|
|
|
@pytest.mark.parametrize('category_class', [Category, CardDefCategory])
|
|
def test_urlname(category_class):
|
|
category_class.wipe()
|
|
test = category_class()
|
|
test.name = 'Test'
|
|
test.description = 'Hello world'
|
|
test.store()
|
|
test = category_class.get(1)
|
|
assert test.url_name == 'test'
|
|
|
|
|
|
@pytest.mark.parametrize('category_class', [Category, CardDefCategory])
|
|
def test_duplicate_urlname(category_class):
|
|
category_class.wipe()
|
|
test = category_class()
|
|
test.name = 'Test'
|
|
test.store()
|
|
test = category_class.get(1)
|
|
assert test.url_name == 'test'
|
|
|
|
test2 = category_class()
|
|
test2.name = 'Test'
|
|
test2.store()
|
|
test2 = category_class.get(2)
|
|
assert test2.url_name == 'test-2'
|
|
|
|
|
|
@pytest.mark.parametrize('category_class', [Category, CardDefCategory])
|
|
def test_sort_positions(category_class):
|
|
category_class.wipe()
|
|
|
|
categories = []
|
|
for i in range(10):
|
|
test = category_class()
|
|
test.name = 'Test %s' % i
|
|
test.position = 10-i
|
|
categories.append(test)
|
|
|
|
# unset some positions, those categories will appear last
|
|
for i in range(8, 10):
|
|
categories[i].position = None
|
|
|
|
category_class.sort_by_position(categories)
|
|
assert categories[0].name == 'Test 7'
|
|
assert categories[-1].name in ('Test 8', 'Test 9')
|
|
|
|
|
|
@pytest.mark.parametrize('category_class', [Category, CardDefCategory])
|
|
def test_xml_export(category_class):
|
|
category_class.wipe()
|
|
test = category_class()
|
|
test.id = 1
|
|
test.name = 'Test'
|
|
test.description = 'Hello world'
|
|
test.store()
|
|
test = category_class.get(1)
|
|
|
|
assert b'<name>Test</name>' in test.export_to_xml_string(include_id=True)
|
|
assert b' id="1"' in test.export_to_xml_string(include_id=True)
|
|
assert b' id="1"' not in test.export_to_xml_string(include_id=False)
|
|
|
|
|
|
@pytest.mark.parametrize('category_class', [Category, CardDefCategory])
|
|
def test_xml_import(category_class):
|
|
category_class.wipe()
|
|
test = category_class()
|
|
test.name = 'Test'
|
|
test.description = 'Hello world'
|
|
test.store()
|
|
test = category_class.get(1)
|
|
|
|
fd = BytesIO(test.export_to_xml_string(include_id=True))
|
|
test2 = category_class.import_from_xml(fd, include_id=True)
|
|
assert test.id == test2.id
|
|
assert test.name == test2.name
|
|
assert test.description == test2.description
|
|
|
|
|
|
def test_load_old_pickle():
|
|
Category.wipe()
|
|
|
|
test = Category()
|
|
test.id = '1'
|
|
test.name = 'Test'
|
|
test.description = 'Hello world'
|
|
|
|
os.mkdir(os.path.join(pub.app_dir, 'categories'))
|
|
fd = open(os.path.join(pub.app_dir, 'categories', '1'), 'wb')
|
|
pickle.dump(test, fd)
|
|
fd.close()
|
|
|
|
test2 = Category.get(1)
|
|
assert test.id == test2.id
|
|
assert test.name == test2.name
|
|
assert test.description == test2.description
|
|
|
|
|
|
@pytest.mark.parametrize('category_class', [Category, CardDefCategory])
|
|
def test_get_by_urlname(category_class):
|
|
category_class.wipe()
|
|
test = category_class()
|
|
test.id = 1
|
|
test.name = 'Test'
|
|
test.description = 'Hello world'
|
|
test.store()
|
|
test = category_class.get(1)
|
|
test2 = category_class.get_by_urlname('test')
|
|
assert test.id == test2.id
|
|
|
|
|
|
@pytest.mark.parametrize('category_class', [Category, CardDefCategory])
|
|
def test_has_urlname(category_class):
|
|
category_class.wipe()
|
|
test = category_class()
|
|
test.id = 1
|
|
test.name = 'Test'
|
|
test.description = 'Hello world'
|
|
test.store()
|
|
test = category_class.get(1)
|
|
|
|
assert category_class.has_urlname('test')
|
|
assert not category_class.has_urlname('foobar')
|
|
|
|
|
|
@pytest.mark.parametrize('category_class', [Category, CardDefCategory])
|
|
def test_remove_self(category_class):
|
|
category_class.wipe()
|
|
test = category_class()
|
|
test.id = 1
|
|
test.name = 'Test'
|
|
test.description = 'Hello world'
|
|
test.store()
|
|
test = category_class.get(1)
|
|
test.remove_self()
|
|
|
|
with pytest.raises(KeyError):
|
|
category_class.get(1)
|