Philipp Schrader | cdb5cfc | 2022-02-20 14:57:07 -0800 | [diff] [blame] | 1 | package requests |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "io" |
| 6 | "net/http" |
Philipp Schrader | cbf5c6a | 2022-02-27 23:25:19 -0800 | [diff] [blame] | 7 | "reflect" |
Philipp Schrader | cdb5cfc | 2022-02-20 14:57:07 -0800 | [diff] [blame] | 8 | "testing" |
| 9 | |
Philipp Schrader | 8747f1b | 2022-02-23 23:56:22 -0800 | [diff] [blame] | 10 | "github.com/frc971/971-Robot-Code/scouting/db" |
Philipp Schrader | d3fac19 | 2022-03-02 20:35:46 -0800 | [diff] [blame] | 11 | "github.com/frc971/971-Robot-Code/scouting/scraping" |
Philipp Schrader | cbf5c6a | 2022-02-27 23:25:19 -0800 | [diff] [blame] | 12 | "github.com/frc971/971-Robot-Code/scouting/webserver/requests/debug" |
Philipp Schrader | cdb5cfc | 2022-02-20 14:57:07 -0800 | [diff] [blame] | 13 | "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/error_response" |
Philipp Schrader | d3fac19 | 2022-03-02 20:35:46 -0800 | [diff] [blame] | 14 | "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/refresh_match_list" |
| 15 | "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/refresh_match_list_response" |
Philipp Schrader | cbf5c6a | 2022-02-27 23:25:19 -0800 | [diff] [blame] | 16 | "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_all_matches" |
| 17 | "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_all_matches_response" |
Philipp Schrader | acf9623 | 2022-03-01 22:03:30 -0800 | [diff] [blame] | 18 | "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_data_scouting" |
| 19 | "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_data_scouting_response" |
Philipp Schrader | d1c4bef | 2022-02-28 22:51:30 -0800 | [diff] [blame] | 20 | "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_matches_for_team" |
| 21 | "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_matches_for_team_response" |
Alex Perry | 81f96ba | 2022-03-13 18:26:19 -0700 | [diff] [blame] | 22 | "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_notes_for_team" |
Philipp Schrader | cdb5cfc | 2022-02-20 14:57:07 -0800 | [diff] [blame] | 23 | "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/submit_data_scouting" |
Philipp Schrader | 30005e4 | 2022-03-06 13:53:58 -0800 | [diff] [blame] | 24 | "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/submit_data_scouting_response" |
Alex Perry | 81f96ba | 2022-03-13 18:26:19 -0700 | [diff] [blame] | 25 | "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/submit_notes" |
Philipp Schrader | cdb5cfc | 2022-02-20 14:57:07 -0800 | [diff] [blame] | 26 | "github.com/frc971/971-Robot-Code/scouting/webserver/server" |
| 27 | flatbuffers "github.com/google/flatbuffers/go" |
| 28 | ) |
| 29 | |
| 30 | // Validates that an unhandled address results in a 404. |
| 31 | func Test404(t *testing.T) { |
Philipp Schrader | 8747f1b | 2022-02-23 23:56:22 -0800 | [diff] [blame] | 32 | db := MockDatabase{} |
Philipp Schrader | cdb5cfc | 2022-02-20 14:57:07 -0800 | [diff] [blame] | 33 | scoutingServer := server.NewScoutingServer() |
Philipp Schrader | d3fac19 | 2022-03-02 20:35:46 -0800 | [diff] [blame] | 34 | HandleRequests(&db, scrapeEmtpyMatchList, scoutingServer) |
Philipp Schrader | cdb5cfc | 2022-02-20 14:57:07 -0800 | [diff] [blame] | 35 | scoutingServer.Start(8080) |
| 36 | defer scoutingServer.Stop() |
| 37 | |
| 38 | resp, err := http.Get("http://localhost:8080/requests/foo") |
| 39 | if err != nil { |
| 40 | t.Fatalf("Failed to get data: %v", err) |
| 41 | } |
| 42 | if resp.StatusCode != http.StatusNotFound { |
| 43 | t.Fatalf("Expected error code 404, but got %d instead", resp.Status) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // Validates that we can submit new data scouting data. |
| 48 | func TestSubmitDataScoutingError(t *testing.T) { |
Philipp Schrader | 8747f1b | 2022-02-23 23:56:22 -0800 | [diff] [blame] | 49 | db := MockDatabase{} |
Philipp Schrader | cdb5cfc | 2022-02-20 14:57:07 -0800 | [diff] [blame] | 50 | scoutingServer := server.NewScoutingServer() |
Philipp Schrader | d3fac19 | 2022-03-02 20:35:46 -0800 | [diff] [blame] | 51 | HandleRequests(&db, scrapeEmtpyMatchList, scoutingServer) |
Philipp Schrader | cdb5cfc | 2022-02-20 14:57:07 -0800 | [diff] [blame] | 52 | scoutingServer.Start(8080) |
| 53 | defer scoutingServer.Stop() |
| 54 | |
| 55 | resp, err := http.Post("http://localhost:8080/requests/submit/data_scouting", "application/octet-stream", bytes.NewReader([]byte(""))) |
| 56 | if err != nil { |
| 57 | t.Fatalf("Failed to send request: %v", err) |
| 58 | } |
| 59 | if resp.StatusCode != http.StatusBadRequest { |
| 60 | t.Fatal("Unexpected status code. Got", resp.Status) |
| 61 | } |
| 62 | |
| 63 | responseBytes, err := io.ReadAll(resp.Body) |
| 64 | if err != nil { |
| 65 | t.Fatal("Failed to read response bytes:", err) |
| 66 | } |
| 67 | errorResponse := error_response.GetRootAsErrorResponse(responseBytes, 0) |
| 68 | |
| 69 | errorMessage := string(errorResponse.ErrorMessage()) |
| 70 | if errorMessage != "Failed to parse SubmitDataScouting: runtime error: index out of range [3] with length 0" { |
| 71 | t.Fatal("Got mismatched error message:", errorMessage) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // Validates that we can submit new data scouting data. |
| 76 | func TestSubmitDataScouting(t *testing.T) { |
Philipp Schrader | 8747f1b | 2022-02-23 23:56:22 -0800 | [diff] [blame] | 77 | db := MockDatabase{} |
Philipp Schrader | cdb5cfc | 2022-02-20 14:57:07 -0800 | [diff] [blame] | 78 | scoutingServer := server.NewScoutingServer() |
Philipp Schrader | d3fac19 | 2022-03-02 20:35:46 -0800 | [diff] [blame] | 79 | HandleRequests(&db, scrapeEmtpyMatchList, scoutingServer) |
Philipp Schrader | cdb5cfc | 2022-02-20 14:57:07 -0800 | [diff] [blame] | 80 | scoutingServer.Start(8080) |
| 81 | defer scoutingServer.Stop() |
| 82 | |
| 83 | builder := flatbuffers.NewBuilder(1024) |
| 84 | builder.Finish((&submit_data_scouting.SubmitDataScoutingT{ |
Philipp Schrader | fa45d74 | 2022-03-18 19:29:05 -0700 | [diff] [blame] | 85 | Team: 971, |
| 86 | Match: 1, |
| 87 | StartingQuadrant: 2, |
| 88 | AutoBall1: true, |
| 89 | AutoBall2: false, |
| 90 | AutoBall3: false, |
| 91 | AutoBall4: false, |
| 92 | AutoBall5: false, |
| 93 | MissedShotsAuto: 9971, |
| 94 | UpperGoalAuto: 9971, |
| 95 | LowerGoalAuto: 9971, |
| 96 | MissedShotsTele: 9971, |
| 97 | UpperGoalTele: 9971, |
| 98 | LowerGoalTele: 9971, |
| 99 | DefenseRating: 9971, |
| 100 | DefenseReceivedRating: 4, |
| 101 | ClimbLevel: submit_data_scouting.ClimbLevelLow, |
| 102 | Comment: "this is a comment", |
Philipp Schrader | cdb5cfc | 2022-02-20 14:57:07 -0800 | [diff] [blame] | 103 | }).Pack(builder)) |
| 104 | |
Philipp Schrader | 30005e4 | 2022-03-06 13:53:58 -0800 | [diff] [blame] | 105 | response, err := debug.SubmitDataScouting("http://localhost:8080", builder.FinishedBytes()) |
Philipp Schrader | cdb5cfc | 2022-02-20 14:57:07 -0800 | [diff] [blame] | 106 | if err != nil { |
Philipp Schrader | 30005e4 | 2022-03-06 13:53:58 -0800 | [diff] [blame] | 107 | t.Fatal("Failed to submit data scouting: ", err) |
Philipp Schrader | cdb5cfc | 2022-02-20 14:57:07 -0800 | [diff] [blame] | 108 | } |
Philipp Schrader | 30005e4 | 2022-03-06 13:53:58 -0800 | [diff] [blame] | 109 | |
| 110 | // We get an empty response back. Validate that. |
| 111 | expected := submit_data_scouting_response.SubmitDataScoutingResponseT{} |
| 112 | if !reflect.DeepEqual(expected, *response) { |
| 113 | t.Fatal("Expected ", expected, ", but got:", *response) |
Philipp Schrader | cdb5cfc | 2022-02-20 14:57:07 -0800 | [diff] [blame] | 114 | } |
Philipp Schrader | cdb5cfc | 2022-02-20 14:57:07 -0800 | [diff] [blame] | 115 | } |
Philipp Schrader | 8747f1b | 2022-02-23 23:56:22 -0800 | [diff] [blame] | 116 | |
Philipp Schrader | cbf5c6a | 2022-02-27 23:25:19 -0800 | [diff] [blame] | 117 | // Validates that we can request the full match list. |
| 118 | func TestRequestAllMatches(t *testing.T) { |
| 119 | db := MockDatabase{ |
| 120 | matches: []db.Match{ |
| 121 | { |
| 122 | MatchNumber: 1, Round: 1, CompLevel: "qual", |
| 123 | R1: 5, R2: 42, R3: 600, B1: 971, B2: 400, B3: 200, |
| 124 | }, |
| 125 | { |
| 126 | MatchNumber: 2, Round: 1, CompLevel: "qual", |
| 127 | R1: 6, R2: 43, R3: 601, B1: 972, B2: 401, B3: 201, |
| 128 | }, |
| 129 | { |
| 130 | MatchNumber: 3, Round: 1, CompLevel: "qual", |
| 131 | R1: 7, R2: 44, R3: 602, B1: 973, B2: 402, B3: 202, |
| 132 | }, |
| 133 | }, |
| 134 | } |
| 135 | scoutingServer := server.NewScoutingServer() |
Philipp Schrader | d3fac19 | 2022-03-02 20:35:46 -0800 | [diff] [blame] | 136 | HandleRequests(&db, scrapeEmtpyMatchList, scoutingServer) |
Philipp Schrader | cbf5c6a | 2022-02-27 23:25:19 -0800 | [diff] [blame] | 137 | scoutingServer.Start(8080) |
| 138 | defer scoutingServer.Stop() |
| 139 | |
| 140 | builder := flatbuffers.NewBuilder(1024) |
| 141 | builder.Finish((&request_all_matches.RequestAllMatchesT{}).Pack(builder)) |
| 142 | |
| 143 | response, err := debug.RequestAllMatches("http://localhost:8080", builder.FinishedBytes()) |
| 144 | if err != nil { |
| 145 | t.Fatal("Failed to request all matches: ", err) |
| 146 | } |
| 147 | |
| 148 | expected := request_all_matches_response.RequestAllMatchesResponseT{ |
| 149 | MatchList: []*request_all_matches_response.MatchT{ |
| 150 | // MatchNumber, Round, CompLevel |
| 151 | // R1, R2, R3, B1, B2, B3 |
| 152 | { |
| 153 | 1, 1, "qual", |
| 154 | 5, 42, 600, 971, 400, 200, |
| 155 | }, |
| 156 | { |
| 157 | 2, 1, "qual", |
| 158 | 6, 43, 601, 972, 401, 201, |
| 159 | }, |
| 160 | { |
| 161 | 3, 1, "qual", |
| 162 | 7, 44, 602, 973, 402, 202, |
| 163 | }, |
| 164 | }, |
| 165 | } |
| 166 | if len(expected.MatchList) != len(response.MatchList) { |
| 167 | t.Fatal("Expected ", expected, ", but got ", *response) |
| 168 | } |
| 169 | for i, match := range expected.MatchList { |
| 170 | if !reflect.DeepEqual(*match, *response.MatchList[i]) { |
| 171 | t.Fatal("Expected for match", i, ":", *match, ", but got:", *response.MatchList[i]) |
| 172 | } |
| 173 | } |
Philipp Schrader | 30005e4 | 2022-03-06 13:53:58 -0800 | [diff] [blame] | 174 | |
Philipp Schrader | cbf5c6a | 2022-02-27 23:25:19 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Philipp Schrader | d1c4bef | 2022-02-28 22:51:30 -0800 | [diff] [blame] | 177 | // Validates that we can request the full match list. |
| 178 | func TestRequestMatchesForTeam(t *testing.T) { |
| 179 | db := MockDatabase{ |
| 180 | matches: []db.Match{ |
| 181 | { |
| 182 | MatchNumber: 1, Round: 1, CompLevel: "qual", |
| 183 | R1: 5, R2: 42, R3: 600, B1: 971, B2: 400, B3: 200, |
| 184 | }, |
| 185 | { |
| 186 | MatchNumber: 2, Round: 1, CompLevel: "qual", |
| 187 | R1: 6, R2: 43, R3: 601, B1: 972, B2: 401, B3: 201, |
| 188 | }, |
| 189 | }, |
| 190 | } |
| 191 | scoutingServer := server.NewScoutingServer() |
Philipp Schrader | d3fac19 | 2022-03-02 20:35:46 -0800 | [diff] [blame] | 192 | HandleRequests(&db, scrapeEmtpyMatchList, scoutingServer) |
Philipp Schrader | d1c4bef | 2022-02-28 22:51:30 -0800 | [diff] [blame] | 193 | scoutingServer.Start(8080) |
| 194 | defer scoutingServer.Stop() |
| 195 | |
| 196 | builder := flatbuffers.NewBuilder(1024) |
| 197 | builder.Finish((&request_matches_for_team.RequestMatchesForTeamT{ |
| 198 | Team: 971, |
| 199 | }).Pack(builder)) |
| 200 | |
| 201 | response, err := debug.RequestMatchesForTeam("http://localhost:8080", builder.FinishedBytes()) |
| 202 | if err != nil { |
| 203 | t.Fatal("Failed to request all matches: ", err) |
| 204 | } |
| 205 | |
| 206 | expected := request_matches_for_team_response.RequestMatchesForTeamResponseT{ |
| 207 | MatchList: []*request_matches_for_team_response.MatchT{ |
| 208 | // MatchNumber, Round, CompLevel |
| 209 | // R1, R2, R3, B1, B2, B3 |
| 210 | { |
| 211 | 1, 1, "qual", |
| 212 | 5, 42, 600, 971, 400, 200, |
| 213 | }, |
| 214 | }, |
| 215 | } |
| 216 | if len(expected.MatchList) != len(response.MatchList) { |
| 217 | t.Fatal("Expected ", expected, ", but got ", *response) |
| 218 | } |
| 219 | for i, match := range expected.MatchList { |
| 220 | if !reflect.DeepEqual(*match, *response.MatchList[i]) { |
| 221 | t.Fatal("Expected for match", i, ":", *match, ", but got:", *response.MatchList[i]) |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
Philipp Schrader | acf9623 | 2022-03-01 22:03:30 -0800 | [diff] [blame] | 226 | // Validates that we can request the stats. |
| 227 | func TestRequestDataScouting(t *testing.T) { |
| 228 | db := MockDatabase{ |
| 229 | stats: []db.Stats{ |
| 230 | { |
| 231 | TeamNumber: 971, MatchNumber: 1, |
Philipp Schrader | fee07e1 | 2022-03-17 22:19:47 -0700 | [diff] [blame] | 232 | StartingQuadrant: 1, |
| 233 | AutoBallPickedUp: [5]bool{true, false, false, false, true}, |
| 234 | ShotsMissed: 1, UpperGoalShots: 2, LowerGoalShots: 3, |
Philipp Schrader | acf9623 | 2022-03-01 22:03:30 -0800 | [diff] [blame] | 235 | ShotsMissedAuto: 4, UpperGoalAuto: 5, LowerGoalAuto: 6, |
Philipp Schrader | fa45d74 | 2022-03-18 19:29:05 -0700 | [diff] [blame] | 236 | PlayedDefense: 7, DefenseReceivedScore: 3, Climbing: 2, |
| 237 | Comment: "a lovely comment", CollectedBy: "john", |
Philipp Schrader | acf9623 | 2022-03-01 22:03:30 -0800 | [diff] [blame] | 238 | }, |
| 239 | { |
| 240 | TeamNumber: 972, MatchNumber: 1, |
Philipp Schrader | fee07e1 | 2022-03-17 22:19:47 -0700 | [diff] [blame] | 241 | StartingQuadrant: 2, |
| 242 | AutoBallPickedUp: [5]bool{false, false, true, false, false}, |
| 243 | ShotsMissed: 2, UpperGoalShots: 3, LowerGoalShots: 4, |
Philipp Schrader | acf9623 | 2022-03-01 22:03:30 -0800 | [diff] [blame] | 244 | ShotsMissedAuto: 5, UpperGoalAuto: 6, LowerGoalAuto: 7, |
Philipp Schrader | fa45d74 | 2022-03-18 19:29:05 -0700 | [diff] [blame] | 245 | PlayedDefense: 8, DefenseReceivedScore: 1, Climbing: 4, |
| 246 | Comment: "another lovely comment", CollectedBy: "andrea", |
Philipp Schrader | acf9623 | 2022-03-01 22:03:30 -0800 | [diff] [blame] | 247 | }, |
| 248 | }, |
| 249 | } |
| 250 | scoutingServer := server.NewScoutingServer() |
Philipp Schrader | d3fac19 | 2022-03-02 20:35:46 -0800 | [diff] [blame] | 251 | HandleRequests(&db, scrapeEmtpyMatchList, scoutingServer) |
Philipp Schrader | acf9623 | 2022-03-01 22:03:30 -0800 | [diff] [blame] | 252 | scoutingServer.Start(8080) |
| 253 | defer scoutingServer.Stop() |
| 254 | |
| 255 | builder := flatbuffers.NewBuilder(1024) |
| 256 | builder.Finish((&request_data_scouting.RequestDataScoutingT{}).Pack(builder)) |
| 257 | |
| 258 | response, err := debug.RequestDataScouting("http://localhost:8080", builder.FinishedBytes()) |
| 259 | if err != nil { |
| 260 | t.Fatal("Failed to request all matches: ", err) |
| 261 | } |
| 262 | |
| 263 | expected := request_data_scouting_response.RequestDataScoutingResponseT{ |
| 264 | StatsList: []*request_data_scouting_response.StatsT{ |
Philipp Schrader | acf9623 | 2022-03-01 22:03:30 -0800 | [diff] [blame] | 265 | { |
Philipp Schrader | 36df73a | 2022-03-17 23:27:24 -0700 | [diff] [blame] | 266 | Team: 971, Match: 1, |
| 267 | MissedShotsAuto: 4, UpperGoalAuto: 5, LowerGoalAuto: 6, |
| 268 | MissedShotsTele: 1, UpperGoalTele: 2, LowerGoalTele: 3, |
Philipp Schrader | fa45d74 | 2022-03-18 19:29:05 -0700 | [diff] [blame] | 269 | DefenseRating: 7, |
| 270 | DefenseReceivedRating: 3, |
| 271 | CollectedBy: "john", |
| 272 | AutoBall1: true, AutoBall2: false, AutoBall3: false, |
Philipp Schrader | 36df73a | 2022-03-17 23:27:24 -0700 | [diff] [blame] | 273 | AutoBall4: false, AutoBall5: true, |
| 274 | StartingQuadrant: 1, |
| 275 | ClimbLevel: request_data_scouting_response.ClimbLevelFailedWithPlentyOfTime, |
Philipp Schrader | fa45d74 | 2022-03-18 19:29:05 -0700 | [diff] [blame] | 276 | Comment: "a lovely comment", |
Philipp Schrader | acf9623 | 2022-03-01 22:03:30 -0800 | [diff] [blame] | 277 | }, |
| 278 | { |
Philipp Schrader | 36df73a | 2022-03-17 23:27:24 -0700 | [diff] [blame] | 279 | Team: 972, Match: 1, |
| 280 | MissedShotsAuto: 5, UpperGoalAuto: 6, LowerGoalAuto: 7, |
| 281 | MissedShotsTele: 2, UpperGoalTele: 3, LowerGoalTele: 4, |
Philipp Schrader | fa45d74 | 2022-03-18 19:29:05 -0700 | [diff] [blame] | 282 | DefenseRating: 8, |
| 283 | DefenseReceivedRating: 1, |
| 284 | CollectedBy: "andrea", |
| 285 | AutoBall1: false, AutoBall2: false, AutoBall3: true, |
Philipp Schrader | 36df73a | 2022-03-17 23:27:24 -0700 | [diff] [blame] | 286 | AutoBall4: false, AutoBall5: false, |
| 287 | StartingQuadrant: 2, |
| 288 | ClimbLevel: request_data_scouting_response.ClimbLevelMedium, |
Philipp Schrader | fa45d74 | 2022-03-18 19:29:05 -0700 | [diff] [blame] | 289 | Comment: "another lovely comment", |
Philipp Schrader | acf9623 | 2022-03-01 22:03:30 -0800 | [diff] [blame] | 290 | }, |
| 291 | }, |
| 292 | } |
| 293 | if len(expected.StatsList) != len(response.StatsList) { |
| 294 | t.Fatal("Expected ", expected, ", but got ", *response) |
| 295 | } |
| 296 | for i, match := range expected.StatsList { |
| 297 | if !reflect.DeepEqual(*match, *response.StatsList[i]) { |
| 298 | t.Fatal("Expected for stats", i, ":", *match, ", but got:", *response.StatsList[i]) |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
Alex Perry | 81f96ba | 2022-03-13 18:26:19 -0700 | [diff] [blame] | 303 | func TestSubmitNotes(t *testing.T) { |
| 304 | database := MockDatabase{} |
| 305 | scoutingServer := server.NewScoutingServer() |
| 306 | HandleRequests(&database, scrapeEmtpyMatchList, scoutingServer) |
| 307 | scoutingServer.Start(8080) |
| 308 | defer scoutingServer.Stop() |
| 309 | |
| 310 | builder := flatbuffers.NewBuilder(1024) |
| 311 | builder.Finish((&submit_notes.SubmitNotesT{ |
| 312 | Team: 971, |
| 313 | Notes: "Notes", |
| 314 | }).Pack(builder)) |
| 315 | |
| 316 | _, err := debug.SubmitNotes("http://localhost:8080", builder.FinishedBytes()) |
| 317 | if err != nil { |
| 318 | t.Fatal("Failed to submit notes: ", err) |
| 319 | } |
| 320 | |
| 321 | expected := []db.NotesData{ |
| 322 | {TeamNumber: 971, Notes: []string{"Notes"}}, |
| 323 | } |
| 324 | |
| 325 | if !reflect.DeepEqual(database.notes, expected) { |
| 326 | t.Fatal("Submitted notes did not match", expected, database.notes) |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | func TestRequestNotes(t *testing.T) { |
| 331 | database := MockDatabase{ |
| 332 | notes: []db.NotesData{{ |
| 333 | TeamNumber: 971, |
| 334 | Notes: []string{"Notes"}, |
| 335 | }}, |
| 336 | } |
| 337 | scoutingServer := server.NewScoutingServer() |
| 338 | HandleRequests(&database, scrapeEmtpyMatchList, scoutingServer) |
| 339 | scoutingServer.Start(8080) |
| 340 | defer scoutingServer.Stop() |
| 341 | |
| 342 | builder := flatbuffers.NewBuilder(1024) |
| 343 | builder.Finish((&request_notes_for_team.RequestNotesForTeamT{ |
| 344 | Team: 971, |
| 345 | }).Pack(builder)) |
| 346 | response, err := debug.RequestNotes("http://localhost:8080", builder.FinishedBytes()) |
| 347 | if err != nil { |
| 348 | t.Fatal("Failed to submit notes: ", err) |
| 349 | } |
| 350 | |
| 351 | if response.Notes[0].Data != "Notes" { |
| 352 | t.Fatal("requested notes did not match", response) |
| 353 | } |
| 354 | } |
| 355 | |
Philipp Schrader | d3fac19 | 2022-03-02 20:35:46 -0800 | [diff] [blame] | 356 | // Validates that we can download the schedule from The Blue Alliance. |
| 357 | func TestRefreshMatchList(t *testing.T) { |
| 358 | scrapeMockSchedule := func(int32, string) ([]scraping.Match, error) { |
| 359 | return []scraping.Match{ |
| 360 | { |
| 361 | CompLevel: "qual", |
| 362 | MatchNumber: 1, |
| 363 | Alliances: scraping.Alliances{ |
| 364 | Red: scraping.Alliance{ |
| 365 | TeamKeys: []string{ |
| 366 | "100", |
| 367 | "200", |
| 368 | "300", |
| 369 | }, |
| 370 | }, |
| 371 | Blue: scraping.Alliance{ |
| 372 | TeamKeys: []string{ |
| 373 | "101", |
| 374 | "201", |
| 375 | "301", |
| 376 | }, |
| 377 | }, |
| 378 | }, |
| 379 | WinningAlliance: "", |
| 380 | EventKey: "", |
| 381 | Time: 0, |
| 382 | PredictedTime: 0, |
| 383 | ActualTime: 0, |
| 384 | PostResultTime: 0, |
| 385 | ScoreBreakdowns: scraping.ScoreBreakdowns{}, |
| 386 | }, |
| 387 | }, nil |
| 388 | } |
| 389 | |
| 390 | database := MockDatabase{} |
| 391 | scoutingServer := server.NewScoutingServer() |
| 392 | HandleRequests(&database, scrapeMockSchedule, scoutingServer) |
| 393 | scoutingServer.Start(8080) |
| 394 | defer scoutingServer.Stop() |
| 395 | |
| 396 | builder := flatbuffers.NewBuilder(1024) |
| 397 | builder.Finish((&refresh_match_list.RefreshMatchListT{}).Pack(builder)) |
| 398 | |
| 399 | response, err := debug.RefreshMatchList("http://localhost:8080", builder.FinishedBytes()) |
| 400 | if err != nil { |
| 401 | t.Fatal("Failed to request all matches: ", err) |
| 402 | } |
| 403 | |
| 404 | // Validate the response. |
| 405 | expected := refresh_match_list_response.RefreshMatchListResponseT{} |
| 406 | if !reflect.DeepEqual(expected, *response) { |
| 407 | t.Fatal("Expected ", expected, ", but got ", *response) |
| 408 | } |
| 409 | |
| 410 | // Make sure that the data made it into the database. |
| 411 | expectedMatches := []db.Match{ |
| 412 | { |
| 413 | MatchNumber: 1, |
| 414 | Round: 1, |
| 415 | CompLevel: "qual", |
| 416 | R1: 100, |
| 417 | R2: 200, |
| 418 | R3: 300, |
| 419 | B1: 101, |
| 420 | B2: 201, |
| 421 | B3: 301, |
| 422 | }, |
| 423 | } |
| 424 | if !reflect.DeepEqual(expectedMatches, database.matches) { |
| 425 | t.Fatal("Expected ", expectedMatches, ", but got ", database.matches) |
| 426 | } |
| 427 | } |
| 428 | |
Philipp Schrader | 8747f1b | 2022-02-23 23:56:22 -0800 | [diff] [blame] | 429 | // A mocked database we can use for testing. Add functionality to this as |
| 430 | // needed for your tests. |
| 431 | |
Philipp Schrader | cbf5c6a | 2022-02-27 23:25:19 -0800 | [diff] [blame] | 432 | type MockDatabase struct { |
| 433 | matches []db.Match |
Philipp Schrader | acf9623 | 2022-03-01 22:03:30 -0800 | [diff] [blame] | 434 | stats []db.Stats |
Alex Perry | 81f96ba | 2022-03-13 18:26:19 -0700 | [diff] [blame] | 435 | notes []db.NotesData |
Philipp Schrader | cbf5c6a | 2022-02-27 23:25:19 -0800 | [diff] [blame] | 436 | } |
Philipp Schrader | 8747f1b | 2022-02-23 23:56:22 -0800 | [diff] [blame] | 437 | |
Philipp Schrader | d3fac19 | 2022-03-02 20:35:46 -0800 | [diff] [blame] | 438 | func (database *MockDatabase) AddToMatch(match db.Match) error { |
| 439 | database.matches = append(database.matches, match) |
Philipp Schrader | 8747f1b | 2022-02-23 23:56:22 -0800 | [diff] [blame] | 440 | return nil |
| 441 | } |
| 442 | |
Philipp Schrader | 30005e4 | 2022-03-06 13:53:58 -0800 | [diff] [blame] | 443 | func (database *MockDatabase) AddToStats(stats db.Stats) error { |
| 444 | database.stats = append(database.stats, stats) |
Philipp Schrader | 8747f1b | 2022-02-23 23:56:22 -0800 | [diff] [blame] | 445 | return nil |
| 446 | } |
| 447 | |
| 448 | func (database *MockDatabase) ReturnMatches() ([]db.Match, error) { |
Philipp Schrader | cbf5c6a | 2022-02-27 23:25:19 -0800 | [diff] [blame] | 449 | return database.matches, nil |
Philipp Schrader | 8747f1b | 2022-02-23 23:56:22 -0800 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | func (database *MockDatabase) ReturnStats() ([]db.Stats, error) { |
Philipp Schrader | acf9623 | 2022-03-01 22:03:30 -0800 | [diff] [blame] | 453 | return database.stats, nil |
Philipp Schrader | 8747f1b | 2022-02-23 23:56:22 -0800 | [diff] [blame] | 454 | } |
| 455 | |
Philipp Schrader | d1c4bef | 2022-02-28 22:51:30 -0800 | [diff] [blame] | 456 | func (database *MockDatabase) QueryMatches(requestedTeam int32) ([]db.Match, error) { |
| 457 | var matches []db.Match |
| 458 | for _, match := range database.matches { |
| 459 | for _, team := range []int32{match.R1, match.R2, match.R3, match.B1, match.B2, match.B3} { |
| 460 | if team == requestedTeam { |
| 461 | matches = append(matches, match) |
| 462 | break |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | return matches, nil |
Philipp Schrader | 8747f1b | 2022-02-23 23:56:22 -0800 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | func (database *MockDatabase) QueryStats(int) ([]db.Stats, error) { |
| 470 | return []db.Stats{}, nil |
| 471 | } |
Philipp Schrader | d3fac19 | 2022-03-02 20:35:46 -0800 | [diff] [blame] | 472 | |
Alex Perry | 81f96ba | 2022-03-13 18:26:19 -0700 | [diff] [blame] | 473 | func (database *MockDatabase) QueryNotes(requestedTeam int32) (db.NotesData, error) { |
| 474 | var results []string |
| 475 | for _, data := range database.notes { |
| 476 | if data.TeamNumber == requestedTeam { |
| 477 | results = append(results, data.Notes[0]) |
| 478 | } |
| 479 | } |
| 480 | return db.NotesData{TeamNumber: requestedTeam, Notes: results}, nil |
| 481 | } |
| 482 | |
| 483 | func (database *MockDatabase) AddNotes(data db.NotesData) error { |
| 484 | database.notes = append(database.notes, data) |
| 485 | return nil |
| 486 | } |
| 487 | |
Philipp Schrader | d3fac19 | 2022-03-02 20:35:46 -0800 | [diff] [blame] | 488 | // Returns an empty match list from the fake The Blue Alliance scraping. |
| 489 | func scrapeEmtpyMatchList(int32, string) ([]scraping.Match, error) { |
| 490 | return nil, nil |
| 491 | } |