cli + tui to publish to leaflet (wip) & manage tasks, notes & watch/read lists 馃崈
charm leaflet readability golang
29
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 186 lines 4.1 kB view raw
1package services 2 3import ( 4 "errors" 5 "io" 6 "testing" 7) 8 9func TestHTTPFuncs(t *testing.T) { 10 origFetch := FetchHTML 11 origMovie := ExtractMovieMetadata 12 origTV := ExtractTVSeriesMetadata 13 origSeason := ExtractTVSeasonMetadata 14 origSearch := ParseSearch 15 16 defer func() { 17 FetchHTML = origFetch 18 ExtractMovieMetadata = origMovie 19 ExtractTVSeriesMetadata = origTV 20 ExtractTVSeasonMetadata = origSeason 21 ParseSearch = origSearch 22 }() 23 24 tc := []struct { 25 name string 26 setup func() 27 call func() error 28 expectErr bool 29 }{ 30 { 31 name: "FetchMovie success", 32 setup: func() { 33 FetchHTML = func(url string) (string, error) { 34 return "<html>movie</html>", nil 35 } 36 ExtractMovieMetadata = func(r io.Reader) (*Movie, error) { 37 return &Movie{Name: "Fake Movie"}, nil 38 } 39 }, 40 call: func() error { 41 m, err := FetchMovie("http://fake") 42 if err != nil { 43 return err 44 } 45 if m.Name != "Fake Movie" { 46 return errors.New("unexpected movie title") 47 } 48 return nil 49 }, 50 }, 51 { 52 name: "FetchMovie error from FetchHTML", 53 setup: func() { 54 FetchHTML = func(url string) (string, error) { 55 return "", errors.New("boom") 56 } 57 }, 58 call: func() error { 59 _, err := FetchMovie("http://fake") 60 return err 61 }, 62 expectErr: true, 63 }, 64 { 65 name: "FetchTVSeries success", 66 setup: func() { 67 FetchHTML = func(url string) (string, error) { 68 return "<html>tv</html>", nil 69 } 70 ExtractTVSeriesMetadata = func(r io.Reader) (*TVSeries, error) { 71 return &TVSeries{Name: "Fake Series"}, nil 72 } 73 }, 74 call: func() error { 75 tv, err := FetchTVSeries("http://fake") 76 if err != nil { 77 return err 78 } 79 if tv.Name != "Fake Series" { 80 return errors.New("unexpected series name") 81 } 82 return nil 83 }, 84 }, 85 { 86 name: "FetchTVSeries error from FetchHTML", 87 setup: func() { 88 FetchHTML = func(url string) (string, error) { 89 return "", errors.New("boom") 90 } 91 }, 92 call: func() error { 93 _, err := FetchTVSeries("http://fake") 94 return err 95 }, 96 expectErr: true, 97 }, 98 { 99 name: "FetchTVSeason success", 100 setup: func() { 101 FetchHTML = func(url string) (string, error) { 102 return "<html>season</html>", nil 103 } 104 ExtractTVSeasonMetadata = func(r io.Reader) (*TVSeason, error) { 105 return &TVSeason{SeasonNumber: 1}, nil 106 } 107 }, 108 call: func() error { 109 season, err := FetchTVSeason("http://fake") 110 if err != nil { 111 return err 112 } 113 if season.SeasonNumber != 1 { 114 return errors.New("unexpected season number") 115 } 116 return nil 117 }, 118 }, 119 { 120 name: "FetchTVSeason error from FetchHTML", 121 setup: func() { 122 FetchHTML = func(url string) (string, error) { 123 return "", errors.New("boom") 124 } 125 }, 126 call: func() error { 127 _, err := FetchTVSeason("http://fake") 128 return err 129 }, 130 expectErr: true, 131 }, 132 { 133 name: "SearchRottenTomatoes success", 134 setup: func() { 135 FetchHTML = func(url string) (string, error) { 136 return "<html>search</html>", nil 137 } 138 ParseSearch = func(r io.Reader) ([]Media, error) { 139 return []Media{{Title: "Fake Result"}}, nil 140 } 141 }, 142 call: func() error { 143 results, err := SearchRottenTomatoes("query") 144 if err != nil { 145 return err 146 } 147 if len(results) != 1 || results[0].Title != "Fake Result" { 148 return errors.New("unexpected search results") 149 } 150 return nil 151 }, 152 }, 153 { 154 name: "SearchRottenTomatoes error from FetchHTML", 155 setup: func() { 156 FetchHTML = func(url string) (string, error) { 157 return "", errors.New("boom") 158 } 159 }, 160 call: func() error { 161 _, err := SearchRottenTomatoes("query") 162 return err 163 }, 164 expectErr: true, 165 }, 166 } 167 168 for _, tt := range tc { 169 t.Run(tt.name, func(t *testing.T) { 170 FetchHTML = origFetch 171 ExtractMovieMetadata = origMovie 172 ExtractTVSeriesMetadata = origTV 173 ExtractTVSeasonMetadata = origSeason 174 ParseSearch = origSearch 175 176 tt.setup() 177 err := tt.call() 178 if tt.expectErr && err == nil { 179 t.Fatalf("expected error, got nil") 180 } 181 if !tt.expectErr && err != nil { 182 t.Fatalf("unexpected error: %v", err) 183 } 184 }) 185 } 186}