Skip to content

Instantly share code, notes, and snippets.

@freezed
Created November 6, 2018 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save freezed/475e47ad48339219294aeeafbafc7ce2 to your computer and use it in GitHub Desktop.
Save freezed/475e47ad48339219294aeeafbafc7ce2 to your computer and use it in GitHub Desktop.
DRY a redundant django-pytest class
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