File: payload_test.go

package info (click to toggle)
golang-github-juju-names 4.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, trixie
  • size: 340 kB
  • sloc: makefile: 14
file content (93 lines) | stat: -rw-r--r-- 2,447 bytes parent folder | download
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2015 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.

package names_test

import (
	"github.com/juju/names/v4"
	jc "github.com/juju/testing/checkers"
	gc "gopkg.in/check.v1"
)

var _ = gc.Suite(&payloadSuite{})

type payloadSuite struct{}

type payloadTest struct {
	input string
}

func checkPayload(c *gc.C, id string, tag names.PayloadTag) {
	c.Check(tag.Kind(), gc.Equals, names.PayloadTagKind)
	c.Check(tag.Id(), gc.Equals, id)
	c.Check(tag.String(), gc.Equals, names.PayloadTagKind+"-"+id)
}

func (s *payloadSuite) TestPayloadTag(c *gc.C) {
	id := "f47ac10b-58cc-4372-a567-0e02b2c3d479"
	tag := names.NewPayloadTag(id)

	c.Check(tag.Kind(), gc.Equals, names.PayloadTagKind)
	c.Check(tag.Id(), gc.Equals, id)
	c.Check(tag.String(), gc.Equals, names.PayloadTagKind+"-"+id)
}

func (s *payloadSuite) TestIsValidPayload(c *gc.C) {
	for i, test := range []struct {
		id     string
		expect bool
	}{
		{"", false},
		{"spam-", false},
		{"spam", true},
		{"spam-and-eggs", true},

		{"f47ac10b-58cc-4372-a567-0e02b2c3d479", true},
	} {
		c.Logf("test %d: %s", i, test.id)
		ok := names.IsValidPayload(test.id)

		c.Check(ok, gc.Equals, test.expect)
	}
}

func (s *payloadSuite) TestParsePayloadTag(c *gc.C) {
	for i, test := range []struct {
		tag      string
		expected names.Tag
		err      error
	}{{
		tag: "",
		err: names.InvalidTagError("", ""),
	}, {
		tag: "payload-",
		err: names.InvalidTagError("payload-", names.PayloadTagKind),
	}, {
		tag:      "payload-spam",
		expected: names.NewPayloadTag("spam"),
	}, {
		tag:      "payload-f47ac10b-58cc-4372-a567-0e02b2c3d479",
		expected: names.NewPayloadTag("f47ac10b-58cc-4372-a567-0e02b2c3d479"),
	}, {
		tag: "spam",
		err: names.InvalidTagError("spam", ""),
	}, {
		tag: "f47ac10b-58cc-4372-a567-0e02b2c3d479",
		err: names.InvalidTagError("f47ac10b-58cc-4372-a567-0e02b2c3d479", ""),
	}, {
		tag: "unit-f47ac10b-58cc-4372-a567-0e02b2c3d479",
		err: names.InvalidTagError("unit-f47ac10b-58cc-4372-a567-0e02b2c3d479", names.UnitTagKind),
	}, {
		tag: "action-f47ac10b-58cc-4372-a567-0e02b2c3d479",
		err: names.InvalidTagError("action-f47ac10b-58cc-4372-a567-0e02b2c3d479", names.PayloadTagKind),
	}} {
		c.Logf("test %d: %s", i, test.tag)
		got, err := names.ParsePayloadTag(test.tag)
		if test.err != nil {
			c.Check(err, jc.DeepEquals, test.err)
		} else {
			c.Check(err, jc.ErrorIsNil)
			c.Check(got, jc.DeepEquals, test.expected)
		}
	}
}