<commit_before>from datetime import date, datetime
from django.core.files.base import ContentFile
from django.utils import timezone
from mock import Mock
from unittest import TestCase
from xml.etree.cElementTree import ParseError

from ratechecker.dataset import CoverSheet
from ratechecker.tests.helpers import get_sample_dataset


class TestDataset(TestCase):
    def test_cover_sheet(self):
        dataset = get_sample_dataset()
        self.assertIsInstance(dataset.cover_sheet, CoverSheet)

    def test_timestamp(self):
        dataset = get_sample_dataset(day=date(2017, 4, 3))
        self.assertEqual(
            dataset.timestamp,
            timezone.make_aware(
                datetime(2017, 4, 3, 0, 0, 0),
                timezone.get_current_timezone()
            )
        )

    def test_load_calls_loaders(self):
        dataset = get_sample_dataset(
            day=date(2017, 4, 3),
            datasets={
                '20170403_key.txt': 'testing',
            }
        )

        loader = Mock()
        loader.return_value = loader
        dataset.loaders = {'key': loader}
        dataset.load()

        loader.load.assert_called_once()

    def test_datafile(self):
        dataset = get_sample_dataset(
            day=date(2017, 4, 3),
            datasets={
                '20170403_something.txt': 'testing',
            }
        )

        f = dataset.datafile('something')
        self.assertEqual(f.read(), 'testing')


class TestCoverSheet(TestCase):
    def test_null_file_raises_typeerror(self):
        with self.assertRaises(TypeError):
            CoverSheet(None)

    def test_invalid_xml_file_raises_parseerror(self):
        f = ContentFile('foo')
        with self.assertRaises(ParseError):
            CoverSheet(f)

    def test_missing_date_raises_valueerror(self):
        f = ContentFile('<data></data>')
        with self.assertRaises(ValueError):
            CoverSheet(f)<commit_msg>Restore test coverage to 100%<commit_after>from datetime import date, datetime
from django.core.files.base import ContentFile
from django.utils import timezone
from mock import Mock
from unittest import TestCase
from xml.etree.cElementTree import ParseError

from ratechecker.dataset import CoverSheet
from ratechecker.tests.helpers import get_sample_dataset


class TestDataset(TestCase):
    def test_cover_sheet(self):
        dataset = get_sample_dataset()
        self.assertIsInstance(dataset.cover_sheet, CoverSheet)

    def test_timestamp(self):
        dataset = get_sample_dataset(day=date(2017, 4, 3))
        self.assertEqual(
            dataset.timestamp,
            timezone.make_aware(
                datetime(2017, 4, 3, 0, 0, 0),
                timezone.get_current_timezone()
            )
        )

    def test_load_calls_loaders(self):
        dataset = get_sample_dataset(
            day=date(2017, 4, 3),
            datasets={
                '20170403_key.txt': 'testing',
            }
        )

        loader = Mock()
        loader.return_value = loader
        dataset.loaders = {'key': loader}
        dataset.load()

        loader.load.assert_called_once()

    def test_load_fails_if_dataset_missing(self):
        dataset = get_sample_dataset(
            day=date(2017, 4, 3),
            datasets={
                '20170403_key.txt': 'testing',
            }
        )

        loader = Mock()
        loader.return_value = loader
        dataset.loaders = {'other_key': loader}

        with self.assertRaises(KeyError):
            dataset.load()

    def test_load_succeeds_even_if_fee_dataset_missing(self):
        dataset = get_sample_dataset(
            day=date(2017, 4, 3),
            datasets={
                '20170403_key.txt': 'testing',
            }
        )

        loader = Mock()
        loader.return_value = loader
        dataset.loaders = {'fee': loader}

        try:
            dataset.load()
        except KeyError:  # pragma: no cover
            self.fail('load should fail even if fee data is missing')

    def test_datafile(self):
        dataset = get_sample_dataset(
            day=date(2017, 4, 3),
            datasets={
                '20170403_something.txt': 'testing',
            }
        )

        f = dataset.datafile('something')
        self.assertEqual(f.read(), 'testing')


class TestCoverSheet(TestCase):
    def test_null_file_raises_typeerror(self):
        with self.assertRaises(TypeError):
            CoverSheet(None)

    def test_invalid_xml_file_raises_parseerror(self):
        f = ContentFile('foo')
        with self.assertRaises(ParseError):
            CoverSheet(f)

    def test_missing_date_raises_valueerror(self):
        f = ContentFile('<data></data>')
        with self.assertRaises(ValueError):
            CoverSheet(f)
{'fraction_non_alphanumeric': 0.09348914858096828, 'fraction_numerical': 0.024415692821368948, 'mean_word_length': 2.2297843665768196, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7537147', 'n_tokens_mistral': 1461, 'n_tokens_neox': 1343, 'n_words': 278}