imdbs/imdb-scraper_test.go

220 lines
5.8 KiB
Go
Raw Normal View History

2023-10-01 09:00:33 +02:00
package imdbs
import "testing"
func TestBuildScrapingURL(t *testing.T) {
data := map[string]string{
"tt2911666": "https://www.imdb.com/title/tt2911666/",
"tt10366206": "https://www.imdb.com/title/tt10366206/",
}
for d, expectedResult := range data {
result := buildScrapingURL(d)
if result != expectedResult {
t.Errorf("\ngot: %q\nwanted: %q\nfor: %q", result, expectedResult, d)
}
}
}
func TestConvertIMDbRuntimeIntoMinutes(t *testing.T) {
data := map[string]int64{
"PT41M": 41,
"PT2H": 120,
"PT2H1M": 121,
}
for d, expectedResult := range data {
result := convertIMDbRuntimeIntoMinutes(d)
if result != expectedResult {
t.Errorf("\ngot: %d\nwanted: %d\nfor: %q", result, expectedResult, d)
}
}
}
func TestGetIMDbIDFromQuery(t *testing.T) {
data := map[string]string{
"tt2861424": "tt2861424",
"https://www.imdb.com/title/tt2861424": "tt2861424",
"https://www.imdb.com/title/tt2861424/": "tt2861424",
"https://www.imdb.com/title/tt2861424/?ref_=vp_vi_tt": "tt2861424",
"https://m.imdb.com/title/tt2861424": "tt2861424",
"https://m.imdb.com/title/tt2861424/": "tt2861424",
"https://m.imdb.com/title/tt2861424/?ref_=vp_vi_tt": "tt2861424",
"https://www.imdb.com/": "",
"https://www.google.com/title/tt2861424/": "",
"tt2861424tt2861424tt2861424": "",
}
for d, expectedResult := range data {
result, _ := GetIMDbIDFromQuery(d)
if result != expectedResult {
t.Errorf("\ngot: %q\nwanted: %q\nfor: %q", result, expectedResult, d)
}
}
}
func TestExtractYearFromIMDbTitle(t *testing.T) {
type tResult struct {
Data string
Title string
ExpectedResult int64
}
data := []tResult{
{
Data: "John Wick: Kapitel 4 (2023) - IMDb",
Title: "John Wick: Kapitel 4",
ExpectedResult: 2023,
},
{
Data: "Matrix (1999) - IMDb",
Title: "Matrix",
ExpectedResult: 1999,
},
{
Data: "Thurgood (Fernsehfilm 2011) - IMDb",
Title: "Thurgood",
ExpectedResult: 2011,
},
{
Data: "Pretty/Handsome (Fernsehfilm 2008) - IMDb",
Title: "Pretty/Handsome",
ExpectedResult: 2008,
},
{
Data: "Red Planet: Deleted Scenes (Video 2000) - IMDb",
Title: "Red Planet: Deleted Scenes",
ExpectedResult: 2000,
},
{
Data: "Last Night in Soho: Deleted Scenes (Video 2022) - IMDb",
Title: "Last Night in Soho: Deleted Scenes",
ExpectedResult: 2022,
},
{
Data: "Eine schrecklich nette Familie (Fernsehserie 19871997) - IMDb",
Title: "Eine schrecklich nette Familie",
ExpectedResult: 1987,
},
{
Data: "Rick and Morty (Fernsehserie 2013 ) - IMDb",
Title: "Rick and Morty",
ExpectedResult: 2013,
},
2023-10-23 23:59:20 +02:00
{
Data: "Unser Kosmos: Die Reise geht weiter (Miniserie 2014) - IMDb",
Title: "Unser Kosmos: Die Reise geht weiter",
ExpectedResult: 2014,
},
2023-10-01 09:00:33 +02:00
}
for _, r := range data {
result := extractYearFromIMDbTitle(r.Data, r.Title)
if result != r.ExpectedResult {
t.Errorf("\ngot: %d\nwanted: %d\nfor: %q", result, r.ExpectedResult, r.Data)
}
}
}
func TestIsValidIMDbID(t *testing.T) {
data := map[string]bool{
"tt0000000": true,
"tt2911666": true,
"tt10366206": true,
"tt0944947": true,
"tt11737520": true,
"tt291166": false,
"tt103662060": false,
"ttt1036620": false,
"https://www.imdb.com/": false,
}
for d, expectedResult := range data {
result := IsValidIMDbID(d)
if result != expectedResult {
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q", result, expectedResult, d)
}
}
}
func TestPrepareChunckOfIMDbTitleOfSeriesForYearExtraction(t *testing.T) {
data := map[string]string{
"(Fernsehserie 19871997)": "(1987)",
"(Fernsehserie 2013 )": "(2013)",
}
for d, expectedResult := range data {
result := prepareChunckOfIMDbTitleOfSeriesForYearExtraction(d)
if result != expectedResult {
t.Errorf("\ngot: %+v\nwanted: %+v\nfor: %q", result, expectedResult, d)
}
}
}
func TestScrap(t *testing.T) {
data := map[string]IMDbEntry{
"tt0000000": {},
"tt2911666": {
AlternateName: "",
IMDbID: "tt2911666",
Rating: 7.4,
RuntimeInMins: 101,
Title: "John Wick",
Type: "Movie",
Year: 2014,
},
"tt7798634": {
AlternateName: "Ready or Not - Auf die Plätze, fertig, tot",
IMDbID: "tt7798634",
Rating: 6.9,
RuntimeInMins: 95,
Title: "Ready or Not",
Type: "Movie",
Year: 2019,
},
"tt0087803": {
AlternateName: "1984",
IMDbID: "tt0087803",
Rating: 7.1,
RuntimeInMins: 113,
Title: "Nineteen Eighty-Four",
Type: "Movie",
Year: 1984,
},
"tt8579674": {
AlternateName: "",
IMDbID: "tt8579674",
Rating: 8.2,
RuntimeInMins: 119,
Title: "1917",
Type: "Movie",
Year: 2019,
},
"tt2861424": {
AlternateName: "",
IMDbID: "tt2861424",
Rating: 9.1,
RuntimeInMins: 0,
Title: "Rick and Morty",
Type: "TVSeries",
Year: 2013,
},
"tt0092400": {
AlternateName: "Eine schrecklich nette Familie",
IMDbID: "tt0092400",
Rating: 8.1,
RuntimeInMins: 0,
Title: "Married with Children",
Type: "TVSeries",
Year: 1987,
},
}
for d, expectedResult := range data {
result, _ := Scrap(d)
if result != expectedResult {
t.Errorf("\ngot: %+v\nwanted: %+v\nfor: %q", result, expectedResult, d)
}
}
}