From 2d4ca83262944a2cc4a1711ab71733f3ea479a98 Mon Sep 17 00:00:00 2001 From: Gui Iribarren Date: Thu, 13 Jul 2023 11:12:42 +0200 Subject: [PATCH] api_test wip --- test/api_test.go | 53 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/test/api_test.go b/test/api_test.go index 70a1d4896..a3e5e9217 100644 --- a/test/api_test.go +++ b/test/api_test.go @@ -85,32 +85,49 @@ func (te *testElection) GenerateVoters(t testing.TB, nvotes int) { } } -func (te *testElection) CreateCensusAndElection(t testing.TB) { - // create a new census - resp, code := te.c.Request("POST", nil, "censuses", "weighted") - qt.Assert(t, code, qt.Equals, 200) - te.censusData = &api.Census{} - qt.Assert(t, json.Unmarshal(resp, te.censusData), qt.IsNil) - id1 := te.censusData.CensusID.String() +func (te *testElection) AddCensusParticipants(t testing.TB, id string) types.HexBytes { + // add a bunch of keys and values (weights) in batches of api.MaxCensusAddBatchSize + for i := 0; i < len(te.voters); i += api.MaxCensusAddBatchSize { + // Get the next chunk of voters + end := i + api.MaxCensusAddBatchSize + if end > len(te.voters) { + end = len(te.voters) + } - // add a bunch of keys and values (weights) - cparts := api.CensusParticipants{} - for _, voter := range te.voters { - cparts.Participants = append(cparts.Participants, api.CensusParticipant{ - Key: voter.key.Address().Bytes(), - Weight: (*types.BigInt)(big.NewInt(1)), - }) + // Add the voters in the chunk to the cparts struct + cparts := api.CensusParticipants{} + for _, voter := range te.voters[i:end] { + cparts.Participants = append(cparts.Participants, api.CensusParticipant{ + Key: voter.key.Address().Bytes(), + Weight: (*types.BigInt)(big.NewInt(1)), + }) + } + + t.Log("wtf") + // POST this chunk of voters + resp, code := te.c.Request("POST", &cparts, "censuses", id, "participants") + qt.Assert(t, code, qt.Equals, 200) + qt.Assert(t, resp, qt.IsNotNil) } - resp, code = te.c.Request("POST", &cparts, "censuses", id1, "participants") - t.Logf("%s", resp) - qt.Assert(t, code, qt.Equals, 200) - resp, code = te.c.Request("POST", nil, "censuses", id1, "publish") + resp, code := te.c.Request("POST", nil, "censuses", id, "publish") qt.Assert(t, code, qt.Equals, 200) qt.Assert(t, json.Unmarshal(resp, te.censusData), qt.IsNil) qt.Assert(t, te.censusData.CensusID, qt.IsNotNil) root := te.censusData.CensusID + return root +} + +func (te *testElection) CreateCensusAndElection(t testing.TB) { + // create a new census + resp, code := te.c.Request("POST", nil, "censuses", "weighted") + qt.Assert(t, code, qt.Equals, 200) + te.censusData = &api.Census{} + qt.Assert(t, json.Unmarshal(resp, te.censusData), qt.IsNil) + id1 := te.censusData.CensusID.String() + root := te.AddCensusParticipants(t, id1) + t.Log(root, id1) for i, voter := range te.voters { censusData := &api.Census{} resp, code = te.c.Request("GET", nil, "censuses", root.String(),