# -*- coding: utf-8 -*-
from ..cache import CachedTranslation

from .base import BaseTestCase
from .translations import FooModel


class CachedTranslationTest(BaseTestCase):
    """
    Tests CachedTranslation class.
    """

    def setUp(self):
        self.create_registry()

    def test_attributes(self):
        from ..models import Translation

        fields = Translation._meta.get_all_field_names()
        fields.remove('id')

        obj = CachedTranslation()

        self.assertTrue(obj.is_new)

        for attr in fields:
            self.assertTrue(hasattr(obj, attr))
            self.assertIsNone(getattr(obj, attr))

        for attr in ('instance', 'translation'):
            self.assertIsNone(getattr(obj, attr))

    def test_instance_and_translation(self):
        from ..models import Translation

        self.instance.language = 'en'
        self.instance.title = 'Hello'
        self.instance.save()

        translation = Translation.objects.first()
        obj = CachedTranslation(**{'instance': self.instance, 'translation': translation})

        self.assertTrue(isinstance(obj.instance, FooModel))
        self.assertTrue(isinstance(obj.translation, Translation))
        self.assertEqual(obj.identifier, self.instance.linguist_identifier)
        self.assertEqual(obj.object_id, self.instance.pk)
        self.assertEqual(obj.language, translation.language)
        self.assertEqual(obj.field_name, translation.field_name)
        self.assertEqual(obj.field_value, translation.field_value)

    def test_from_object(self):
        from ..models import Translation

        self.instance.language = 'en'
        self.instance.title = 'Hello'
        self.instance.save()

        translation = Translation.objects.first()
        obj = CachedTranslation()
        obj = obj.from_object(translation)

        self.assertEqual(obj.identifier, translation.identifier)
        self.assertEqual(obj.object_id, translation.object_id)
        self.assertEqual(obj.language, translation.language)
        self.assertEqual(obj.field_name, translation.field_name)
        self.assertEqual(obj.field_value, translation.field_value)

@@ -17,16 +17,49 @@
 f-8 -*-%0A
+from ..models import Translation%0A
 from ..c
@@ -126,27 +126,21 @@
 e%0Afrom .
-translation
+model
 s import
@@ -245,17 +245,16 @@
     %22%22%22%0A
-%0A
     def 
@@ -283,23 +283,69 @@
 elf.
-create_registry
+setup_models()%0A        self.instance.clear_translations_cache
 ()%0A%0A
@@ -358,21 +358,14 @@
 f te
-st_attributes
+arDown
 (sel
@@ -380,41 +380,80 @@
     
-from ..models import Translation%0A
+self.instance.clear_translations_cache()%0A%0A    def test_attributes(self):
 %0A   
@@ -883,50 +883,8 @@
 f):%0A
-        from ..models import Translation%0A%0A
     
@@ -897,32 +897,41 @@
 lf.instance.
+activate_
 language
  = 'en'%0A    
@@ -910,39 +910,38 @@
 ctivate_language
- = 
+(
 'en'
+)
 %0A        self.in
@@ -1625,50 +1625,8 @@
 f):%0A
-        from ..models import Translation%0A%0A
     
@@ -1643,24 +1643,33 @@
 nstance.
+activate_
 language
  = 'en'%0A
@@ -1664,15 +1664,14 @@
 uage
- = 
+(
 'en'
+)
 %0A   

Update test_cache module.