test_models.py 2.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# 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/>.

18 19
from collections import deque

20
from django.test import TestCase
21 22
from mock import MagicMock

23
from .models import TestClass
24
from ..models import HumanSortField
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


class MethodCacheTestCase(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)
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82


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"),
                  (lambda s: s == "a", u"aaababaa"): ("aaa", "b", "abaa"),
                  }
        for (pred, val), result in values.iteritems():
            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)))
            self.assertEquals((a, b, c), result)

    def test_get_normalized(self):
        values = {("1234abc56", 4): "1234abc0056",
                  ("abc567", 2): "abc567",
                  ("aaababaa", 8): "aaababaa",
                  ("aa4ababaa", 2): "aa04ababaa",
                  ("aa4aba24baa4", 4): "aa0004aba0024baa0004",
                  }
        for (val, length), result in values.iteritems():
            obj = MagicMock(spec=HumanSortField, maximum_number_length=length,
                            _partition=HumanSortField._partition)

            test_result = HumanSortField.get_normalized_value(obj, val)
            self.assertEquals(test_result, result)