2011-04-23 06:00:10 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
|
|
|
"""
|
|
|
|
test_engine.py
|
|
|
|
|
|
|
|
Created by Paul Makepeace on 2011-04-22.
|
|
|
|
Copyright (c) 2011 Real Programmers. All rights reserved.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import unittest
|
|
|
|
import urllib
|
2011-04-24 23:20:53 +02:00
|
|
|
from google.refine import Facet, TextFacet, NumericFacet, Engine, FacetsResponse
|
2011-04-23 06:00:10 +02:00
|
|
|
|
|
|
|
class FacetTest(unittest.TestCase):
|
|
|
|
def test_init(self):
|
2011-04-24 23:20:53 +02:00
|
|
|
facet = TextFacet('column name')
|
2011-04-23 06:00:10 +02:00
|
|
|
engine = Engine(facet)
|
2011-04-24 23:20:53 +02:00
|
|
|
self.assertEqual(facet.selection, [])
|
2011-04-23 06:00:10 +02:00
|
|
|
self.assertTrue(str(engine))
|
2011-04-24 21:45:32 +02:00
|
|
|
|
2011-04-23 06:00:10 +02:00
|
|
|
def test_serialize(self):
|
|
|
|
engine = Engine()
|
|
|
|
engine_json = engine.as_json()
|
|
|
|
self.assertEqual(engine_json, '{"facets": [], "mode": "row-based"}')
|
2011-04-24 23:20:53 +02:00
|
|
|
facet = TextFacet(column='column')
|
|
|
|
self.assertEqual(facet.as_dict(), {'selectError': False, 'name': 'column', 'selection': [], 'expression': 'value', 'invert': False, 'columnName': 'column', 'selectBlank': False, 'omitBlank': False, 'type': 'list', 'omitError': False})
|
|
|
|
facet = NumericFacet(column='column')
|
|
|
|
self.assertEqual(facet.as_dict(), {'selectBlank': True, 'name': 'column', 'selectError': True, 'expression': 'value', 'selection': [], 'selectNumeric': True, 'columnName': 'column', 'selectNonNumeric': True, 'type': 'range'})
|
2011-04-24 21:45:32 +02:00
|
|
|
|
2011-04-24 18:43:11 +02:00
|
|
|
def test_add_facet(self):
|
2011-04-24 23:20:53 +02:00
|
|
|
facet = TextFacet(column='Party Code')
|
2011-04-24 18:43:11 +02:00
|
|
|
engine = Engine(facet)
|
2011-04-24 23:20:53 +02:00
|
|
|
engine.add_facet(TextFacet(column='Ethnicity'))
|
2011-04-24 18:43:11 +02:00
|
|
|
self.assertEqual(len(engine.facets), 2)
|
2011-04-24 21:45:32 +02:00
|
|
|
self.assertEqual(len(engine), 2)
|
2011-04-23 06:00:10 +02:00
|
|
|
|
2011-04-24 21:07:49 +02:00
|
|
|
def test_selections(self):
|
2011-04-24 23:20:53 +02:00
|
|
|
facet = TextFacet('column name')
|
2011-04-24 21:07:49 +02:00
|
|
|
facet.include('element')
|
2011-04-24 23:20:53 +02:00
|
|
|
self.assertEqual(len(facet.selection), 1)
|
2011-04-24 21:07:49 +02:00
|
|
|
facet.include('element 2')
|
2011-04-24 23:20:53 +02:00
|
|
|
self.assertEqual(len(facet.selection), 2)
|
2011-04-24 21:07:49 +02:00
|
|
|
facet.exclude('element')
|
2011-04-24 23:20:53 +02:00
|
|
|
self.assertEqual(len(facet.selection), 1)
|
2011-04-24 21:45:32 +02:00
|
|
|
facet.reset()
|
2011-04-24 23:20:53 +02:00
|
|
|
self.assertEqual(len(facet.selection), 0)
|
2011-04-24 21:45:32 +02:00
|
|
|
|
|
|
|
|
2011-04-23 06:00:10 +02:00
|
|
|
def test_facets_response(self):
|
|
|
|
response = """{"facets":[{"name":"Party Code","expression":"value","columnName":"Party Code","invert":false,"choices":[{"v":{"v":"D","l":"D"},"c":3700,"s":false},{"v":{"v":"R","l":"R"},"c":1613,"s":false},{"v":{"v":"N","l":"N"},"c":15,"s":false},{"v":{"v":"O","l":"O"},"c":184,"s":false}],"blankChoice":{"s":false,"c":1446}}],"mode":"row-based"}"""
|
2011-04-24 21:56:24 +02:00
|
|
|
response = FacetsResponse(json.loads(response))
|
|
|
|
facets = response.facets
|
|
|
|
self.assertEqual(facets[0].choices['D'].count, 3700)
|
|
|
|
self.assertEqual(facets[0].blank_choice.count, 1446)
|
|
|
|
|
2011-04-24 21:45:32 +02:00
|
|
|
|
2011-04-23 06:00:10 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|