-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_app.py
69 lines (54 loc) · 1.85 KB
/
test_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import unittest
from app import app
from unittest.mock import patch, MagicMock
import pandas as pd
import os
from mylib.extract import extract
class FlaskAppTests(unittest.TestCase):
def setUp(self):
# Set up the test client
self.app = app.test_client()
self.app.testing = True
def test_home_page(self):
# Test the home page
response = self.app.get('/')
self.assertEqual(response.status_code, 200)
self.assertIn(b'Movie Request Form', response.data)
def test_movie_recommendations(self):
# Test the movie recommendations
response = self.app.post('/', data={
'name': 'Test User',
'genre': 'Drama',
'mood': 'Relaxed'
})
self.assertEqual(response.status_code, 200)
class ExtractTests(unittest.TestCase):
@patch('mylib.extract.requests.get')
@patch('mylib.extract.os.getenv')
def test_extract(self, mock_getenv, mock_get):
# Mock the environment variable
mock_getenv.return_value = 'fake_token'
# Mock the API response
mock_response = MagicMock()
mock_response.json.return_value = {
"results": [
{
"id": 1,
"title": "Test Movie",
"genre_ids": [28, 12]
}
]
}
mock_get.return_value = mock_response
# Call the extract function
file_path = extract(file_path="test_movies.csv", directory="test_data")
# Check if the file is created
self.assertTrue(os.path.exists(file_path))
# Read the CSV file and check its content
movie_pd = pd.read_csv(file_path)
self.assertEqual(len(movie_pd), 499)
# Clean up
os.remove(file_path)
os.rmdir("test_data")
if __name__ == '__main__':
unittest.main()