#-*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.test import TestCase
from .models import Fruit
from .fixtures import create_fixtures
from nece.exceptions import NonTranslatableFieldError


class TranslationTest(TestCase):
    def setUp(self):
        create_fixtures()

    def test_basic_queries(self):
        Fruit.objects.all()
        Fruit.objects.filter(name='apple')
        Fruit.objects.values()
        Fruit.objects.values_list()
        Fruit.objects.earliest('pk')
        Fruit.objects.latest('pk')

    def test_language_filter(self):
        self.assertEqual(Fruit.objects.language('en_us').get(
            name='apple').name, 'apple')
        self.assertEqual(Fruit.objects.language('de_de').get(
            name="Apfel").name, 'Apfel')

    def test_language_or_default(self):
        fruits = Fruit.objects.language_or_default('tr_tr')
        self.assertEqual(fruits.count(), 3)

    def test_language_or_none(self):
        fruit = Fruit.objects.get(name='apple')
        self.assertEqual(fruit.language_or_none('en').name, 'apple')
        self.assertEqual(fruit.language_or_none('tr').name, 'elma')
        self.assertEqual(fruit.language_or_none('gibrish'), None)

    def test_language_switch(self):
        fruit = Fruit.objects.get(name='apple')
        self.assertEqual(fruit.name, 'apple')
        fruit.language('tr_tr')
        self.assertEqual(fruit.name, 'elma')
        self.assertEqual(fruit.default_language.name, 'apple')
        fruit.language('de_de')
        self.assertEqual(fruit.name, 'Apfel')
        self.assertEqual(fruit.default_language.name, 'apple')

    def test_save_correct_languages(self):
        fruit = Fruit.objects.get(name='apple')
        fruit.translate(name='not apple')
        fruit.language('tr_tr')
        fruit.translate(name='elma değil')
        self.assertEqual(fruit.translations['tr_tr']['name'], 'elma değil')
        fruit.language('de_de')
        fruit.translate(name='nicht Apfel')
        self.assertEqual(fruit.translations['de_de']['name'], 'nicht Apfel')
        self.assertEqual(fruit.default_language.name, 'not apple')
        fruit.save()

    def test_get_by_language(self):
        self.assertEqual(
            Fruit.objects.language('tr_tr').get(name='elma').name, 'elma')

    def test_nontranslatable_fields(self):
        fruit = Fruit.objects.get(name='apple')
        with self.assertRaises(NonTranslatableFieldError) as error:
            fruit.translate('it_it', dummy_field='hello')
        self.assertEqual(error.exception.fieldname, 'dummy_field')

    def test_translation_mapping(self):
        self.assertTrue(Fruit.objects.language('tr').exists())
        self.assertEqual(Fruit.objects.language('tr')[0].name, 'elma')

    def test_language_as_dict(self):
        fruit = Fruit.objects.get(name='apple')
        self.assertEqual(fruit.language_as_dict(),  # default lang
                         {'benefits': u'good for health', 'name': u'apple'})
        self.assertEqual(fruit.language_as_dict('en_us'),
                         {'benefits': u'good for health', 'name': u'apple'})
        fruit.translate('az_az', name='alma')
        self.assertEqual(fruit.language_as_dict('az_az'),
                         {'name': 'alma'})
        self.assertEqual(fruit.language_as_dict('non_existant'), {})

    def test_values(self):
        names = Fruit.objects.values()
        self.assertEqual(names.count(), Fruit.objects.count())
        self.assertEqual(len(names), Fruit.objects.count())

85a55a0d79b159083cf954017fc2aa7e52ae8cc3
<mask token>


