|
| 1 | +package status_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "math/rand" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + "unicode" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + "go.rtnl.ai/radish/status" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + defaultInvalid = []any{"foo", "123", "INVALID", 257, -1, 3.14, struct{}{}, true, false} |
| 16 | + statusValues = []status.Status{ |
| 17 | + status.StatusUnknown, |
| 18 | + status.StatusPending, |
| 19 | + status.StatusRunning, |
| 20 | + status.StatusSuccess, |
| 21 | + status.StatusFailure, |
| 22 | + status.StatusRetry, |
| 23 | + status.StatusRevoked, |
| 24 | + status.StatusScheduled, |
| 25 | + } |
| 26 | + statusStrings = []string{ |
| 27 | + "unknown", |
| 28 | + "pending", |
| 29 | + "running", |
| 30 | + "success", |
| 31 | + "failure", |
| 32 | + "retry", |
| 33 | + "revoked", |
| 34 | + "scheduled", |
| 35 | + } |
| 36 | +) |
| 37 | + |
| 38 | +const ( |
| 39 | + dbVarcharLimit = 16 |
| 40 | +) |
| 41 | + |
| 42 | +func TestString(t *testing.T) { |
| 43 | + for i, enum := range statusValues { |
| 44 | + require.Equal(t, statusStrings[i], enum.String(), "expected status to have string representation %q, got %q", statusStrings[i], enum.String()) |
| 45 | + } |
| 46 | + |
| 47 | + // Test Zero Values |
| 48 | + zero := status.Status(0) |
| 49 | + require.Equal(t, status.StatusUnknown.String(), zero.String(), "expected status to have string representation \"unknown\" for zero value") |
| 50 | + |
| 51 | + empty, err := status.Parse("") |
| 52 | + require.NoError(t, err, "failed to parse empty string for status") |
| 53 | + require.Equal(t, status.StatusUnknown.String(), empty.String(), "expected status to have string representation \"unknown\" for empty string not %q", empty.String()) |
| 54 | +} |
| 55 | + |
| 56 | +func TestStringBounds(t *testing.T) { |
| 57 | + max := uint8(0) |
| 58 | + min := uint8(255) |
| 59 | + |
| 60 | + for i := range statusValues { |
| 61 | + if uint8(i) > max { |
| 62 | + max = uint8(i) |
| 63 | + } |
| 64 | + if uint8(i) < min { |
| 65 | + min = uint8(i) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + above := status.Status(max + 1) |
| 70 | + require.Equal(t, status.StatusUnknown.String(), above.String(), "expected status to have string representation \"unknown\" for unknown value") |
| 71 | + |
| 72 | + // Test zero value |
| 73 | + if min > 0 { |
| 74 | + zero := status.Status(0) |
| 75 | + require.Equal(t, status.StatusUnknown.String(), zero.String(), "expected status to have string representation \"unknown\" for zero value") |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func TestParse(t *testing.T) { |
| 80 | + t.Run("Valid", func(t *testing.T) { |
| 81 | + makeTestCases := func(i int, enum status.Status) []any { |
| 82 | + tests := make([]any, 0, 8) |
| 83 | + tests = append(tests, statusStrings[i]) |
| 84 | + tests = append(tests, enum.Uint8()) |
| 85 | + tests = append(tests, float64(enum.Uint8())) |
| 86 | + tests = append(tests, enum) |
| 87 | + tests = append(tests, strings.ToUpper(statusStrings[i]), strings.ToLower(statusStrings[i])) |
| 88 | + tests = append(tests, mixedCase(statusStrings[i])) |
| 89 | + |
| 90 | + return tests |
| 91 | + } |
| 92 | + |
| 93 | + for i, enum := range statusValues { |
| 94 | + tests := makeTestCases(i, enum) |
| 95 | + for _, input := range tests { |
| 96 | + actual, err := status.Parse(input) |
| 97 | + require.NoError(t, err, "failed to parse valid status value %#v", input) |
| 98 | + require.Equal(t, enum, actual, "expected parsing valid status value %#v", input) |
| 99 | + } |
| 100 | + } |
| 101 | + }) |
| 102 | + |
| 103 | + t.Run("Invalid", func(t *testing.T) { |
| 104 | + for _, str := range defaultInvalid { |
| 105 | + actual, err := status.Parse(str) |
| 106 | + require.Error(t, err, "expected parsing invalid status value %q to error", str) |
| 107 | + require.Equal(t, uint8(0), actual.Uint8(), "expected parsing invalid status value %q to return zero value, got %d", str, actual.Uint8()) |
| 108 | + } |
| 109 | + }) |
| 110 | +} |
| 111 | + |
| 112 | +func TestJSON(t *testing.T) { |
| 113 | + t.Run("Serialization", func(t *testing.T) { |
| 114 | + for _, enum := range statusValues { |
| 115 | + orig := status.Status(enum.Uint8()) |
| 116 | + data, err := json.Marshal(orig) |
| 117 | + require.NoError(t, err, "failed to marshal status value %q", orig.String()) |
| 118 | + |
| 119 | + cmp := status.Status(0) |
| 120 | + err = json.Unmarshal(data, &cmp) |
| 121 | + require.NoError(t, err, "failed to unmarshal status value %q", orig.String()) |
| 122 | + require.Equal(t, orig, cmp, "unmarshaled status does not match original") |
| 123 | + } |
| 124 | + }) |
| 125 | + |
| 126 | + t.Run("Errors", func(t *testing.T) { |
| 127 | + inputs := make([][]byte, 0, len(defaultInvalid)+2) |
| 128 | + inputs = append(inputs, []byte(`"unquoted`), []byte(`{"missing":}`)) // add bad JSON inputs |
| 129 | + |
| 130 | + // Add parse errors |
| 131 | + for _, v := range defaultInvalid { |
| 132 | + if data, err := json.Marshal(v); err == nil { |
| 133 | + inputs = append(inputs, data) |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + for _, data := range inputs { |
| 138 | + cmp := status.Status(0) |
| 139 | + err := json.Unmarshal(data, &cmp) |
| 140 | + require.Error(t, err, "expected unmarshaling invalid status JSON value %q to error", string(data)) |
| 141 | + require.Equal(t, uint8(0), cmp.Uint8(), "expected unmarshaling invalid status JSON value %q to return zero value, got %d", string(data), cmp.Uint8()) |
| 142 | + } |
| 143 | + }) |
| 144 | +} |
| 145 | + |
| 146 | +func TestDatabase(t *testing.T) { |
| 147 | + // TODO: implement scan and value tests |
| 148 | + t.Run("VARCHAR", func(t *testing.T) { |
| 149 | + // Ensure that all string representations are less than or equal to the db VARCHAR limit |
| 150 | + for _, enum := range statusValues { |
| 151 | + require.LessOrEqual(t, len(enum.String()), dbVarcharLimit, "expected status value %q to be less than or equal to %d characters", enum.String(), dbVarcharLimit) |
| 152 | + } |
| 153 | + }) |
| 154 | +} |
| 155 | + |
| 156 | +func mixedCase(s string) string { |
| 157 | + b := make([]rune, len(s)) |
| 158 | + for i, r := range s { |
| 159 | + // Flip a coin and make the character upper or lower case |
| 160 | + if rand.Intn(2) == 0 { |
| 161 | + r = unicode.ToLower(r) |
| 162 | + } else { |
| 163 | + r = unicode.ToUpper(r) |
| 164 | + } |
| 165 | + b[i] = r |
| 166 | + } |
| 167 | + return string(b) |
| 168 | +} |
0 commit comments