Skip to content

Commit

Permalink
test(functions): test for ClickHouse functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jayvynl committed Dec 23, 2023
1 parent 9fd74c0 commit d82d2b2
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 0 deletions.
Empty file.
12 changes: 12 additions & 0 deletions tests/clickhouse_functions/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""
Tests for Function expressions.
"""
from clickhouse_backend import models


class Author(models.ClickhouseModel):
name = models.StringField(max_length=50)
alias = models.StringField(max_length=50, null=True, blank=True)
goes_by = models.StringField(max_length=50, null=True, blank=True)
birthday = models.DateTime64Field(null=True)
age = models.UInt16Field(default=30)
58 changes: 58 additions & 0 deletions tests/clickhouse_functions/test_datetime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from datetime import datetime

import pytz
from django.test import TestCase
from django.utils import timezone

from clickhouse_backend import models
from clickhouse_backend.utils.timezone import get_timezone

from .models import Author


class DateTimeTests(TestCase):
@classmethod
def setUpTestData(cls):
cls.john = Author.objects.create(
name="John Smith",
alias="smithj",
# https://stackoverflow.com/a/18862958
birthday=timezone.make_aware(
datetime(2023, 11, 30, 16), pytz.timezone(get_timezone())
),
)
cls.elena = Author.objects.create(
name="Élena Jordan",
alias="elena",
birthday=datetime(2023, 11, 30, 16, tzinfo=pytz.utc),
)

def test_yyyymm(self):
john = Author.objects.annotate(v=models.toYYYYMM("birthday")).get(
id=self.john.id
)
self.assertEqual(john.v, 202311)
elena = Author.objects.annotate(
v=models.toYYYYMM("birthday", "Asia/Shanghai")
).get(id=self.elena.id)
self.assertEqual(elena.v, 202312)

def test_yyyymmdd(self):
john = Author.objects.annotate(v=models.toYYYYMMDD("birthday")).get(
id=self.john.id
)
self.assertEqual(john.v, 20231130)
elena = Author.objects.annotate(
v=models.toYYYYMMDD("birthday", "Asia/Shanghai")
).get(id=self.elena.id)
self.assertEqual(elena.v, 20231201)

def test_yyyymmddhhmmss(self):
john = Author.objects.annotate(v=models.toYYYYMMDDhhmmss("birthday")).get(
id=self.john.id
)
self.assertEqual(john.v, 20231130160000)
elena = Author.objects.annotate(
v=models.toYYYYMMDDhhmmss("birthday", "Asia/Shanghai")
).get(id=self.elena.id)
self.assertEqual(elena.v, 20231201000000)
54 changes: 54 additions & 0 deletions tests/clickhouse_functions/test_hashes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from django.test import TestCase

from clickhouse_backend import models

from .models import Author


class HashTests(TestCase):
@classmethod
def setUpTestData(cls):
cls.john = Author.objects.create(name="John Smith")

def test(self):
for func in [
models.halfMD5,
models.sipHash64,
models.sipHash128,
models.sipHash128Reference,
models.cityHash64,
models.farmHash64,
models.farmFingerprint64,
]:
Author.objects.annotate(
v=func("name", "alias", "goes_by", "birthday", "age")
)

for func in [
models.sipHash64Keyed,
models.sipHash128Keyed,
models.sipHash128ReferenceKeyed,
]:
Author.objects.annotate(
v=func("age", "age", "name", "alias", "goes_by", "birthday", "age")
)

for func in [
models.MD4,
models.MD5,
models.SHA1,
models.SHA224,
models.SHA256,
models.SHA512,
models.BLAKE3,
models.URLHash,
]:
Author.objects.annotate(v=func("name"))

for func in [
models.intHash32,
models.intHash64,
]:
Author.objects.annotate(v=func("age"))

Author.objects.annotate(v=models.URLHash("name", 2))
19 changes: 19 additions & 0 deletions tests/clickhouse_functions/test_other.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django.test import TestCase

from clickhouse_backend import models

from .models import Author


class OtherTests(TestCase):
@classmethod
def setUpTestData(cls):
cls.john = Author.objects.create(name="John Smith")

def test_currentdatabase(self):
john = Author.objects.annotate(v=models.currentDatabase()).get(id=self.john.id)
self.assertEqual(john.v, "test_default")

def test_hostname(self):
john = Author.objects.annotate(v=models.hostName()).get(id=self.john.id)
self.assertTrue(john.v)
14 changes: 14 additions & 0 deletions tests/clickhouse_functions/test_random.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from django.test import TestCase

from clickhouse_backend import models

from .models import Author


class RandomTests(TestCase):
@classmethod
def setUpTestData(cls):
cls.john = Author.objects.create(name="John Smith")

def test_rand(self):
Author.objects.annotate(v=models.Rand()).get(id=self.john.id)
25 changes: 25 additions & 0 deletions tests/clickhouse_functions/test_tuples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from django.test import TestCase

from clickhouse_backend import models

from .models import Author


class TupleTests(TestCase):
@classmethod
def setUpTestData(cls):
cls.john = Author.objects.create(name="John Smith", age=30)

def test_tuple(self):
john = Author.objects.annotate(v=models.Tuple("name", "age")).get(
id=self.john.id
)
self.assertEqual(john.v, ("John Smith", 30))

def test_tupleelement(self):
john = Author.objects.annotate(
v=models.tupleElement(
models.Tuple("name", "age"), 1, output_field=models.UInt16Field()
)
).get(id=self.john.id)
self.assertEqual(john.v, 30)

0 comments on commit d82d2b2

Please sign in to comment.