File indexing completed on 2024-04-28 16:01:35

0001 /*
0002     Copyright (c) 2014 David Faure <faure@kde.org>
0003 
0004     This library is free software; you can redistribute it and/or
0005     modify it under the terms of the GNU Lesser General Public
0006     License as published by the Free Software Foundation; either
0007     version 2.1 of the License, or (at your option) version 3, or any
0008     later version accepted by the membership of KDE e.V. (or its
0009     successor approved by the membership of KDE e.V.), which shall
0010     act as a proxy defined in Section 6 of version 3 of the license.
0011 
0012     This library is distributed in the hope that it will be useful,
0013     but WITHOUT ANY WARRANTY; without even the implied warranty of
0014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015     Lesser General Public License for more details.
0016 
0017     You should have received a copy of the GNU Lesser General Public
0018     License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0019 
0020 */
0021 
0022 #undef QT_NO_CAST_FROM_ASCII
0023 #include <QTest>
0024 #include <QSignalSpy>
0025 #include <themoviedbapi.h>
0026 #include <searchjob.h>
0027 #include <tvsearchjob.h>
0028 #include <tvshowinfojob.h>
0029 #include <creditsjob.h>
0030 #include <movieinfojob.h>
0031 #include <tvseasondblist.h>
0032 #include <tvseasoninfojob.h>
0033 #include <tvepisodedblist.h>
0034 #include <QDebug>
0035 
0036 using namespace TmdbQt;
0037 
0038 class SearchTest : public QObject
0039 {
0040     Q_OBJECT
0041 
0042 public:
0043     SearchTest();
0044 
0045 private slots:
0046     void testSearch();
0047     void testTvSearch();
0048     void testMovieInfo();
0049     void testMovieInfoFightClub();
0050     void testTvShowInfo();
0051     void testTvSeasonInfo();
0052     void testCredits();
0053     void testEpisodeCredits();
0054 
0055 private:
0056     TheMovieDbApi m_api;
0057 };
0058 
0059 static const char s_key[] = "6c125ca74f059b4c88bc49e1b09e241e"; // themoviedb.org api key given to David Faure for KVideoManager
0060 
0061 SearchTest::SearchTest()
0062     : m_api(QString::fromLatin1(s_key))
0063 {
0064     QVERIFY(!m_api.isInitialized());
0065     QSignalSpy initSpy(&m_api, &TheMovieDbApi::initialized);
0066     QVERIFY(initSpy.wait());
0067     QVERIFY(m_api.isInitialized());
0068 }
0069 
0070 void SearchTest::testSearch()
0071 {
0072     const QString title = QString::fromUtf8("De l'autre côté du lit");
0073     SearchJob *job = m_api.searchMovie(title);
0074     QSignalSpy spy(job, &SearchJob::result);
0075     QVERIFY(spy.wait());
0076     QVERIFY2(!job->hasError(), qPrintable(job->errorMessage()));
0077     MovieDbList movies = job->searchResult();
0078     if (movies.count() > 1 && movies.at(1).id() == 15142) // "Dream a little dream" is "De l'autre côté du rêve", a fuzzy match...
0079         movies.removeAt(1);
0080     QCOMPARE(movies.count(), 1);
0081     MovieDb movie = movies.first();
0082     QCOMPARE(movie.id(), 15709);
0083     QCOMPARE(movie.releaseDate(), QDate(2009, 1, 7));
0084     QCOMPARE(movie.title(), QString::fromLatin1("Changing Sides"));
0085     QCOMPARE(movie.originalTitle(), title);
0086     QVERIFY(movie.backdropPath().contains(QLatin1String(".jpg")));
0087     QVERIFY(movie.posterPath().contains(QLatin1String(".jpg")));
0088     // new: the overview is actually available here
0089     QCOMPARE(movie.overview(), QStringLiteral("Ariane and Hugo decide to exchange the lives they lead to escape from their routine, which after ten years of marriage, gives them the feeling of being hamsters in a wheel. She suddenly finds herself at the head of a construction equipment rental company and he tries to take the role of a house calling jewelry salesman...But is life really better when you live it on the other side of the bed?"));
0090 
0091     const QString backdrop = movie.backdropUrl(QLatin1String("w92")).toString();
0092     QVERIFY2(backdrop.startsWith(QLatin1String("http://image.tmdb.org/t/p/w92/")), qPrintable(backdrop));
0093     const QString poster = movie.posterUrl(QLatin1String("w92")).toString();
0094     QVERIFY2(poster.startsWith(QLatin1String("http://image.tmdb.org/t/p/w92/")), qPrintable(poster));
0095 }
0096 
0097 void SearchTest::testTvSearch()
0098 {
0099     const QString name = QString::fromLatin1("Breaking Bad");
0100     TvSearchJob *job = m_api.searchTvShow(name);
0101     QSignalSpy spy(job, &TvSearchJob::result);
0102     QVERIFY(spy.wait());
0103     QVERIFY2(!job->hasError(), qPrintable(job->errorMessage()));
0104     TvShowDbList tvshows = job->searchResult();
0105     QCOMPARE(tvshows.count(), 1);
0106     TvShowDb tvshow = tvshows.first();
0107     QCOMPARE(tvshow.id(), 1396);
0108     QCOMPARE(tvshow.firstAiredDate(), QDate(2008, 1, 20));
0109     QCOMPARE(tvshow.name(), name);
0110     QCOMPARE(tvshow.originalName(), name);
0111     QVERIFY(tvshow.backdropPath().contains(QLatin1String(".jpg")));
0112     QVERIFY(tvshow.posterPath().contains(QLatin1String(".jpg")));
0113 
0114     const QString backdrop = tvshow.backdropUrl(QLatin1String("w92")).toString();
0115     QVERIFY2(backdrop.startsWith(QLatin1String("http://image.tmdb.org/t/p/w92/")), qPrintable(backdrop));
0116     const QString poster = tvshow.posterUrl(QLatin1String("w92")).toString();
0117     QVERIFY2(poster.startsWith(QLatin1String("http://image.tmdb.org/t/p/w92/")), qPrintable(poster));
0118 }
0119 
0120 void SearchTest::testMovieInfo()
0121 {
0122     MovieInfoJob *job = m_api.getMovieInfo(15709);
0123     QSignalSpy spy(job, &MovieInfoJob::result);
0124     QVERIFY(spy.wait());
0125     QVERIFY2(!job->hasError(), qPrintable(job->errorMessage()));
0126 
0127     MovieDb movie = job->searchResult();
0128     QCOMPARE(movie.overview(), QStringLiteral("Ariane and Hugo decide to exchange the lives they lead to escape from their routine, which after ten years of marriage, gives them the feeling of being hamsters in a wheel. She suddenly finds herself at the head of a construction equipment rental company and he tries to take the role of a house calling jewelry salesman...But is life really better when you live it on the other side of the bed?"));
0129     QCOMPARE(movie.productionCompanyNames(), QStringList() << "PROCIREP" << "Fidélité Films" << "OCS" << "Wild Bunch" << "Mars Distribution" << "TF1 Films Production");
0130     QCOMPARE(movie.budget(), 0); // not filled in
0131     QCOMPARE(movie.revenue(), 0); // not filled in
0132     QCOMPARE(movie.runtime(), 93);
0133 }
0134 
0135 void SearchTest::testMovieInfoFightClub()
0136 {
0137     MovieInfoJob *job = m_api.getMovieInfo(550);
0138     QSignalSpy spy(job, &MovieInfoJob::result);
0139     QVERIFY(spy.wait());
0140     QVERIFY2(!job->hasError(), qPrintable(job->errorMessage()));
0141 
0142     MovieDb movie = job->searchResult();
0143     QVERIFY(movie.productionCompanyNames().contains("20th Century Fox"));
0144     QCOMPARE(movie.budget(), 63000000);
0145     QCOMPARE(movie.revenue(), 100853753);
0146     QCOMPARE(movie.runtime(), 139);
0147 }
0148 
0149 void SearchTest::testTvShowInfo()
0150 {
0151     TvShowInfoJob *job = m_api.getTvShowInfo(1396);
0152     QSignalSpy spy(job, &TvShowInfoJob::result);
0153     QVERIFY(spy.wait());
0154     QVERIFY2(!job->hasError(), qPrintable(job->errorMessage()));
0155 
0156     TvShowDb tvshow = job->searchResult();
0157     QCOMPARE(tvshow.overview(), QStringLiteral("When Walter White, a New Mexico chemistry teacher, is diagnosed with Stage III cancer and given a prognosis of only two years left to live. He becomes filled with a sense of fearlessness and an unrelenting desire to secure his family's financial future at any cost as he enters the dangerous world of drugs and crime."));
0158 
0159     TvSeasonDbList seasons = tvshow.seasons();
0160     QCOMPARE(seasons.size(), 6);
0161 
0162     QCOMPARE(seasons[0].seasonNumber(), 0);
0163     QCOMPARE(seasons[1].seasonNumber(), 1);
0164     QCOMPARE(seasons[2].seasonNumber(), 2);
0165     QCOMPARE(seasons[3].seasonNumber(), 3);
0166     QCOMPARE(seasons[4].seasonNumber(), 4);
0167     QCOMPARE(seasons[5].seasonNumber(), 5);
0168 
0169     QCOMPARE(seasons[0].airDate(), QDate(2009, 2, 16));
0170     QCOMPARE(seasons[1].airDate(), QDate(2008, 1, 20));
0171     QCOMPARE(seasons[2].airDate(), QDate(2009, 3, 8));
0172     QCOMPARE(seasons[3].airDate(), QDate(2010, 3, 21));
0173     QCOMPARE(seasons[4].airDate(), QDate(2011, 7, 17));
0174     QCOMPARE(seasons[5].airDate(), QDate(2012, 7, 15));
0175 
0176     QVERIFY(!seasons[0].posterPath().isEmpty());
0177     QVERIFY(!seasons[1].posterPath().isEmpty());
0178     QVERIFY(!seasons[2].posterPath().isEmpty());
0179     QVERIFY(!seasons[3].posterPath().isEmpty());
0180     QVERIFY(!seasons[4].posterPath().isEmpty());
0181     QVERIFY(!seasons[5].posterPath().isEmpty());
0182 }
0183 
0184 void SearchTest::testTvSeasonInfo()
0185 {
0186     TvSeasonInfoJob *job = m_api.getTvSeasonInfo(1396, 1);
0187     QSignalSpy spy(job, &TvSeasonInfoJob::result);
0188     QVERIFY(spy.wait());
0189     QVERIFY2(!job->hasError(), qPrintable(job->errorMessage()));
0190 
0191     TvSeasonDb season = job->searchResult();
0192     QCOMPARE(season.id(), 3572);
0193     QCOMPARE(season.overview(), QStringLiteral("High school chemistry teacher Walter White's life is suddenly transformed by a dire medical diagnosis. Street-savvy former student Jesse Pinkman \"teaches\" Walter a new trade."));
0194     QCOMPARE(season.name(), QStringLiteral("Season 1"));
0195 
0196     TvEpisodeDbList episodes = season.episodes();
0197     QCOMPARE(episodes.size(), 7);
0198 
0199     QCOMPARE(episodes[0].airDate(), QDate(2008, 1, 20));
0200     QCOMPARE(episodes[0].episodeNumber(), 1);
0201     QCOMPARE(episodes[0].name(), QStringLiteral("Pilot"));
0202     QCOMPARE(episodes[0].overview(), QStringLiteral("When an unassuming high school chemistry teacher discovers he has a rare form of lung cancer, he decides to team up with a former student and create a top of the line crystal meth in a used RV, to provide for his family once he is gone."));
0203     QVERIFY(!episodes[0].stillPath().isEmpty());
0204 }
0205 
0206 void SearchTest::testCredits()
0207 {
0208     CreditsJob *job = m_api.getCredits(15709);
0209     QSignalSpy spy(job, &CreditsJob::result);
0210     QVERIFY(spy.wait());
0211     QVERIFY2(!job->hasError(), qPrintable(job->errorMessage()));
0212     const PersonList cast = job->cast();
0213     QVERIFY(cast.count() >= 30);
0214     const Person firstPerson = cast.at(0);
0215     QCOMPARE(firstPerson.name(), QStringLiteral("Dany Boon"));
0216     QCOMPARE(firstPerson.character(), QStringLiteral("Hugo Marciac"));
0217     QVERIFY2(firstPerson.profilePath().contains(QLatin1String(".jpg")), qPrintable(firstPerson.profilePath()));
0218     // TODO profileUrl
0219 
0220     const PersonList crew = job->crew();
0221     QCOMPARE(crew.count(), 11);
0222     const Person firstCrew = crew.at(0);
0223     QCOMPARE(firstCrew.name(), QStringLiteral("Olivier Delbosc"));
0224     QCOMPARE(firstCrew.department(), QStringLiteral("Production"));
0225     QCOMPARE(firstCrew.job(), QStringLiteral("Producer"));
0226 }
0227 
0228 void SearchTest::testEpisodeCredits()
0229 {
0230     CreditsJob *job = m_api.getEpisodeCredits(1396, 1, 2);
0231     QSignalSpy spy(job, &CreditsJob::result);
0232     QVERIFY(spy.wait());
0233     QVERIFY2(!job->hasError(), qPrintable(job->errorMessage()));
0234     const PersonList cast = job->cast();
0235     QCOMPARE(cast.count(), 6);
0236     const PersonList crew = job->crew();
0237     QCOMPARE(crew.count(), 4);
0238 }
0239 
0240 QTEST_MAIN(SearchTest)
0241 
0242 #include "searchtest.moc"