How to use get_mock method in Httmock

Best Python code snippet using httmock_python

allocation_test.py

Source:allocation_test.py Github

copy

Full Screen

1"""Unit test for treadmill.cli.allocation2"""3from __future__ import absolute_import4from __future__ import division5from __future__ import print_function6from __future__ import unicode_literals7import unittest8import click9import click.testing10import mock11import treadmill12from treadmill import plugin_manager13class AllocationTest(unittest.TestCase):14 """Mock test for treadmill.cli.allocation"""15 def setUp(self):16 """Setup common test variables"""17 self.runner = click.testing.CliRunner()18 self.alloc_cli = plugin_manager.load('treadmill.cli',19 'allocation').init()20 @mock.patch('treadmill.restclient.get')21 @mock.patch('treadmill.restclient.delete',22 mock.Mock(return_value=mock.MagicMock()))23 @mock.patch('treadmill.context.Context.admin_api',24 mock.Mock(return_value=['http://xxx:1234']))25 def test_allocation_delete(self, get_mock):26 """Test cli.allocation: delete"""27 # delete a tenant28 # subtest case 1: no subtenant and reservation29 # initiate two returning objects from two restclient.get invocations30 return_mock1 = mock.Mock()31 return_mock2 = mock.Mock()32 # this one is for get all tenants33 return_mock1.json.return_value = [{34 '_id': None,35 'tenant': 'tent',36 'systems': [1, 2, 3]}]37 # this one is for get all reservations under 'tent'38 return_mock2.json.return_value = []39 get_mock.side_effect = [return_mock1, return_mock2]40 result = self.runner.invoke(self.alloc_cli,41 ['delete', 'tent'])42 self.assertEqual(result.exit_code, 0)43 treadmill.restclient.delete.assert_called_with(44 ['http://xxx:1234'],45 '/tenant/tent'46 )47 calls = [mock.call(['http://xxx:1234'], '/tenant/'),48 mock.call(['http://xxx:1234'], '/allocation/tent')]49 get_mock.assert_has_calls(calls)50 self.assertEqual(treadmill.restclient.get.call_count, 2)51 # subtest case 2: has subtenant52 get_mock.reset_mock()53 get_mock.return_value = mock.DEFAULT54 get_mock.side_effect = None55 return_mock1.json.return_value = [56 {'_id': None,57 'tenant': 'tent',58 'systems': [1, 2, 3]},59 {'_id': None,60 'tenant': 'tent:subtent',61 'systems': [1, 2, 3]}]62 get_mock.return_value = return_mock163 result = self.runner.invoke(self.alloc_cli,64 ['delete', 'tent'])65 self.assertEqual(result.exit_code, 0)66 get_mock.assert_called_once_with(['http://xxx:1234'], '/tenant/')67 # subtest case 3: tenant does not exist68 get_mock.reset_mock()69 get_mock.return_value = mock.DEFAULT70 from treadmill.restclient import NotFoundError71 get_mock.side_effect = [return_mock2, NotFoundError]72 result = self.runner.invoke(self.alloc_cli,73 ['delete', 'tent'])74 self.assertEqual(result.exit_code, 1)75 calls = [mock.call(['http://xxx:1234'], '/tenant/'),76 mock.call(['http://xxx:1234'], '/allocation/tent')]77 get_mock.assert_has_calls(calls)78 self.assertEqual(treadmill.restclient.get.call_count, 2)79 # subtest case 4: has reservation80 get_mock.reset_mock()81 get_mock.return_value = mock.DEFAULT82 return_mock1.json.return_value = [83 {'_id': None,84 'tenant': 'tent',85 'systems': [1, 2, 3]}]86 return_mock2.json.return_value = [{'_id': 'tent/dev'}]87 get_mock.side_effect = [return_mock1, return_mock2]88 result = self.runner.invoke(self.alloc_cli,89 ['delete', 'tent'])90 self.assertEqual(result.exit_code, 0)91 calls = [mock.call(['http://xxx:1234'], '/tenant/'),92 mock.call().json(),93 mock.call(['http://xxx:1234'], '/allocation/tent')]94 get_mock.assert_has_calls(calls)95 self.assertEqual(treadmill.restclient.get.call_count, 2)96 # delete all reservations97 result = self.runner.invoke(self.alloc_cli,98 ['delete', 'tent/dev'])99 self.assertEqual(result.exit_code, 0)100 treadmill.restclient.delete.assert_called_with(101 ['http://xxx:1234'],102 '/allocation/tent/dev'103 )104 # delete a reservation105 result = self.runner.invoke(self.alloc_cli,106 ['delete', 'tent/dev/rr'])107 self.assertEqual(result.exit_code, 0)108 treadmill.restclient.delete.assert_called_with(109 ['http://xxx:1234'],110 '/allocation/tent/dev/reservation/rr'111 )112 @mock.patch('treadmill.restclient.put')113 @mock.patch('treadmill.restclient.get')114 @mock.patch('treadmill.context.Context.admin_api',115 mock.Mock(return_value=['http://xxx:1234']))116 def test_allocation_configure(self, get_mock, put_mock):117 """Test cli.allocation: configure"""118 get_mock.return_value.json.return_value = {'systems': [1, 2]}119 self.runner.invoke(120 self.alloc_cli, ['configure', 'tent/dev', '--systems', '3']121 )122 put_mock.assert_called_with(123 [u'http://xxx:1234'],124 u'/tenant/tent/dev',125 payload={126 u'systems': [1, 2, 3]127 }128 )129 put_mock.reset_mock()130 self.runner.invoke(131 self.alloc_cli,132 ['configure', 'tent/dev', '--systems', '3', '--set']133 )134 put_mock.assert_called_with(135 [u'http://xxx:1234'],136 u'/tenant/tent/dev',137 payload={138 u'systems': [3]139 }140 )141 @mock.patch('treadmill.restclient.put')142 @mock.patch('treadmill.restclient.post')143 @mock.patch('treadmill.restclient.get')144 @mock.patch('treadmill.cli.allocation._display_tenant', mock.Mock())145 @mock.patch('treadmill.context.Context.admin_api',146 mock.Mock(return_value=['http://xxx:1234']))147 def test_allocation_reserve(self, get_mock, post_mock, put_mock):148 """Test cli.allocation: reserve"""149 return_mock1 = mock.Mock()150 return_mock2 = mock.Mock()151 return_mock1.json.return_value = [{152 '_id': None,153 'tenant': 'tent',154 'systems': [1, 2, 3]}]155 return_mock2.json.return_value = {"cpu": "0%",156 "disk": "0M",157 "rank_adjustment": 10,158 "partition": "_default",159 "memory": "0M",160 "assignments": [],161 "rank": 100,162 "max_utilization": None,163 "_id": "tent/qa/test-v3",164 "cell": "test-v3",165 "traits": []}166 get_mock.side_effect = [return_mock1,167 treadmill.restclient.NotFoundError,168 return_mock1, return_mock2,169 return_mock1, return_mock2]170 result = self.runner.invoke(171 self.alloc_cli, ['reserve', 'tent', '--env', 'qa',172 '--cell', 'test-v3', '--empty'])173 self.assertEqual(result.exit_code, 0)174 result = self.runner.invoke(175 self.alloc_cli, ['reserve', 'tent', '--env', 'qa',176 '--cell', 'test-v3', '--memory', '125M',177 '--partition', 'aq7'])178 self.assertEqual(result.exit_code, 0)179 result = self.runner.invoke(180 self.alloc_cli, ['reserve', 'tent', '--env', 'qa',181 '--cell', 'test-v3',182 '--max-utilization', '10'])183 self.assertEqual(result.exit_code, 0)184 result = self.runner.invoke(185 self.alloc_cli, ['reserve', 'tent', '--env', 'qa',186 '--cell', 'test-v3', '--traits', 'X,Y'])187 self.assertEqual(result.exit_code, 1)188 call1 = mock.call(['http://xxx:1234'], '/tenant/tent')189 call2 = mock.call(['http://xxx:1234'],190 '/allocation/tent/qa/reservation/test-v3')191 calls = [call1, call2, call1, call2, call1, call2, call1]192 self.assertEqual(get_mock.call_count, 7)193 get_mock.assert_has_calls(calls, any_order=False)194 call1 = mock.call(['http://xxx:1234'], '/allocation/tent/qa',195 payload={'environment': 'qa'})196 call2 = mock.call(['http://xxx:1234'],197 '/allocation/tent/qa/reservation/test-v3',198 payload={'memory': '0M', 'cpu': '0%', 'disk': '0M'})199 calls = [call1, call2, call1, call1]200 post_mock.assert_has_calls(calls, any_order=False)201 self.assertEqual(post_mock.call_count, 4)202 call1 = mock.call(['http://xxx:1234'],203 '/allocation/tent/qa/reservation/' +204 'test-v3',205 payload={'memory': '125M',206 'partition': 'aq7',207 'cpu': '0%',208 'disk': '0M'})209 call2 = mock.call(['http://xxx:1234'],210 '/allocation/tent/qa/reservation/' +211 'test-v3',212 payload={'memory': '0M',213 'partition': '_default',214 'cpu': '0%',215 'disk': '0M',216 'max_utilization': 10})217 calls = [call1, call2]218 self.assertEqual(put_mock.call_count, 2)219 put_mock.assert_has_calls(calls, any_order=False)220if __name__ == '__main__':...

