Skip to content

Commit

Permalink
tests and doc
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed May 26, 2023
1 parent 6fea52e commit 5d9c385
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 23 deletions.
9 changes: 7 additions & 2 deletions cmd/slackdump/internal/apiconfig/apiconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import (
"github.com/rusq/slackdump/v2/cmd/slackdump/internal/golang/base"
)

// schemaJSONpath is the path to the schema JSON file for the limits yaml
// configuration file.
// TODO: update once released
const schemaJSONpath = "https://raw.githubusercontent.com/rusq/slackdump/cli-remake/cmd/slackdump/internal/apiconfig/schema.json"

var CmdConfig = &base.Command{
Expand Down Expand Up @@ -41,7 +44,7 @@ func Load(filename string) (slackdump.Limits, error) {
}
defer f.Close()

return readLimits(f)
return applyLimits(f)
}

// Save saves the config to the file.
Expand All @@ -55,7 +58,9 @@ func Save(filename string, limits slackdump.Limits) error {
return writeLimits(f, limits)
}

func readLimits(r io.Reader) (slackdump.Limits, error) {
// applyLimits reads the limits from the reader, validates them and applies to
// the global config.
func applyLimits(r io.Reader) (slackdump.Limits, error) {
var limits slackdump.Limits
dec := yaml.NewDecoder(r)
dec.KnownFields(true)
Expand Down
85 changes: 71 additions & 14 deletions cmd/slackdump/internal/apiconfig/apiconfig_test.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
package apiconfig

import (
"bytes"
"io"
"reflect"
"strings"
"testing"

"github.com/rusq/slackdump/v2"
"github.com/stretchr/testify/assert"
)

const (
sampleLimitsYaml = `workers: 4
sampleLimitsYaml = `# yaml-language-server: $schema=https://raw.githubusercontent.com/rusq/slackdump/cli-remake/cmd/slackdump/internal/apiconfig/schema.json
workers: 4
download_retries: 3
tier_2:
boost: 20
burst: 1
retries: 20
boost: 20
burst: 1
retries: 20
tier_3:
boost: 120
burst: 1
retries: 3
boost: 120
burst: 1
retries: 3
tier_4:
boost: 10
burst: 1
retries: 3
boost: 10
burst: 1
retries: 3
per_request:
conversations: 100
channels: 100
replies: 200
conversations: 100
channels: 100
replies: 200
`
// workers set to 55 in this one, tier2.retries to 330
updatedConfigYaml = `workers: 55
Expand All @@ -51,6 +54,31 @@ per_request:
`
)

var testLimits = slackdump.Limits{
Workers: 4,
DownloadRetries: 3,
Tier2: slackdump.TierLimit{
Boost: 20,
Burst: 1,
Retries: 20,
},
Tier3: slackdump.TierLimit{
Boost: 120,
Burst: 1,
Retries: 3,
},
Tier4: slackdump.TierLimit{
Boost: 10,
Burst: 1,
Retries: 3,
},
Request: slackdump.RequestLimit{
Conversations: 100,
Channels: 100,
Replies: 200,
},
}

func Test_readConfig(t *testing.T) {
type args struct {
r io.Reader
Expand Down Expand Up @@ -105,7 +133,7 @@ func Test_readConfig(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := readLimits(tt.args.r)
got, err := applyLimits(tt.args.r)
t.Log(tt.name)
if (err != nil) != tt.wantErr {
t.Errorf("readConfig() error = %v, wantErr %v", err, tt.wantErr)
Expand All @@ -117,3 +145,32 @@ func Test_readConfig(t *testing.T) {
})
}
}

func Test_writeLimits(t *testing.T) {
type args struct {
cfg slackdump.Limits
}
tests := []struct {
name string
args args
wantW string
wantErr bool
}{
{
"writes limits and comments",
args{testLimits},
sampleLimitsYaml,
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := &bytes.Buffer{}
if err := writeLimits(w, tt.args.cfg); (err != nil) != tt.wantErr {
t.Errorf("writeLimits() error = %v, wantErr %v", err, tt.wantErr)
return
}
assert.Equal(t, tt.wantW, w.String())
})
}
}
3 changes: 2 additions & 1 deletion cmd/slackdump/internal/apiconfig/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func runConfigNew(ctx context.Context, cmd *base.Command, args []string) error {
return err
}

// printConfigOK outputs the confirmation message to the user.
func printConfigOK(filename string) (n int, err error) {
return fmt.Printf("Your new API limits config is ready: %q\n", filename)
}
Expand Down Expand Up @@ -99,7 +100,7 @@ func shouldOverwrite(filename string, override bool) bool {
}

// maybeFixExt checks if the extension is one of .yaml or .yml, and if not
// appends it to teh file.
// appends it to the file.
func maybeFixExt(filename string) string {
if ext := filepath.Ext(filename); !(ext == ".yaml" || ext == ".yml") {
return maybeAppendExt(filename, ".yaml")
Expand Down
8 changes: 5 additions & 3 deletions cmd/slackdump/internal/apiconfig/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
"tier_3": {
"$ref": "#/definitions/Tier"
},
"tier_4": {
"$ref": "#/definitions/Tier"
},
"per_request": {
"$ref": "#/definitions/PerRequest"
}
Expand Down Expand Up @@ -55,12 +58,12 @@
"description": "Number of replies to fetch per request",
"minimum": 1,
"maximum": 1000

}
},
"title": "PerRequest"
},
"Tier": {
"title": "Tier",
"type": "object",
"additionalProperties": false,
"description": "Rate limiting tier",
Expand All @@ -78,8 +81,7 @@
"type": "integer",
"description": "Number of times to retry a request"
}
},
"title": "Tier"
}
}
}
}
7 changes: 4 additions & 3 deletions slackdump.1
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ Allows to perform different operations on the API limits configuration
files.
.It Cm convert
Convert between formats.
.It Cm diag Ar subcommand
Contains various diagnostic utilities. Developers might ask to run
these commands to help with debugging.
.It Cm tools Ar subcommand
Contains various diagnostic and convenience utilities. Developers might ask
to run these commands to help with debugging. See TOOLS section for more
information.
.It Cm dump
Dump selected channels or threads.
.It Cm emoji
Expand Down

0 comments on commit 5d9c385

Please sign in to comment.