#-*- coding: utf-8 -*-
'''
Created on Jan 23, 2013

@author: lennyn
'''

import unittest
import sys
import os
import re
import sqlite3
import schemaParser
import jsonpickle
import json
import StringIO

from const import DB_FILE


class TestParse(unittest.TestCase):

    def setUp(self):
        self.conn = sqlite3.connect(DB_FILE)

    def tearDown(self):
        self.conn.close()

    def sdftest_parse0(self):
        print unicode(schemaParser.parseSchema(u'Npn-{Or,Dupa,("na"^Tsacc)^"że"^S}')).encode('utf-8')

    def test_parse(self):
        print unicode(schemaParser.parseSchema(u'-("aż"^S)')).encode('utf-8')
#        print schemaParser.parseSchema('("dupa"^NPMod)')
#        print schemaParser.parseSchema('({("dupa"^NPMod)})')
#        print schemaParser.parseSchema('{Npn,K^S,"ze"^S}-Npacc')
#        print schemaParser.parseSchema('{Npn,K^S,"że"^S}-Npacc')
        for row in self.conn.execute('SELECT schema from t_schem;'):
            schemaText = row[0]
            try:
                schemaText = row[0]
                #                schemaText = re.sub(ur'("\)[ ]?([A-Z])', r'\1)^\2', schemaText)
                if schemaText != row[0]:
                    print u'{0} ===> {1}'.format(row[0], schemaText).encode('utf-8')
                res = schemaParser.parseSchema(schemaText)
                print unicode(res).encode('utf-8')
                print 'OK', row[0].encode('utf-8')
            except Exception as ex:
                print 'ERROR', schemaText.encode('utf-8'), ':::', ex, u'CHANGED FROM {0}'.format(row[0]).encode('utf-8') if schemaText != row[0] else ''
#            jsonStr = jsonpickle.encode(schemaParser.parseSchema(row[0]))
#            print json.dumps(json.load(StringIO.StringIO(jsonStr)), sort_keys=True, indent=4)
#            print '^^^^^^^^^'


if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()