from decimal import Decimal
import os

from django.test import TestCase

from unipath import Path

from ....utils import refresh
from ...media import tests
from ..models import Transcript, TranscriptMedia

MEDIA_TESTDATA_PATH = Path(tests.__file__).parent.child('testdata')

RAW_MEDIA_PATH = MEDIA_TESTDATA_PATH.child('raw').child(
    'NA-472-2012-12-23-Final-excerpt.mp3').absolute()


class TranscriptsTestCase(TestCase):

    def test_transcript_starts_out_with_unknown_length(self):
        transcript = Transcript.objects.create(title='test')
        self.assertEqual(transcript.length, None)

    def test_setting_transcript_length_creates_fragments_and_stitches(self):
        t = Transcript.objects.create(title='test')
        t.set_length('3.33')
        f0, = t.fragments.all()
        self.assertEqual(f0.start, Decimal('0.00'))
        self.assertEqual(f0.end, Decimal('3.33'))
        self.assertEqual(t.stitches.count(), 0)

        t = Transcript.objects.create(title='test')
        t.set_length('7.77')
        f0, = t.fragments.all()
        self.assertEqual(f0.start, Decimal('0.00'))
        self.assertEqual(f0.end, Decimal('7.77'))
        self.assertEqual(t.stitches.count(), 0)

        t = Transcript.objects.create(title='test')
        t.set_length('17.77')
        f0, f1, f2 = t.fragments.all()
        self.assertEqual(f0.start, Decimal('0.00'))
        self.assertEqual(f0.end, Decimal('5.00'))
        self.assertEqual(f1.start, Decimal('5.00'))
        self.assertEqual(f1.end, Decimal('10.00'))
        self.assertEqual(f2.start, Decimal('10.00'))
        self.assertEqual(f2.end, Decimal('17.77'))
        s0, s1 = t.stitches.all()
        self.assertEqual(s0.left, f0)
        self.assertEqual(s0.right, f1)
        self.assertEqual(s0.state, 'notready')
        self.assertEqual(s1.left, f1)
        self.assertEqual(s1.right, f2)
        self.assertEqual(s1.state, 'notready')


if os.environ.get('FAST_TEST') != '1':

    from django.core.files import File


    class SlowTranscriptsTestCase(TestCase):

        def test_transcript_with_processed_media_has_length(self):

            transcript = Transcript.objects.create(
                title='test transcript',
            )
            raw_media = TranscriptMedia(
                transcript=transcript,
                is_processed=False,
                is_full_length=True,
            )
            with open(RAW_MEDIA_PATH, 'rb') as f:
                raw_media.file.save('{transcript.id}_raw.mp3'.format(**locals()), File(f))
            raw_media.save()

            # Process raw media.
            raw_media.create_processed_task()
            transcript = refresh(transcript)

            # Check length.
            expected_length = 5 * 60  # 5 minutes.
            self.assertAlmostEqual(
                transcript.length, expected_length, delta=0.2)

[((57, 3, 57, 30), 'os.environ.get', 'os.environ.get', ({(57, 18, 57, 29): '"""FAST_TEST"""'}, {}), "('FAST_TEST')", False, 'import os\n'), ((12, 22, 12, 42), 'unipath.Path', 'Path', ({(12, 27, 12, 41): 'tests.__file__'}, {}), '(tests.__file__)', False, 'from unipath import Path\n'), ((28, 35, 28, 50), 'decimal.Decimal', 'Decimal', ({(28, 43, 28, 49): '"""0.00"""'}, {}), "('0.00')", False, 'from decimal import Decimal\n'), ((29, 33, 29, 48), 'decimal.Decimal', 'Decimal', ({(29, 41, 29, 47): '"""3.33"""'}, {}), "('3.33')", False, 'from decimal import Decimal\n'), ((35, 35, 35, 50), 'decimal.Decimal', 'Decimal', ({(35, 43, 35, 49): '"""0.00"""'}, {}), "('0.00')", False, 'from decimal import Decimal\n'), ((36, 33, 36, 48), 'decimal.Decimal', 'Decimal', ({(36, 41, 36, 47): '"""7.77"""'}, {}), "('7.77')", False, 'from decimal import Decimal\n'), ((42, 35, 42, 50), 'decimal.Decimal', 'Decimal', ({(42, 43, 42, 49): '"""0.00"""'}, {}), "('0.00')", False, 'from decimal import Decimal\n'), ((43, 33, 43, 48), 'decimal.Decimal', 'Decimal', ({(43, 41, 43, 47): '"""5.00"""'}, {}), "('5.00')", False, 'from decimal import Decimal\n'), ((44, 35, 44, 50), 'decimal.Decimal', 'Decimal', ({(44, 43, 44, 49): '"""5.00"""'}, {}), "('5.00')", False, 'from decimal import Decimal\n'), ((45, 33, 45, 49), 'decimal.Decimal', 'Decimal', ({(45, 41, 45, 48): '"""10.00"""'}, {}), "('10.00')", False, 'from decimal import Decimal\n'), ((46, 35, 46, 51), 'decimal.Decimal', 'Decimal', ({(46, 43, 46, 50): '"""10.00"""'}, {}), "('10.00')", False, 'from decimal import Decimal\n'), ((47, 33, 47, 49), 'decimal.Decimal', 'Decimal', ({(47, 41, 47, 48): '"""17.77"""'}, {}), "('17.77')", False, 'from decimal import Decimal\n'), ((75, 82, 75, 89), 'django.core.files.File', 'File', ({(75, 87, 75, 88): 'f'}, {}), '(f)', False, 'from django.core.files import File\n')]