test_models.py 3.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# Copyright 2014 Budapest University of Technology and Economics (BME IK)
#
# This file is part of CIRCLE Cloud.
#
# CIRCLE is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# CIRCLE is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with CIRCLE.  If not, see <http://www.gnu.org/licenses/>.

from collections import deque

from django.test import TestCase
Karsa Zoltán István committed
21
from unittest.mock import MagicMock
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

from .celery_mock import MockCeleryMixin
from .models import TestClass
from ..models import HumanSortField
from ..models import activitycontextimpl


class MethodCacheTestCase(MockCeleryMixin, TestCase):
    def test_cache(self):
        t1 = TestClass(1)
        t2 = TestClass(2)

        val1a = t1.method('a')
        val1b = t1.method('a')
        val2a = t2.method('a')
        val2b = t2.method('a')
        val2b = t2.method('a')

        self.assertEqual(val1a, val1b)
        self.assertEqual(val2a, val2b)
        self.assertNotEqual(val1a, val2a)

        t1.method('b')
        self.assertEqual(t1.called, 2)
        self.assertEqual(t2.called, 1)

    def test_invalidate(self):
        t1 = TestClass(1)
        val1a = t1.method('a')
        val1b = t1.method('a', invalidate_cache=True)
        t1.method('a')
        self.assertEqual(val1a, val1b)
        self.assertEqual(t1.called, 2)


class TestHumanSortField(TestCase):

    def test_partition(self):
        values = {(lambda s: s.isdigit(), "1234abc56"): ("1234", "abc", "56"),
                  (lambda s: s.isalpha(), "abc567"): ("abc", "567", ""),
                  (lambda s: s == "a", "aaababaa"): ("aaa", "b", "abaa"),
Kohl Krisztofer committed
63
                  (lambda s: s == "a", "aaababaa"): ("aaa", "b", "abaa"),
64
                  }
Kohl Krisztofer committed
65
        for (pred, val), result in list(values.items()):
66 67 68 69
            a, b, c = HumanSortField._partition(deque(val), pred)
            assert isinstance(c, deque)
            c = ''.join(c)
            # print "%s, %s => %s" % (val, str(pred), str((a, b, c)))
Kohl Krisztofer committed
70
            self.assertEqual((a, b, c), result)
71 72 73 74 75 76 77 78

    def test_get_normalized(self):
        values = {("1234abc56", 4): "1234abc0056",
                  ("abc567", 2): "abc567",
                  ("aaababaa", 8): "aaababaa",
                  ("aa4ababaa", 2): "aa04ababaa",
                  ("aa4aba24baa4", 4): "aa0004aba0024baa0004",
                  }
Kohl Krisztofer committed
79
        for (val, length), result in list(values.items()):
80 81 82 83
            obj = MagicMock(spec=HumanSortField, maximum_number_length=length,
                            _partition=HumanSortField._partition)

            test_result = HumanSortField.get_normalized_value(obj, val)
Kohl Krisztofer committed
84
            self.assertEqual(test_result, result)
85 86 87 88 89 90 91 92 93


class ActivityContextTestCase(TestCase):
    class MyException(Exception):
        pass

    def test_unicode(self):
        act = MagicMock()
        gen = activitycontextimpl(act)
Kohl Krisztofer committed
94
        next(gen)
95
        with self.assertRaises(self.MyException):
Kohl Krisztofer committed
96
            gen.throw(self.MyException('test\xe1'))
97 98 99 100

    def test_str(self):
        act = MagicMock()
        gen = activitycontextimpl(act)
Kohl Krisztofer committed
101
        next(gen)
102 103
        with self.assertRaises(self.MyException):
            gen.throw(self.MyException('test\xbe'))