From f90e29ce966d7524a9df0409729fb7be743eb933 Mon Sep 17 00:00:00 2001 From: Will Daly Date: Mon, 1 Apr 2013 08:55:06 -0400 Subject: [PATCH] Fixed a test error that occurred because the problem was not set to done before resetting it. To avoid to small chance that we'll get the same seed when we reset, try to get a new seed multiple times. --- .../xmodule/xmodule/tests/test_capa_module.py | 53 ++++++++++++++----- 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/common/lib/xmodule/xmodule/tests/test_capa_module.py b/common/lib/xmodule/xmodule/tests/test_capa_module.py index 1b923c13f8..1ebb83d2e4 100644 --- a/common/lib/xmodule/xmodule/tests/test_capa_module.py +++ b/common/lib/xmodule/xmodule/tests/test_capa_module.py @@ -890,31 +890,60 @@ class CapaModuleTest(unittest.TestCase): self.assertEqual(seed, module.seed) def test_random_seed_with_reset(self): - rerandomize_options = ['never', 'per_student', 'always', 'onreset'] + + def _reset_and_get_seed(module): + ''' + Reset the XModule and return the module's seed + ''' - for rerandomize in rerandomize_options: + # Simulate submitting an attempt + module.done = True + + # Reset the problem + module.reset_problem({}) + + # Get return the seed + return module.seed + + def _retry_and_check(num_tries, test_func): + ''' + Returns True if *test_func* was successful + (returned True) within *num_tries* attempts + + *test_func* must be a function + of the form test_func() -> bool + ''' + success = False + for i in range(num_tries): + if test_func() is True: + success = True + break + return success + + # Run the test for each possible rerandomize value + for rerandomize in ['never', 'per_student', 'always', 'onreset']: module = CapaFactory.create(rerandomize=rerandomize) # Get the seed seed = module.lcp.seed - # Reset the problem - module.reset_problem({}) - # We do NOT want the seed to reset if rerandomize # is set to 'never' -- it should still be 1 # The seed also stays the same if we're randomizing # 'per_student': the same student should see the same problem if rerandomize in ['never', 'per_student']: - self.assertEqual(seed, module.seed) + self.assertEqual(seed, _reset_and_get_seed(module)) # Otherwise, we expect the seed to change # to another valid seed else: - - # After we save, the seed is stored in the module - get_request_dict = { CapaFactory.input_key(): '3.14'} - module.save_problem(get_request_dict) - self.assertEqual(seed, module.seed) - self.assertTrue(module.seed is not None) + # Since there's a small chance we might get the + # same seed again, give it 5 chances + # to generate a different seed + success = _retry_and_check(5, + lambda: _reset_and_get_seed(module) != seed) + + self.assertTrue(module.seed != None) + msg = 'Could not get a new seed from reset after 5 tries' + self.assertTrue(success, msg)