class TranslationTest(TestCase):
    <mask token>

    def test_basic_queries(self):
        Fruit.objects.all()
        Fruit.objects.filter(name='apple')
        Fruit.objects.values()
        Fruit.objects.values_list()
        Fruit.objects.earliest('pk')
        Fruit.objects.latest('pk')

    def test_language_filter(self):
        self.assertEqual(Fruit.objects.language('en_us').get(name='apple').
            name, 'apple')
        self.assertEqual(Fruit.objects.language('de_de').get(name='Apfel').
            name, 'Apfel')

    def test_language_or_default(self):
        fruits = Fruit.objects.language_or_default('tr_tr')
        self.assertEqual(fruits.count(), 3)
    <mask token>

    def test_language_switch(self):
        fruit = Fruit.objects.get(name='apple')
        self.assertEqual(fruit.name, 'apple')
        fruit.language('tr_tr')
        self.assertEqual(fruit.name, 'elma')
        self.assertEqual(fruit.default_language.name, 'apple')
        fruit.language('de_de')
        self.assertEqual(fruit.name, 'Apfel')
        self.assertEqual(fruit.default_language.name, 'apple')
    <mask token>
    <mask token>
    <mask token>

    def test_translation_mapping(self):
        self.assertTrue(Fruit.objects.language('tr').exists())
        self.assertEqual(Fruit.objects.language('tr')[0].name, 'elma')

    def test_language_as_dict(self):
        fruit = Fruit.objects.get(name='apple')
        self.assertEqual(fruit.language_as_dict(), {'benefits':
            u'good for health', 'name': u'apple'})
        self.assertEqual(fruit.language_as_dict('en_us'), {'benefits':
            u'good for health', 'name': u'apple'})
        fruit.translate('az_az', name='alma')
        self.assertEqual(fruit.language_as_dict('az_az'), {'name': 'alma'})
        self.assertEqual(fruit.language_as_dict('non_existant'), {})

    def test_values(self):
        names = Fruit.objects.values()
        self.assertEqual(names.count(), Fruit.objects.count())
        self.assertEqual(len(names), Fruit.objects.count())

<mask token>


class TranslationTest(TestCase):
    <mask token>

    def test_basic_queries(self):
        Fruit.objects.all()
        Fruit.objects.filter(name='apple')
        Fruit.objects.values()
        Fruit.objects.values_list()
        Fruit.objects.earliest('pk')
        Fruit.objects.latest('pk')

    def test_language_filter(self):
        self.assertEqual(Fruit.objects.language('en_us').get(name='apple').
            name, 'apple')
        self.assertEqual(Fruit.objects.language('de_de').get(name='Apfel').
            name, 'Apfel')

    def test_language_or_default(self):
        fruits = Fruit.objects.language_or_default('tr_tr')
        self.assertEqual(fruits.count(), 3)
    <mask token>

    def test_language_switch(self):
        fruit = Fruit.objects.get(name='apple')
        self.assertEqual(fruit.name, 'apple')
        fruit.language('tr_tr')
        self.assertEqual(fruit.name, 'elma')
        self.assertEqual(fruit.default_language.name, 'apple')
        fruit.language('de_de')
        self.assertEqual(fruit.name, 'Apfel')
        self.assertEqual(fruit.default_language.name, 'apple')
    <mask token>
    <mask token>

    def test_nontranslatable_fields(self):
        fruit = Fruit.objects.get(name='apple')
        with self.assertRaises(NonTranslatableFieldError) as error:
            fruit.translate('it_it', dummy_field='hello')
        self.assertEqual(error.exception.fieldname, 'dummy_field')

    def test_translation_mapping(self):
        self.assertTrue(Fruit.objects.language('tr').exists())
        self.assertEqual(Fruit.objects.language('tr')[0].name, 'elma')

    def test_language_as_dict(self):
        fruit = Fruit.objects.get(name='apple')
        self.assertEqual(fruit.language_as_dict(), {'benefits':
            u'good for health', 'name': u'apple'})
        self.assertEqual(fruit.language_as_dict('en_us'), {'benefits':
            u'good for health', 'name': u'apple'})
        fruit.translate('az_az', name='alma')
        self.assertEqual(fruit.language_as_dict('az_az'), {'name': 'alma'})
        self.assertEqual(fruit.language_as_dict('non_existant'), {})

    def test_values(self):
        names = Fruit.objects.values()
        self.assertEqual(names.count(), Fruit.objects.count())
        self.assertEqual(len(names), Fruit.objects.count())

<mask token>


