# encoding: utf-8
#
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Author: Kyle Lahnakoski (kyle@lahnakoski.com)
#

from __future__ import division
from __future__ import unicode_literals

from mo_testing.fuzzytestcase import FuzzyTestCase

from pyLibrary.queries.query import _normalize_edge, _normalize_select
from tests.test_jx import NULL


class TestQueryNormalization(FuzzyTestCase):
    def test_complex_edge_with_no_name(self):
        edge = {"value": ["a", "c"]}
        self.assertRaises(Exception, _normalize_edge, edge)

    def test_complex_edge_value(self):
        edge = {"name": "n", "value": ["a", "c"]}

        result = _normalize_edge(edge)[0]
        expected = {"name": "n", "value": NULL, "domain": {"dimension": {"fields": ["a", "c"]}}}
        self.assertEqual(result, expected)

    def test_naming_select(self):
        select = {"value": "result.duration", "aggregate": "avg"}
        result = _normalize_select(select, None)
        #DEEP NAMES ARE ALLOWED, AND NEEDED TO BUILD STRUCTURE FROM A QUERY
        expected = [{"name": "result.duration", "value": "result.duration", "aggregate": "average"}]
        self.assertEqual(result, expected)

@@ -340,16 +340,59 @@
 terals%0A%0A
+from mo_json import value2json, json2value%0A
 from mo_
@@ -434,16 +434,16 @@
 estCase%0A
-
 %0Afrom py
@@ -510,39 +510,8 @@
 ect%0A
-from tests.test_jx import NULL%0A
 %0A%0Acl
@@ -796,32 +796,54 @@
        result = 
+json2value(value2json(
 _normalize_edge(
@@ -850,16 +850,18 @@
 edge)%5B0%5D
+))
 %0A       
@@ -877,35 +877,89 @@
  = %7B
-%22name%22: %22n%22, %22value%22: NULL,
+%0A            %22name%22: %22n%22,%0A            %22value%22: %7B%22tuple%22: %5B%22a%22, %22c%22%5D%7D,%0A           
  %22do
@@ -1002,16 +1002,25 @@
 , %22c%22%5D%7D%7D
+%0A        
 %7D%0A      

update test