Skip to content

Commit

Permalink
Add tests for disabling constraint validation
Browse files Browse the repository at this point in the history
  • Loading branch information
prakanth97 committed Jul 22, 2024
1 parent f254072 commit 0bbdaa6
Showing 1 changed file with 139 additions and 11 deletions.
150 changes: 139 additions & 11 deletions ballerina/tests/constraint_annotation_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public type ValidationPerson record {|
option: {
value: "PAST",
message: "Date of birth should be past value"
},
},
message: "Invalid date found for date of birth"
}
time:Date dob;
Expand Down Expand Up @@ -87,9 +87,9 @@ function testValidConstraintAnnotationForParseString() returns error? {
test:assertEquals(person.dob.month, 12);
test:assertEquals(person.dob.day, 31);
test:assertEquals(person.family.id, 2221);
test:assertEquals(person.family.members.length(), 2);
test:assertEquals(person.family.members[0], "John");
test:assertEquals(person.family.members[1], "Doe");
test:assertEquals(person.family.members.length(), 2);
test:assertEquals(person.family.members[0], "John");
test:assertEquals(person.family.members[1], "Doe");
}

@constraint:Array {
Expand All @@ -108,7 +108,7 @@ public type ValidationItem record {|
function testInvalidConstraintAnnotationForParseString(string sourceData, typedesc<record {}> expType, string expectedError) {
anydata|Error err = parseString(sourceData, {}, expType);
test:assertEquals(err is Error, true);
test:assertEquals((<error> err).message(), expectedError);
test:assertEquals((<error>err).message(), expectedError);
}

function invalidConstraintAnnotation() returns [string, typedesc<record {}>, string][] {
Expand Down Expand Up @@ -196,7 +196,7 @@ function invalidConstraintAnnotation() returns [string, typedesc<record {}>, str
`,
ValidationPerson,
"Validation failed for '$.height:maxFractionDigits' constraint(s)."
],
],
[
string `
<Person>
Expand Down Expand Up @@ -254,6 +254,70 @@ function invalidConstraintAnnotation() returns [string, typedesc<record {}>, str
];
}

@test:Config {
groups: ["constraint-validation"],
dataProvider: disableConstraintValidation
}
function testDisableConstraintValidationForParseString(string sourceData, typedesc<record {}> expType, anydata result) returns error? {
anydata err = check parseString(sourceData, {enableConstraintValidation: false}, expType);
test:assertEquals(err, result);
}

function disableConstraintValidation() returns [string, typedesc<record {}>, anydata][] {
return [
[
string `
<Person>
<name>John Doe</name>
<age>4</age>
<height>185.215</height>
<dob>
<year>5000</year>
<month>12</month>
<day>31</day>
</dob>
<family>
<id>22213</id>
<members>John</members>
<members>Doe</members>
<members>Ross</members>
</family>
</Person>
`,
ValidationPerson,
{
"name": "John Doe",
"age": 4,
"height": 185.215,
"dob": {
"year": 5000,
"month": 12,
"day": 31
},
"family": {
"id": 22213,
"members": [
"John",
"Doe",
"Ross"
]
}
}
],
[
string `
<Item>
<weight>1.2</weight>
<weight>2.3</weight>
<weight>3.4</weight>
</Item>
`,
ValidationItem,
<ValidationItem> {"weight": [1.2, 2.3, 3.4]}
]
];
}

@test:Config {
groups: ["constraint-validation"]
}
Expand Down Expand Up @@ -283,9 +347,9 @@ function testValidConstraintAnnotationForParseAsType() returns error? {
test:assertEquals(person.dob.year, 1990);
test:assertEquals(person.dob.month, 12);
test:assertEquals(person.dob.day, 31);
test:assertEquals(person.family.members.length(), 2);
test:assertEquals(person.family.members[0], "John");
test:assertEquals(person.family.members[1], "Doe");
test:assertEquals(person.family.members.length(), 2);
test:assertEquals(person.family.members[0], "John");
test:assertEquals(person.family.members[1], "Doe");
}

@test:Config {
Expand All @@ -295,7 +359,7 @@ function testValidConstraintAnnotationForParseAsType() returns error? {
function testInvalidConstraintAnnotationForParseAsType(xml sourceData, typedesc<record {}> expType, string expectedError) {
anydata|Error err = parseAsType(sourceData, {}, expType);
test:assertEquals(err is Error, true);
test:assertEquals((<error> err).message(), expectedError);
test:assertEquals((<error>err).message(), expectedError);
}

function invalidConstraintAnnotationForParseAsType() returns [xml, typedesc<record {}>, string][] {
Expand Down Expand Up @@ -383,7 +447,7 @@ function invalidConstraintAnnotationForParseAsType() returns [xml, typedesc<reco
`,
ValidationPerson,
"Validation failed for '$.height:maxFractionDigits' constraint(s)."
],
],
[
xml `
<Person>
Expand Down Expand Up @@ -440,3 +504,67 @@ function invalidConstraintAnnotationForParseAsType() returns [xml, typedesc<reco
]
];
}

@test:Config {
groups: ["constraint-validation"],
dataProvider: disableConstraintValidationForParseAsType
}
function testDisableConstraintValidationForParseAsType(xml sourceData, typedesc<record {}> expType, anydata result) returns error? {
anydata err = check parseAsType(sourceData, {enableConstraintValidation: false}, expType);
test:assertEquals(err, result);
}

function disableConstraintValidationForParseAsType() returns [xml, typedesc<record {}>, anydata][] {
return [
[
xml `
<Person>
<name>John Doe</name>
<age>4</age>
<height>185.215</height>
<dob>
<year>5000</year>
<month>12</month>
<day>31</day>
</dob>
<family>
<id>22213</id>
<members>John</members>
<members>Doe</members>
<members>Ross</members>
</family>
</Person>
`,
ValidationPerson,
{
"name": "John Doe",
"age": 4,
"height": 185.215,
"dob": {
"year": 5000,
"month": 12,
"day": 31
},
"family": {
"id": 22213,
"members": [
"John",
"Doe",
"Ross"
]
}
}
],
[
xml `
<Item>
<weight>1.2</weight>
<weight>2.3</weight>
<weight>3.4</weight>
</Item>
`,
ValidationItem,
<ValidationItem> {"weight": [1.2, 2.3, 3.4]}
]
];
}

0 comments on commit 0bbdaa6

Please sign in to comment.