class TranslationTest(TestCase):

    def setUp(self):
        create_fixtures()

    def test_basic_queries(self):
        Fruit.objects.all()
        Fruit.objects.filter(name='apple')
        Fruit.objects.values()
        Fruit.objects.values_list()
        Fruit.objects.earliest('pk')
        Fruit.objects.latest('pk')

    def test_language_filter(self):
        self.assertEqual(Fruit.objects.language('en_us').get(name='apple').
            name, 'apple')
        self.assertEqual(Fruit.objects.language('de_de').get(name='Apfel').
            name, 'Apfel')

    def test_language_or_default(self):
        fruits = Fruit.objects.language_or_default('tr_tr')
        self.assertEqual(fruits.count(), 3)

    def test_language_or_none(self):
        fruit = Fruit.objects.get(name='apple')
        self.assertEqual(fruit.language_or_none('en').name, 'apple')
        self.assertEqual(fruit.language_or_none('tr').name, 'elma')
        self.assertEqual(fruit.language_or_none('gibrish'), None)

    def test_language_switch(self):
        fruit = Fruit.objects.get(name='apple')
        self.assertEqual(fruit.name, 'apple')
        fruit.language('tr_tr')
        self.assertEqual(fruit.name, 'elma')
        self.assertEqual(fruit.default_language.name, 'apple')
        fruit.language('de_de')
        self.assertEqual(fruit.name, 'Apfel')
        self.assertEqual(fruit.default_language.name, 'apple')

    def test_save_correct_languages(self):
        fruit = Fruit.objects.get(name='apple')
        fruit.translate(name='not apple')
        fruit.language('tr_tr')
        fruit.translate(name='elma değil')
        self.assertEqual(fruit.translations['tr_tr']['name'], 'elma değil')
        fruit.language('de_de')
        fruit.translate(name='nicht Apfel')
        self.assertEqual(fruit.translations['de_de']['name'], 'nicht Apfel')
        self.assertEqual(fruit.default_language.name, 'not apple')
        fruit.save()
    <mask token>

    def test_nontranslatable_fields(self):
        fruit = Fruit.objects.get(name='apple')
        with self.assertRaises(NonTranslatableFieldError) as error:
            fruit.translate('it_it', dummy_field='hello')
        self.assertEqual(error.exception.fieldname, 'dummy_field')

    def test_translation_mapping(self):
        self.assertTrue(Fruit.objects.language('tr').exists())
        self.assertEqual(Fruit.objects.language('tr')[0].name, 'elma')

    def test_language_as_dict(self):
        fruit = Fruit.objects.get(name='apple')
        self.assertEqual(fruit.language_as_dict(), {'benefits':
            u'good for health', 'name': u'apple'})
        self.assertEqual(fruit.language_as_dict('en_us'), {'benefits':
            u'good for health', 'name': u'apple'})
        fruit.translate('az_az', name='alma')
        self.assertEqual(fruit.language_as_dict('az_az'), {'name': 'alma'})
        self.assertEqual(fruit.language_as_dict('non_existant'), {})

    def test_values(self):
        names = Fruit.objects.values()
        self.assertEqual(names.count(), Fruit.objects.count())
        self.assertEqual(len(names), Fruit.objects.count())

<mask token>


class TranslationTest(TestCase):

    def setUp(self):
        create_fixtures()

    def test_basic_queries(self):
        Fruit.objects.all()
        Fruit.objects.filter(name='apple')
        Fruit.objects.values()
        Fruit.objects.values_list()
        Fruit.objects.earliest('pk')
        Fruit.objects.latest('pk')

    def test_language_filter(self):
        self.assertEqual(Fruit.objects.language('en_us').get(name='apple').
            name, 'apple')
        self.assertEqual(Fruit.objects.language('de_de').get(name='Apfel').
            name, 'Apfel')

    def test_language_or_default(self):
        fruits = Fruit.objects.language_or_default('tr_tr')
        self.assertEqual(fruits.count(), 3)

    def test_language_or_none(self):
        fruit = Fruit.objects.get(name='apple')
        self.assertEqual(fruit.language_or_none('en').name, 'apple')
        self.assertEqual(fruit.language_or_none('tr').name, 'elma')
        self.assertEqual(fruit.language_or_none('gibrish'), None)

    def test_language_switch(self):
        fruit = Fruit.objects.get(name='apple')
        self.assertEqual(fruit.name, 'apple')
        fruit.language('tr_tr')
        self.assertEqual(fruit.name, 'elma')
        self.assertEqual(fruit.default_language.name, 'apple')
        fruit.language('de_de')
        self.assertEqual(fruit.name, 'Apfel')
        self.assertEqual(fruit.default_language.name, 'apple')

    def test_save_correct_languages(self):
        fruit = Fruit.objects.get(name='apple')
        fruit.translate(name='not apple')
        fruit.language('tr_tr')
        fruit.translate(name='elma değil')
        self.assertEqual(fruit.translations['tr_tr']['name'], 'elma değil')
        fruit.language('de_de')
        fruit.translate(name='nicht Apfel')
        self.assertEqual(fruit.translations['de_de']['name'], 'nicht Apfel')
        self.assertEqual(fruit.default_language.name, 'not apple')
        fruit.save()

    def test_get_by_language(self):
        self.assertEqual(Fruit.objects.language('tr_tr').get(name='elma').
            name, 'elma')

    def test_nontranslatable_fields(self):
        fruit = Fruit.objects.get(name='apple')
        with self.assertRaises(NonTranslatableFieldError) as error:
            fruit.translate('it_it', dummy_field='hello')
        self.assertEqual(error.exception.fieldname, 'dummy_field')

    def test_translation_mapping(self):
        self.assertTrue(Fruit.objects.language('tr').exists())
        self.assertEqual(Fruit.objects.language('tr')[0].name, 'elma')

    def test_language_as_dict(self):
        fruit = Fruit.objects.get(name='apple')
        self.assertEqual(fruit.language_as_dict(), {'benefits':
            u'good for health', 'name': u'apple'})
        self.assertEqual(fruit.language_as_dict('en_us'), {'benefits':
            u'good for health', 'name': u'apple'})
        fruit.translate('az_az', name='alma')
        self.assertEqual(fruit.language_as_dict('az_az'), {'name': 'alma'})
        self.assertEqual(fruit.language_as_dict('non_existant'), {})

    def test_values(self):
        names = Fruit.objects.values()
        self.assertEqual(names.count(), Fruit.objects.count())
        self.assertEqual(len(names), Fruit.objects.count())

