Created
November 6, 2018 10:48
-
-
Save freezed/475e47ad48339219294aeeafbafc7ce2 to your computer and use it in GitHub Desktop.
DRY a redundant django-pytest class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TestSearch: | |
""" | |
Non-regresion tests #27 : non error if a search do not return products | |
""" | |
CLIENT = Client() | |
WITNESS = { | |
'templates': [ | |
'ersatz/result.html', | |
'base.html', | |
'omega/searchform.html', | |
] | |
} | |
RESPONSE_EMPTY = {'error': API['EMPTY'],'status': False} | |
RESPONSE_NO_PROD = { | |
'error': API['NO_PROD'].format('foobar'), | |
'status': False, | |
} | |
def fake_get_search_empty_context(self, request): | |
""" ersatz.views.toolbox.get_search_context() fake function """ | |
return self.RESPONSE_EMPTY | |
def fake_get_search_no_product_context(self, request): | |
""" ersatz.views.toolbox.get_search_context() fake function """ | |
return self.RESPONSE_NO_PROD | |
def test_empty_search(self, monkeypatch): | |
self.WITNESS['response'] = self.RESPONSE_EMPTY | |
monkeypatch.setattr('ersatz.views.toolbox.get_search_context', self.fake_get_search_empty_context) | |
response = self.CLIENT.get('/ersatz/search/') | |
assert response.status_code == 200 | |
assert self.WITNESS['templates'] == [t.name for t in response.templates] | |
assert self.WITNESS['response']['error'] == response.context['error'] | |
def test_no_product_search(self, monkeypatch): | |
self.WITNESS['response'] = self.RESPONSE_NO_PROD | |
monkeypatch.setattr('ersatz.views.toolbox.get_search_context', self.fake_get_search_no_product_context) | |
response = self.CLIENT.get('/ersatz/search/', {'s':'foobar'}) | |
assert response.status_code == 200 | |
assert self.WITNESS['templates'] == [t.name for t in response.templates] | |
assert self.WITNESS['response']['error'] == response.context['error'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment