Skip to content

Instantly share code, notes, and snippets.

@ghing
Created January 20, 2015 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghing/9b7f8fa9bc66db31d600 to your computer and use it in GitHub Desktop.
Save ghing/9b7f8fa9bc66db31d600 to your computer and use it in GitHub Desktop.
Test python-elections from filesystem
"""AP client that reads data from file instead of from the AP FTP server"""
import os
from elections import AP
class MockFTP(object):
def quit(self):
pass
class FilesystemAP(AP):
def __init__(self, results_path, username=None, password=None):
super(FilesystemAP, self).__init__(username, password)
self._results_path = results_path
@property
def ftp(self):
if not self._ftp:
self._ftp = MockFTP()
return self._ftp
def _fetch(self, path):
clean_path = path.lstrip('/')
full_path = os.path.join(self._results_path, clean_path)
return open(full_path)
# -*- coding: utf-8 -*-
"""Test loading 2011 Illinois municipal election results"""
import os
from .client import FilesystemAP
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
# This directory should mirror the layout of AP's FTP server
# For example:
#
# tests/data/
# ├── IL
# │   └── flat
# │   └── IL.txt
# └── inits
# └── IL
# ├── IL_pol.txt
# ├── IL_race.txt
# ├── IL_ru.txt
# └── IL_rura.txt
DATA_DIR = os.path.join(THIS_DIR, 'data')
def get_client():
return FilesystemAP(DATA_DIR)
def test_load():
client = get_client()
il = client.get_state('IL')
mayor = il.filter_races(office_name='Mayor')[0]
assert len(mayor.state.results) == 6
rahm = next(r for r in mayor.state.results
if r.candidate.last_name == "Emanuel")
assert rahm.candidate.is_winner == True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment