From bf680379556baad1d1416509b2f3e74da446c12f Mon Sep 17 00:00:00 2001 From: Connor Osborn Date: Mon, 18 Jun 2018 12:20:25 -0700 Subject: [PATCH] Fix test failures when run consecutively Problem: Running tests twice causes failures Solution: Remove the memoize cache before test runs This would fail if run twice: ./manage.py test --keepdb jetstream.test_tas_api.TestJetstream.test_validate_account It would fail because the cassettes did not reflect the correct number of plays, i.e. api calls didn't happen like we expected. Normally cassettes store the results of a first http call, and intercept and respond to all future calls with the results of the first. We assert that the cassette was called a certain number of times. However, django-memoize intercepts the functions that would even make these http calls, so the cassettes were under-reporting the number of playbacks. --- jetstream/test_tas_api.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/jetstream/test_tas_api.py b/jetstream/test_tas_api.py index 9e9f4c8bc..2df14c501 100644 --- a/jetstream/test_tas_api.py +++ b/jetstream/test_tas_api.py @@ -1,5 +1,5 @@ import json - +import memoize import freezegun import vcr from django.test import TestCase, override_settings, modify_settings @@ -23,6 +23,14 @@ class TestJetstream(TestCase): """Tests for Jetstream allocation source API""" + def setUp(self): + # Because we use memoize to cache the tacc api, calling tacc_api_get + # doesn't necessarily trigger an http request. This means that a + # cassette will not necessarily be played. In order to test cassette + # playback, we just need to clear the memoize cache + from jetstream.tas_api import tacc_api_get + memoize.delete_memoized(tacc_api_get) + @my_vcr.use_cassette() def test_validate_account(self, cassette): """Test for a valid account based on the business logic assigned by Jetstream"""