Skip to content

Commit

Permalink
feat: add support for Certifications
Browse files Browse the repository at this point in the history
  • Loading branch information
leandcesar committed Mar 11, 2023
1 parent 2b1802b commit f3e8699
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
48 changes: 48 additions & 0 deletions tests/test_certifications.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from unittest.mock import patch

import pytest

from themoviedb import routes, schemas


@pytest.mark.asyncio
async def test_certifications_movie(get_data, assert_data):
data = get_data("certifications/movie_list")

with patch("themoviedb.routes.base.ClientSession.request") as mocked:
mocked.return_value.__aenter__.return_value.json.return_value = data
certifications = await routes.Certifications().movie()
mocked.assert_called_with(
"GET",
"https://api.themoviedb.org/3/certification/movie/list",
params={
"api_key": "TEST_TMDB_KEY",
"language": "en-US",
"region": "US",
"watch_region": "US",
}
)

assert isinstance(certifications, schemas.Certifications)
assert assert_data(certifications, data)


@pytest.mark.asyncio
async def test_certifications_tv(get_data, assert_data):
data = get_data("certifications/tv_list")

with patch("themoviedb.routes.base.ClientSession.request") as mocked:
mocked.return_value.__aenter__.return_value.json.return_value = data
certifications = await routes.Certifications().tv()
mocked.assert_called_with(
"GET",
"https://api.themoviedb.org/3/certification/tv/list",
params={
"api_key": "TEST_TMDB_KEY",
"language": "en-US",
"region": "US",
"watch_region": "US",
}
)
assert isinstance(certifications, schemas.Certifications)
assert assert_data(certifications, data)
1 change: 1 addition & 0 deletions themoviedb/routes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from themoviedb.routes.base import Base
from themoviedb.routes.certifications import Certifications
from themoviedb.routes.credit import Credit
from themoviedb.routes.companies import Company
from themoviedb.routes.collections import Collection
Expand Down
22 changes: 22 additions & 0 deletions themoviedb/routes/certifications.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
from themoviedb import schemas, utils
from themoviedb.routes.base import Base


class Certifications(Base):

async def movie(self) -> schemas.Certifications:
"""Get an up to date list of the officially supported movie certifications on TMDB.
See more: https://developers.themoviedb.org/3/certifications/get-movie-certifications
"""
data = await self.request("certification/movie/list")
return utils.as_dataclass(schemas.Certifications, data)

async def tv(self) -> schemas.Certifications:
"""Get an up to date list of the officially supported TV show certifications on TMDB.
See more: https://developers.themoviedb.org/3/certifications/get-tv-certifications
"""
data = await self.request("certification/tv/list")
return utils.as_dataclass(schemas.Certifications, data)
1 change: 1 addition & 0 deletions themoviedb/schemas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from themoviedb.schemas._result import Dates, Result, ResultWithID, ResultWithPage
from themoviedb.schemas.alternative_names import AlternativeName, AlternativeNames
from themoviedb.schemas.alternative_titles import AlternativeTitle, AlternativeTitles
from themoviedb.schemas.certifications import Certification, Certifications
from themoviedb.schemas.changes import Change
from themoviedb.schemas.collections import Collection, Collections
from themoviedb.schemas.companies import Companies, Company
Expand Down
29 changes: 29 additions & 0 deletions themoviedb/schemas/certifications.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from dataclasses import dataclass
from typing import Dict, List, Optional

from themoviedb.schemas._result import Result


@dataclass
class Certification:
certification: Optional[str] = None
meaning: Optional[str] = None
order: Optional[int] = None


@dataclass
class Certifications(Result):
certifications: Optional[Dict[str, List[Certification]]] = None

def __post_init__(self) -> None:
self.results = self.certifications

def __bool__(self) -> bool:
return bool(self.results)

def __getitem__(self, region: str) -> List[Certification]:
return self.results[region]

@property
def regions(self) -> List[str]:
return list(self.results.keys())

0 comments on commit f3e8699

Please sign in to comment.