Skip to content

Commit

Permalink
add files to test execution
Browse files Browse the repository at this point in the history
  • Loading branch information
KevKibe committed Oct 30, 2024
1 parent 81aa0be commit 4e061c9
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/test-kaggle-action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
username: ${{ secrets.KAGGLE_USERNAME }}
key: ${{ secrets.KAGGLE_KEY }}
title: "Kernel Test v2"
custom_script: "print('Success')"
custom_script: "!pytest test.py && !python fetch_data.py --url "https://jsonplaceholder.typicode.com/posts/1""
enable_internet: true
enable_gpu: false
enable_tpu: false
Expand Down
24 changes: 24 additions & 0 deletions fetch_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import requests
import argparse

def fetch_data(url):
"""
Fetches data from the given URL and returns it as JSON.
"""
try:
response = requests.get(url)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
return None

if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Fetch JSON data from a specified URL.")
parser.add_argument('-u', '--url', type=str, required=True, help="The URL to fetch data from")

args = parser.parse_args()
url = args.url

data = fetch_data(url)
print(data)
42 changes: 42 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import unittest
from unittest.mock import patch
import requests
from fetch_data import fetch_data

class TestFetchData(unittest.TestCase):

@patch('requests.get')
def test_fetch_data_success(self, mock_get):
mock_response = unittest.mock.Mock()
mock_response.raise_for_status.return_value = None # Simulate no HTTP error
mock_response.json.return_value = {"userId": 1, "id": 1, "title": "Test Post", "body": "This is a test."}
mock_get.return_value = mock_response

url = "https://jsonplaceholder.typicode.com/posts/1"
result = fetch_data(url)

self.assertEqual(result, {"userId": 1, "id": 1, "title": "Test Post", "body": "This is a test."})

@patch('requests.get')
def test_fetch_data_http_error(self, mock_get):
mock_response = unittest.mock.Mock()
mock_response.raise_for_status.side_effect = requests.exceptions.HTTPError("404 Not Found")
mock_get.return_value = mock_response

url = "https://jsonplaceholder.typicode.com/posts/1"
result = fetch_data(url)

self.assertIsNone(result)

@patch('requests.get')
def test_fetch_data_request_exception(self, mock_get):
mock_get.side_effect = requests.exceptions.RequestException("Network error")

url = "https://jsonplaceholder.typicode.com/posts/1"
result = fetch_data(url)

self.assertIsNone(result)


if __name__ == '__main__':
unittest.main()

0 comments on commit 4e061c9

Please sign in to comment.