コード例 #1
0
class TestStandardizedFilenames(TestCase):
    """
    Standardized filenames are the names under which source data
    will be stored both locally during development and on S3
    for archival purposes.
    """

    def setUp(self):
        self.datasource = Datasource()
        self.allegany_jurisdiction = {
            'ocd_id': 'ocd-division/country:us/state:md/county:allegany',
            'fips': '24001',
            'name':'Allegany',
            'url_name': 'allegany'
        }

    def test_state_leg_filename_general_racewide_results(self):
        raw_url = "http://www.elections.state.md.us/elections/2012/election_data/State_Legislative_Districts_2012_General.csv"
        actual = self.datasource._generate_state_leg_filename(raw_url, '2012-11-06')
        self.assertEquals('20121106__md__general__state_legislative.csv', actual)

    def test_state_leg_filename_primary_racewide_results(self):
        raw_url = "http://www.elections.state.md.us/elections/2012/election_data/State_Legislative_Districts_Democratic_2012_Primary.csv"
        actual = self.datasource._generate_state_leg_filename(raw_url, '2012-04-03')
        self.assertEquals("20120403__md__democratic__primary__state_legislative.csv", actual)

    def test_county_filename_general_county_results(self):
        raw_url = "http://www.elections.state.md.us/elections/2012/election_data/Allegany_County_2012_General.csv"
        actual = self.datasource._generate_county_filename(raw_url, '2012-11-06', self.allegany_jurisdiction)
        self.assertEquals("20121106__md__general__allegany.csv", actual)

    def test_county_filename_general_precinct_results(self):
        raw_url = "http://www.elections.state.md.us/elections/2012/election_data/Allegany_By_Precinct_2012_General.csv"
        actual = self.datasource._generate_county_filename(raw_url, '2012-11-06', self.allegany_jurisdiction)
        self.assertEquals("20121106__md__general__allegany__precinct.csv", actual)

    def test_county_filename_primary_countywide_results(self):
        raw_url = "http://www.elections.state.md.us/elections/2012/election_data/Allegany_County_Democratic_2012_Primary.csv"
        actual = self.datasource._generate_county_filename(raw_url, '2012-04-03', self.allegany_jurisdiction)
        self.assertEquals("20120403__md__democratic__primary__allegany.csv", actual)

    def test_county_filename_primary_precinct_results(self):
        raw_url = "http://www.elections.state.md.us/elections/2012/election_data/Allegany_By_Precinct_Democratic_2012_Primary.csv"
        actual = self.datasource._generate_county_filename(raw_url, '2012-04-03', self.allegany_jurisdiction)
        self.assertEquals("20120403__md__democratic__primary__allegany__precinct.csv", actual)

    # 2000 and 2004 files end with the 4-digit year
    def test_2000_state_leg_filename_general_results(self):
        raw_url = "http://www.elections.state.md.us/elections/2000/election_data/State_Legislative_Districts_General_2000.csv"
        actual = self.datasource._generate_state_leg_filename(raw_url, '2000-11-07')
        self.assertEquals("20001107__md__general__state_legislative.csv",  actual)

    def test_2004_state_leg_filename_general_countywide_results(self):
        raw_url = "http://www.elections.state.md.us/elections/2004/election_data/State_Legislative_Districts_General_2004.csv"
        actual = self.datasource._generate_state_leg_filename(raw_url, '2004-11-02')
        self.assertEquals("20041102__md__general__state_legislative.csv", actual)

    def test_2004_county_filename_general_countywide_results(self):
        raw_url = "http://www.elections.state.md.us/elections/2004/election_data/Allegany_County_General_2004.csv"
        actual = self.datasource._generate_county_filename(raw_url, '2004-11-02', self.allegany_jurisdiction)
        self.assertEquals("20041102__md__general__allegany.csv", actual)

    def test_2004_county_filename_primary_precinct_results(self):
        raw_url = "http://www.elections.state.md.us/elections/2004/election_data/Allegany_County_Democratic_Primary_2004.csv"
        actual = self.datasource._generate_county_filename(raw_url, '2004-03-02', self.allegany_jurisdiction)
        self.assertEquals("20040302__md__democratic__primary__allegany.csv", actual)

    def test_2000_county_filename_general_countywide_results(self):
        raw_url = "http://www.elections.state.md.us/elections/2000/election_data/Allegany_County_General_2000.csv"
        actual = self.datasource._generate_county_filename(raw_url, '2000-11-07', self.allegany_jurisdiction)
        self.assertEquals("20001107__md__general__allegany.csv", actual)

    def test_2000_county_filename_precinct_results(self):
        raw_url = "http://www.elections.state.md.us/elections/2000/election_data/Allegany_By_Precinct_2000.csv"
        # there are no 2000 precinct results so this should raise an error
        self.assertRaises(
            AttributeError,
            self.datasource._generate_county_filename,
            raw_url, '2000-11-07', self.allegany_jurisdiction,
        )

    # 2002 only has two files -- primary and general
    def test_2002_general_filename(self):
        raw_url = "http://www.elections.state.md.us/elections/2002/results/g_all_offices.txt"
        actual = self.datasource._generate_2002_filename(raw_url)
        self.assertEquals("20021105__md__general.txt", actual)

    def test_2002_primary_filename(self):
        raw_url = "http://www.elections.state.md.us/elections/2002/results/p_all_offices.txt"
        actual = self.datasource._generate_2002_filename(raw_url)
        self.assertEquals("20020910__md__primary.txt", actual)