Full Screen

Full Screen

test_github_checks.py

Source:test_github_checks.py Github

copy

Full Screen

1import unittest2from ..automerge import _get_github_checks3@unittest.mock.patch("conda_forge_tick_action.automerge._get_checks")4def test_get_github_checks_nochecks(get_mock):5 get_mock.return_value = {}6 stat = _get_github_checks(1, 2, 3)7 get_mock.assert_called_once_with(1, 2, 3)8 assert stat == {}9@unittest.mock.patch("conda_forge_tick_action.automerge._get_checks")10def test_get_github_checks_ignores_self(get_mock):11 get_mock.return_value = [12 {13 'app': {'slug': 'github-actions'},14 'status': 'blah',15 'conclusion': 'blah',16 },17 ]18 stat = _get_github_checks(1, 2, 3)19 get_mock.assert_called_once_with(1, 2, 3)20 assert stat == {}21@unittest.mock.patch("conda_forge_tick_action.automerge._get_checks")22def test_check_github_checks_all_pending(get_mock):23 get_mock.return_value = [24 {25 'app': {'slug': 'c1'},26 'status': 'blah',27 'conclusion': 'blah',28 },29 {30 'app': {'slug': 'c2'},31 'status': 'blah',32 'conclusion': 'blah',33 },34 ]35 stat = _get_github_checks(1, 2, 3)36 get_mock.assert_called_once_with(1, 2, 3)37 assert stat == {"c1": None, "c2": None}38@unittest.mock.patch("conda_forge_tick_action.automerge._get_checks")39def test_check_github_checks_all_fail(get_mock):40 get_mock.return_value = [41 {42 'app': {'slug': 'c1'},43 'status': 'completed',44 'conclusion': 'error',45 },46 {47 'app': {'slug': 'c2'},48 'status': 'completed',49 'conclusion': 'failure',50 },51 ]52 stat = _get_github_checks(1, 2, 3)53 get_mock.assert_called_once_with(1, 2, 3)54 assert stat == {"c1": False, "c2": False}55@unittest.mock.patch("conda_forge_tick_action.automerge._get_checks")56def test_check_github_checks_all_success(get_mock):57 get_mock.return_value = [58 {59 'app': {'slug': 'c1'},60 'status': 'completed',61 'conclusion': 'success',62 },63 {64 'app': {'slug': 'c2'},65 'status': 'completed',66 'conclusion': 'success',67 },68 ]69 stat = _get_github_checks(1, 2, 3)70 get_mock.assert_called_once_with(1, 2, 3)71 assert stat == {"c1": True, "c2": True}72@unittest.mock.patch("conda_forge_tick_action.automerge._get_checks")73def test_check_github_checks_success_plus_pending(get_mock):74 get_mock.return_value = [75 {76 'app': {'slug': 'c1'},77 'status': 'blah',78 'conclusion': 'success',79 },80 {81 'app': {'slug': 'c2'},82 'status': 'completed',83 'conclusion': 'success',84 },85 ]86 stat = _get_github_checks(1, 2, 3)87 get_mock.assert_called_once_with(1, 2, 3)88 assert stat == {"c1": None, "c2": True}89@unittest.mock.patch("conda_forge_tick_action.automerge._get_checks")90def test_check_github_checks_success_plus_fail(get_mock):91 get_mock.return_value = [92 {93 'app': {'slug': 'c1'},94 'status': 'completed',95 'conclusion': 'error',96 },97 {98 'app': {'slug': 'c2'},99 'status': 'completed',100 'conclusion': 'failure',101 },102 {103 'app': {'slug': 'c3'},104 'status': 'completed',105 'conclusion': 'success',106 },107 ]108 stat = _get_github_checks(1, 2, 3)109 get_mock.assert_called_once_with(1, 2, 3)110 assert stat == {"c1": False, "c2": False, "c3": True}111@unittest.mock.patch("conda_forge_tick_action.automerge._get_checks")112def test_check_github_checks_pending_plus_fail(get_mock):113 get_mock.return_value = [114 {115 'app': {'slug': 'c1'},116 'status': 'completed',117 'conclusion': 'error',118 },119 {120 'app': {'slug': 'c2'},121 'status': 'completed',122 'conclusion': 'failure',123 },124 {125 'app': {'slug': 'c3'},126 'status': 'blah',127 'conclusion': 'success',128 },129 ]130 stat = _get_github_checks(1, 2, 3)131 get_mock.assert_called_once_with(1, 2, 3)132 assert stat == {"c1": False, "c2": False, "c3": None}133@unittest.mock.patch("conda_forge_tick_action.automerge._get_checks")134def test_check_github_checks_pending_plus_success_plus_fail(get_mock):135 get_mock.return_value = [136 {137 'app': {'slug': 'c1'},138 'status': 'completed',139 'conclusion': 'error',140 },141 {142 'app': {'slug': 'c2'},143 'status': 'completed',144 'conclusion': 'failure',145 },146 {147 'app': {'slug': 'c3'},148 'status': 'blah',149 'conclusion': 'success',150 },151 {152 'app': {'slug': 'c4'},153 'status': 'completed',154 'conclusion': 'success',155 },156 ]157 stat = _get_github_checks(1, 2, 3)158 get_mock.assert_called_once_with(1, 2, 3)...

