github.com/martinohmann/rfoutlet@v1.2.1-0.20220707195255-8a66aa411105/internal/config/config_test.go (about)

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/martinohmann/rfoutlet/internal/outlet"
     9  	"github.com/martinohmann/rfoutlet/internal/schedule"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestLoad(t *testing.T) {
    15  	c, err := Load("testdata/full.yaml")
    16  
    17  	require.NoError(t, err)
    18  	assert.Equal(t, "0.0.0.0:1234", c.ListenAddress)
    19  	require.Len(t, c.OutletGroups, 2)
    20  	assert.Len(t, c.OutletGroups[0].Outlets, 2)
    21  	assert.Len(t, c.OutletGroups[1].Outlets, 1)
    22  }
    23  
    24  func TestLoadWithDefaults(t *testing.T) {
    25  	c, err := LoadWithDefaults("testdata/partial.yaml")
    26  
    27  	require.NoError(t, err)
    28  	assert.Equal(t, DefaultConfig.ListenAddress, c.ListenAddress)
    29  	assert.Empty(t, c.StateFile)
    30  	assert.Equal(t, uint(42), c.GPIO.ReceivePin)
    31  	assert.Equal(t, DefaultConfig.GPIO.TransmitPin, c.GPIO.TransmitPin)
    32  	require.Len(t, c.OutletGroups, 2)
    33  }
    34  
    35  func TestLoadInvalid(t *testing.T) {
    36  	_, err := Load("testdata/invalid.yml")
    37  	assert.Error(t, err)
    38  }
    39  
    40  func TestLoadNonexistent(t *testing.T) {
    41  	_, err := Load("testdata/idonotexist.yml")
    42  	assert.Error(t, err)
    43  }
    44  
    45  func TestLoadWithReader(t *testing.T) {
    46  	cfg := `
    47  outletGroups:
    48    - id: foo
    49      displayName: Foo`
    50  
    51  	r := strings.NewReader(cfg)
    52  	c, err := LoadWithReader(r)
    53  	require.NoError(t, err)
    54  	require.Len(t, c.OutletGroups, 1)
    55  	assert.Equal(t, "foo", c.OutletGroups[0].ID)
    56  	assert.Equal(t, "Foo", c.OutletGroups[0].DisplayName)
    57  }
    58  
    59  type errorReader struct{}
    60  
    61  func (errorReader) Read(p []byte) (n int, err error) {
    62  	return 1, fmt.Errorf("error")
    63  }
    64  
    65  func TestLoadWithBadReader(t *testing.T) {
    66  	_, err := LoadWithReader(errorReader{})
    67  	assert.Error(t, err)
    68  }
    69  
    70  func TestConfig_BuildOutletGroups(t *testing.T) {
    71  	config := Config{
    72  		GPIO: GPIOConfig{
    73  			DefaultProtocol:    1,
    74  			DefaultPulseLength: 123,
    75  		},
    76  		OutletGroups: []OutletGroupConfig{
    77  			{
    78  				ID:          "foo",
    79  				DisplayName: "Foo",
    80  				Outlets: []OutletConfig{
    81  					{
    82  						ID:      "bar",
    83  						CodeOn:  1,
    84  						CodeOff: 2,
    85  					},
    86  				},
    87  			},
    88  		},
    89  	}
    90  
    91  	expected := []*outlet.Group{
    92  		{
    93  			ID:          "foo",
    94  			DisplayName: "Foo",
    95  			Outlets: []*outlet.Outlet{
    96  				{
    97  					ID:          "bar",
    98  					DisplayName: "bar",
    99  					CodeOn:      1,
   100  					CodeOff:     2,
   101  					Schedule:    schedule.New(),
   102  					Protocol:    1,
   103  					PulseLength: 123,
   104  				},
   105  			},
   106  		},
   107  	}
   108  
   109  	assert.Equal(t, expected, config.BuildOutletGroups())
   110  }