<commit_before>"""Tests for importing NaPTAN data
"""
import os
import json
import vcr
from django.test import TestCase, override_settings
from ...models import Region, AdminArea, StopPoint, Locality, Service, StopUsage
from ..commands import (update_naptan, import_stop_areas, import_stops, import_stops_in_area,
                        import_stop_area_hierarchy)


DIR = os.path.dirname(os.path.abspath(__file__))
FIXTURES_DIR = os.path.join(DIR, 'fixtures')


class UpdateNaptanTest(TestCase):
    """Test the update_naptan command
    """
    command = update_naptan.Command()

    def test_get_old_rows(self):
        self.assertIsNone(self.command.get_old_rows())

    def test_get_diff(self):
        new_rows = [{
            'id': 1,
            'cell': [
                'S',
                'Aberdeen',
                '639',
                '07/06/2016',
                '1354',
                '143',
                '0',
                '0',
                '0',
                '0',
                '0',
                'V2',
                '7/17/2016'
            ]
        }]
        self.assertEqual(self.command.get_diff(new_rows, None), (['S'], ['639']))
        self.assertEqual(self.command.get_diff(new_rows, new_rows), ([], []))

    @override_settings(DATA_DIR=FIXTURES_DIR)
    def test_handle(self):
        json_path = os.path.join(FIXTURES_DIR, 'NaPTAN', 'NPTGLastSubs_Load.ashx')

        with vcr.use_cassette(os.path.join(FIXTURES_DIR, 'naptan.yml')):
            with self.assertRaises(TypeError):
                # will error because no file exists yet
                self.command.handle()

            with open(os.path.join(json_path), 'w') as open_file:
                json.dump({'rows': [{
                    'id': 109,
                    'cell': ['S', 'Shetland', '603', '10/09/2017', '918', '45', '0', '0', '378', '', '', '', '']
                }]}, open_file)

            self.command.handle()

        # verify that a pretend zipfile containing some text was downloaded and saved
        zipfile_path = os.path.join(FIXTURES_DIR, 'NaPTAN', 'naptan.zip')
        with open(zipfile_path) as open_file:
            self.assertEqual(open_file.read(), 'these pretzels are making me thirsty')

        # simulate a problem with the region-specific NaPTAN download, so all regions are downloaded
        with vcr.use_cassette(os.path.join(FIXTURES_DIR, 'naptan-error.yml')):
            with override_settings(DATA_DIR=FIXTURES_DIR):
                self.command.handle()
        with open(zipfile_path) as open_file:
            self.assertEqual(open_file.read(), 'these pretzels are making me thirsty again')

        # clean up afterwards
        os.remove(zipfile_path)
        os.remove(json_path)


class ImportNaptanTest(TestCase):
    """Test the import_stops, import_stop_areas, import_stops_in_area and
    import_stop_area_hierarchy commands
    """

    @classmethod
    def setUpTestData(cls):
        cls.region = Region.objects.create(id='GB', name='Great Britain')
        cls.admin_area = AdminArea.objects.create(id=34, atco_code=2, region_id='GB')
        cls.locality_1 = Locality.objects.create(id='E0054410', name='Baglan', admin_area_id=34)
        cls.locality_2 = Locality.objects.create(id='N0078801', name='Port Talbot', admin_area_id=34)

        command = import_stops.Command()
        for filename in ('Stops.csv', 'StopPoints.csv'):
            command.input = os.path.join(FIXTURES_DIR, filename)
            command.handle()

        cls.stop_area = import_stop_areas.Command().handle_row({
            'GridType': 'U',
            'Status': 'act',
            'Name': '<NAME>',<commit_msg>Fix Travis, don't rely on preexistence of empty NaPTAN fixtures dir<commit_after>"""Tests for importing NaPTAN data
"""
import os
import json
import vcr
from django.test import TestCase, override_settings
from ...models import Region, AdminArea, StopPoint, Locality, Service, StopUsage
from ..commands import (update_naptan, import_stop_areas, import_stops, import_stops_in_area,
                        import_stop_area_hierarchy)


DIR = os.path.dirname(os.path.abspath(__file__))
FIXTURES_DIR = os.path.join(DIR, 'fixtures')


class UpdateNaptanTest(TestCase):
    """Test the update_naptan command
    """
    command = update_naptan.Command()

    def test_get_old_rows(self):
        self.assertIsNone(self.command.get_old_rows())

    def test_get_diff(self):
        new_rows = [{
            'id': 1,
            'cell': [
                'S',
                'Aberdeen',
                '639',
                '07/06/2016',
                '1354',
                '143',
                '0',
                '0',
                '0',
                '0',
                '0',
                'V2',
                '7/17/2016'
            ]
        }]
        self.assertEqual(self.command.get_diff(new_rows, None), (['S'], ['639']))
        self.assertEqual(self.command.get_diff(new_rows, new_rows), ([], []))

    @override_settings(DATA_DIR=FIXTURES_DIR)
    def test_handle(self):
        naptan_dir = os.path.join(FIXTURES_DIR, 'NaPTAN')
        json_path = os.path.join(naptan_dir, 'NPTGLastSubs_Load.ashx')
        os.mkdir(naptan_dir)

        with vcr.use_cassette(os.path.join(FIXTURES_DIR, 'naptan.yml')):
            with self.assertRaises(TypeError):
                # will error because no file exists yet
                self.command.handle()

            with open(os.path.join(json_path), 'w') as open_file:
                json.dump({'rows': [{
                    'id': 109,
                    'cell': ['S', 'Shetland', '603', '10/09/2017', '918', '45', '0', '0', '378', '', '', '', '']
                }]}, open_file)

            self.command.handle()

        # verify that a pretend zipfile containing some text was downloaded and saved
        zipfile_path = os.path.join(naptan_dir, 'naptan.zip')
        with open(zipfile_path) as open_file:
            self.assertEqual(open_file.read(), 'these pretzels are making me thirsty')

        # simulate a problem with the region-specific NaPTAN download, so all regions are downloaded
        with vcr.use_cassette(os.path.join(FIXTURES_DIR, 'naptan-error.yml')):
            with override_settings(DATA_DIR=FIXTURES_DIR):
                self.command.handle()
        with open(zipfile_path) as open_file:
            self.assertEqual(open_file.read(), 'these pretzels are making me thirsty again')

        # clean up afterwards
        os.remove(zipfile_path)
        os.remove(json_path)
        os.rmdir(naptan_dir)


class ImportNaptanTest(TestCase):
    """Test the import_stops, import_stop_areas, import_stops_in_area and
    import_stop_area_hierarchy commands
    """

    @classmethod
    def setUpTestData(cls):
        cls.region = Region.objects.create(id='GB', name='Great Britain')
        cls.admin_area = AdminArea.objects.create(id=34, atco_code=2, region_id='GB')
        cls.locality_1 = Locality.objects.create(id='E0054410', name='Baglan', admin_area_id=34)
        cls.locality_2 = Locality.objects.create(id='N0078801', name='<NAME>', admin_area_id=34)

        command = import_stops.Command()
        for filename in ('Stops.csv', 'StopPoints.csv'):
            command.input = os.path.join(FIXTURES_DIR, filename)
            command.handle()

        cls.stop_area = import_stop_areas.Command().handle_row({
            'GridType': 'U',
            'Status': 'act',
            'Name': '<NAME>',
{'fraction_non_alphanumeric': 0.12709030100334448, 'fraction_numerical': 0.02193979933110368, 'mean_word_length': 2.3079646017699114, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '363078', 'n_tokens_mistral': 2516, 'n_tokens_neox': 2313, 'n_words': 463}