# Path: ratechecker/dataset.py
# class Dataset(object):
#     loaders = {
#         "adjustment": AdjustmentLoader,
#         "product": ProductLoader,
#         "rate": RateLoader,
#         "region": RegionLoader,
#     }
# 
#     def __init__(self, f):
#         self.zf = ZipFile(f)
# 
#     @cached_property
#     def cover_sheet(self):
#         with self.zf.open("CoverSheet.xml") as f:
#             return CoverSheet(f)
# 
#     @cached_property
#     def timestamp(self):
#         ts = datetime.combine(self.cover_sheet.date, time.min)
#         return timezone.make_aware(ts, timezone.get_current_timezone())
# 
#     @cached_property
#     def filename_prefix(self):
#         return self.cover_sheet.date.strftime("%Y%m%d")
# 
#     def load(self):
#         # Sort the list of loaders so that Region loads last, as a bellwether
#         for key, loader_cls in sorted(self.loaders.items()):
#             try:
#                 f = self.datafile(key)
#             except KeyError:
#                 # The fees data is expected to be temporarily unavailable,
#                 # so if the fees file is not found, we skip it and
#                 # continue loading the other data types.
#                 if key == "fee":
#                     continue
#                 raise
# 
#             # The zip file may be opened as binary, but we want to process the
#             # files that it contains as text.
#             f_text = io.TextIOWrapper(f)
#             loader = loader_cls(f_text, data_timestamp=self.timestamp)
#             loader.load()
# 
#     def datafile(self, name):
#         filename = "{}_{}.txt".format(self.filename_prefix, name)
#         return self.zf.open(filename)
# 
# Path: ratechecker/models.py
# class Product(models.Model):
#     """Loan Product."""
# 
#     FIXED = "FIXED"
#     ARM = "ARM"
# 
#     PAYMENT_TYPE_CHOICES = (
#         (FIXED, "Fixed Rate Mortgage"),
#         (ARM, "Adjustable Rate Mortgage"),
#     )
# 
#     JUMBO = "JUMBO"
#     CONF = "CONF"
#     AGENCY = "AGENCY"
#     FHA = "FHA"
#     VA = "VA"
#     VA_HB = "VA-HB"
#     FHA_HB = "FHA-HB"
# 
#     LOAN_TYPE_CHOICES = (
#         (JUMBO, "Jumbo Mortgage"),
#         (CONF, "Conforming Loan"),
#         (AGENCY, "Agency Loan"),
#         (FHA, "Federal Housing Administration Loan"),
#         (VA, "Veterans Affairs Loan"),
#         (VA_HB, "VA-HB Loan"),
#         (FHA_HB, "FHA-HB Loan"),
#     )
# 
#     REFI = "REFI"
#     PURCH = "PURCH"
# 
#     LOAN_PURPOSE_CHOICES = ((REFI, "Refinance"), (PURCH, "Purchase"))
# 
#     ARM_TYPES = ["3-1", "5-1", "7-1", "10-1"]
# 
#     plan_id = models.IntegerField(primary_key=True)
#     institution = models.CharField(max_length=16)
#     loan_purpose = models.CharField(
#         max_length=12, choices=LOAN_PURPOSE_CHOICES
#     )
#     pmt_type = models.CharField(
#         max_length=12, choices=PAYMENT_TYPE_CHOICES, default=FIXED
#     )
#     loan_type = models.CharField(max_length=12, choices=LOAN_TYPE_CHOICES)
#     loan_term = models.IntegerField()
#     int_adj_term = models.IntegerField(
#         null=True,
#         help_text="1st part of the ARM definition. E.g. 5 in 5/1 ARM",
#     )
#     adj_period = models.PositiveSmallIntegerField(null=True)
#     io = models.BooleanField()
#     arm_index = models.CharField(max_length=96, null=True)
# 
#     iac_help = "Max percentage points the rate can adjust at first adjustment."
#     int_adj_cap = models.IntegerField(null=True, help_text=iac_help)
# 
#     cap_text = "Max percentage points adjustable at each subsequent adjustment"
#     annual_cap = models.IntegerField(null=True, help_text=cap_text)
# 
#     loan_cap = models.IntegerField(
#         null=True,
#         help_text="Total lifetime maximum change that the ARM rate can have.",
#     )
#     arm_margin = models.DecimalField(max_digits=6, decimal_places=4, null=True)
#     ai_value = models.DecimalField(max_digits=6, decimal_places=4, null=True)
#     min_ltv = models.DecimalField(max_digits=6, decimal_places=3, null=True)
#     max_ltv = models.DecimalField(max_digits=6, decimal_places=3, null=True)
#     min_fico = models.IntegerField()
#     max_fico = models.IntegerField()
#     min_loan_amt = models.DecimalField(max_digits=12, decimal_places=2)
#     max_loan_amt = models.DecimalField(max_digits=12, decimal_places=2)
# 
#     # We're not really using the next three fields, but I'll leave them
#     # here for completeness
#     single_family = models.BooleanField(default=True)
#     condo = models.BooleanField(default=False)
#     coop = models.BooleanField(default=False)
#     data_timestamp = models.DateTimeField()


