Skip to content

Commit

Permalink
Fixed problem with not failing when status code != 200 during getting…
Browse files Browse the repository at this point in the history
… config to be extended
  • Loading branch information
pz2 committed Jun 5, 2024
1 parent c3ad3c3 commit eb4b124
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
36 changes: 36 additions & 0 deletions test/extends/valhalla_extends_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import unittest
from unittest.mock import patch, Mock

from valhalla.extends.valhalla_extends import get_from_url


class TestGetFromUrl(unittest.TestCase):

@patch('requests.get')
def test_get_from_url_success(self, mock_get):
# given:
mock_response = Mock()
mock_response.status_code = 200
mock_response.text = "Success"
mock_get.return_value = mock_response

# when:
result = get_from_url('http://example.com')

# then:
self.assertEqual(result, "Success")

@patch('requests.get')
@patch('valhalla.extends.valhalla_extends.exit')
def test_get_from_url_failure(self, mock_exit, mock_get):
# given:
mock_response = Mock()
mock_response.status_code = 404
mock_response.text = "Not Found"
mock_get.return_value = mock_response

# when:
get_from_url('http://example.com')

# then:
mock_exit.assert_called_with(1)
2 changes: 1 addition & 1 deletion valhalla/common/get_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from yaml import safe_load

from valhalla.common.logger import info, error, warn
from valhalla.extends.ValhallaExtends import ValhallaExtends
from valhalla.extends.valhalla_extends import ValhallaExtends


class MergeRequestConfig:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@

from valhalla.common.logger import info, error
from valhalla.extends.merge_dicts import merge
from valhalla.common.resolver import resolve


def get_from_url(url):
result = ""
data = requests.get(resolve(url)).text
response = requests.get(url)

if response.status_code != 200:
info(f"Error: Received status code {response.status_code} from url: {url}")
exit(1)

data = response.text
for line in data:
result += line
info("Loaded from ULR")

info("Loaded from URL")
info("===========================================")
print(result)
info(result)
info("===========================================")
return result

Expand Down

0 comments on commit eb4b124

Please sign in to comment.