configtest

package
v0.0.0-...-93b1e75 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 27, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package configtest is used as a helper in tests that need configuration service implementing config.Interface.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoData error is returned when predefined configuration data is nil.
	ErrNoData = errors.New("no data")

	// ErrNoSuchKey error is returned when requested key is not exists in predefined configuration data.
	ErrNoSuchKey = errors.New("no such key")

	// ErrOutputNotPointer error is returned when provided output parameter is not a pointer.
	ErrOutputNotPointer = errors.New("output must be a pointer")
)
View Source
var ExpectedSampleConfig = SampleConfigType{
	Name: "Peter",
	Profile: UserProfile{
		Sex:     "m",
		Age:     32,
		Married: true,
		Children: []ChildProfile{
			{Name: "George", Weight: 5.4, Age: 5},
			{Name: "Olivia", Weight: 12.2, Age: 12},
		},
	},
}

ExpectedSampleConfig variable contains expected result of configuration parsing.

View Source
var ExpectedSampleRawDataJSON = map[string]interface{}{
	"name": "Peter",
	"profile": map[string]interface{}{
		"sex":     "m",
		"age":     32.,
		"married": true,
		"children": []interface{}{
			map[string]interface{}{"name": "George", "weight": 5.4, "age": 5.},
			map[string]interface{}{"name": "Olivia", "weight": 12.2, "age": 12.},
		},
	},
}

ExpectedSampleRawDataJSON variable contains expected raw result of configuration parsing by JSON encoder.

View Source
var ExpectedSampleRawDataTOML = map[string]interface{}{
	"name": "Peter",
	"profile": map[string]interface{}{
		"sex":     "m",
		"age":     int64(32),
		"married": true,
		"children": []map[string]interface{}{
			{"name": "George", "weight": 5.4, "age": int64(5)},
			{"name": "Olivia", "weight": 12.2, "age": int64(12)},
		},
	},
}

ExpectedSampleRawDataTOML variable contains expected raw result of configuration parsing by TOML encoder.

View Source
var ExpectedSampleRawDataYAML = map[string]interface{}{
	"name": "Peter",
	"profile": map[string]interface{}{
		"sex":     "m",
		"age":     32,
		"married": true,
		"children": []interface{}{
			map[string]interface{}{"name": "George", "weight": 5.4, "age": 5},
			map[string]interface{}{"name": "Olivia", "weight": 12.2, "age": 12},
		},
	},
}

ExpectedSampleRawDataYAML variable contains expected raw result of configuration parsing by YAML encoder.

View Source
var SampleConfigDataJSON = []byte(`{
  "name": "Peter",
  "profile": {
    "sex": "m",
    "age": 32,
    "married": true,
    "children": [
      { "name": "George", "weight": 5.4, "age": 5},
      { "name": "Olivia", "weight": 12.2, "age": 12}
    ]
  }
}`)

SampleConfigDataJSON variable contains sample configuration as raw JSON data.

View Source
var SampleConfigDataTOML = []byte(`
name = "Peter"

[profile]
sex = "m"
age = 32
married = true

[[profile.children]]
name = "George"
weight = 5.4
age = 5

[[profile.children]]
name = "Olivia"
weight = 12.2
age = 12
`)

SampleConfigDataTOML variable contains sample configuration as raw TOML data.

View Source
var SampleConfigDataWrongJSON = []byte(`{
  "name": "Peter",
`)

SampleConfigDataWrongJSON variable contains sample wrong JSON data.

View Source
var SampleConfigDataWrongTOML = []byte(`
name = Peter
`)

SampleConfigDataWrongTOML variable contains sample wrong TOML data.

View Source
var SampleConfigDataWrongYAML = []byte(`
name: - Peter
`)

SampleConfigDataWrongYAML variable contains sample wrong YAML data.

View Source
var SampleConfigDataYAML = []byte(`
name: Peter
profile:
  sex: m
  age: 32
  married: true
  children:
    - name: George
      weight: 5.4
      age: 5
    - name: Olivia
      weight: 12.2
      age: 12
`)

SampleConfigDataYAML variable contains sample configuration as raw YAML data.

Functions

func CleanEtcd

func CleanEtcd(t *testing.T, key string)

CleanEtcd function clears provided key to restore etcd to its original state.

func SetupEtcd

func SetupEtcd(t *testing.T, key string)

SetupEtcd function prepares etcd for testing by setting provided key to sample config JSON data.

func SetupEtcdWrong

func SetupEtcdWrong(t *testing.T, key string)

SetupEtcdWrong function prepares etcd for testing by setting provided key to sample wrong config JSON data.

func TestSampleConfig

func TestSampleConfig(t *testing.T, cfg *config.Config, expectedRawData map[string]interface{})

TestSampleConfig function can be used to test provided configuration against expected raw data.

Types

type ChildProfile

type ChildProfile struct {
	Name   string
	Weight float32
	Age    int8
}

ChildProfile structure used as inner aggregate type sample configuration.

type Config

type Config struct {
	// contains filtered or unexported fields
}

Config structure implementing config.Interface.

func New

func New(data map[string]interface{}) *Config

New function creates configuration service using static predefined map of data.

func (*Config) Get

func (config *Config) Get(out interface{}) error

Get method fills structure that 'out' parameter points to with predefined data under empty key. It will return an error if 'out' is not a pointer or predefined data is nil. If empty key contains an error that error will be returned.

func (*Config) GetByKey

func (config *Config) GetByKey(key string, out interface{}) error

GetByKey method fills structure that 'out' parameter points to with predefined data under the provided key. It will return an error if 'out' is not a pointer or predefined data is nil or there is no such key in that data. If corresponding key contains an error that error will be returned.

func (*Config) IsNoSuchKeyError

func (config *Config) IsNoSuchKeyError(err error) bool

IsNoSuchKeyError checks if provided error is 'NoSuchKey' one.

type SampleConfigType

type SampleConfigType struct {
	Name    string
	Profile UserProfile
}

SampleConfigType structure used for sample configuration.

type UserProfile

type UserProfile struct {
	Sex      string
	Age      int
	Married  bool
	Children []ChildProfile
}

UserProfile structure used as inner type in sample configuration.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL