Skip to content

test: unit test to document validation behavior of parameters #387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 30, 2025

Conversation

Emyrk
Copy link
Member

@Emyrk Emyrk commented Apr 30, 2025

Locking in the behavior of validations before I change things. My goal is to move all validation logic into this provider. At present, some of it is done in coder/coder.

If you see #381, adding more strict validation would be a regression in behavior. So I might have to add 2 validation modes so that template import can still be done with relaxed valiation.

Regardless, this test will help illustrate what I change when I add stricter validation.

I confirmed this test passes on a commit 3 weeks ago, before any of my refactors.

Some things we should probably fix or document:

  • The default value can be invalid if an input value is valid. So the default is not checked.
  • The value input does not need to be in the optionset, this is enforced in coder/coder

NOTE: Monotonic is not yet checked, it should be added into the terraform

@Emyrk Emyrk marked this pull request as ready for review April 30, 2025 16:02
Comment on lines +1055 to 1074
}, {
Name: "ValidListOfStrings",
Type: "list(string)",
Value: `["first","second","third"]`,
MinDisabled: true,
MaxDisabled: true,
}, {
Name: "InvalidListOfStrings",
Type: "list(string)",
Value: `["first","second","third"`,
MinDisabled: true,
MaxDisabled: true,
Error: regexp.MustCompile("is not valid list of strings"),
}, {
Name: "EmptyListOfStrings",
Type: "list(string)",
Value: `[]`,
MinDisabled: true,
MaxDisabled: true,
}} {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some extra test cases down here too

Comment on lines +771 to +831
for _, line := range lines[2:] {
columns := strings.Split(line, "|")
columns = columns[1 : len(columns)-1]
for i := range columns {
// Trim the whitespace from all columns
columns[i] = strings.TrimSpace(columns[i])
}

if columns[0] == "" {
continue // Skip rows with empty names
}

optional, err := strconv.ParseBool(columns[8])
if columns[8] != "" {
// Value does not matter if not specified
require.NoError(t, err)
}

var rerr *regexp.Regexp
if columns[9] != "" {
rerr, err = regexp.Compile(columns[9])
if err != nil {
t.Fatalf("failed to parse error column %q: %v", columns[9], err)
}
}
var options []string
if columns[4] != "" {
options = strings.Split(columns[4], ",")
}

var validation *provider.Validation
if columns[5] != "" {
// Min-Max validation should look like:
// 1-10 :: min=1, max=10
// -10 :: max=10
// 1- :: min=1
if validMinMax.MatchString(columns[5]) {
parts := strings.Split(columns[5], "-")
min, _ := strconv.ParseInt(parts[0], 10, 64)
max, _ := strconv.ParseInt(parts[1], 10, 64)
validation = &provider.Validation{
Min: int(min),
MinDisabled: parts[0] == "",
Max: int(max),
MaxDisabled: parts[1] == "",
Monotonic: "",
Regex: "",
Error: "{min} < {value} < {max}",
}
} else {
validation = &provider.Validation{
Min: 0,
MinDisabled: true,
Max: 0,
MaxDisabled: true,
Monotonic: "",
Regex: columns[5],
Error: "regex error",
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Extract this to a testutil package maybe?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it cannot be reused outside the test, I am going to leave it here.

Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments below, but these are optional for a new unit test.

@Emyrk Emyrk merged commit ac2f9bf into main Apr 30, 2025
6 checks passed
@Emyrk Emyrk deleted the stevenmasley/validation_tests branch April 30, 2025 19:30
@github-actions github-actions bot locked and limited conversation to collaborators Apr 30, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants