from django.test import TestCase

from  .lib import build_dict, build_query, trim


class TestBuildQuery(TestCase):
    def test_build_dict(self):
        items = [
            'aa:{bb:{cc:{f3:{_op3:$f3}}}}',  # 1
            'aa:{bb:{f2:{_op2:$f2}}}',  # 2
            'aa:{dd:{f4:{_op4:$f4}}}',  # 3
            'aa:{bb:{f1:{_op1:$f1}}}',  # 1
        ]
        expected = {
            'aa': {
                'bb': {
                    'cc': {
                        'f3': {
                            '_op3': '$f3'
                        }
                    },
                    'f1': {
                        '_op1': '$f1'
                    },
                    'f2': {
                        '_op2': '$f2'
                    },
                },
                'dd': {
                    'f4': {
                        '_op4': '$f4'
                    }
                }
            }
        }
        assert build_dict(items) == expected

    def test_build_query(self):
        items = [
            'aa:{bb:{cc:{f3:{_op3:$f3}}}}',  # 1
            'aa:{bb:{f2:{_op2:$f2}}}',  # 2
            'aa:{dd:{f4:{_op4:$f4}}}',  # 3
            'aa:{bb:{f1:{_op1:$f1}}}',  # 1
        ]
        expected = '''
            aa: {
                bb: {
                    cc: {f3: {_op3: $f3}},
                    f1: {_op1: $f1},
                    f2: {_op2: $f2}
                },
                dd: {f4: {_op4: $f4}}
            }
            '''
        assert build_query(items) == trim(expected)