コード例 #2
0
class TestStandardizedFilenames(TestCase):
    """
    Standardized filenames are the names under which source data
    will be stored both locally during development and on S3
    for archival purposes.
    """
    def setUp(self):
        self.datasource = Datasource()
        self.allegany_jurisdiction = {
            'ocd_id': 'ocd-division/country:us/state:md/county:allegany',
            'fips': '24001',
            'name': 'Allegany',
            'url_name': 'allegany'
        }

    def test_state_leg_filename_general_racewide_results(self):
        raw_url = "http://www.elections.state.md.us/elections/2012/election_data/State_Legislative_Districts_2012_General.csv"
        actual = self.datasource._generate_state_leg_filename(
            raw_url, '2012-11-06')
        self.assertEquals('20121106__md__general__state_legislative.csv',
                          actual)

    def test_state_leg_filename_primary_racewide_results(self):
        raw_url = "http://www.elections.state.md.us/elections/2012/election_data/State_Legislative_Districts_Democratic_2012_Primary.csv"
        actual = self.datasource._generate_state_leg_filename(
            raw_url, '2012-04-03')
        self.assertEquals(
            "20120403__md__democratic__primary__state_legislative.csv", actual)

    def test_county_filename_general_county_results(self):
        raw_url = "http://www.elections.state.md.us/elections/2012/election_data/Allegany_County_2012_General.csv"
        actual = self.datasource._generate_county_filename(
            raw_url, '2012-11-06', self.allegany_jurisdiction)
        self.assertEquals("20121106__md__general__allegany.csv", actual)

    def test_county_filename_general_precinct_results(self):
        raw_url = "http://www.elections.state.md.us/elections/2012/election_data/Allegany_By_Precinct_2012_General.csv"
        actual = self.datasource._generate_county_filename(
            raw_url, '2012-11-06', self.allegany_jurisdiction)
        self.assertEquals("20121106__md__general__allegany__precinct.csv",
                          actual)

    def test_county_filename_primary_countywide_results(self):
        raw_url = "http://www.elections.state.md.us/elections/2012/election_data/Allegany_County_Democratic_2012_Primary.csv"
        actual = self.datasource._generate_county_filename(
            raw_url, '2012-04-03', self.allegany_jurisdiction)
        self.assertEquals("20120403__md__democratic__primary__allegany.csv",
                          actual)

    def test_county_filename_primary_precinct_results(self):
        raw_url = "http://www.elections.state.md.us/elections/2012/election_data/Allegany_By_Precinct_Democratic_2012_Primary.csv"
        actual = self.datasource._generate_county_filename(
            raw_url, '2012-04-03', self.allegany_jurisdiction)
        self.assertEquals(
            "20120403__md__democratic__primary__allegany__precinct.csv",
            actual)

    # 2000 and 2004 files end with the 4-digit year
    def test_2000_state_leg_filename_general_results(self):
        raw_url = "http://www.elections.state.md.us/elections/2000/election_data/State_Legislative_Districts_General_2000.csv"
        actual = self.datasource._generate_state_leg_filename(
            raw_url, '2000-11-07')
        self.assertEquals("20001107__md__general__state_legislative.csv",
                          actual)

    def test_2004_state_leg_filename_general_countywide_results(self):
        raw_url = "http://www.elections.state.md.us/elections/2004/election_data/State_Legislative_Districts_General_2004.csv"
        actual = self.datasource._generate_state_leg_filename(
            raw_url, '2004-11-02')
        self.assertEquals("20041102__md__general__state_legislative.csv",
                          actual)

    def test_2004_county_filename_general_countywide_results(self):
        raw_url = "http://www.elections.state.md.us/elections/2004/election_data/Allegany_County_General_2004.csv"
        actual = self.datasource._generate_county_filename(
            raw_url, '2004-11-02', self.allegany_jurisdiction)
        self.assertEquals("20041102__md__general__allegany.csv", actual)

    def test_2004_county_filename_primary_precinct_results(self):
        raw_url = "http://www.elections.state.md.us/elections/2004/election_data/Allegany_County_Democratic_Primary_2004.csv"
        actual = self.datasource._generate_county_filename(
            raw_url, '2004-03-02', self.allegany_jurisdiction)
        self.assertEquals("20040302__md__democratic__primary__allegany.csv",
                          actual)

    def test_2000_county_filename_general_countywide_results(self):
        raw_url = "http://www.elections.state.md.us/elections/2000/election_data/Allegany_County_General_2000.csv"
        actual = self.datasource._generate_county_filename(
            raw_url, '2000-11-07', self.allegany_jurisdiction)
        self.assertEquals("20001107__md__general__allegany.csv", actual)

    def test_2000_county_filename_precinct_results(self):
        raw_url = "http://www.elections.state.md.us/elections/2000/election_data/Allegany_By_Precinct_2000.csv"
        # there are no 2000 precinct results so this should raise an error
        self.assertRaises(
            AttributeError,
            self.datasource._generate_county_filename,
            raw_url,
            '2000-11-07',
            self.allegany_jurisdiction,
        )

    # 2002 only has two files -- primary and general
    def test_2002_general_filename(self):
        raw_url = "http://www.elections.state.md.us/elections/2002/results/g_all_offices.txt"
        actual = self.datasource._generate_2002_filename(raw_url)
        self.assertEquals("20021105__md__general.txt", actual)

    def test_2002_primary_filename(self):
        raw_url = "http://www.elections.state.md.us/elections/2002/results/p_all_offices.txt"
        actual = self.datasource._generate_2002_filename(raw_url)
        self.assertEquals("20020910__md__primary.txt", actual)