Full Screen

Full Screen

test_api.py

Source:test_api.py Github

copy

Full Screen

1import mock2import pytest3import requests4from requests.exceptions import RequestException5from banter.api import ChanApi, ChanApiError6class TestChanApi(object):7 def setup(self):8 self.api = ChanApi('foo')9 self.response_mock = requests.models.Response()10 self.response_mock.status_code = 20011 self.response_mock._content = b'{"data": []}'12 @mock.patch('requests.get')13 def test_get_resource_requests_exception(self, get_mock):14 get_mock.side_effect = RequestException('Something went wrong')15 with pytest.raises(ChanApiError) as exc:16 self.api.get_resource()17 assert 'Unable to fetch' in str(exc)18 @mock.patch('requests.get')19 def test_get_resource_json_exception(self, get_mock):20 response = requests.models.Response()21 response.status_code = 20022 response._content = '{"foo": "bar"}'23 get_mock.return_value = response24 with pytest.raises(ChanApiError) as exc:25 self.api.get_resource()26 assert 'Invalid Json' in str(exc)27 @mock.patch('requests.get')28 def test_get_resource_response_not_ok(self, get_mock):29 response = requests.models.Response()30 response.status_code = 40431 response._content = 'Not Found'32 get_mock.return_value = response33 with pytest.raises(ChanApiError) as exc:34 self.api.get_resource()35 assert '404 - Not Found' in str(exc)36 @mock.patch('requests.get')37 def test_success_call(self, get_mock):38 get_mock.return_value = self.response_mock39 # no args40 api = ChanApi('foo')41 api.get_resource()42 get_mock.assert_called_with('http://a.4cdn.org/foo.json')43 # args44 api = ChanApi('foo/{0}')45 api.get_resource(1)46 get_mock.assert_called_with('http://a.4cdn.org/foo/1.json')47 @mock.patch('requests.get')48 def test_get_thread(self, get_mock):49 get_mock.return_value = self.response_mock50 ChanApi.get_thread('g', '1')51 get_mock.assert_called_with('http://a.4cdn.org/g/thread/1.json')52 @mock.patch('requests.get')53 def test_get_page(self, get_mock):54 get_mock.return_value = self.response_mock55 ChanApi.get_page('g', '6')56 get_mock.assert_called_with('http://a.4cdn.org/g/6.json')57 @mock.patch('requests.get')58 def test_get_catalog(self, get_mock):59 get_mock.return_value = self.response_mock60 ChanApi.get_catalog('g')61 get_mock.assert_called_with('http://a.4cdn.org/g/catalog.json')62 @mock.patch('requests.get')63 def test_get_threads(self, get_mock):64 get_mock.return_value = self.response_mock65 ChanApi.get_threads('g')66 get_mock.assert_called_with('http://a.4cdn.org/g/threads.json')67 @mock.patch('requests.get')68 def test_get_archive(self, get_mock):69 get_mock.return_value = self.response_mock70 ChanApi.get_archive('g')71 get_mock.assert_called_with('http://a.4cdn.org/g/archive.json')72 @mock.patch('requests.get')73 def test_get_boards(self, get_mock):74 get_mock.return_value = self.response_mock75 ChanApi.get_boards()...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Httmock automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful