# Path: libre/apps/origins/models.py
# class OriginPath(Origin, ContainerOrigin):
#     origin_type = _('disk path')
# 
#     path = models.TextField(blank=True, null=True, verbose_name=_('path to file'), help_text=_('Location to a file in the filesystem.'))
# 
#     def get_binary_iterator(self):
#         """
#         Generator to read a file piece by piece.
#         """
#         CHUNK_SIZE = 1024
#         file_object = open(self.path)
# 
#         while True:
#             data = file_object.read(CHUNK_SIZE)
#             if not data:
#                 break
#             yield data
# 
#         file_object.close()
# 
#     @property
#     def identifier(self):
#         return self.path
# 
#     class Meta:
#         verbose_name = _('disk path origin')
#         verbose_name_plural = _('disk path origins')
# 
# class OriginURLFile(OriginURL, ContainerOrigin):
#     origin_type = _('URL file')
# 
#     class Meta:
#         verbose_name = _('URL file origin')
#         verbose_name_plural = _('URL file origins')


import os
from django.conf import settings
from django.test import TestCase
from .models import OriginPath, OriginURLFile


class URLFileTestCase(TestCase):
    def setUp(self):
        self.origin_url = OriginURLFile.objects.create(label='test origin', url='http://www.census.gov/population/estimates/puerto-rico/prmunnet.txt')
        self.origin_url.copy_data()

    def tearDown(self):
        self.origin_url.discard_copy()

    def test_data_iterator_creation(self):
        self.assertEqual(temp_path_status(self.origin_url.data_iterator.name), 'File exists')

    def test_hash(self):
        self.assertEqual(self.origin_url.new_hash, '81f81877c664b9863628e253ebfdff2cc53b05cbf8020735cb37eef46901ebe8')

    def test_iterator(self):
        self.assertEqual(self.origin_url.data_iterator.next(), 'PR-99-1 Estimates of the Population of Puerto Rico Municipios, July 1, 1999, and\r\n')
        self.assertEqual(self.origin_url.data_iterator.next(), 'Demographic Components of Population Change: April 1, 1990 to July 1, 1999\r\n')
        self.origin_url.data_iterator.seek(0)
        self.assertEqual(self.origin_url.data_iterator.next(), 'PR-99-1 Estimates of the Population of Puerto Rico Municipios, July 1, 1999, and\r\n')

    def test_data_iterator(self):
        self.assertEqual(len(self.origin_url.data_iterator.read()), 10713)
        self.assertEqual(len(self.origin_url.data_iterator.read()), 0)


class OriginPathTestCase(TestCase):
    def setUp(self):
        self.origin_url = OriginPath.objects.create(label='test origin', path=os.path.join(settings.PROJECT_ROOT, 'contrib', 'sample_data', TEST_FIXED_WIDTH_FILE))

# Path: libre/apps/origins/models.py
# class OriginPath(Origin, ContainerOrigin):
#     origin_type = _('disk path')
# 
#     path = models.TextField(blank=True, null=True, verbose_name=_('path to file'), help_text=_('Location to a file in the filesystem.'))
# 
#     def get_binary_iterator(self):
#         """
#         Generator to read a file piece by piece.
#         """
#         CHUNK_SIZE = 1024
#         file_object = open(self.path)
# 
#         while True:
#             data = file_object.read(CHUNK_SIZE)
#             if not data:
#                 break
#             yield data
# 
#         file_object.close()
# 
#     @property
#     def identifier(self):
#         return self.path
# 
#     class Meta:
#         verbose_name = _('disk path origin')
#         verbose_name_plural = _('disk path origins')
# 
# class OriginURLFile(OriginURL, ContainerOrigin):
#     origin_type = _('URL file')
# 
#     class Meta:
#         verbose_name = _('URL file origin')
#         verbose_name_plural = _('URL file origins')


# Path: libre/apps/origins/tests.py
import os
from django.conf import settings
from django.test import TestCase
from .models import OriginPath, OriginURLFile



class URLFileTestCase(TestCase):
    def setUp(self):
        self.origin_url = OriginURLFile.objects.create(label='test origin', url='http://www.census.gov/population/estimates/puerto-rico/prmunnet.txt')
        self.origin_url.copy_data()

    def tearDown(self):
        self.origin_url.discard_copy()

    def test_data_iterator_creation(self):
        self.assertEqual(temp_path_status(self.origin_url.data_iterator.name), 'File exists')

    def test_hash(self):
        self.assertEqual(self.origin_url.new_hash, '81f81877c664b9863628e253ebfdff2cc53b05cbf8020735cb37eef46901ebe8')

    def test_iterator(self):
        self.assertEqual(self.origin_url.data_iterator.next(), 'PR-99-1 Estimates of the Population of Puerto Rico Municipios, July 1, 1999, and\r\n')
        self.assertEqual(self.origin_url.data_iterator.next(), 'Demographic Components of Population Change: April 1, 1990 to July 1, 1999\r\n')
        self.origin_url.data_iterator.seek(0)
        self.assertEqual(self.origin_url.data_iterator.next(), 'PR-99-1 Estimates of the Population of Puerto Rico Municipios, July 1, 1999, and\r\n')

    def test_data_iterator(self):
        self.assertEqual(len(self.origin_url.data_iterator.read()), 10713)
        self.assertEqual(len(self.origin_url.data_iterator.read()), 0)


class OriginPathTestCase(TestCase):
    def setUp(self):
        self.origin_url = OriginPath.objects.create(label='test origin', path=os.path.join(settings.PROJECT_ROOT, 'contrib', 'sample_data', TEST_FIXED_WIDTH_FILE))

        self.origin_url.copy_data()