import unittest

from ecmind_blue_client.ecm.builder import DmsQueryBuilderHol
from ecmind_blue_client.ecm.builder._common import DmsQueryParamValue
from ecmind_blue_client.ecm.builder._dms_query_builder_lol import DmsQueryBuilderLol


class TestQueryBuilder(unittest.TestCase):

    def test_hol_query_builder(self):
        b = DmsQueryBuilderHol()
        b.baseparams = True
        b.fileinfo = True
        b.icons = True
        b.maxhits = 100
        b.variants = True
        b.fulltext_query("Server-API")
        b.param("TableValue", "Test Table")

        with b.object_type("Unittest_DMS_Document") as ot:
            with ot.fields("ALL") as f:
                f.field("Name", 1, "ASC")

            with ot.condition_object() as co:
                with co.table_condition("TableField") as tc:
                    tc.table_column("TableCol1", DmsQueryParamValue("TableValue"))

            with ot.condition_object("Unittest_DMS_Subregister") as co:
                co.field_condition("Name", ["Subregister 1", "Subregister 2"], "IN")

        with b.object_type("Unittest_DMS_Subregister") as ot:
            with ot.condition_object() as co:
                co.field_condition("Name", "Subregister 1")

        xml = b.to_xml().decode("UTF-8")

        with open("tests/testdata/test_hol_query_builder.xml", "r", encoding="utf-8") as f:
            self.assertEqual(xml, f.read(), "HOL Query does not match")

    def test_lol_query_builder(self):
        b = DmsQueryBuilderLol()
        b.baseparams = True
        b.fileinfo = True
        b.icons = True
        b.maxhits = 100
        b.variants = True
        b.fulltext_query("Server-API")

        b.param("TableValue", "Test Table")

        with b.object_type("Unittest_DMS_Document") as ot:
            with ot.fields("ALL") as f:
                f.field("Name", 1, "ASC")

            with ot.condition_object() as co:
                with co.table_condition("TableField") as tc:
                    tc.table_column("TableCol1", DmsQueryParamValue("TableValue"))

            with ot.condition_object("Unittest_DMS_Subregister") as co:
                co.field_condition("Name", ["Subregister 1", "Subregister 2"], "IN")

        with b.object_type("Unittest_DMS_Subregister") as ot:
            with ot.condition_object() as co:
                co.field_condition("Name", "Subregister 1")

        xml = b.to_xml().decode("UTF-8")
        print(xml)

        with open("tests/testdata/test_lol_query_builder.xml", "r", encoding="utf-8") as f:
            self.assertEqual(xml, f.read(), "LOL Query does not match")


if __name__ == "__main__":
    unittest.main()
