Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Refactoring of Test Cases
Browse files Browse the repository at this point in the history
1. Replaced usage of OrderedDict usage with dictionary.
2. Rewritten test cases that required internal working knowledge.
3. assertRaises replaced with assertRaisesRegex
  • Loading branch information
pradeepsixer committed Jan 9, 2018
1 parent b01ee40 commit 56e7c45
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 23 deletions.
6 changes: 6 additions & 0 deletions django_snow/helpers/snow_request_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,9 @@ def get_snow_group_guid(self, group_name):
self.group_guid_dict[group_name] = result['sys_id']

return self.group_guid_dict[group_name]

def clear_group_guid_cache(self):
"""
Clear the SNow Group Name - GUID cache.
"""
self.group_guid_dict.clear()
83 changes: 60 additions & 23 deletions testapp/tests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import uuid
from collections import OrderedDict

from django.test import TestCase, override_settings
from requests.exceptions import HTTPError
Expand Down Expand Up @@ -28,6 +27,9 @@ def setUp(self):
self.mock_pysnow_client = mock.MagicMock()
self.change_request_handler = ChangeRequestHandler()

def tearDown(self):
self.change_request_handler.clear_group_guid_cache()

def test__get_client(self, mock_pysnow):
mock_pysnow.Client.return_value = self.mock_pysnow_client
self.assertIs(
Expand Down Expand Up @@ -80,11 +82,12 @@ def test_create_change_request(self, mock_pysnow):
self.assertEqual(co.assignment_group_guid, fake_insert_retval['assignment_group']['value'])

def test_create_change_request_parameters(self, mock_pysnow):
expected_payload = OrderedDict()
expected_payload['type'] = 'normal'
expected_payload['assignment_group'] = 'bar'
expected_payload['short_description'] = 'Title'
expected_payload['description'] = 'Description'
expected_payload = {
'type': 'normal',
'assignment_group': 'bar',
'short_description': 'Title',
'description': 'Description'
}

fake_insert_retval = {
'sys_id': uuid.uuid4(),
Expand All @@ -100,20 +103,20 @@ def test_create_change_request_parameters(self, mock_pysnow):
self.mock_pysnow_client.resource.return_value = fake_resource
mock_pysnow.Client.return_value = self.mock_pysnow_client

payload = OrderedDict()
payload['type'] = 'normal'
payload['assignment_group'] = 'bar'
payload = {
'type': 'normal',
'assignment_group': 'bar'
}
self.change_request_handler.create_change_request('Title', 'Description', None, payload=payload)
fake_resource.create.assert_called_with(payload=expected_payload)

def test_create_change_request_default_parameters(self, mock_pysnow):
expected_payload = OrderedDict()
expected_payload['short_description'] = 'Title'
expected_payload['description'] = 'Description'
expected_payload['type'] = 'standard'
expected_payload['assignment_group'] = 'bar'

self.change_request_handler.group_guid_dict['assignment_group'] = 'bar'
expected_payload = {
'short_description': 'Title',
'description': 'Description',
'type': 'standard',
'assignment_group': 'bar'
}

fake_insert_retval = {
'sys_id': uuid.uuid4(),
Expand All @@ -125,11 +128,17 @@ def test_create_change_request_default_parameters(self, mock_pysnow):
}

fake_resource = mock.MagicMock()

# For Assignment Group GUID
fake_asgn_group_guid_response = mock.MagicMock()
fake_asgn_group_guid_response.one.return_value = {'sys_id': 'bar'}
fake_resource.get.return_value = fake_asgn_group_guid_response

fake_resource.create.return_value = fake_insert_retval
self.mock_pysnow_client.resource.return_value = fake_resource
mock_pysnow.Client.return_value = self.mock_pysnow_client

self.change_request_handler.create_change_request('Title', 'Description', None, payload=OrderedDict())
self.change_request_handler.create_change_request('Title', 'Description', None, payload={})
fake_resource.create.assert_called_with(payload=expected_payload)

def test_create_change_request_raises_exception_for_http_error(self, mock_pysnow):
Expand All @@ -144,7 +153,7 @@ def test_create_change_request_raises_exception_for_http_error(self, mock_pysnow
self.mock_pysnow_client.resource.return_value = fake_resource
mock_pysnow.Client.return_value = self.mock_pysnow_client

with self.assertRaises(ChangeRequestException):
with self.assertRaisesRegex(ChangeRequestException, 'Could not create change request due to.*'):
self.change_request_handler.create_change_request('Title', 'Description', None, payload={})

def test_create_change_request_raises_exception_when_error_in_result(self, mock_pysnow):
Expand All @@ -158,7 +167,7 @@ def test_create_change_request_raises_exception_when_error_in_result(self, mock_
self.mock_pysnow_client.resource.return_value = fake_resource
mock_pysnow.Client.return_value = self.mock_pysnow_client

with self.assertRaises(ChangeRequestException):
with self.assertRaisesRegex(ChangeRequestException, 'Could not create change request due to.*'):
self.change_request_handler.create_change_request('Title', 'Description', None, payload={})

@mock.patch('django_snow.helpers.snow_request_handler.ChangeRequestHandler.update_change_request')
Expand Down Expand Up @@ -219,7 +228,7 @@ def test_update_change_request_raises_exception_for_http_error(self, mock_pysnow
self.mock_pysnow_client.resource.return_value = fake_resource
mock_pysnow.Client.return_value = self.mock_pysnow_client

with self.assertRaises(ChangeRequestException):
with self.assertRaisesRegex(ChangeRequestException, 'Could not update change request due to '):
self.change_request_handler.update_change_request(fake_change_order, payload='{"foo": "bar"}')

def test_update_change_request_raises_exception_for_error_in_result(self, mock_pysnow):
Expand All @@ -230,12 +239,23 @@ def test_update_change_request_raises_exception_for_error_in_result(self, mock_p
self.mock_pysnow_client.resource.return_value = fake_resource
mock_pysnow.Client.return_value = self.mock_pysnow_client

with self.assertRaises(ChangeRequestException):
with self.assertRaisesRegex(ChangeRequestException, 'Could not update change request due to '):
self.change_request_handler.update_change_request(fake_change_order, payload='{"foo": "bar"}')

def test_get_snow_group_guid_cached_result(self, mock_pysnow):
self.change_request_handler.group_guid_dict['foo'] = 'bar'
self.assertEqual(self.change_request_handler.get_snow_group_guid('foo'), 'bar')
fake_resource = mock.MagicMock()
fake_response = mock.MagicMock()
fake_response.one.return_value = {'sys_id': 'bar'}
fake_resource.get.return_value = fake_response
self.mock_pysnow_client.resource.return_value = fake_resource
mock_pysnow.Client.return_value = self.mock_pysnow_client

self.change_request_handler.get_snow_group_guid('foo')
cached_guid = self.change_request_handler.get_snow_group_guid('foo')

# resource.get() should be called only once, since the value from previous call should have been cached.
fake_resource.get.assert_called_once_with(query={'name': 'foo'})
self.assertEqual(cached_guid, 'bar')

def test_get_snow_group_guid(self, mock_pysnow):
fake_resource = mock.MagicMock()
Expand All @@ -246,3 +266,20 @@ def test_get_snow_group_guid(self, mock_pysnow):
mock_pysnow.Client.return_value = self.mock_pysnow_client

self.assertEqual(self.change_request_handler.get_snow_group_guid('hello'), 'yo')

def test_clear_snow_group_guid_cache(self, mock_pysnow):
fake_resource = mock.MagicMock()
fake_response = mock.MagicMock()
fake_response.one.return_value = {'sys_id': 'some_id'}
fake_resource.get.return_value = fake_response
self.mock_pysnow_client.resource.return_value = fake_resource
mock_pysnow.Client.return_value = self.mock_pysnow_client

self.change_request_handler.get_snow_group_guid('hello')
self.change_request_handler.get_snow_group_guid('hello')
self.assertEqual(fake_resource.get.call_count, 1)

self.change_request_handler.clear_group_guid_cache()
self.change_request_handler.get_snow_group_guid('hello')
self.change_request_handler.get_snow_group_guid('hello')
self.assertEqual(fake_resource.get.call_count, 2)

0 comments on commit 56e7c45

Please sign in to comment.