# Path: tests/compat.py
# def wrap_testdata(func):
#     return func
# 
# Path: tests/testapp/models.py
# class Alphabet(Model):
#     a = IntegerField(default=1)
#     b = IntegerField(default=2)
#     c = IntegerField(default=3)
#     d = CharField(max_length=32)
#     e = CharField(max_length=32, null=True)
#     f = IntegerField(null=True)
#     g = DecimalField(default=0, decimal_places=2, max_digits=10)
# 
# class Author(Model):
#     name = CharField(max_length=32, db_index=True)
#     tutor = ForeignKey("self", on_delete=CASCADE, null=True, related_name="tutees")
#     bio = TextField()
#     birthday = DateTimeField(null=True)
#     deathday = DateTimeField(null=True)


import pytest
from django.db.models import F, TextField
from django.test import TestCase
from django_mysql.models import BitAnd, BitOr, BitXor, GroupConcat
from django_mysql.test.utils import override_mysql_variables
from tests.compat import wrap_testdata
from tests.testapp.models import Alphabet, Author
        # Manual:
        # "This function returns 0 if there were no matching rows."
        assert out == {"a__bitxor": 0}


class GroupConcatTests(TestCase):
    @classmethod
    @wrap_testdata
    def setUpTestData(cls):
        super().setUpTestData()
        cls.shakes = Author.objects.create(name="William Shakespeare")
        cls.jk = Author.objects.create(name="JK Rowling", tutor=cls.shakes)
        cls.grisham = Author.objects.create(name="Grisham", tutor=cls.shakes)

        cls.str_tutee_ids = [str(cls.jk.id), str(cls.grisham.id)]

    def test_basic_aggregate_ids(self):
        out = self.shakes.tutees.aggregate(tids=GroupConcat("id"))
        concatted_ids = ",".join(self.str_tutee_ids)
        assert out == {"tids": concatted_ids}

    def test_basic_aggregate_ids_output_field(self):
        out = self.shakes.tutees.aggregate(
            tids=GroupConcat("id", output_field=TextField())
        )
        concatted_ids = ",".join(self.str_tutee_ids)
        assert out == {"tids": concatted_ids}

    def test_basic_annotate_ids(self):
        concat = GroupConcat("tutees__id")

# Path: tests/compat.py
# def wrap_testdata(func):
#     return func
# 
# Path: tests/testapp/models.py
# class Alphabet(Model):
#     a = IntegerField(default=1)
#     b = IntegerField(default=2)
#     c = IntegerField(default=3)
#     d = CharField(max_length=32)
#     e = CharField(max_length=32, null=True)
#     f = IntegerField(null=True)
#     g = DecimalField(default=0, decimal_places=2, max_digits=10)
# 
# class Author(Model):
#     name = CharField(max_length=32, db_index=True)
#     tutor = ForeignKey("self", on_delete=CASCADE, null=True, related_name="tutees")
#     bio = TextField()
#     birthday = DateTimeField(null=True)
#     deathday = DateTimeField(null=True)


# Path: tests/testapp/test_aggregates.py
import pytest
from django.db.models import F, TextField
from django.test import TestCase
from django_mysql.models import BitAnd, BitOr, BitXor, GroupConcat
from django_mysql.test.utils import override_mysql_variables
from tests.compat import wrap_testdata
from tests.testapp.models import Alphabet, Author

        # Manual:
        # "This function returns 0 if there were no matching rows."
        assert out == {"a__bitxor": 0}


class GroupConcatTests(TestCase):
    @classmethod
    @wrap_testdata
    def setUpTestData(cls):
        super().setUpTestData()
        cls.shakes = Author.objects.create(name="William Shakespeare")
        cls.jk = Author.objects.create(name="JK Rowling", tutor=cls.shakes)
        cls.grisham = Author.objects.create(name="Grisham", tutor=cls.shakes)

        cls.str_tutee_ids = [str(cls.jk.id), str(cls.grisham.id)]

    def test_basic_aggregate_ids(self):
        out = self.shakes.tutees.aggregate(tids=GroupConcat("id"))
        concatted_ids = ",".join(self.str_tutee_ids)
        assert out == {"tids": concatted_ids}

    def test_basic_aggregate_ids_output_field(self):
        out = self.shakes.tutees.aggregate(
            tids=GroupConcat("id", output_field=TextField())
        )
        concatted_ids = ",".join(self.str_tutee_ids)
        assert out == {"tids": concatted_ids}

    def test_basic_annotate_ids(self):
        concat = GroupConcat("tutees__id")

        shakey = Author.objects.annotate(tids=concat).get(id=self.shakes.id)