Skip to content

Commit c7429f5

Browse files
committed
test: Add test case for Tag model unmarshaling without tagged_items
1 parent e36f15f commit c7429f5

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed

tests/205_test.go

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
*
3+
# Netbox tag requiring tagged_items on unmarshal while the API doesn't return that information #205
4+
```
5+
> Hello!
6+
>
7+
> https://github.com/netbox-community/go-netbox/blob/master/model_tag.go#L478
8+
>
9+
> The UnmarshalJSON method requires the `tagged_items` property in the JSON from netbox. When creating an object, that field is not shown:
10+
>
11+
>
12+
>
13+
> {
14+
> "id" : 10,
15+
> "url" : "http://localhost:8001/api/extras/tags/10/",
16+
> "display_url" : "http://localhost:8001/extras/tags/10/",
17+
> "display" : "test-tag_basic-vu6hw8lmkf",
18+
> "name" : "test-tag_basic-vu6hw8lmkf",
19+
> "slug" : "test-tag_basic-2xheda2ay7",
20+
> "color" : "112233",
21+
> "description" : "This is a test",
22+
> "object_types" : [ ],
23+
> "created" : "2025-02-27T15:00:25.402848Z",
24+
> "last_updated" : "2025-02-27T15:00:25.402859Z"
25+
> }
26+
>
27+
>
28+
>
29+
>
30+
>
31+
>
32+
>
33+
> I used the following to create a tag:
34+
>
35+
>
36+
>
37+
> api_res, _, err := client.ExtrasAPI.
38+
> ExtrasTagsCreate(ctx).
39+
> TagRequest(*tagRequest).Execute()
40+
>
41+
>
42+
>
43+
>
44+
>
45+
>
46+
```
47+
*
48+
*/
49+
package main
50+
51+
import (
52+
"context"
53+
"testing"
54+
55+
"github.com/netbox-community/go-netbox/v4"
56+
)
57+
58+
type Seed205 struct {
59+
Tags []*netbox.Tag
60+
}
61+
62+
func (s *Seed205) Cleanup(t *testing.T, client *netbox.APIClient) {
63+
t.Helper()
64+
for _, tag := range s.Tags {
65+
t.Logf("Deleting tag %s", tag.Name)
66+
res, err := client.ExtrasAPI.ExtrasTagsDestroy(context.Background(), tag.GetId()).Execute()
67+
if err != nil {
68+
fatalHttp(t, "failed to delete tag", err, res)
69+
}
70+
}
71+
}
72+
73+
func HSeed205(t *testing.T, client *netbox.APIClient) *Seed205 {
74+
t.Helper()
75+
seed := &Seed205{}
76+
seed.Tags = []*netbox.Tag{}
77+
78+
// Create a tag
79+
tagRequest := &netbox.TagRequest{
80+
Name: randString(10),
81+
Slug: randString(10),
82+
Color: "112233",
83+
Description: netbox.PtrString("This is a test"),
84+
}
85+
86+
tag, res, err := client.ExtrasAPI.ExtrasTagsCreate(context.Background()).TagRequest(*tagRequest).Execute()
87+
if err != nil {
88+
fatalHttp(t, "failed to create tag", err, res)
89+
}
90+
seed.Tags = append(seed.Tags, tag)
91+
92+
return seed
93+
}
94+
95+
func Test205(t *testing.T) {
96+
harness := GetHarness(t)
97+
defer harness.Cleanup(t)
98+
client := harness.client
99+
100+
seed := HSeed205(t, harness.client)
101+
harness.AddCleanup(seed)
102+
103+
// Test creating a tag
104+
tagRequest := &netbox.TagRequest{
105+
Name: randString(10),
106+
Slug: randString(10),
107+
Color: "112233",
108+
Description: netbox.PtrString("This is a test"),
109+
}
110+
111+
tag, res, err := client.ExtrasAPI.ExtrasTagsCreate(context.Background()).TagRequest(*tagRequest).Execute()
112+
if err != nil {
113+
fatalHttp(t, "failed to create tag", err, res)
114+
}
115+
seed.Tags = append(seed.Tags, tag)
116+
117+
// Test retrieving a tag
118+
retrievedTag, res, err := client.ExtrasAPI.ExtrasTagsRetrieve(context.Background(), tag.GetId()).Execute()
119+
if err != nil {
120+
fatalHttp(t, "failed to retrieve tag", err, res)
121+
}
122+
123+
if retrievedTag.GetId() != tag.GetId() {
124+
t.Fatalf("expected tag ID %d, got %d", tag.GetId(), retrievedTag.GetId())
125+
}
126+
127+
// Test listing tags
128+
tags, res, err := client.ExtrasAPI.ExtrasTagsList(context.Background()).Limit(10).Execute()
129+
if err != nil {
130+
fatalHttp(t, "failed to list tags", err, res)
131+
}
132+
133+
if len(tags.Results) < 1 {
134+
t.Fatalf("expected at least 1 tag, got %d", len(tags.Results))
135+
}
136+
}

0 commit comments

Comments
 (0)