remove mock plpy dependencies

This commit is contained in:
Andy Eschbacher 2016-11-18 17:46:29 +00:00
parent a9add4b49c
commit a8bd122762
4 changed files with 36 additions and 40 deletions

View File

@ -2,7 +2,6 @@ import unittest
from mock_plpy import MockPlPy from mock_plpy import MockPlPy
plpy = MockPlPy() plpy = MockPlPy()
from mock_plpy import MockDBResponse
import sys import sys
sys.modules['plpy'] = plpy sys.modules['plpy'] = plpy

View File

@ -52,16 +52,3 @@ class MockPlPy:
if result[0].match(query): if result[0].match(query):
return result[1] return result[1]
return [] return []
class MockDBResponse:
def __init__(self, data, colnames=None):
self.data = data
if colnames is None:
self.colnames = data[0].keys()
else:
self.colnames = colnames
def colnames(self):
return self.colnames

View File

@ -7,7 +7,7 @@ import numpy as np
# #
# import sys # import sys
# sys.modules['plpy'] = plpy # sys.modules['plpy'] = plpy
from helper import plpy, fixture_file from helper import fixture_file
from crankshaft.clustering import Kmeans from crankshaft.clustering import Kmeans
from crankshaft.clustering import QueryRunner from crankshaft.clustering import QueryRunner
import crankshaft.clustering as cc import crankshaft.clustering as cc
@ -60,8 +60,6 @@ class KMeansNonspatialTest(unittest.TestCase):
"""Testing class for k-means non-spatial""" """Testing class for k-means non-spatial"""
def setUp(self): def setUp(self):
plpy._reset()
self.params = {"subquery": "SELECT * FROM TABLE", self.params = {"subquery": "SELECT * FROM TABLE",
"n_clusters": 5} "n_clusters": 5}

View File

@ -7,19 +7,30 @@ import numpy as np
# #
# import sys # import sys
# sys.modules['plpy'] = plpy # sys.modules['plpy'] = plpy
from helper import plpy, fixture_file, MockDBResponse from helper import fixture_file
from crankshaft.clustering import Moran
import crankshaft.clustering as cc from crankshaft.clustering import QueryRunner
import crankshaft.pysal_utils as pu import crankshaft.pysal_utils as pu
from crankshaft import random_seeds from crankshaft import random_seeds
import json import json
from collections import OrderedDict from collections import OrderedDict
class FakeQueryRunner(QueryRunner):
def __init__(self, mocked_result):
self.mocked_result = mocked_result
def get_result(self, query):
return self.mocked_result
def get_columns(self, query):
return self.mocked_result
class MoranTest(unittest.TestCase): class MoranTest(unittest.TestCase):
"""Testing class for Moran's I functions""" """Testing class for Moran's I functions"""
def setUp(self): def setUp(self):
plpy._reset()
self.params = {"id_col": "cartodb_id", self.params = {"id_col": "cartodb_id",
"attr1": "andy", "attr1": "andy",
"attr2": "jay_z", "attr2": "jay_z",
@ -39,36 +50,36 @@ class MoranTest(unittest.TestCase):
def test_map_quads(self): def test_map_quads(self):
"""Test map_quads""" """Test map_quads"""
self.assertEqual(cc.map_quads(1), 'HH') from crankshaft.clustering import map_quads
self.assertEqual(cc.map_quads(2), 'LH') self.assertEqual(map_quads(1), 'HH')
self.assertEqual(cc.map_quads(3), 'LL') self.assertEqual(map_quads(2), 'LH')
self.assertEqual(cc.map_quads(4), 'HL') self.assertEqual(map_quads(3), 'LL')
self.assertEqual(cc.map_quads(33), None) self.assertEqual(map_quads(4), 'HL')
self.assertEqual(cc.map_quads('andy'), None) self.assertEqual(map_quads(33), None)
self.assertEqual(map_quads('andy'), None)
def test_quad_position(self): def test_quad_position(self):
"""Test lisa_sig_vals""" """Test lisa_sig_vals"""
from crankshaft.clustering import quad_position
quads = np.array([1, 2, 3, 4], np.int) quads = np.array([1, 2, 3, 4], np.int)
ans = np.array(['HH', 'LH', 'LL', 'HL']) ans = np.array(['HH', 'LH', 'LL', 'HL'])
test_ans = cc.quad_position(quads) test_ans = quad_position(quads)
self.assertTrue((test_ans == ans).all()) self.assertTrue((test_ans == ans).all())
def test_moran_local(self): def test_local_stat(self):
"""Test Moran's I local""" """Test Moran's I local"""
data = [OrderedDict([('id', d['id']), data = [OrderedDict([('id', d['id']),
('attr1', d['value']), ('attr1', d['value']),
('neighbors', d['neighbors'])]) ('neighbors', d['neighbors'])])
for d in self.neighbors_data] for d in self.neighbors_data]
db_resp = MockDBResponse(data) moran = Moran(FakeQueryRunner(data))
plpy._define_result('select', db_resp)
random_seeds.set_random_seeds(1234) random_seeds.set_random_seeds(1234)
result = cc.moran_local('subquery', 'value', result = moran.local_stat('subquery', 'value',
'knn', 5, 99, 'the_geom', 'cartodb_id') 'knn', 5, 99, 'the_geom', 'cartodb_id')
result = [(row[0], row[1]) for row in result] result = [(row[0], row[1]) for row in result]
zipped_values = zip(result, self.moran_data) zipped_values = zip(result, self.moran_data)
@ -83,10 +94,10 @@ class MoranTest(unittest.TestCase):
'attr2': 1, 'attr2': 1,
'neighbors': d['neighbors']} for d in self.neighbors_data] 'neighbors': d['neighbors']} for d in self.neighbors_data]
plpy._define_result('select', data)
random_seeds.set_random_seeds(1234) random_seeds.set_random_seeds(1234)
result = cc.moran_local_rate('subquery', 'numerator', 'denominator', moran = Moran(FakeQueryRunner(data))
'knn', 5, 99, 'the_geom', 'cartodb_id') result = moran.local_rate_stat('subquery', 'numerator', 'denominator',
'knn', 5, 99, 'the_geom', 'cartodb_id')
result = [(row[0], row[1]) for row in result] result = [(row[0], row[1]) for row in result]
zipped_values = zip(result, self.moran_data) zipped_values = zip(result, self.moran_data)
@ -99,10 +110,11 @@ class MoranTest(unittest.TestCase):
data = [{'id': d['id'], data = [{'id': d['id'],
'attr1': d['value'], 'attr1': d['value'],
'neighbors': d['neighbors']} for d in self.neighbors_data] 'neighbors': d['neighbors']} for d in self.neighbors_data]
plpy._define_result('select', data)
random_seeds.set_random_seeds(1235) random_seeds.set_random_seeds(1235)
result = cc.moran('table', 'value', moran = Moran(FakeQueryRunner(data))
'knn', 5, 99, 'the_geom', 'cartodb_id') result = moran.global_stat('table', 'value',
'knn', 5, 99, 'the_geom',
'cartodb_id')
result_moran = result[0][0] result_moran = result[0][0]
expected_moran = np.array([row[0] for row in self.moran_data]).mean() expected_moran = np.array([row[0] for row in self.moran_data]).mean()