Skip to content

Commit

Permalink
add optional payload arg tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielburnworth committed Sep 24, 2024
1 parent b88c789 commit b95b3a3
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions tests/tests_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,25 @@ def test_api_get_endpoint_only(self, mock_request):
)
self.assertEqual(response, expected_response)

@patch('requests.request')
def test_api_get_with_payload(self, mock_request):
'''Test api_get: with payload'''
mock_response = Mock()
expected_response = {'device': 'info'}
mock_response.json.return_value = expected_response
mock_response.status_code = 200
mock_response.text = 'text'
mock_request.return_value = mock_response
# Call with endpoint only
response = self.fb.api_get('device', payload={'key': 'value'})
mock_request.assert_called_once_with(
method='GET',
url='https://my.farm.bot/api/device',
**REQUEST_KWARGS_WITH_PAYLOAD,
json={'key': 'value'},
)
self.assertEqual(response, expected_response)

@patch('requests.request')
def test_api_get_with_id(self, mock_request):
'''POSITIVE TEST: function called with valid ID'''
Expand Down Expand Up @@ -372,6 +391,24 @@ def test_api_post(self, mock_request):
])
self.assertEqual(point, {'name': 'new name'})

@patch('requests.request')
def test_api_post_no_payload(self, mock_request):
'''test api_post: no payload'''
mock_response = Mock()
mock_response.status_code = 200
mock_response.text = 'text'
mock_response.json.return_value = {'name': 'new name'}
mock_request.return_value = mock_response
point = self.fb.api_post('points')
mock_request.assert_has_calls([call(
method='POST',
url='https://my.farm.bot/api/points',
**REQUEST_KWARGS,
),
call().json(),
])
self.assertEqual(point, {'name': 'new name'})

@patch('requests.request')
def test_api_delete(self, mock_request):
'''test api_delete function'''
Expand All @@ -388,6 +425,24 @@ def test_api_delete(self, mock_request):
)
self.assertEqual(result, {'name': 'deleted'})

@patch('requests.request')
def test_api_delete_with_payload(self, mock_request):
'''test api_delete: with payload'''
mock_response = Mock()
mock_response.status_code = 200
mock_response.text = 'text'
mock_response.json.return_value = {'name': 'deleted'}
mock_request.return_value = mock_response
result = self.fb.api_delete(
'points', 12345, payload={'key': 'value'})
mock_request.assert_called_once_with(
method='DELETE',
url='https://my.farm.bot/api/points/12345',
**REQUEST_KWARGS_WITH_PAYLOAD,
json={'key': 'value'},
)
self.assertEqual(result, {'name': 'deleted'})

@patch('requests.request')
def test_api_delete_requests_disabled(self, mock_request):
'''test api_delete function: requests disabled'''
Expand Down

0 comments on commit b95b3a3

Please sign in to comment.