From d9c30af91bc7c82c8ca468d92f311d949de35295 Mon Sep 17 00:00:00 2001 From: Sergey Date: Sun, 25 Feb 2024 01:50:45 +0300 Subject: [PATCH] chore: test Config --- pkg/config/toml_config/toml_config_test.go | 201 +++++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100644 pkg/config/toml_config/toml_config_test.go diff --git a/pkg/config/toml_config/toml_config_test.go b/pkg/config/toml_config/toml_config_test.go new file mode 100644 index 0000000..137603a --- /dev/null +++ b/pkg/config/toml_config/toml_config_test.go @@ -0,0 +1,201 @@ +package toml_config_test + +import ( + tomlConfig "main/pkg/config/toml_config" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestTomlConfigNoChains(t *testing.T) { + t.Parallel() + + config := tomlConfig.TomlConfig{} + require.Error(t, config.Validate()) +} + +func TestTomlConfigInvalidTimezone(t *testing.T) { + t.Parallel() + + config := tomlConfig.TomlConfig{ + Chains: tomlConfig.Chains{ + {}, + }, + Timezone: "invalid", + } + require.Error(t, config.Validate()) +} + +func TestTomlConfigInvalidChain(t *testing.T) { + t.Parallel() + + config := tomlConfig.TomlConfig{ + Chains: tomlConfig.Chains{ + {}, + }, + Timezone: "Etc/UTC", + } + require.Error(t, config.Validate()) +} + +func TestTomlConfigInvalidReporter(t *testing.T) { + t.Parallel() + + config := tomlConfig.TomlConfig{ + Chains: tomlConfig.Chains{ + { + Name: "chain", + TendermintNodes: []string{"node"}, + APINodes: []string{"node"}, + Queries: []string{"event.key = 'value'"}, + }, + }, + Reporters: tomlConfig.Reporters{ + {}, + }, + Timezone: "Etc/UTC", + } + require.Error(t, config.Validate()) +} + +func TestTomlConfigInvalidSubscription(t *testing.T) { + t.Parallel() + + config := tomlConfig.TomlConfig{ + Chains: tomlConfig.Chains{ + { + Name: "chain", + TendermintNodes: []string{"node"}, + APINodes: []string{"node"}, + Queries: []string{"event.key = 'value'"}, + }, + }, + Reporters: tomlConfig.Reporters{ + { + Name: "test", + Type: "telegram", + TelegramConfig: &tomlConfig.TelegramConfig{ + Chat: 1, + Token: "xxx:yyy", + Admins: []int64{123}, + }, + }, + }, + Subscriptions: tomlConfig.Subscriptions{ + {}, + }, + Timezone: "Etc/UTC", + } + require.Error(t, config.Validate()) +} + +func TestTomlConfigChainSubscriptionChainNotFound(t *testing.T) { + t.Parallel() + + config := tomlConfig.TomlConfig{ + Chains: tomlConfig.Chains{ + { + Name: "chain", + TendermintNodes: []string{"node"}, + APINodes: []string{"node"}, + Queries: []string{"event.key = 'value'"}, + }, + }, + Reporters: tomlConfig.Reporters{ + { + Name: "test", + Type: "telegram", + TelegramConfig: &tomlConfig.TelegramConfig{ + Chat: 1, + Token: "xxx:yyy", + Admins: []int64{123}, + }, + }, + }, + Subscriptions: tomlConfig.Subscriptions{ + { + Name: "name", + Reporter: "reporter", + ChainSubscriptions: tomlConfig.ChainSubscriptions{ + {Chain: "nonexistent"}, + }, + }, + }, + Timezone: "Etc/UTC", + } + require.Error(t, config.Validate()) +} + +func TestTomlConfigSubscriptionReporterNotFound(t *testing.T) { + t.Parallel() + + config := tomlConfig.TomlConfig{ + Chains: tomlConfig.Chains{ + { + Name: "chain", + TendermintNodes: []string{"node"}, + APINodes: []string{"node"}, + Queries: []string{"event.key = 'value'"}, + }, + }, + Reporters: tomlConfig.Reporters{ + { + Name: "test", + Type: "telegram", + TelegramConfig: &tomlConfig.TelegramConfig{ + Chat: 1, + Token: "xxx:yyy", + Admins: []int64{123}, + }, + }, + }, + Subscriptions: tomlConfig.Subscriptions{ + { + Name: "name", + Reporter: "nonexistent", + ChainSubscriptions: tomlConfig.ChainSubscriptions{ + {Chain: "chain"}, + }, + }, + }, + Timezone: "Etc/UTC", + } + require.Error(t, config.Validate()) +} + +func TestTomlConfigValid(t *testing.T) { + t.Parallel() + + config := tomlConfig.TomlConfig{ + Chains: tomlConfig.Chains{ + { + Name: "chain", + TendermintNodes: []string{"node"}, + APINodes: []string{"node"}, + Queries: []string{"event.key = 'value'"}, + }, + }, + Reporters: tomlConfig.Reporters{ + { + Name: "test", + Type: "telegram", + TelegramConfig: &tomlConfig.TelegramConfig{ + Chat: 1, + Token: "xxx:yyy", + Admins: []int64{123}, + }, + }, + }, + Subscriptions: tomlConfig.Subscriptions{ + { + Name: "name", + Reporter: "test", + ChainSubscriptions: tomlConfig.ChainSubscriptions{ + {Chain: "chain"}, + }, + }, + }, + Timezone: "Etc/UTC", + } + require.NoError(t, config.Validate()) +}