File: floating_ip_test.go

package info (click to toggle)
golang-github-hetznercloud-hcloud-go 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,072 kB
  • sloc: sh: 5; makefile: 2
file content (54 lines) | stat: -rw-r--r-- 1,192 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package schema

import (
	"bytes"
	"encoding/json"
	"testing"
)

func TestFloatingIPCreateRequest(t *testing.T) {
	var (
		oneLabel    = map[string]string{"foo": "bar"}
		nilLabels   map[string]string
		emptyLabels = map[string]string{}
	)

	testCases := []struct {
		name string
		in   FloatingIPCreateRequest
		out  []byte
	}{
		{
			name: "no labels",
			in:   FloatingIPCreateRequest{Type: "ipv4"},
			out:  []byte(`{"type":"ipv4"}`),
		},
		{
			name: "one label",
			in:   FloatingIPCreateRequest{Type: "ipv4", Labels: &oneLabel},
			out:  []byte(`{"type":"ipv4","labels":{"foo":"bar"}}`),
		},
		{
			name: "nil labels",
			in:   FloatingIPCreateRequest{Type: "ipv4", Labels: &nilLabels},
			out:  []byte(`{"type":"ipv4","labels":null}`),
		},
		{
			name: "empty labels",
			in:   FloatingIPCreateRequest{Type: "ipv4", Labels: &emptyLabels},
			out:  []byte(`{"type":"ipv4","labels":{}}`),
		},
	}

	for _, testCase := range testCases {
		t.Run(testCase.name, func(t *testing.T) {
			data, err := json.Marshal(testCase.in)
			if err != nil {
				t.Fatal(err)
			}
			if !bytes.Equal(data, testCase.out) {
				t.Fatalf("output %s does not match %s", data, testCase.out)
			}
		})
	}
}