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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use std::borrow::Cow;
const PATH: &str = "/search/tv";
#[derive(Clone, Debug, Default)]
pub struct TVShowSearch {
pub query: String,
pub language: Option<String>,
pub page: Option<u32>,
pub include_adult: bool,
pub first_air_date_year: Option<u16>,
}
impl TVShowSearch {
pub fn new(query: String) -> Self {
Self {
query,
language: None,
page: None,
include_adult: false,
first_air_date_year: None,
}
}
pub fn with_language(mut self, value: Option<String>) -> Self {
self.language = value;
self
}
pub fn with_page(mut self, value: Option<u32>) -> Self {
self.page = value;
self
}
pub fn with_include_adult(mut self, value: bool) -> Self {
self.include_adult = value;
self
}
pub fn with_first_air_date_year(mut self, value: Option<u16>) -> Self {
self.first_air_date_year = value;
self
}
}
impl crate::prelude::Command for TVShowSearch {
type Output = crate::common::PaginatedResult<super::TVShowShort>;
fn path(&self) -> Cow<'static, str> {
Cow::Borrowed(PATH)
}
fn params(&self) -> Vec<(&'static str, Cow<'_, str>)> {
let mut res = vec![("query", Cow::Borrowed(self.query.as_str()))];
if let Some(language) = self.language.as_ref() {
res.push(("language", Cow::Borrowed(language.as_str())));
}
if let Some(page) = self.page {
res.push(("page", Cow::Owned(page.to_string())));
}
if self.include_adult {
res.push(("include_adult", Cow::Borrowed("true")));
}
if let Some(first_air_date_year) = self.first_air_date_year {
res.push((
"first_air_date_year",
Cow::Owned(first_air_date_year.to_string()),
));
}
res
}
}
#[cfg(test)]
mod tests {
use super::TVShowSearch;
use crate::prelude::Command;
use crate::Client;
use mockito::{mock, Matcher};
#[tokio::test]
async fn it_works() {
let client = Client::new("secret".into()).with_base_url(mockito::server_url());
let cmd = TVShowSearch::new("Whatever".into());
let _m = mock("GET", super::PATH)
.match_query(Matcher::AllOf(vec![
Matcher::UrlEncoded("api_key".into(), "secret".into()),
Matcher::UrlEncoded("query".into(), "Whatever".into()),
]))
.with_status(200)
.with_header("content-type", "application/json")
.with_body(include_str!("../../assets/search-tv.json"))
.create();
let result = cmd.execute(&client).await.unwrap();
assert_eq!(result.page, 1);
assert!(!result.results.is_empty());
assert!(result.total_pages > 0);
assert!(result.total_results > 0);
let item = result.results.first().unwrap();
assert_eq!(item.inner.name, "Game of Thrones");
}
#[tokio::test]
async fn invalid_api_key() {
let client = Client::new("secret".into()).with_base_url(mockito::server_url());
let cmd = TVShowSearch::new("Whatever".into());
let _m = mock("GET", super::PATH)
.match_query(Matcher::AllOf(vec![
Matcher::UrlEncoded("api_key".into(), "secret".into()),
Matcher::UrlEncoded("query".into(), "Whatever".into()),
]))
.with_status(401)
.with_header("content-type", "application/json")
.with_body(include_str!("../../assets/invalid-api-key.json"))
.create();
let err = cmd.execute(&client).await.unwrap_err();
let server_err = err.as_server_error().unwrap();
assert_eq!(server_err.body.as_other_error().unwrap().status_code, 7);
}
#[tokio::test]
async fn resource_not_found() {
let client = Client::new("secret".into()).with_base_url(mockito::server_url());
let cmd = TVShowSearch::new("Whatever".into());
let _m = mock("GET", super::PATH)
.match_query(Matcher::AllOf(vec![
Matcher::UrlEncoded("api_key".into(), "secret".into()),
Matcher::UrlEncoded("query".into(), "Whatever".into()),
]))
.with_status(404)
.with_header("content-type", "application/json")
.with_body(include_str!("../../assets/resource-not-found.json"))
.create();
let err = cmd.execute(&client).await.unwrap_err();
let server_err = err.as_server_error().unwrap();
assert_eq!(server_err.body.as_other_error().unwrap().status_code, 34);
}
}
#[cfg(all(test, feature = "integration"))]
mod integration_tests {
use super::TVShowSearch;
use crate::prelude::Command;
use crate::Client;
#[tokio::test]
async fn search_simpsons() {
let secret = std::env::var("TMDB_TOKEN_V3").unwrap();
let client = Client::new(secret);
let cmd = TVShowSearch::new("simpsons".into());
let result = cmd.execute(&client).await.unwrap();
assert_eq!(result.page, 1);
assert_eq!(result.results.len(), 3);
assert_eq!(result.total_pages, 1);
assert_eq!(result.total_results, 3);
let item = result.results.first().unwrap();
assert_eq!(item.inner.name, "The Simpsons");
}
}