|
| 1 | +package unit |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/linode/linodego" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | +) |
| 11 | + |
| 12 | +func TestListTags(t *testing.T) { |
| 13 | + // Load the fixture data for tags |
| 14 | + fixtureData, err := fixtures.GetFixture("tags_list") |
| 15 | + assert.NoError(t, err) |
| 16 | + |
| 17 | + var base ClientBaseCase |
| 18 | + base.SetUp(t) |
| 19 | + defer base.TearDown(t) |
| 20 | + |
| 21 | + base.MockGet("tags", fixtureData) |
| 22 | + |
| 23 | + tags, err := base.Client.ListTags(context.Background(), &linodego.ListOptions{}) |
| 24 | + assert.NoError(t, err) |
| 25 | + |
| 26 | + assert.NotEmpty(t, tags, "Expected non-empty tag list") |
| 27 | + assert.Equal(t, "example-tag", tags[0].Label, "Expected tag label to be 'example-tag'") |
| 28 | +} |
| 29 | + |
| 30 | +func TestCreateTag(t *testing.T) { |
| 31 | + // Load the fixture data for tag creation |
| 32 | + fixtureData, err := fixtures.GetFixture("tag_create") |
| 33 | + assert.NoError(t, err) |
| 34 | + |
| 35 | + var base ClientBaseCase |
| 36 | + base.SetUp(t) |
| 37 | + defer base.TearDown(t) |
| 38 | + |
| 39 | + base.MockPost("tags", fixtureData) |
| 40 | + |
| 41 | + opts := linodego.TagCreateOptions{ |
| 42 | + Label: "new-tag", |
| 43 | + } |
| 44 | + |
| 45 | + tag, err := base.Client.CreateTag(context.Background(), opts) |
| 46 | + assert.NoError(t, err, "Expected no error when creating tag") |
| 47 | + |
| 48 | + // Verify the created tag's label |
| 49 | + assert.Equal(t, "new-tag", tag.Label, "Expected created tag label to match input") |
| 50 | +} |
| 51 | + |
| 52 | + |
| 53 | +func TestDeleteTag(t *testing.T) { |
| 54 | + var base ClientBaseCase |
| 55 | + base.SetUp(t) |
| 56 | + defer base.TearDown(t) |
| 57 | + |
| 58 | + tagLabel := "delete-tag" |
| 59 | + base.MockDelete(fmt.Sprintf("tags/%s", tagLabel), nil) |
| 60 | + |
| 61 | + err := base.Client.DeleteTag(context.Background(), tagLabel) |
| 62 | + assert.NoError(t, err, "Expected no error when deleting tag") |
| 63 | +} |
| 64 | + |
| 65 | +func TestListTaggedObjects(t *testing.T) { |
| 66 | + // Load the fixture data for tagged objects |
| 67 | + fixtureData, err := fixtures.GetFixture("tagged_objects_list") |
| 68 | + assert.NoError(t, err) |
| 69 | + |
| 70 | + var base ClientBaseCase |
| 71 | + base.SetUp(t) |
| 72 | + defer base.TearDown(t) |
| 73 | + |
| 74 | + tagLabel := "example-tag" |
| 75 | + base.MockGet(fmt.Sprintf("tags/%s", tagLabel), fixtureData) |
| 76 | + |
| 77 | + taggedObjects, err := base.Client.ListTaggedObjects(context.Background(), tagLabel, &linodego.ListOptions{}) |
| 78 | + assert.NoError(t, err) |
| 79 | + |
| 80 | + assert.NotEmpty(t, taggedObjects, "Expected non-empty tagged objects list") |
| 81 | + assert.Equal(t, "linode", taggedObjects[0].Type, "Expected tagged object type to be 'linode'") |
| 82 | +} |
| 83 | + |
| 84 | +func TestSortedObjects(t *testing.T) { |
| 85 | + // Load the fixture data for tagged objects |
| 86 | + fixtureData, err := fixtures.GetFixture("tagged_objects_list") |
| 87 | + assert.NoError(t, err) |
| 88 | + |
| 89 | + var base ClientBaseCase |
| 90 | + base.SetUp(t) |
| 91 | + defer base.TearDown(t) |
| 92 | + |
| 93 | + tagLabel := "example-tag" |
| 94 | + base.MockGet(fmt.Sprintf("tags/%s", tagLabel), fixtureData) |
| 95 | + |
| 96 | + taggedObjects, err := base.Client.ListTaggedObjects(context.Background(), tagLabel, &linodego.ListOptions{}) |
| 97 | + assert.NoError(t, err) |
| 98 | + |
| 99 | + sortedObjects, err := taggedObjects.SortedObjects() |
| 100 | + assert.NoError(t, err) |
| 101 | + |
| 102 | + assert.NotEmpty(t, sortedObjects.Instances, "Expected non-empty instances list in sorted objects") |
| 103 | + assert.Equal(t, "example-instance", sortedObjects.Instances[0].Label, "Expected instance label to be 'example-instance'") |
| 104 | +} |
0 commit comments