#-*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.test import TestCase
from .models import Fruit
from .fixtures import create_fixtures
from nece.exceptions import NonTranslatableFieldError


class TranslationTest(TestCase):
    def setUp(self):
        create_fixtures()

    def test_basic_queries(self):
        Fruit.objects.all()
        Fruit.objects.filter(name='apple')
        Fruit.objects.values()
        Fruit.objects.values_list()
        Fruit.objects.earliest('pk')
        Fruit.objects.latest('pk')

    def test_language_filter(self):
        self.assertEqual(Fruit.objects.language('en_us').get(
            name='apple').name, 'apple')
        self.assertEqual(Fruit.objects.language('de_de').get(
            name="Apfel").name, 'Apfel')

    def test_language_or_default(self):
        fruits = Fruit.objects.language_or_default('tr_tr')
        self.assertEqual(fruits.count(), 3)

    def test_language_or_none(self):
        fruit = Fruit.objects.get(name='apple')
        self.assertEqual(fruit.language_or_none('en').name, 'apple')
        self.assertEqual(fruit.language_or_none('tr').name, 'elma')
        self.assertEqual(fruit.language_or_none('gibrish'), None)

    def test_language_switch(self):
        fruit = Fruit.objects.get(name='apple')
        self.assertEqual(fruit.name, 'apple')
        fruit.language('tr_tr')
        self.assertEqual(fruit.name, 'elma')
        self.assertEqual(fruit.default_language.name, 'apple')
        fruit.language('de_de')
        self.assertEqual(fruit.name, 'Apfel')
        self.assertEqual(fruit.default_language.name, 'apple')

    def test_save_correct_languages(self):
        fruit = Fruit.objects.get(name='apple')
        fruit.translate(name='not apple')
        fruit.language('tr_tr')
        fruit.translate(name='elma değil')
        self.assertEqual(fruit.translations['tr_tr']['name'], 'elma değil')
        fruit.language('de_de')
        fruit.translate(name='nicht Apfel')
        self.assertEqual(fruit.translations['de_de']['name'], 'nicht Apfel')
        self.assertEqual(fruit.default_language.name, 'not apple')
        fruit.save()

    def test_get_by_language(self):
        self.assertEqual(
            Fruit.objects.language('tr_tr').get(name='elma').name, 'elma')

    def test_nontranslatable_fields(self):
        fruit = Fruit.objects.get(name='apple')
        with self.assertRaises(NonTranslatableFieldError) as error:
            fruit.translate('it_it', dummy_field='hello')
        self.assertEqual(error.exception.fieldname, 'dummy_field')

    def test_translation_mapping(self):
        self.assertTrue(Fruit.objects.language('tr').exists())
        self.assertEqual(Fruit.objects.language('tr')[0].name, 'elma')

    def test_language_as_dict(self):
        fruit = Fruit.objects.get(name='apple')
        self.assertEqual(fruit.language_as_dict(),  # default lang
                         {'benefits': u'good for health', 'name': u'apple'})
        self.assertEqual(fruit.language_as_dict('en_us'),
                         {'benefits': u'good for health', 'name': u'apple'})
        fruit.translate('az_az', name='alma')
        self.assertEqual(fruit.language_as_dict('az_az'),
                         {'name': 'alma'})
        self.assertEqual(fruit.language_as_dict('non_existant'), {})

    def test_values(self):
        names = Fruit.objects.values()
        self.assertEqual(names.count(), Fruit.objects.count())
        self.assertEqual(len(names), Fruit.objects.count())
