2011-04-23 06:00:10 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
|
|
|
"""
|
|
|
|
test_refine.py
|
|
|
|
|
|
|
|
Created by Paul Makepeace on 2011-04-22.
|
|
|
|
Copyright (c) 2011 Real Programmers. All rights reserved.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import unittest
|
|
|
|
from google.refine import REFINE_HOST, REFINE_PORT
|
|
|
|
from google.refine import Facet, Engine
|
|
|
|
from google.refine import RefineServer, Refine, RefineProject
|
|
|
|
|
2011-04-23 21:33:21 +02:00
|
|
|
PATH_TO_TEST_DATA = os.path.join('google', 'test', 'data')
|
2011-04-24 18:43:11 +02:00
|
|
|
|
2011-04-23 06:00:10 +02:00
|
|
|
class RefineTestCase(unittest.TestCase):
|
2011-04-23 21:33:21 +02:00
|
|
|
project_file = None
|
|
|
|
project = None
|
2011-04-24 21:07:49 +02:00
|
|
|
# {1}, {2}
|
2011-04-23 06:00:10 +02:00
|
|
|
def setUp(self):
|
|
|
|
self.server = RefineServer()
|
|
|
|
self.refine = Refine(self.server)
|
2011-04-23 21:33:21 +02:00
|
|
|
if self.project_file:
|
|
|
|
self.project = self.refine.new_project(
|
|
|
|
os.path.join(PATH_TO_TEST_DATA, self.project_file))
|
2011-04-23 06:00:10 +02:00
|
|
|
|
2011-04-23 21:33:21 +02:00
|
|
|
def tearDown(self):
|
|
|
|
if self.project:
|
|
|
|
self.project.delete()
|
|
|
|
self.project = None
|
2011-04-24 18:43:11 +02:00
|
|
|
|
|
|
|
|
2011-04-23 06:00:10 +02:00
|
|
|
class RefineServerTest(RefineTestCase):
|
|
|
|
def test_init(self):
|
|
|
|
self.assertEqual(self.server.server, 'http://%s:%s' % (REFINE_HOST, REFINE_PORT))
|
|
|
|
server = RefineServer('http://refine.example/')
|
|
|
|
self.assertEqual(server.server, 'http://refine.example')
|
|
|
|
|
|
|
|
def test_list_projects(self):
|
|
|
|
projects = self.refine.list_projects()
|
|
|
|
self.assertTrue(isinstance(projects, dict))
|
|
|
|
|
2011-04-23 18:49:18 +02:00
|
|
|
def test_get_version(self):
|
|
|
|
version_info = self.refine.get_version()
|
|
|
|
for item in ('revision', 'version', 'full_version', 'full_name'):
|
|
|
|
self.assertTrue(item in version_info)
|
|
|
|
|
2011-04-23 06:00:10 +02:00
|
|
|
|
|
|
|
class RefineTest(RefineTestCase):
|
2011-04-23 21:33:21 +02:00
|
|
|
project_file = 'duplicates.csv'
|
|
|
|
|
2011-04-23 06:00:10 +02:00
|
|
|
def test_new_project(self):
|
2011-04-23 18:49:18 +02:00
|
|
|
self.assertTrue(isinstance(self.project, RefineProject))
|
|
|
|
|
|
|
|
def test_get_models(self):
|
|
|
|
self.assertEqual(self.project.key_column, 'email')
|
|
|
|
self.assertTrue('email' in self.project.columns)
|
|
|
|
self.assertEqual(self.project.column_index['name'], 1)
|
2011-04-24 18:43:11 +02:00
|
|
|
|
2011-04-23 18:49:18 +02:00
|
|
|
def test_delete_project(self):
|
|
|
|
self.assertTrue(self.project.delete())
|
2011-04-23 06:00:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TutorialTestFacets(RefineTestCase):
|
2011-04-23 21:33:21 +02:00
|
|
|
project_file = 'louisiana-elected-officials.csv'
|
2011-04-23 06:00:10 +02:00
|
|
|
|
2011-04-24 18:43:11 +02:00
|
|
|
def test_get_rows(self):
|
2011-04-24 21:07:49 +02:00
|
|
|
# {3}
|
2011-04-24 18:43:11 +02:00
|
|
|
response = self.project.get_rows(limit=10)
|
|
|
|
self.assertEqual(len(response.rows), 10)
|
|
|
|
self.assertEqual(response.limit, 10)
|
|
|
|
self.assertEqual(response.total, 6958)
|
|
|
|
for row in response.rows:
|
|
|
|
self.assertFalse(row.flagged)
|
|
|
|
self.assertFalse(row.starred)
|
|
|
|
|
2011-04-23 21:33:21 +02:00
|
|
|
def test_basic_facet(self):
|
2011-04-24 21:07:49 +02:00
|
|
|
# {4}
|
2011-04-24 21:45:32 +02:00
|
|
|
party_code_facet = Facet(column='Party Code')
|
|
|
|
response = self.project.text_facet(party_code_facet)
|
2011-04-24 21:31:06 +02:00
|
|
|
pc = response.facets[0]
|
2011-04-23 06:00:10 +02:00
|
|
|
self.assertEqual(pc.name, 'Party Code')
|
|
|
|
self.assertEqual(pc.choices['D'].count, 3700)
|
|
|
|
self.assertEqual(pc.choices['N'].count, 15)
|
|
|
|
self.assertEqual(pc.blank_choice.count, 1446)
|
2011-04-24 21:07:49 +02:00
|
|
|
# {5}, {6}
|
2011-04-24 21:45:32 +02:00
|
|
|
engine = Engine(party_code_facet)
|
2011-04-24 21:07:49 +02:00
|
|
|
ethnicity_facet = Facet(column='Ethnicity')
|
|
|
|
engine.add_facet(ethnicity_facet)
|
2011-04-24 18:43:11 +02:00
|
|
|
self.project.engine = engine
|
2011-04-24 21:31:06 +02:00
|
|
|
response = self.project.text_facet()
|
|
|
|
e = response.facets[1]
|
2011-04-23 06:00:10 +02:00
|
|
|
self.assertEqual(e.choices['B'].count, 1255)
|
|
|
|
self.assertEqual(e.choices['W'].count, 4469)
|
2011-04-24 21:07:49 +02:00
|
|
|
# {7}
|
|
|
|
ethnicity_facet.include('B')
|
|
|
|
response = self.project.get_rows()
|
|
|
|
self.assertEqual(response.filtered, 1255)
|
|
|
|
indexes = [r.index for r in response.rows]
|
|
|
|
self.assertEqual(indexes, [1, 2, 3, 4, 6, 12, 18, 26, 28, 32])
|
2011-04-24 21:31:06 +02:00
|
|
|
# {8}
|
|
|
|
response = self.project.text_facet()
|
|
|
|
pc = response.facets[0]
|
|
|
|
self.assertEqual(pc.name, 'Party Code')
|
|
|
|
self.assertEqual(pc.choices['D'].count, 1179)
|
|
|
|
self.assertEqual(pc.choices['R'].count, 11)
|
|
|
|
self.assertEqual(pc.blank_choice.count, 46)
|
2011-04-24 21:45:32 +02:00
|
|
|
# {9}
|
|
|
|
party_code_facet.include('R')
|
|
|
|
response = self.project.text_facet()
|
|
|
|
e = response.facets[1]
|
|
|
|
self.assertEqual(e.choices['B'].count, 11)
|
|
|
|
# {10}
|
|
|
|
party_code_facet.reset()
|
|
|
|
ethnicity_facet.reset()
|
|
|
|
response = self.project.get_rows()
|
|
|
|
self.assertEqual(response.filtered, 6958)
|
2011-04-24 21:59:32 +02:00
|
|
|
# {11}
|
|
|
|
office_title_facet = Facet('Office Title')
|
|
|
|
self.project.engine.add_facet(office_title_facet)
|
|
|
|
response = self.project.text_facet()
|
|
|
|
self.assertEqual(len(response.facets[2].choices), 76)
|
|
|
|
|
2011-04-23 06:00:10 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|