import os
import pickle
import shutil

import pytest

from django.utils.six import BytesIO
from quixote import cleanup
from wcs import publisher

from wcs.categories import Category

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)


def test_store():
    Category.wipe()
    test = Category()
    test.name = 'Test'
    test.description = 'Hello world'
    test.store()
    test2 = Category.get(1)
    assert test.id == test2.id
    assert test.name == test2.name
    assert test.description == test2.description


def test_urlname():
    Category.wipe()
    test = Category()
    test.name = 'Test'
    test.description = 'Hello world'
    test.store()
    test = Category.get(1)
    assert test.url_name == 'test'


def test_sort_positions():
    Category.wipe()

    categories = []
    for i in range(10):
        test = Category()
        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.sort_by_position(categories)
    assert categories[0].name == 'Test 7'
    assert categories[-1].name in ('Test 8', 'Test 9')


def test_xml_export():
    Category.wipe()
    test = Category()
    test.id = 1
    test.name = 'Test'
    test.description = 'Hello world'
    test.store()
    test = Category.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)


def test_xml_import():
    Category.wipe()
    test = Category()
    test.name = 'Test'
    test.description = 'Hello world'
    test.store()
    test = Category.get(1)

    fd = BytesIO(test.export_to_xml_string(include_id=True))
    test2 = Category.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


def test_get_by_urlname():
    Category.wipe()
    test = Category()
    test.id = 1
    test.name = 'Test'
    test.description = 'Hello world'
    test.store()
    test = Category.get(1)
    test2 = Category.get_by_urlname('test')
    assert test.id == test2.id


def test_has_urlname():
    Category.wipe()
    test = Category()
    test.id = 1
    test.name = 'Test'
    test.description = 'Hello world'
    test.store()
    test = Category.get(1)

    assert Category.has_urlname('test')
    assert not Category.has_urlname('foobar')


def test_remove_self():
    Category.wipe()
    test = Category()
    test.id = 1
    test.name = 'Test'
    test.description = 'Hello world'
    test.store()
    test = Category.get(1)
    test.remove_self()

    with pytest.raises(KeyError):
        Category.get(1)
