import os
import pdb
import unittest

from xml.etree import cElementTree as xtree
from binder import Binder, BinderKey
from yqltable import YqlTable

import readline, rlcompleter
readline.parse_and_bind('tab: complete')

class TestYqlTable(unittest.TestCase):

    def setUp(self,):
        self.table_desc = {
            'name': 'mytest',
            'author': 'josuebrunel',
            'apiKeyURL': 'http://josuebrunel.org/api',
            'documentationURL': 'http://josuebrunel.org/doc.html',
            'sampleQuery': 'SELECT * FROM mytable',
        }

        self.table = YqlTable(**self.table_desc)

        self.binder_desc = {
            'name': 'select',
            'itemPath': 'products.product',
            'produces': 'xml'
        }

        self.binder = Binder(**self.binder_desc)

        self.key_desc = {
            'id': 'artist',
            'type': 'xs:string',
            'paramType': 'path'
        }

        self.key = BinderKey(**self.key_desc)

    def test_add_binder(self,):
        self.assertEqual(self.table.addBinder(self.binder),True)
        xtree.tostring(self.table.etree)

    def test_add_input_to_binder(self,):
        self.assertEqual(self.binder.addInput(self.key),True)
        xtree.tostring(self.binder.etree)

    def test_add_function_from_file(self,):
        self.assertEqual(self.binder.addFunction('', from_file='jscode.js'),True)
        xtree.tostring(self.binder.etree)

    def test_save_file(self,):
        self.table.save()
        self.assertEquals(os.path.isfile('mytest.xml'),True) 

    def test_save_with_another_name(self):
        name = "toto"
        self.table.save(name)
        self.assertEquals(os.path.isfile(name+'.xml'),True)

    def test_save_to_different_location(self,):
        fname = "titi"
        path = 'data'
        name = os.path.join(path,fname)
        self.table.save(name=fname, path=path)
        self.assertEquals(os.path.isfile(name+'.xml'),True)

    def tearUp(self):
        os.path.unlink('mytest.xml')
        os.path.unlink('toto.xml')
        

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


@@ -30,16 +30,43 @@
 nittest%0A
+from xml.dom import minidom
 %0Afrom xm
@@ -1000,24 +1000,269 @@
 .key_desc)%0A%0A
+    def xml_pretty_print(self, data):%0A        %22%22%22Pretty print xml data%0A        %22%22%22%0A        raw_string = xtree.tostring(data, 'utf-8')%0A        parsed_string = minidom.parseString(raw_string)%0A        return parsed_string.toprettyxml(indent='%5Ct')%0A%0A
     def test
@@ -1273,32 +1273,32 @@
 _binder(self,):%0A
-
         self.ass
@@ -1350,38 +1350,51 @@
 ue)%0A        
-xtree.tostring
+print self.xml_pretty_print
 (self.table.
@@ -1508,38 +1508,51 @@
 ue)%0A        
-xtree.tostring
+print self.xml_pretty_print
 (self.binder
@@ -1694,30 +1694,43 @@
         
-xtree.tostring
+print self.xml_pretty_print
 (self.bi

add pretty print for xml in tests