Skip to content

Commit b54f359

Browse files
committed
Merge branch 'test/#204'
2 parents d2e3ae3 + 1b35bf3 commit b54f359

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed

tests/205_test.go

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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+
Description: netbox.PtrString("This is a test"),
83+
}
84+
85+
tag, res, err := client.ExtrasAPI.ExtrasTagsCreate(context.Background()).TagRequest(*tagRequest).Execute()
86+
if err != nil {
87+
fatalHttp(t, "failed to create tag", err, res)
88+
}
89+
seed.Tags = append(seed.Tags, tag)
90+
91+
return seed
92+
}
93+
94+
func Test205(t *testing.T) {
95+
harness := GetHarness(t)
96+
defer harness.Cleanup(t)
97+
client := harness.client
98+
99+
seed := HSeed205(t, harness.client)
100+
harness.AddCleanup(seed)
101+
102+
// Test creating a tag
103+
tagRequest := &netbox.TagRequest{
104+
Name: randString(10),
105+
Slug: randString(10),
106+
Description: netbox.PtrString("This is a test"),
107+
}
108+
109+
tag, res, err := client.ExtrasAPI.ExtrasTagsCreate(context.Background()).TagRequest(*tagRequest).Execute()
110+
if err != nil {
111+
fatalHttp(t, "failed to create tag", err, res)
112+
}
113+
seed.Tags = append(seed.Tags, tag)
114+
115+
// Test retrieving a tag
116+
retrievedTag, res, err := client.ExtrasAPI.ExtrasTagsRetrieve(context.Background(), tag.GetId()).Execute()
117+
if err != nil {
118+
fatalHttp(t, "failed to retrieve tag", err, res)
119+
}
120+
121+
if retrievedTag.GetId() != tag.GetId() {
122+
t.Fatalf("expected tag ID %d, got %d", tag.GetId(), retrievedTag.GetId())
123+
}
124+
125+
// Test listing tags
126+
tags, res, err := client.ExtrasAPI.ExtrasTagsList(context.Background()).Limit(10).Execute()
127+
if err != nil {
128+
fatalHttp(t, "failed to list tags", err, res)
129+
}
130+
131+
if len(tags.Results) < 1 {
132+
t.Fatalf("expected at least 1 tag, got %d", len(tags.Results))
133+
}
134+
}

0 commit comments

Comments
 (0)