scouting: Add support /requests/request/all_matches
This patch adds a new endpoint that accepts the `RequestAllMatches`
message. It simply returns the full list of matches that the database
knows about.
I decided to change public `int` members in the `db` module to `int32`
so they match the flatbuffer definition. This makes comparison
simpler.
Signed-off-by: Philipp Schrader <philipp.schrader@gmail.com>
Change-Id: I9bb2eed020e2889644f5a122105a232a68f2f4bd
diff --git a/scouting/webserver/requests/requests_test.go b/scouting/webserver/requests/requests_test.go
index 0125da5..26256be 100644
--- a/scouting/webserver/requests/requests_test.go
+++ b/scouting/webserver/requests/requests_test.go
@@ -4,10 +4,14 @@
"bytes"
"io"
"net/http"
+ "reflect"
"testing"
"github.com/frc971/971-Robot-Code/scouting/db"
+ "github.com/frc971/971-Robot-Code/scouting/webserver/requests/debug"
"github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/error_response"
+ "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_all_matches"
+ "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/request_all_matches_response"
"github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/submit_data_scouting"
_ "github.com/frc971/971-Robot-Code/scouting/webserver/requests/messages/submit_data_scouting_response"
"github.com/frc971/971-Robot-Code/scouting/webserver/server"
@@ -92,10 +96,71 @@
// TODO(phil): Can we use scouting/webserver/requests/debug here?
}
+// Validates that we can request the full match list.
+func TestRequestAllMatches(t *testing.T) {
+ db := MockDatabase{
+ matches: []db.Match{
+ {
+ MatchNumber: 1, Round: 1, CompLevel: "qual",
+ R1: 5, R2: 42, R3: 600, B1: 971, B2: 400, B3: 200,
+ },
+ {
+ MatchNumber: 2, Round: 1, CompLevel: "qual",
+ R1: 6, R2: 43, R3: 601, B1: 972, B2: 401, B3: 201,
+ },
+ {
+ MatchNumber: 3, Round: 1, CompLevel: "qual",
+ R1: 7, R2: 44, R3: 602, B1: 973, B2: 402, B3: 202,
+ },
+ },
+ }
+ scoutingServer := server.NewScoutingServer()
+ HandleRequests(&db, scoutingServer)
+ scoutingServer.Start(8080)
+ defer scoutingServer.Stop()
+
+ builder := flatbuffers.NewBuilder(1024)
+ builder.Finish((&request_all_matches.RequestAllMatchesT{}).Pack(builder))
+
+ response, err := debug.RequestAllMatches("http://localhost:8080", builder.FinishedBytes())
+ if err != nil {
+ t.Fatal("Failed to request all matches: ", err)
+ }
+
+ expected := request_all_matches_response.RequestAllMatchesResponseT{
+ MatchList: []*request_all_matches_response.MatchT{
+ // MatchNumber, Round, CompLevel
+ // R1, R2, R3, B1, B2, B3
+ {
+ 1, 1, "qual",
+ 5, 42, 600, 971, 400, 200,
+ },
+ {
+ 2, 1, "qual",
+ 6, 43, 601, 972, 401, 201,
+ },
+ {
+ 3, 1, "qual",
+ 7, 44, 602, 973, 402, 202,
+ },
+ },
+ }
+ if len(expected.MatchList) != len(response.MatchList) {
+ t.Fatal("Expected ", expected, ", but got ", *response)
+ }
+ for i, match := range expected.MatchList {
+ if !reflect.DeepEqual(*match, *response.MatchList[i]) {
+ t.Fatal("Expected for match", i, ":", *match, ", but got:", *response.MatchList[i])
+ }
+ }
+}
+
// A mocked database we can use for testing. Add functionality to this as
// needed for your tests.
-type MockDatabase struct{}
+type MockDatabase struct {
+ matches []db.Match
+}
func (database *MockDatabase) AddToMatch(db.Match) error {
return nil
@@ -106,7 +171,7 @@
}
func (database *MockDatabase) ReturnMatches() ([]db.Match, error) {
- return []db.Match{}, nil
+ return database.matches, nil
}
func (database *MockDatabase) ReturnStats() ([]db.Stats, error) {