Skip to content

Commit

Permalink
tests: added some tests for NestedModelField
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredhendrickson13 committed Nov 22, 2023
1 parent feb71f4 commit ae78972
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ class NestedModelField extends Field
private string $model_class_short;
public Model $model;
private ModelSet $validation_modelset;

/**
* @param string $model_class The Model class that is nested within this Field.
* @param bool $required Set to `true` if this field is required to be explicitly set. $required and $default are
* mutually exclusive.
* @param mixed|null $default The default value to assign this field if no value was found. If not set, it assumed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class DNSResolverHostOverride extends Model
$this->aliases = new NestedModelField(
model_class: "DNSResolverHostOverrideAlias",
default: [],
allow_empty: true,
help_text: "Additional alias hostnames that should resolve the same IP(s)."
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,95 @@
namespace API\Tests;

use API\Core\TestCase;
use API\Fields\NestedModelField;

class APIFieldsNestedModelFieldTestCase extends TestCase
{
/**
* Ensures NestedModelField objects are constructed with the necessary properties.
*/
public function test_construct() {
# Create a NestedModelObject to test with
$field = new NestedModelField("FirewallAlias", required: true);

# Ensure the nested Model object is created and matches the assigned `model_class`
$this->assert_equals($field->model_class, "\\API\\Models\\FirewallAlias");
$this->assert_equals($field->model_class, "\\".get_class($field->model));

# Ensure this field inherits its `many` value from the nested Model's `many` value
$this->assert_equals($field->many, $field->model->many);

# Ensure the internal_name and internal_namespace derive from the nested Model's config path
$this->assert_equals($field->internal_name, "alias");
$this->assert_equals($field->internal_namespace, "aliases");
}

/**
* Ensures that the NestedModelField `validate_extra()` method performs the nested Model's `validate()` method.
*/
public function test_nested_validate() {
# Create a NestedModelObject to test with
$field = new NestedModelField("FirewallAlias", required: true);

# Leave out the required `name` parameter and ensure the NestedModelField `validate_extra()` fails
$this->assert_throws_response(
response_id: "FIELD_IS_REQUIRED",
code: 400,
callable: function () use ($field) {
$field->validate_extra(["not_name" => "test"]);
}
);

# Set an invalid `type` choice and ensure the NestedModelField catches it
$this->assert_throws_response(
response_id: "FIELD_INVALID_CHOICE",
code: 400,
callable: function () use ($field) {
$field->validate_extra(["name" => "test", "type" => "not an alias type"]);
}
);
}

/**
* Ensures `unique` validation works for NestedModelFields.
*/
public function test_unique_validation() {
# Create a NestedModelObject to test with
$field = new NestedModelField("FirewallAlias", required: true);
$field->name = "test";

$this->assert_throws_response(
response_id: "MODEL_FIELD_MUST_BE_UNIQUE",
code: 400,
callable: function () use ($field) {
$field->value = [
["name" => "thismustbeunique", "type" => "host"],
["name" => "thismustbeunique", "type" => "host"]
];
$field->validate();
}
);
}

/**
* Ensures `unique_together` validation works for NestedModelFields.
*/
public function test_unique_together_validation() {
# Create a NestedModelObject to test with. Use the DNSResolverHostOverride model for this test since its `host`
# and `domain` fields must be unique together.
$field = new NestedModelField("DNSResolverHostOverride", required: true);
$field->name = "test";

$this->assert_throws_response(
response_id: "MODEL_FIELDS_NOT_UNIQUE_TOGETHER",
code: 400,
callable: function () use ($field) {
$field->value = [
["host" => "mustbeuniquewithdomain", "domain" => "mustbeuniquewithhost", "ip" => ["127.0.0.1"]],
["host" => "mustbeuniquewithdomain", "domain" => "mustbeuniquewithhost", "ip" => ["127.0.0.2"]],
];
$field->validate();
}
);
}
}

0 comments on commit ae78972

Please sign in to comment.