Skip to content

Commit 5a42597

Browse files
committed
test(functions): test for ClickHouse functions
1 parent d60520c commit 5a42597

File tree

7 files changed

+182
-0
lines changed

7 files changed

+182
-0
lines changed

tests/clickhouse_functions/__init__.py

Whitespace-only changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
Tests for Function expressions.
3+
"""
4+
from clickhouse_backend import models
5+
6+
7+
class Author(models.ClickhouseModel):
8+
name = models.StringField(max_length=50)
9+
alias = models.StringField(max_length=50, null=True, blank=True)
10+
goes_by = models.StringField(max_length=50, null=True, blank=True)
11+
birthday = models.DateTime64Field(null=True)
12+
age = models.UInt16Field(default=30)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from datetime import datetime
2+
3+
import pytz
4+
from django.test import TestCase
5+
from django.utils import timezone
6+
7+
from clickhouse_backend import models
8+
from clickhouse_backend.utils.timezone import get_timezone
9+
10+
from .models import Author
11+
12+
13+
class DateTimeTests(TestCase):
14+
@classmethod
15+
def setUpTestData(cls):
16+
cls.john = Author.objects.create(
17+
name="John Smith",
18+
alias="smithj",
19+
# https://stackoverflow.com/a/18862958
20+
birthday=timezone.make_aware(
21+
datetime(2023, 11, 30, 16), pytz.timezone(get_timezone())
22+
),
23+
)
24+
cls.elena = Author.objects.create(
25+
name="Élena Jordan",
26+
alias="elena",
27+
birthday=datetime(2023, 11, 30, 16, tzinfo=pytz.utc),
28+
)
29+
30+
def test_yyyymm(self):
31+
john = Author.objects.annotate(v=models.toYYYYMM("birthday")).get(
32+
id=self.john.id
33+
)
34+
self.assertEqual(john.v, 202311)
35+
elena = Author.objects.annotate(
36+
v=models.toYYYYMM("birthday", "Asia/Shanghai")
37+
).get(id=self.elena.id)
38+
self.assertEqual(elena.v, 202312)
39+
40+
def test_yyyymmdd(self):
41+
john = Author.objects.annotate(v=models.toYYYYMMDD("birthday")).get(
42+
id=self.john.id
43+
)
44+
self.assertEqual(john.v, 20231130)
45+
elena = Author.objects.annotate(
46+
v=models.toYYYYMMDD("birthday", "Asia/Shanghai")
47+
).get(id=self.elena.id)
48+
self.assertEqual(elena.v, 20231201)
49+
50+
def test_yyyymmddhhmmss(self):
51+
john = Author.objects.annotate(v=models.toYYYYMMDDhhmmss("birthday")).get(
52+
id=self.john.id
53+
)
54+
self.assertEqual(john.v, 20231130160000)
55+
elena = Author.objects.annotate(
56+
v=models.toYYYYMMDDhhmmss("birthday", "Asia/Shanghai")
57+
).get(id=self.elena.id)
58+
self.assertEqual(elena.v, 20231201000000)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from django.test import TestCase
2+
3+
from clickhouse_backend import models
4+
5+
from .models import Author
6+
7+
8+
class HashTests(TestCase):
9+
@classmethod
10+
def setUpTestData(cls):
11+
cls.john = Author.objects.create(name="John Smith")
12+
13+
def test(self):
14+
for func in [
15+
models.halfMD5,
16+
models.sipHash64,
17+
models.sipHash128,
18+
models.sipHash128Reference,
19+
models.cityHash64,
20+
models.farmHash64,
21+
models.farmFingerprint64,
22+
]:
23+
Author.objects.annotate(
24+
v=func("name", "alias", "goes_by", "birthday", "age")
25+
)
26+
27+
for func in [
28+
models.sipHash64Keyed,
29+
models.sipHash128Keyed,
30+
models.sipHash128ReferenceKeyed,
31+
]:
32+
Author.objects.annotate(
33+
v=func("age", "age", "name", "alias", "goes_by", "birthday", "age")
34+
)
35+
36+
for func in [
37+
models.MD4,
38+
models.MD5,
39+
models.SHA1,
40+
models.SHA224,
41+
models.SHA256,
42+
models.SHA512,
43+
models.BLAKE3,
44+
models.URLHash,
45+
]:
46+
Author.objects.annotate(v=func("name"))
47+
48+
for func in [
49+
models.intHash32,
50+
models.intHash64,
51+
]:
52+
Author.objects.annotate(v=func("age"))
53+
54+
Author.objects.annotate(v=models.URLHash("name", 2))
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from django.test import TestCase
2+
3+
from clickhouse_backend import models
4+
5+
from .models import Author
6+
7+
8+
class OtherTests(TestCase):
9+
@classmethod
10+
def setUpTestData(cls):
11+
cls.john = Author.objects.create(name="John Smith")
12+
13+
def test_currentdatabase(self):
14+
john = Author.objects.annotate(v=models.currentDatabase()).get(id=self.john.id)
15+
self.assertEqual(john.v, "test_default")
16+
17+
def test_hostname(self):
18+
john = Author.objects.annotate(v=models.hostName()).get(id=self.john.id)
19+
self.assertTrue(john.v)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from django.test import TestCase
2+
3+
from clickhouse_backend import models
4+
5+
from .models import Author
6+
7+
8+
class RandomTests(TestCase):
9+
@classmethod
10+
def setUpTestData(cls):
11+
cls.john = Author.objects.create(name="John Smith")
12+
13+
def test_rand(self):
14+
Author.objects.annotate(v=models.Rand()).get(id=self.john.id)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from django.test import TestCase
2+
3+
from clickhouse_backend import models
4+
5+
from .models import Author
6+
7+
8+
class TupleTests(TestCase):
9+
@classmethod
10+
def setUpTestData(cls):
11+
cls.john = Author.objects.create(name="John Smith", age=30)
12+
13+
def test_tuple(self):
14+
john = Author.objects.annotate(v=models.Tuple("name", "age")).get(
15+
id=self.john.id
16+
)
17+
self.assertEqual(john.v, ("John Smith", 30))
18+
19+
def test_tupleelement(self):
20+
john = Author.objects.annotate(
21+
v=models.tupleElement(
22+
models.Tuple("name", "age"), 1, output_field=models.UInt16Field()
23+
)
24+
).get(id=self.john.id)
25+
self.assertEqual(john.v, 30)

0 commit comments

Comments
 (0)