package webserver import ( "fmt" "testing" "time" irc "git.martyn.berlin/martyn/twitchsingstools/internal/irc" ) func TestDateRegex(t *testing.T) { var sourceValue videoStruct sourceValue.Title = "Duet with FullOfEmily: Words Fail" sourceValue.CreatedAt = "2018-03-02T20:53:41Z" convert, err := twitchVidToSingsVid(sourceValue) if err != nil { t.Errorf("twitchVidToSingsVid threw error : %s\n", err.Error()) } fmt.Printf("Testing Video date '%s'\n", sourceValue.CreatedAt) if convert.Date.Format(time.RFC1123Z) != "Fri, 02 Mar 2018 20:53:41 +0000" { t.Errorf("%s should give Date of %s, gave '%s'\n", sourceValue.CreatedAt, "Fri, 02 Mar 2018 20:53:41 +0000", convert.Date.Format(time.RFC1123Z)) } else { fmt.Printf("Parsed date is %s\n", convert.Date.Format(time.RFC1123Z)) } } func TestDuetRegexes(t *testing.T) { var sourceValue videoStruct sourceValue.Title = "Duet with FullOfEmily: Words Fail" sourceValue.CreatedAt = "2018-03-02T20:53:41Z" convert, err := twitchVidToSingsVid(sourceValue) fmt.Printf("Testing Video title '%s'\n", sourceValue.Title) if err != nil { t.Errorf("twitchVidToSingsVid threw error : %s\n", err.Error()) } if convert.SongTitle != "Words Fail" { t.Errorf("%s should give Title %s, gave '%s'\n", sourceValue.Title, "Words Fail", convert.SongTitle) } else { fmt.Printf("Song Title is '%s'\n", convert.SongTitle) } if convert.OtherSinger != "FullOfEmily" { t.Errorf("%s should give Other Singer of %s, gave '%s'\n", sourceValue.Title, "FullOfEmily", convert.OtherSinger) } else { fmt.Printf("Other Singer is '%s'\n", convert.OtherSinger) } if !convert.Duet { t.Errorf("%s should be reported as Duet, returned false\n", sourceValue.Title) } else { fmt.Println("Correctly seen as Duet") } } func TestSoloRegexes(t *testing.T) { var sourceValue videoStruct sourceValue.Title = "Solo performance: Freedom! '90 x Cups" sourceValue.CreatedAt = "2018-03-02T20:53:41Z" convert, err := twitchVidToSingsVid(sourceValue) fmt.Printf("Testing Video title '%s'\n", sourceValue.Title) if err != nil { t.Errorf("twitchVidToSingsVid threw error : %s\n", err.Error()) } if convert.SongTitle != "Freedom! '90 x Cups" { t.Errorf("%s should give Title %s, gave '%s'\n", sourceValue.Title, "Freedom! '90 x Cups", convert.SongTitle) } else { fmt.Printf("Song Title is '%s'\n", convert.SongTitle) } if convert.Duet { t.Errorf("%s should be reported as Not a Duet, returned true!\n", sourceValue.Title) } else { fmt.Println("Correctly seen as Solo performance") } } func TestCalculatedDates(t *testing.T) { var record irc.SingsVideoStruct format := "2006-01-02 15:04:05 +0000 UTC" var mockCache []irc.SingsVideoStruct record.Date, _ = time.Parse(format, "2020-07-13 19:43:11 +0000 UTC") record.SongTitle = "Words Fail" record.OtherSinger = "FullOfEmily" record.Duet = true mockCache = append(mockCache, record) //2020-07-13 19:43:11 +0000 UTC Words Fail FullOfEmily record.Date, _ = time.Parse(format, "2020-07-13 19:20:27 +0000 UTC") record.SongTitle = "Only Us" record.OtherSinger = "PrincessPashley" record.Duet = true mockCache = append(mockCache, record) //2020-07-13 19:20:27 +0000 UTC Only Us PrincessPashley record.Date, _ = time.Parse(format, "2020-07-11 16:20:57 +0000 UTC") record.SongTitle = "Words Fail" record.OtherSinger = "springfanS" record.Duet = true mockCache = append(mockCache, record) //2020-07-11 16:20:57 +0000 UTC Words Fail springfanS record.Date, _ = time.Parse(format, "2020-06-06 17:00:00 +0000 UTC") record.SongTitle = "Don't Speak" record.OtherSinger = "PrincessPashley" record.Duet = true mockCache = append(mockCache, record) //2020-06-06 17:00:00 +0000 UTC Don't Speak PrincessPashley // First test, only sung once with this person testPerson := "FullOfEmily" testDate, _ := time.Parse(format, "2020-07-13 19:43:11 +0000 UTC") checker := calculateLastSungSingerDate(mockCache, testPerson) if checker.IsZero() { t.Errorf("[functional test] There is a record in the cache for %s, but we got zero from the check!\n", testPerson) } else { if checker.Equal(testDate) { fmt.Printf("[functional test] Correctly assertained last sing with %s was %v\n", testPerson, testDate) } else { t.Errorf("[functional test] Expected to have last sung with %s on %v, found %v", testPerson, testDate, checker) } } // Second test, sang more than once with this person testPerson = "PrincessPashley" testDate, _ = time.Parse(format, "2020-07-13 19:20:27 +0000 UTC") checker = calculateLastSungSingerDate(mockCache, testPerson) if checker.IsZero() { t.Errorf("[functional test] There is a record in the cache for %s, but we got zero from the check!\n", testPerson) } else { if checker.Equal(testDate) { fmt.Printf("[functional test] Correctly assertained last sing with %s was %v\n", testPerson, testDate) } else { t.Errorf("[functional test] Expected to have last sung with %s on %v, found %v", testPerson, testDate, checker) } } updateCalculatedFields(mockCache) // Test for full cache - same as first test testRecord := mockCache[0] testPerson = testRecord.OtherSinger testDate = mockCache[0].Date if testRecord.LastSungSinger.IsZero() { t.Errorf("[full cache test] There is a record in the cache for %s, but we got zero from the check!\n", testPerson) } else { if testRecord.LastSungSinger.Equal(testDate) { fmt.Printf("[full cache test] Correctly assertained last sing with %s was %v\n", testPerson, testDate) } else { t.Errorf("[full cache test] Expected to have last sung with %s on %v, found %v", testPerson, testDate, testRecord.LastSungSinger) } } // Test for full cache - same as second test testRecord = mockCache[1] testPerson = testRecord.OtherSinger testDate = mockCache[1].Date if testRecord.LastSungSinger.IsZero() { t.Errorf("[full cache test - 2nd hit] There is a record in the cache for %s, but we got zero from the check!\n", testPerson) } else { if testRecord.LastSungSinger.Equal(testDate) { fmt.Printf("[full cache test - 2nd hit] Correctly assertained last sing with %s was %v\n", testPerson, testDate) } else { t.Errorf("[full cache test - 2nd hit] Expected to have last sung with %s on %v, found %v", testPerson, testDate, testRecord.LastSungSinger) } } // Test for full cache - should give same date as 2nd test, but with record of earlier sing. testRecord = mockCache[3] testPerson = testRecord.OtherSinger testDate = mockCache[1].Date if testRecord.LastSungSinger.IsZero() { t.Errorf("[full cache test - 3rd hit] There is a record in the cache for %s, but we got zero from the check!\n", testPerson) } else { if testRecord.LastSungSinger.Equal(testDate) { fmt.Printf("[full cache test - 3rd hit] Correctly assertained last sing with %s was %v\n", testPerson, testDate) } else { t.Errorf("[full cache test - 3rd hit] Expected to have last sung with %s on %v, found %v", testPerson, testDate, testRecord.LastSungSinger) } } // First song test, only sung this song once testSong := mockCache[1].SongTitle testDate = mockCache[1].Date checker = calculateLastSungSongDate(mockCache, testSong) if checker.IsZero() { t.Errorf("[functional test] There is a record in the cache for %s, but we got zero from the check!\n", testSong) } else { if checker.Equal(testDate) { fmt.Printf("[functional test] Correctly assertained last sing of %s was %v\n", testPerson, testSong) } else { t.Errorf("[functional test] Expected to have last sing of %s on %v, found %v", testPerson, testSong, checker) } } // Same check, but against the cache testSong = mockCache[1].SongTitle testDate = mockCache[1].Date checker = mockCache[1].LastSungSong if checker.IsZero() { t.Errorf("[functional test] There is a record in the cache for %s, but we got zero from the check!\n", testSong) } else { if checker.Equal(testDate) { fmt.Printf("[functional test] Correctly assertained last sing of %s was %v\n", testSong, testDate) } else { t.Errorf("[functional test] Expected to have last sing of %s on %v, found %v", testSong, testDate, checker) } } // Second song test, this time we expect the latest sing to match testSong = mockCache[2].SongTitle testDate = mockCache[0].Date checker = calculateLastSungSongDate(mockCache, testSong) if checker.IsZero() { t.Errorf("[functional test] There is a record in the cache for %s, but we got zero from the check!\n", testSong) } else { if checker.Equal(testDate) { fmt.Printf("[functional test] Correctly assertained last sing of %s was %v\n", testSong, testDate) } else { t.Errorf("[functional test] Expected to have last sing of %s on %v, found %v", testSong, testDate, checker) } } // Now check cache for same as above testSong = mockCache[2].SongTitle testDate = mockCache[0].Date checker = mockCache[2].LastSungSong if checker.IsZero() { t.Errorf("[functional test] There is a record in the cache for %s, but we got zero from the check!\n", testSong) } else { if checker.Equal(testDate) { fmt.Printf("[functional test] Correctly assertained last sing of %s was %v\n", testSong, testDate) } else { t.Errorf("[functional test] Expected to have last sing of %s on %v, found %v", testSong, testDate, checker) } } } // func TestHumanDate(t *testing.T) { // format := "2006-01-02 15:04:05 +0000 UTC" // d, _ := time.Parse(format, "2020-06-06 17:00:00 +0000 UTC") // t.Errorf("%v\n", humanize.Time(d)) // t.Errorf("%v\n", humanTimeFromTimeString("2020-07-11 16:20:57 +0000 UTC")) // }