# coding=utf-8
import os

from lxml import etree
from django.test import SimpleTestCase

from corehq.apps.app_manager.models import Application
from corehq.apps.app_manager.tests.util import SuiteMixin
from corehq.apps.app_manager.translations import escape_output_value

import commcare_translations


class AppManagerTranslationsTest(SimpleTestCase, SuiteMixin):
    root = os.path.dirname(__file__)
    file_path = ('data', 'suite')

    def test_escape_output_value(self):
        test_cases = [
            ('hello', '<value>hello</value>'),
            ('abc < def > abc', '<value>abc &lt; def &gt; abc</value>'),
            ("bee's knees", "<value>bee's knees</value>"),
            ('unfortunate <xml expression', '<value>unfortunate &lt;xml expression</value>'),
            (u'क्लिक', '<value>&#2325;&#2381;&#2354;&#2367;&#2325;</value>'),
            ('&#39', '<value>&amp;#39</value>'),
            ('Here is a ref <output value="/data/no_media" /> with some trailing text and bad < xml.',
             '<value>Here is a ref <output value="/data/no_media"/> with some trailing text and bad &lt; xml.</value>')

        ]
        for input, expected_output in test_cases:
            self.assertEqual(expected_output, etree.tostring(escape_output_value(input)))

    def test_language_names(self):
        app_json = self.get_json('app')
        app_json['langs'] = ['en', 'fra', 'hin', 'pol']
        app = Application.wrap(app_json)
        app.create_suite()
        app_strings = app.create_app_strings('default')
        app_strings_dict = commcare_translations.loads(app_strings)
        self.assertEqual(app_strings_dict['en'], 'English')
        self.assertEqual(app_strings_dict['fra'], u'Français')
        self.assertEqual(app_strings_dict['hin'], u'हिंदी')
        self.assertEqual(app_strings_dict['pol'], 'polski')

# coding=utf-8
import os

from lxml import etree
from django.test import TestCase

from corehq.apps.app_manager.models import Application
from corehq.apps.app_manager.tests.util import SuiteMixin
from corehq.apps.app_manager.translations import escape_output_value

import commcare_translations


class AppManagerTranslationsTest(TestCase, SuiteMixin):
    root = os.path.dirname(__file__)
    file_path = ('data', 'suite')

    def test_escape_output_value(self):
        test_cases = [
            ('hello', '<value>hello</value>'),
            ('abc < def > abc', '<value>abc &lt; def &gt; abc</value>'),
            ("bee's knees", "<value>bee's knees</value>"),
            ('unfortunate <xml expression', '<value>unfortunate &lt;xml expression</value>'),
            (u'क्लिक', '<value>&#2325;&#2381;&#2354;&#2367;&#2325;</value>'),
            ('&#39', '<value>&amp;#39</value>'),
            ('Here is a ref <output value="/data/no_media" /> with some trailing text and bad < xml.',
             '<value>Here is a ref <output value="/data/no_media"/> with some trailing text and bad &lt; xml.</value>')

        ]
        for input, expected_output in test_cases:
            self.assertEqual(expected_output, etree.tostring(escape_output_value(input)))

    def test_language_names(self):
        app_json = self.get_json('app')
        app_json['langs'] = ['en', 'fra', 'hin', 'pol']
        app = Application.wrap(app_json)
        app.create_suite()
        app_strings = app.create_app_strings('default')
        app_strings_dict = commcare_translations.loads(app_strings)
        self.assertEqual(app_strings_dict['en'], 'English')
        self.assertEqual(app_strings_dict['fra'], u'Français')
        self.assertEqual(app_strings_dict['hin'], u'हिंदी')
        self.assertEqual(app_strings_dict['pol'], 'polski')

fix broken test