debian-forge-composer/internal/common/states_test.go
Martin Sehnoutka 62d186cd1b common: refactor build states into a separate file
These states will be used for tracking the image builds and compose
states in the rest of our codebase. There should be no change in the
behavior. It is a 1 to 1 replacement with the only difference of using
type alias instead of plain string.
2020-02-12 11:17:26 +01:00

59 lines
1.2 KiB
Go

package common
import (
"encoding/json"
"reflect"
"testing"
)
func TestJSONConversions(t *testing.T) {
type TestJson struct {
Ibs ImageBuildState `json:"ibs"`
Cs ComposeState `json:"cs"`
}
typedCases := []TestJson{
{
Ibs: IBWaiting,
Cs: CWaiting,
},
{
Ibs: IBRunning,
Cs: CFailed,
},
}
strCases := []string{
`{"ibs": "WAITING", "cs": "WAITING"}`,
`{"ibs": "RUNNING", "cs": "FAILED"}`,
}
for n, c := range strCases {
var inputStringAsStruct *TestJson
err := json.Unmarshal([]byte(c), &inputStringAsStruct)
if err != nil {
t.Fatal("Failed to unmarshal:", err)
}
if reflect.DeepEqual(inputStringAsStruct, typedCases[n]) {
t.Error("Unmarshaled compose request is not the one expected")
}
}
var byteArrays [][]byte
for _, c := range typedCases {
data, err := json.Marshal(c)
if err != nil {
t.Fatal("Failed to marshal state:", err)
}
byteArrays = append(byteArrays, data)
}
for n, b := range byteArrays {
var inputStringAsStruct *TestJson
err := json.Unmarshal(b, &inputStringAsStruct)
if err != nil {
t.Fatal("Failed to unmarshal:", err)
}
if reflect.DeepEqual(inputStringAsStruct, typedCases[n]) {
t.Error("Unmarshaled compose request is not the one expected")
}
}
}