import os
from datetime import date
from django.test import TestCase
from ratechecker.dataset import Dataset
from ratechecker.models import Product




SAMPLE_DATASET_FILENAME = os.path.abspath(
    os.path.join(__file__, "../../../data/sample.zip")
)


class TestDataset(TestCase):
    def setUp(self):
        self.expected_date = date(2017, 3, 2)

    def test_load_date(self):
        with open(SAMPLE_DATASET_FILENAME, "rb") as f:
            d = Dataset(f)
            self.assertEqual(d.cover_sheet.date, self.expected_date)

    def test_load_products(self):
        with open(SAMPLE_DATASET_FILENAME, "rb") as f:

# Path: ratechecker/dataset.py
# class Dataset(object):
#     loaders = {
#         "adjustment": AdjustmentLoader,
#         "product": ProductLoader,
#         "rate": RateLoader,
#         "region": RegionLoader,
#     }
# 
#     def __init__(self, f):
#         self.zf = ZipFile(f)
# 
#     @cached_property
#     def cover_sheet(self):
#         with self.zf.open("CoverSheet.xml") as f:
#             return CoverSheet(f)
# 
#     @cached_property
#     def timestamp(self):
#         ts = datetime.combine(self.cover_sheet.date, time.min)
#         return timezone.make_aware(ts, timezone.get_current_timezone())
# 
#     @cached_property
#     def filename_prefix(self):
#         return self.cover_sheet.date.strftime("%Y%m%d")
# 
#     def load(self):
#         # Sort the list of loaders so that Region loads last, as a bellwether
#         for key, loader_cls in sorted(self.loaders.items()):
#             try:
#                 f = self.datafile(key)
#             except KeyError:
#                 # The fees data is expected to be temporarily unavailable,
#                 # so if the fees file is not found, we skip it and
#                 # continue loading the other data types.
#                 if key == "fee":
#                     continue
#                 raise
# 
#             # The zip file may be opened as binary, but we want to process the
#             # files that it contains as text.
#             f_text = io.TextIOWrapper(f)
#             loader = loader_cls(f_text, data_timestamp=self.timestamp)
#             loader.load()
# 
#     def datafile(self, name):
#         filename = "{}_{}.txt".format(self.filename_prefix, name)
#         return self.zf.open(filename)
# 
# Path: ratechecker/models.py
# class Product(models.Model):
#     """Loan Product."""
# 
#     FIXED = "FIXED"
#     ARM = "ARM"
# 
#     PAYMENT_TYPE_CHOICES = (
#         (FIXED, "Fixed Rate Mortgage"),
#         (ARM, "Adjustable Rate Mortgage"),
#     )
# 
#     JUMBO = "JUMBO"
#     CONF = "CONF"
#     AGENCY = "AGENCY"
#     FHA = "FHA"
#     VA = "VA"
#     VA_HB = "VA-HB"
#     FHA_HB = "FHA-HB"
# 
#     LOAN_TYPE_CHOICES = (
#         (JUMBO, "Jumbo Mortgage"),
#         (CONF, "Conforming Loan"),
#         (AGENCY, "Agency Loan"),
#         (FHA, "Federal Housing Administration Loan"),
#         (VA, "Veterans Affairs Loan"),
#         (VA_HB, "VA-HB Loan"),
#         (FHA_HB, "FHA-HB Loan"),
#     )
# 
#     REFI = "REFI"
#     PURCH = "PURCH"
# 
#     LOAN_PURPOSE_CHOICES = ((REFI, "Refinance"), (PURCH, "Purchase"))
# 
#     ARM_TYPES = ["3-1", "5-1", "7-1", "10-1"]
# 
#     plan_id = models.IntegerField(primary_key=True)
#     institution = models.CharField(max_length=16)
#     loan_purpose = models.CharField(
#         max_length=12, choices=LOAN_PURPOSE_CHOICES
#     )
#     pmt_type = models.CharField(
#         max_length=12, choices=PAYMENT_TYPE_CHOICES, default=FIXED
#     )
#     loan_type = models.CharField(max_length=12, choices=LOAN_TYPE_CHOICES)
#     loan_term = models.IntegerField()
#     int_adj_term = models.IntegerField(
#         null=True,
#         help_text="1st part of the ARM definition. E.g. 5 in 5/1 ARM",
#     )
#     adj_period = models.PositiveSmallIntegerField(null=True)
#     io = models.BooleanField()
#     arm_index = models.CharField(max_length=96, null=True)
# 
#     iac_help = "Max percentage points the rate can adjust at first adjustment."
#     int_adj_cap = models.IntegerField(null=True, help_text=iac_help)
# 
#     cap_text = "Max percentage points adjustable at each subsequent adjustment"
#     annual_cap = models.IntegerField(null=True, help_text=cap_text)
# 
#     loan_cap = models.IntegerField(
#         null=True,
#         help_text="Total lifetime maximum change that the ARM rate can have.",
#     )
#     arm_margin = models.DecimalField(max_digits=6, decimal_places=4, null=True)
#     ai_value = models.DecimalField(max_digits=6, decimal_places=4, null=True)
#     min_ltv = models.DecimalField(max_digits=6, decimal_places=3, null=True)
#     max_ltv = models.DecimalField(max_digits=6, decimal_places=3, null=True)
#     min_fico = models.IntegerField()
#     max_fico = models.IntegerField()
#     min_loan_amt = models.DecimalField(max_digits=12, decimal_places=2)
#     max_loan_amt = models.DecimalField(max_digits=12, decimal_places=2)
# 
#     # We're not really using the next three fields, but I'll leave them
#     # here for completeness
#     single_family = models.BooleanField(default=True)
#     condo = models.BooleanField(default=False)
#     coop = models.BooleanField(default=False)
#     data_timestamp = models.DateTimeField()


# Path: ratechecker/tests/data/test_data.py
import os
from datetime import date
from django.test import TestCase
from ratechecker.dataset import Dataset
from ratechecker.models import Product





SAMPLE_DATASET_FILENAME = os.path.abspath(
    os.path.join(__file__, "../../../data/sample.zip")
)


class TestDataset(TestCase):
    def setUp(self):
        self.expected_date = date(2017, 3, 2)

    def test_load_date(self):
        with open(SAMPLE_DATASET_FILENAME, "rb") as f:
            d = Dataset(f)
            self.assertEqual(d.cover_sheet.date, self.expected_date)

    def test_load_products(self):
        with open(SAMPLE_DATASET_FILENAME, "rb") as f:

            Dataset(f).load()