From 928aad0cd3b234f264d588460365182d373b91c5 Mon Sep 17 00:00:00 2001 From: Iori Mizutani Date: Wed, 28 Jun 2023 09:07:37 +0200 Subject: [PATCH] test: add match test --- .github/workflows/build.yml | 3 - .github/workflows/codecov.yml | 3 - .github/workflows/golanci-lint.yml | 3 - rule.go | 39 ++++--- rule_test.go | 172 +++++++++++++++++++++++------ 5 files changed, 164 insertions(+), 56 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 47b7a7a..d9c7ee4 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -3,9 +3,6 @@ on: pull_request: branches: - main - push: - branches: - - main jobs: build: strategy: diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml index 7d231ab..f05f1b3 100644 --- a/.github/workflows/codecov.yml +++ b/.github/workflows/codecov.yml @@ -3,9 +3,6 @@ on: pull_request: branches: - main - push: - branches: - - main jobs: codecov: runs-on: ubuntu-latest diff --git a/.github/workflows/golanci-lint.yml b/.github/workflows/golanci-lint.yml index 7b99c47..1133eaa 100644 --- a/.github/workflows/golanci-lint.yml +++ b/.github/workflows/golanci-lint.yml @@ -1,8 +1,5 @@ name: golangci-lint on: - push: - branches: - - main pull_request: branches: - main diff --git a/rule.go b/rule.go index 3f4e81f..784b4db 100644 --- a/rule.go +++ b/rule.go @@ -52,20 +52,8 @@ type Rule struct { // 4. match the criteria func (r *Rule) Match(stationID string, p *Prog) bool { // 1. check Window - if r.HasWindow() { - startTime, err := time.ParseInLocation(DatetimeLayout, p.Ft, Location) - if err != nil { - log.Printf("invalid start time format '%s': %s", p.Ft, err) - return false - } - fetchWindow, err := time.ParseDuration(r.Window) - if err != nil { - log.Printf("parsing [%s].past failed: %v (using 24h)", r.Name, err) - fetchWindow = time.Hour * 24 - } - if startTime.Add(fetchWindow).Before(CurrentTime) { - return false // skip before the fetch window - } + if !r.MatchWindow(p.Ft) { + return false } // 2. check dow if !r.MatchDoW(p.Ft) { @@ -145,7 +133,7 @@ func (r *Rule) MatchKeyword(p *Prog) bool { log.Printf("rule[%s] matched with pfm: '%s'", r.Name, p.Pfm) return true } else if strings.Contains(p.Info, r.Keyword) { - log.Printf("rule[%s] matched with info: \n%s", r.Name, strings.ReplaceAll(p.Info, "\n", "")) + log.Printf("rule[%s] matched with info: %s", r.Name, strings.ReplaceAll(p.Info, "\n", "")) return true } else if strings.Contains(p.Desc, r.Keyword) { log.Printf("rule[%s] matched with desc: '%s'", r.Name, strings.ReplaceAll(p.Desc, "\n", "")) @@ -192,6 +180,27 @@ func (r *Rule) MatchTitle(title string) bool { return false } +func (r *Rule) MatchWindow(ft string) bool { + if !r.HasWindow() { + return true + } + startTime, err := time.ParseInLocation(DatetimeLayout, ft, Location) + if err != nil { + log.Printf("invalid start time format '%s': %s", ft, err) + return false + } + fetchWindow, err := time.ParseDuration(r.Window) + if err != nil { + log.Printf("parsing [%s].window failed: %v (using 24h)", r.Name, err) + fetchWindow = time.Hour * 24 + } + if startTime.Add(fetchWindow).Before(CurrentTime) { + return false // skip the program outside the fetch window + } + + return true +} + func (r *Rule) SetName(name string) { r.Name = name } diff --git a/rule_test.go b/rule_test.go index 81d2686..4dbc6d9 100644 --- a/rule_test.go +++ b/rule_test.go @@ -1,6 +1,80 @@ package radicron -import "testing" +import ( + "testing" + "time" +) + +var matchtests = []struct { + in *Rule + stationID string + p *Prog + out bool +}{ + { + &Rule{"matchtests", "Title", []string{}, "Keyword", "Pfm", "FMT", ""}, + "FMT", + &Prog{ + "ID", + "FMT", + "20230625050000", + "20230625060000", + "Title", + "Keyword", + "", + "Pfm", + []string{}, + ProgGenre{}, + "", + }, + true, + }, + { + &Rule{"matchtests", "RadioProgram", []string{}, "Keyword", "Pfm", "FMT", ""}, + "FMT", + &Prog{ + "ID", + "FMT", + "20230625050000", + "20230625060000", + "Title", // title doesn't match + "Keyword", + "", + "Pfm", + []string{}, + ProgGenre{}, + "", + }, + false, + }, + { + &Rule{"matchtests", "RadioProgram", []string{}, "", "Someone", "FMT", ""}, + "FMT", + &Prog{ + "ID", + "FMT", + "20230625050000", + "20230625060000", + "RadioProgram", + "", + "", + "Pfm", // Pfm doesn't match + []string{}, + ProgGenre{}, + "", + }, + false, + }, +} + +func TestMatch(t *testing.T) { + for _, tt := range matchtests { + got := tt.in.Match(tt.stationID, tt.p) + if got != tt.out { + t.Errorf("(%v).Match => %v, want %v", tt.in, got, tt.out) + } + } +} var dowtests = []struct { in *Rule @@ -8,17 +82,17 @@ var dowtests = []struct { out bool }{ { - &Rule{"Name", "Title", []string{}, "Keyword", "Pfm", "StationID", "Window"}, + &Rule{"dowtests", "Title", []string{}, "Keyword", "Pfm", "StationID", "Window"}, "20230625050000", // sun true, }, { - &Rule{"Name", "Title", []string{"sun"}, "Keyword", "Pfm", "StationID", "Window"}, + &Rule{"dowtests", "Title", []string{"sun"}, "Keyword", "Pfm", "StationID", "Window"}, "20230625050000", // sun true, }, { - &Rule{"Name", "Title", []string{"mon", "tue"}, "Keyword", "Pfm", "StationID", "Window"}, + &Rule{"dowtests", "Title", []string{"mon", "tue"}, "Keyword", "Pfm", "StationID", "Window"}, "20230625050000", // sun false, }, @@ -39,7 +113,7 @@ var keywordtests = []struct { out bool }{ { - &Rule{"Name", "Title", []string{}, "", "Pfm", "StationID", "Window"}, + &Rule{"keywordtests", "Title", []string{}, "", "Pfm", "StationID", "Window"}, &Prog{ "ID", "StationID", @@ -56,7 +130,7 @@ var keywordtests = []struct { true, }, { - &Rule{"Name", "Title", []string{}, "Keyword", "Pfm", "StationID", "Window"}, + &Rule{"keywordtests", "Title", []string{}, "Keyword", "Pfm", "StationID", "Window"}, &Prog{ "ID", "StationID", @@ -73,7 +147,7 @@ var keywordtests = []struct { true, }, { - &Rule{"Name", "Title", []string{}, "Keyword", "Pfm", "StationID", "Window"}, + &Rule{"keywordtests", "Title", []string{}, "Keyword", "Pfm", "StationID", "Window"}, &Prog{ "ID", "StationID", @@ -90,7 +164,7 @@ var keywordtests = []struct { true, }, { - &Rule{"Name", "Title", []string{}, "Keyword", "Pfm", "StationID", "Window"}, + &Rule{"keywordtests", "Title", []string{}, "Keyword", "Pfm", "StationID", "Window"}, &Prog{ "ID", "StationID", @@ -107,7 +181,7 @@ var keywordtests = []struct { true, }, { - &Rule{"Name", "Title", []string{}, "Keyword", "Pfm", "StationID", "Window"}, + &Rule{"keywordtests", "Title", []string{}, "Keyword", "Pfm", "StationID", "Window"}, &Prog{ "test", "test", @@ -124,7 +198,7 @@ var keywordtests = []struct { true, }, { - &Rule{"Name", "Title", []string{}, "Keyword", "Pfm", "StationID", "Window"}, + &Rule{"keywordtests", "Title", []string{}, "Keyword", "Pfm", "StationID", "Window"}, &Prog{ "test", "test", @@ -141,7 +215,7 @@ var keywordtests = []struct { true, }, { - &Rule{"Name", "Title", []string{}, "Keyword", "Pfm", "StationID", "Window"}, + &Rule{"keywordtests", "Title", []string{}, "Keyword", "Pfm", "StationID", "Window"}, &Prog{ "ID", "StationID", @@ -174,17 +248,17 @@ var pfmtests = []struct { out bool }{ { - &Rule{"Name", "Title", []string{"sun"}, "Keyword", "", "StationID", "Window"}, + &Rule{"pfmtests", "Title", []string{"sun"}, "Keyword", "", "StationID", "Window"}, "Pfm", true, }, { - &Rule{"", "", []string{}, "", "Pfm", "", ""}, + &Rule{"pfmtests", "", []string{}, "", "Pfm", "", ""}, "Pfm", true, }, { - &Rule{"", "", []string{}, "", "Pfm", "", ""}, + &Rule{"pfmtests", "", []string{}, "", "Pfm", "", ""}, "Someone", false, }, @@ -205,17 +279,17 @@ var stationtests = []struct { out bool }{ { - &Rule{"Name", "Title", []string{"sun"}, "Keyword", "Pfm", "FMT", "Window"}, + &Rule{"stationtests", "Title", []string{"sun"}, "Keyword", "Pfm", "FMT", "Window"}, "FMT", true, }, { - &Rule{"", "", []string{}, "", "", "", ""}, + &Rule{"stationtests", "", []string{}, "", "", "", ""}, "FMT", true, }, { - &Rule{"", "", []string{}, "", "", "FMT", ""}, + &Rule{"stationtests", "", []string{}, "", "", "FMT", ""}, "TBS", false, }, @@ -236,17 +310,17 @@ var titletests = []struct { out bool }{ { - &Rule{"Name", "Title", []string{"sun"}, "Keyword", "Pfm", "FMT", "Window"}, + &Rule{"titletests", "Title", []string{"sun"}, "Keyword", "Pfm", "FMT", "Window"}, "Title", true, }, { - &Rule{"", "", []string{}, "", "", "", ""}, + &Rule{"titletests", "", []string{}, "", "", "", ""}, "Title", true, }, { - &Rule{"", "Title", []string{}, "", "", "FMT", ""}, + &Rule{"titletests", "Title", []string{}, "", "", "FMT", ""}, "Radio", false, }, @@ -261,16 +335,50 @@ func TestMatchTitle(t *testing.T) { } } +var windowtests = []struct { + in *Rule + ft string + out bool +}{ + { + &Rule{"windowtests", "Title", []string{"sun"}, "Keyword", "Pfm", "FMT", ""}, + "20230625050000", + true, + }, + { + &Rule{"windowtests", "", []string{}, "", "", "", "24h"}, + time.Now().Add(-1 * time.Hour).Format("20060102150405"), + true, + }, + { + &Rule{"windowtests", "", []string{}, "", "", "", "24h"}, + time.Now().Add(time.Duration(-48) * time.Hour).Format("20060102150405"), + false, + }, +} + +func TestMatchWindow(t *testing.T) { + Location, _ = time.LoadLocation(TZTokyo) + CurrentTime = time.Now().In(Location) + + for _, tt := range windowtests { + got := tt.in.MatchWindow(tt.ft) + if got != tt.out { + t.Errorf("(%v).MatchWindow => %v, want %v", tt.in, got, tt.out) + } + } +} + var ruletests = []struct { in *Rule out bool }{ { - &Rule{"Name", "Title", []string{"sun"}, "Keyword", "Pfm", "StationID", "Window"}, + &Rule{"ruletests", "Title", []string{"sun"}, "Keyword", "Pfm", "StationID", "Window"}, true, }, { - &Rule{"", "", []string{}, "", "", "", ""}, + &Rule{"ruletests", "", []string{}, "", "", "", ""}, false, }, } @@ -339,16 +447,16 @@ func TestHasRuleFor(t *testing.T) { }{ { Rules{ - &Rule{"Name", "Title", []string{}, "Keyword", "Pfm", "FMT", "Window"}, - &Rule{"Name", "Title", []string{}, "Keyword", "Pfm", "TBS", "Window"}, + &Rule{"rulestests", "Title", []string{}, "Keyword", "Pfm", "FMT", "Window"}, + &Rule{"rulestests", "Title", []string{}, "Keyword", "Pfm", "TBS", "Window"}, }, "FMT", true, }, { Rules{ - &Rule{"Name", "Title", []string{}, "Keyword", "Pfm", "FMT", "Window"}, - &Rule{"Name", "Title", []string{}, "Keyword", "Pfm", "TBS", "Window"}, + &Rule{"rulestests", "Title", []string{}, "Keyword", "Pfm", "FMT", "Window"}, + &Rule{"rulestests", "Title", []string{}, "Keyword", "Pfm", "TBS", "Window"}, }, "MBS", false, @@ -363,26 +471,26 @@ func TestHasRuleFor(t *testing.T) { } func TestHasRuleWithoutStationID(t *testing.T) { - var rulestests = []struct { + var hrwsitests = []struct { in Rules out bool }{ { Rules{ - &Rule{"Name", "Title", []string{}, "Keyword", "Pfm", "", "Window"}, - &Rule{"Name", "Title", []string{}, "Keyword", "Pfm", "TBS", "Window"}, + &Rule{"hrwsitests", "Title", []string{}, "Keyword", "Pfm", "", "Window"}, + &Rule{"hrwsitests", "Title", []string{}, "Keyword", "Pfm", "TBS", "Window"}, }, true, }, { Rules{ - &Rule{"Name", "Title", []string{}, "Keyword", "Pfm", "FMT", "Window"}, - &Rule{"Name", "Title", []string{}, "Keyword", "Pfm", "TBS", "Window"}, + &Rule{"hrwsitests", "Title", []string{}, "Keyword", "Pfm", "FMT", "Window"}, + &Rule{"hrwsitests", "Title", []string{}, "Keyword", "Pfm", "TBS", "Window"}, }, false, }, } - for _, tt := range rulestests { + for _, tt := range hrwsitests { res := tt.in.HasRuleWithoutStationID() if tt.out != res { t.Errorf("(%v).HasRuleWithoutStationID() => %v, want %v", tt.in, res, tt.out)