2020-02-07 11:05:19 +03:00
|
|
|
import sys
|
|
|
|
from unittest import skipIf
|
|
|
|
|
2019-06-25 16:10:04 +03:00
|
|
|
from django.test import TestCase
|
2019-02-02 21:02:22 +03:00
|
|
|
|
|
|
|
from django_clickhouse.compatibility import namedtuple
|
|
|
|
|
|
|
|
|
|
|
|
class NamedTupleTest(TestCase):
|
|
|
|
def test_defaults(self):
|
2019-06-25 16:10:04 +03:00
|
|
|
TestTuple = namedtuple('TestTuple', ('a', 'b', 'c'), defaults=[3])
|
2019-02-02 21:02:22 +03:00
|
|
|
self.assertTupleEqual((1, 2, 3), tuple(TestTuple(1, b=2)))
|
|
|
|
self.assertTupleEqual((1, 2, 4), tuple(TestTuple(1, 2, 4)))
|
|
|
|
self.assertTupleEqual((1, 2, 4), tuple(TestTuple(a=1, b=2, c=4)))
|
|
|
|
|
2020-02-07 11:05:19 +03:00
|
|
|
@skipIf(sys.version_info < (3, 7),
|
|
|
|
"On python < 3.7 this error is not raised, as not given defaults are filled by None")
|
|
|
|
def test_no_required_value(self):
|
2019-06-25 16:10:04 +03:00
|
|
|
TestTuple = namedtuple('TestTuple', ('a', 'b', 'c'), defaults=[3])
|
2019-02-03 21:05:54 +03:00
|
|
|
|
2020-02-07 11:05:19 +03:00
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
TestTuple(b=1, c=4)
|
|
|
|
|
|
|
|
def test_duplicate_value(self):
|
|
|
|
TestTuple = namedtuple('TestTuple', ('a', 'b', 'c'), defaults=[3])
|
2019-02-02 21:02:22 +03:00
|
|
|
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
TestTuple(1, 2, 3, c=4)
|
|
|
|
|
|
|
|
def test_different_defaults(self):
|
|
|
|
# Test that 2 tuple type defaults don't affect each other
|
2019-06-25 16:10:04 +03:00
|
|
|
TestTuple = namedtuple('TestTuple', ('a', 'b', 'c'), defaults=[3])
|
|
|
|
OtherTuple = namedtuple('TestTuple', ('a', 'b', 'c'), defaults=[4])
|
2019-02-02 21:02:22 +03:00
|
|
|
t1 = TestTuple(a=1, b=2)
|
|
|
|
t2 = OtherTuple(a=3, b=4)
|
|
|
|
self.assertTupleEqual((1, 2, 3), tuple(t1))
|
|
|
|
self.assertTupleEqual((3, 4, 4), tuple(t2))
|
|
|
|
|
|
|
|
def test_defaults_cache(self):
|
|
|
|
# Test that 2 tuple instances don't affect each other's defaults
|
2019-06-25 16:10:04 +03:00
|
|
|
TestTuple = namedtuple('TestTuple', ('a', 'b', 'c'), defaults=[3])
|
2019-02-02 21:02:22 +03:00
|
|
|
self.assertTupleEqual((1, 2, 4), tuple(TestTuple(a=1, b=2, c=4)))
|
|
|
|
self.assertTupleEqual((1, 2, 3), tuple(TestTuple(a=1, b=2)))
|
|
|
|
|
2019-02-03 12:24:21 +03:00
|
|
|
def test_equal(self):
|
|
|
|
TestTuple = namedtuple('TestTuple', ('a', 'b', 'c'))
|
|
|
|
t1 = TestTuple(1, 2, 3)
|
|
|
|
t2 = TestTuple(1, 2, 3)
|
|
|
|
self.assertEqual(t1, t2)
|
|
|
|
self.assertEqual((1, 2, 3), t1)
|
|
|
|
|
2019-02-02 21:02:22 +03:00
|
|
|
|