Files
payments/internal/models/config_test.go
CrimsonandGitHub ad704e958b feat(polling): allow system-wide polling default to be configured via env variable (#615)
* Manage configuration of polling period in connectors manager and enable override via env variables

* Treat connector validation errors as http 4xx

* Perform strict config validation only if connector being loaded for install or update

* Add flags to configure notification outbox schedules

* Only configure outbox schedule creation in worker

* Improve manager config validation test coverage
2025-12-08 09:32:29 +00:00

106 lines
1.9 KiB
Go

package models_test
import (
"encoding/json"
"testing"
"time"
"github.com/formancehq/payments/internal/models"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestConfigMarshalJSON(t *testing.T) {
t.Parallel()
config := models.Config{
Name: "test-config",
PollingPeriod: 5 * time.Minute,
}
data, err := json.Marshal(config)
// Then
require.NoError(t, err)
var jsonMap map[string]interface{}
err = json.Unmarshal(data, &jsonMap)
// Then
require.NoError(t, err)
assert.Equal(t, "test-config", jsonMap["name"])
assert.Equal(t, "5m0s", jsonMap["pollingPeriod"])
}
func TestConfigUnmarshalJSON(t *testing.T) {
t.Parallel()
t.Run("valid config", func(t *testing.T) {
t.Parallel()
// Given
jsonData := `{
"name": "test-config",
"pollingPeriod": "5m"
}`
var config models.Config
err := json.Unmarshal([]byte(jsonData), &config)
// Then
require.NoError(t, err)
assert.Equal(t, "test-config", config.Name)
assert.Equal(t, 5*time.Minute, config.PollingPeriod)
})
t.Run("invalid polling period", func(t *testing.T) {
t.Parallel()
// Given
jsonData := `{
"name": "test-config",
"pollingPeriod": "invalid"
}`
var config models.Config
err := json.Unmarshal([]byte(jsonData), &config)
// Then
require.Error(t, err)
})
t.Run("zero values", func(t *testing.T) {
t.Parallel()
// Given
jsonData := `{
"name": "test-config",
"pollingPeriod": "0s"
}`
var config models.Config
err := json.Unmarshal([]byte(jsonData), &config)
// Then
require.NoError(t, err)
assert.Equal(t, "test-config", config.Name)
assert.Equal(t, 0*time.Second, config.PollingPeriod) // Zero is allowed
})
t.Run("invalid json", func(t *testing.T) {
t.Parallel()
// Given
jsonData := `{invalid json}`
var config models.Config
err := json.Unmarshal([]byte(jsonData), &config)
// Then
require.Error(t, err)
})
}