From 19b7ca176eeba4d7226b0533066f3e83b84acf77 Mon Sep 17 00:00:00 2001 From: Kat Busch Date: Tue, 30 Jan 2018 18:00:16 -0800 Subject: [PATCH] Fix age test that is based on current date (#56) * Fix age test that is based on current date * Skip failing test * Add failing test with different weight options * Lint * Print debug for circle run * Add another possible solution * weights weren't close enough --- README.md | 2 +- requirements.txt | 2 +- test/test_inputs.py | 12 ++++++++---- test/test_listbalancer.py | 39 +++++++++++++++++++++++++++++---------- 4 files changed, 39 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index bf271ef..cef4291 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ You should now have the local doppelganger code & testing tools installed. From the repo root, run the tests to confirm everything is working: ```shell -nosetests +py.test ``` diff --git a/requirements.txt b/requirements.txt index 742917f..b9e3406 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,7 @@ cvxpy>=0.4.8 numpy>=1.11.0 pandas>=0.19.0 -pomegranate>=0.8.1 +pomegranate==0.8.1 requests>=2.0.0 six>=1.10.0 future>=0.16.0 diff --git a/test/test_inputs.py b/test/test_inputs.py index 3d0aebf..326daf4 100644 --- a/test/test_inputs.py +++ b/test/test_inputs.py @@ -3,8 +3,10 @@ from __future__ import ( absolute_import, division, print_function, unicode_literals ) - +import datetime import unittest +from mock import patch, Mock + from doppelganger import inputs @@ -12,9 +14,11 @@ class InputsTest(unittest.TestCase): def test_yyyy_to_age(self): - for sample_birthday in [19490301, '19490301', '194903', 194903]: - age = inputs.yyyy_to_age(sample_birthday) - self.assertEqual(age, 68) + mock_today = datetime.date(2017, 1, 29) + with patch('datetime.date', Mock(today=lambda: mock_today)): + for sample_birthday in [19490301, '19490301', '194903', 194903]: + age = inputs.yyyy_to_age(sample_birthday) + self.assertEqual(age, 68) def test_yyyy_to_age_none(self): for sample_birthday in ['', float('nan')]: diff --git a/test/test_listbalancer.py b/test/test_listbalancer.py index d922dbd..ea89b0f 100644 --- a/test/test_listbalancer.py +++ b/test/test_listbalancer.py @@ -67,16 +67,33 @@ def _mock_list_relaxed(self): ]]) _, n_controls = hh_table.shape - expected_weights = np.matrix([ - [45.], - [52.], - [65.], - [98.] - ]) + # There are multiple possible solutions and the result varies + expected_weights_options = ( + np.matrix([ + [39], + [48.5], + [67], + [116.5], + ]), + np.matrix( + [ + [30.], + [37.], + [56.], + [158.] + ]), + np.matrix( + [ + [45.], + [52.], + [65.], + [98.] + ]), + ) mu = np.mat([1] * n_controls) - return (hh_table, A, w, mu, expected_weights) + return (hh_table, A, w, mu, expected_weights_options) def _mock_list_inconsistent(self): hh_table = np.mat([ @@ -214,10 +231,12 @@ def test_balance_cvx(self): hh_weights, expected_weights, rtol=0.01, atol=0) def test_balance_cvx_relaxed(self): - hh_table, A, w, mu, expected_weights = self._mock_list_relaxed() + hh_table, A, w, mu, expected_weights_options = self._mock_list_relaxed() hh_weights, _ = listbalancer.balance_cvx(hh_table, A, w, mu) - np.testing.assert_allclose( - hh_weights, expected_weights, rtol=0.01, atol=0) + self.assertTrue(any( + np.allclose(hh_weights, expected_weights, rtol=0.01, atol=0) + for expected_weights in expected_weights_options) + ) def test_balance_multi_cvx(self): hh_table, A, w, mu, expected_weights = self._mock_list_inconsistent()