Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for IMDB module #958

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/scrape_up/imdb/imdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ def scrape_genre_movies(self, genre):
genre_data = scraper.scrape_genre_movies(genre)

json_data = json.dumps(genre_data, indent=4)
print(json_data)
```
Return\n
```python
Expand All @@ -103,8 +102,6 @@ def scrape_genre_movies(self, genre):
try:
url = "https://www.imdb.com/search/title/?genres={}&sort=user_rating,desc&title_type=feature&num_votes=25000,&pf_rd_m=A2FGELUUNOQJNL&pf_rd_p=5aab685f-35eb-40f3-95f7-c53f09d542c3&pf_rd_r=N97GEQS6R7J9EV7V770D&pf_rd_s=right-6&pf_rd_t=15506&pf_rd_i=top&ref_=chttp_gnr_16"
formatted_url = url.format(genre)
print(formatted_url)

resp = requests.get(formatted_url, headers=self.headers)
content = BeautifulSoup(resp.content, "lxml")
genres = [
Expand Down
61 changes: 61 additions & 0 deletions src/test/imdb_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import unittest
from scrape_up.imdb import IMDB

class IMDBTest(unittest.TestCase):
"""
imdb module test.\n
| Method | Details |
| ---------------------| ------------------------------------------------------------------- |
| `.top_rated()` | Returns the list of top rated movies |
| `.scrape_genre_movies(genre)` | Returns the list of movies of a specific genre |
| `.top_rated_shows()` | Returns the list of top rated TV shows |
"""

def setUp(self):
self.scraper = IMDB()

def test_top_rated(self):
response = self.scraper.top_rated()
self.assertGreater(len(response), 0, "Top rated movies list is empty")
self.assertIsInstance(response, list, "Top rated movies is not a list")
self.assertTrue(
all(
isinstance(movie, dict) and "title" in movie and "year" in movie and "duration" in movie and "rating" in movie
for movie in response
),
"Incorrect format for top rated movies",
)

def test_scrape_genre_movies(self):
genre = "Adventure"
response = self.scraper.scrape_genre_movies(genre)

self.assertIsInstance(response, list, f"{genre} movies is not a list")
self.assertGreater(len(response), 0, f"{genre} movies list is empty")

for movie in response:
self.assertIsInstance(movie, dict)
self.assertIn("title", movie)
self.assertIn("year", movie)
self.assertIn("certificate", movie)
self.assertIn("time", movie)
self.assertIn("genre", movie)
self.assertIn("rating", movie)
self.assertIn("simple_desc", movie)
self.assertIn("votes", movie)

def test_top_rated_shows(self):
response = self.scraper.top_rated_shows()

self.assertIsInstance(response, list, "Top rated shows is not a list")
self.assertGreater(len(response), 0, "Top rated shows list is empty")

for show in response:
self.assertIsInstance(show, dict)
self.assertIn("title", show)
self.assertIn("year", show)
self.assertIn("episode", show)
self.assertIn("rating", show)

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