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