Skip to content

Commit

Permalink
Add unit test for adding devices
Browse files Browse the repository at this point in the history
  • Loading branch information
danesparza committed Aug 23, 2018
1 parent 248b31e commit 926dfe7
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions data/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,3 +487,44 @@ func TestConfig_GetAllDevices_NoItems_Successful(t *testing.T) {
t.Errorf("GetAllDevices failed: Should have returned no devices but returned %v instead", len(response))
}
}

func TestConfig_GetAllDevices_WithItems_Successful(t *testing.T) {
// Arrange
filename := "testing.db"
defer os.Remove(filename)
defer viper.Reset()

db := data.ConfigDB{
Database: filename}

// Try adding some devices:
db.AddOrUpdateDevice(data.Device{
Name: "Unit test 1",
Type: "system",
})

db.AddOrUpdateDevice(data.Device{
Name: "Unit test 2",
Type: "hs110",
IPAddress: "192.168.1.99",
})

db.AddOrUpdateDevice(data.Device{
Name: "Unit test 2",
Type: "hs110",
IPAddress: "192.168.1.85",
})
numberOfItems := 3

// Act
response, err := db.GetAllDevices()

// Assert
if err != nil {
t.Errorf("GetAllDevices failed: Should have returned config items without error: %s", err)
}

if len(response) != numberOfItems {
t.Errorf("GetAllDevices failed: Should have returned %d config items but returned %v instead", numberOfItems, len(response))
}
}

0 comments on commit 926dfe7

Please sign in to comment.