diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 3124575..6986b36 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -20,6 +20,9 @@ test: stage: test variables: GIN_MODE: debug + INFLUXDB_DB: db + services: + - influxdb script: - go get -v -t ./... - go test -v -coverprofile=$CI_PROJECT_DIR/coverage.out diff --git a/influx_test.go b/influx_test.go new file mode 100644 index 0000000..dcce4f7 --- /dev/null +++ b/influx_test.go @@ -0,0 +1,24 @@ +package main + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_WriteStateToInflux(t *testing.T) { + state := State{Temp: 32} + + // Test failure with empty config + err := WriteStateToInflux(state, Influx{}) + assert.NotEqual(t, nil, err) + + // Test failure with missing database + err = WriteStateToInflux(state, Influx{Addr: "http://127.0.0.1:8086"}) + assert.NotEqual(t, nil, err) + + // Test success with writing to database + config := Influx{Addr: "http://127.0.0.1:8086", Database: "db"} + err = WriteStateToInflux(state, config) + assert.Equal(t, nil, err) +} diff --git a/web_test.go b/web_test.go index 78f4f6a..75afc92 100644 --- a/web_test.go +++ b/web_test.go @@ -256,3 +256,35 @@ func Test_GetGinAccounts(t *testing.T) { assert.Equal(t, testUsers, actualUsers) } + +func Test_reloadWebConfig(t *testing.T) { + // Save zero-valued config + testConfig := Config{ + Sensors: []Sensor{}, + Users: []User{}, + ListenAddr: ":8080", + } + + newConfig := Config{ + Sensors: []Sensor{}, + Users: []User{}, + BaseURL: "http://localhost:8080", + ListenAddr: ":8080", + } + + // Create a temp file to store newConfig + tmpfile, err := ioutil.TempFile("", "tempgopher") + assert.Equal(t, nil, err) + defer os.Remove(tmpfile.Name()) // Remove the tempfile when done + SaveConfig(tmpfile.Name(), newConfig) + + // Test that newConfig is copied to testConfig + err = reloadWebConfig(&testConfig, tmpfile.Name()) + assert.Equal(t, nil, err) + assert.Equal(t, testConfig, newConfig) + + // Test the error case + err = reloadWebConfig(&testConfig, "/does/not/exist") + assert.NotEqual(t, nil, err) + +}