Skip to content

Instantly share code, notes, and snippets.

@ghing ghing/client.py
Created Jan 20, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.