|
| 1 | +# Copyright (c) 2019 WayScript, Inc. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +from unittest import TestCase |
| 5 | +from unittest.mock import patch |
| 6 | + |
| 7 | +from wayscript import WayScript |
| 8 | +from wayscript.exceptions import InvalidApiKeyException |
| 9 | + |
| 10 | + |
| 11 | +class TestWayScript( TestCase ): |
| 12 | + dummy_api_key = "_DUMMY_API_KEY_DUMMY_API_KEY_DUMMY_API_KEY_" |
| 13 | + program_id = 1234 |
| 14 | + variables = [ 'one', 'two', 'three' ] |
| 15 | + api_url = 'https://wayscript.com/api' |
| 16 | + |
| 17 | + def test_api_key( self ): |
| 18 | + with self.assertRaises( InvalidApiKeyException ): |
| 19 | + WayScript( '' ) |
| 20 | + |
| 21 | + with self.assertRaises( InvalidApiKeyException ): |
| 22 | + WayScript( 'foobar' ) |
| 23 | + |
| 24 | + self.assertIsNotNone( WayScript( self.dummy_api_key ) ) |
| 25 | + |
| 26 | + def test_run_program( self ): |
| 27 | + wayscript = WayScript( self.dummy_api_key ) |
| 28 | + |
| 29 | + with patch( 'requests.post' ) as post_request: |
| 30 | + wayscript.run_program( self.program_id ) |
| 31 | + post_request.assert_called_once_with( self.api_url, params = { 'api_key': self.dummy_api_key, |
| 32 | + 'program_id': self.program_id, |
| 33 | + 'run_async': False } ) |
| 34 | + |
| 35 | + def test_run_program_with_variables( self ): |
| 36 | + wayscript = WayScript( self.dummy_api_key ) |
| 37 | + |
| 38 | + with patch( 'requests.post' ) as post_request: |
| 39 | + wayscript.run_program( self.program_id, variables = self.variables ) |
| 40 | + post_request.assert_called_once_with( self.api_url, params = { 'api_key': self.dummy_api_key, |
| 41 | + 'program_id': self.program_id, |
| 42 | + 'run_async': False, |
| 43 | + 'variables': self.variables } ) |
| 44 | + |
| 45 | + def test_run_program_async( self ): |
| 46 | + wayscript = WayScript( self.dummy_api_key ) |
| 47 | + |
| 48 | + with patch( 'requests.post' ) as post_request: |
| 49 | + wayscript.run_program( self.program_id, variables = self.variables, run_async = True ) |
| 50 | + post_request.assert_called_once_with( self.api_url, params = { 'api_key': self.dummy_api_key, |
| 51 | + 'program_id': self.program_id, |
| 52 | + 'run_async': True, |
| 53 | + 'variables': self.variables } ) |
| 54 | + |
| 55 | + def test_empty_variables( self ): |
| 56 | + wayscript = WayScript( self.dummy_api_key ) |
| 57 | + |
| 58 | + with patch( 'requests.post' ) as post_request: |
| 59 | + wayscript.run_program( self.program_id, variables = [ ], run_async = True ) |
| 60 | + post_request.assert_called_once_with( self.api_url, params = { 'api_key': self.dummy_api_key, |
| 61 | + 'program_id': self.program_id, |
| 62 | + 'run_async': True } ) |
| 63 | + |
| 64 | + def test_returns_response( self ): |
| 65 | + wayscript = WayScript( self.dummy_api_key ) |
| 66 | + |
| 67 | + with patch( 'requests.post', return_value = 'ok' ) as post_request: |
| 68 | + response = wayscript.run_program( self.program_id, run_async = True ) |
| 69 | + post_request.assert_called_once_with( self.api_url, params = { 'api_key': self.dummy_api_key, |
| 70 | + 'program_id': self.program_id, |
| 71 | + 'run_async': True } ) |
| 72 | + self.assertEqual( response, 'ok' ) |
0 commit comments