Create dummy amendments with performance test script.

This commit is contained in:
Oskar Hahn 2021-04-07 22:04:42 +02:00 committed by Emanuel Schütze
parent 1edf4437a0
commit 23fcc3a7d0
3 changed files with 120 additions and 1 deletions

View File

@ -107,7 +107,7 @@ func CheckStatus(resp *http.Response, err error) (*http.Response, error) {
return nil, fmt.Errorf("sending login request: %w", err) return nil, fmt.Errorf("sending login request: %w", err)
} }
if resp.StatusCode != 200 { if resp.StatusCode < 200 || resp.StatusCode > 299 {
body, err := io.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
body = []byte("[can not read body]") body = []byte("[can not read body]")

View File

@ -0,0 +1,118 @@
package cmd
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/OpenSlides/OpenSlides/performance/client"
"github.com/spf13/cobra"
)
const helpCreateAmendments = `Creates a motions with amendments
...`
func cmdCreateAmendments(cfg *config) *cobra.Command {
var amendmentAmount int
cmd := &cobra.Command{
Use: "create_amendments",
Short: "Creates a motion with amendments.",
Long: helpCreateAmendments,
RunE: func(cmd *cobra.Command, args []string) error {
c, err := client.New(cfg.domain, cfg.username, cfg.password)
if err != nil {
return fmt.Errorf("creating client: %w", err)
}
if err := c.Login(); err != nil {
return fmt.Errorf("login client: %w", err)
}
addr := "https://" + cfg.domain
motionID, err := createMotion(c, addr, amendmentAmount)
if err != nil {
return fmt.Errorf("create motion: %w", err)
}
for i := 0; i < amendmentAmount; i++ {
createAmendment(c, addr, motionID, i)
}
fmt.Println(motionID)
return nil
},
}
cmd.Flags().IntVarP(&amendmentAmount, "amount", "a", 10, "Amount of amendments.")
return cmd
}
func createMotion(c *client.Client, addr string, amendmentAmount int) (int, error) {
var text string
for i := 1; i <= amendmentAmount; i++ {
text += fmt.Sprintf(`<p>Absatz %d</p>\n`, i)
}
payload := fmt.Sprintf(`{
"title":"Testmotion",
"text":"%s",
"attachments_id":[],
"agenda_type":2,
"supporters_id":[],
"workflow_id":1
}`, text)
req, err := http.NewRequest("POST", addr+"/rest/motions/motion/", strings.NewReader(payload))
if err != nil {
return 0, fmt.Errorf("sending motion create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
resp, err := client.CheckStatus(c.Do(req))
if err != nil {
return 0, err
}
defer resp.Body.Close()
var respBody struct {
Data struct {
ID int `json:"id"`
} `json:"data"`
}
if err := json.NewDecoder(resp.Body).Decode(&respBody); err != nil {
return 0, fmt.Errorf("reading response: %w", err)
}
return respBody.Data.ID, nil
}
func createAmendment(c *client.Client, addr string, motionID, amendmentID int) error {
prevParagraphs := strings.Repeat(`null,`, amendmentID)
payload := fmt.Sprintf(`{
"title":"Änderungsantrag zu %d",
"text":"<p>Neu %d</p>",
"parent_id":%d,
"tags_id":[],
"amendment_paragraphs":[%s"<p>Neu %d</p>"],
"workflow_id":"1"
}`, motionID, amendmentID+1, motionID, prevParagraphs, amendmentID+1)
req, err := http.NewRequest("POST", addr+"/rest/motions/motion/", strings.NewReader(payload))
if err != nil {
return fmt.Errorf("creating amendment %d create request: %w", amendmentID, err)
}
req.Header.Set("Content-Type", "application/json")
if _, err := client.CheckStatus(c.Do(req)); err != nil {
return fmt.Errorf("sending amendment %d create request: %w", amendmentID, err)
}
return nil
}

View File

@ -37,6 +37,7 @@ func Execute() error {
cmdConnect(cfg), cmdConnect(cfg),
cmdCreateUsers(cfg), cmdCreateUsers(cfg),
cmdVotes(cfg), cmdVotes(cfg),
cmdCreateAmendments(cfg),
) )
return cmd.Execute() return cmd.Execute()