Skip to content

Commit 854de68

Browse files
committed
Add new test cases for date and time
1 parent 8a8f3f2 commit 854de68

File tree

10 files changed

+1126
-26
lines changed

10 files changed

+1126
-26
lines changed

JsonSchema.Tests/JsonSchema.Tests.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
<Description>This project contains test cases for JsonSchema project.</Description>
66
<Authors>Relogic Labs</Authors>
77
<Company>Relogic Labs</Company>
8-
<Version>1.7.0</Version>
9-
<AssemblyVersion>1.7.0</AssemblyVersion>
8+
<Version>1.8.0</Version>
9+
<AssemblyVersion>1.8.0</AssemblyVersion>
1010
<Copyright>Copyright © Relogic Labs. All rights reserved.</Copyright>
1111
<NeutralLanguage>en</NeutralLanguage>
1212
<TargetFrameworks>net5.0;net6.0;net7.0</TargetFrameworks>
@@ -28,4 +28,4 @@
2828
<ProjectReference Include="..\JsonSchema\JsonSchema.csproj" />
2929
</ItemGroup>
3030

31-
</Project>
31+
</Project>

JsonSchema.Tests/RelogicLabs/JsonSchema/Tests/Negative/ArrayTests.cs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,109 @@ public void When_EmptyArrayInObject_ExceptionThrown()
189189
Assert.AreEqual(NEMT02, exception.Code);
190190
Console.WriteLine(exception);
191191
}
192+
193+
[TestMethod]
194+
public void When_JsonWrongLengthInObject_ExceptionThrown()
195+
{
196+
var schema =
197+
"""
198+
@length*(2) #array* #object
199+
""";
200+
var json =
201+
"""
202+
{
203+
"key1": [10, 20],
204+
"key2": [10]
205+
}
206+
""";
207+
JsonSchema.IsValid(schema, json);
208+
var exception = Assert.ThrowsException<JsonSchemaException>(
209+
() => JsonAssert.IsValid(schema, json));
210+
Assert.AreEqual(ALEN01, exception.Code);
211+
Console.WriteLine(exception);
212+
}
213+
214+
[TestMethod]
215+
public void When_JsonWrongMinimumLengthInObject_ExceptionThrown()
216+
{
217+
var schema =
218+
"""
219+
@length*(2, 4) #array* #object
220+
""";
221+
var json =
222+
"""
223+
{
224+
"key1": [10, 20],
225+
"key2": [10]
226+
}
227+
""";
228+
JsonSchema.IsValid(schema, json);
229+
var exception = Assert.ThrowsException<JsonSchemaException>(
230+
() => JsonAssert.IsValid(schema, json));
231+
Assert.AreEqual(ALEN02, exception.Code);
232+
Console.WriteLine(exception);
233+
}
234+
235+
[TestMethod]
236+
public void When_JsonWrongMaximumLengthInObject_ExceptionThrown()
237+
{
238+
var schema =
239+
"""
240+
@length*(2, 4) #array* #object
241+
""";
242+
var json =
243+
"""
244+
{
245+
"key1": [10, 20],
246+
"key2": [10, 20, 30, 40, 50]
247+
}
248+
""";
249+
JsonSchema.IsValid(schema, json);
250+
var exception = Assert.ThrowsException<JsonSchemaException>(
251+
() => JsonAssert.IsValid(schema, json));
252+
Assert.AreEqual(ALEN03, exception.Code);
253+
Console.WriteLine(exception);
254+
}
255+
256+
[TestMethod]
257+
public void When_JsonWrongMinimumLengthWithUndefinedInObject_ExceptionThrown()
258+
{
259+
var schema =
260+
"""
261+
@length*(2, !) #array* #object
262+
""";
263+
var json =
264+
"""
265+
{
266+
"key1": [10, 20],
267+
"key2": [10]
268+
}
269+
""";
270+
JsonSchema.IsValid(schema, json);
271+
var exception = Assert.ThrowsException<JsonSchemaException>(
272+
() => JsonAssert.IsValid(schema, json));
273+
Assert.AreEqual(ALEN04, exception.Code);
274+
Console.WriteLine(exception);
275+
}
276+
277+
[TestMethod]
278+
public void When_JsonWrongMaximumLengthWithUndefinedInObject_ExceptionThrown()
279+
{
280+
var schema =
281+
"""
282+
@length*(!, 4) #array* #object
283+
""";
284+
var json =
285+
"""
286+
{
287+
"key1": [10, 20],
288+
"key2": [10, 20, 30, 40, 50]
289+
}
290+
""";
291+
JsonSchema.IsValid(schema, json);
292+
var exception = Assert.ThrowsException<JsonSchemaException>(
293+
() => JsonAssert.IsValid(schema, json));
294+
Assert.AreEqual(ALEN05, exception.Code);
295+
Console.WriteLine(exception);
296+
}
192297
}

JsonSchema.Tests/RelogicLabs/JsonSchema/Tests/Negative/DateTimeTests.cs

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,4 +467,218 @@ public void When_InvalidTimePatternCauseLexerError_ExceptionThrown()
467467
Assert.AreEqual(DLEX01, exception.Code);
468468
Console.WriteLine(exception);
469469
}
470+
471+
[TestMethod]
472+
public void When_JsonDateNotValidWithBothRange_ExceptionThrown()
473+
{
474+
var schema =
475+
"""
476+
@range*("2010-01-01", "2010-12-31") #date* #array
477+
""";
478+
var json =
479+
"""
480+
[
481+
"2010-01-01",
482+
"2010-02-01",
483+
"2010-06-30",
484+
"2009-12-31",
485+
"2011-01-01"
486+
]
487+
""";
488+
JsonSchema.IsValid(schema, json);
489+
var exception = Assert.ThrowsException<JsonSchemaException>(
490+
() => JsonAssert.IsValid(schema, json));
491+
Assert.AreEqual(DRNG01, exception.Code);
492+
Console.WriteLine(exception);
493+
}
494+
495+
[TestMethod]
496+
public void When_JsonTimeNotValidWithBothRange_ExceptionThrown()
497+
{
498+
var schema =
499+
"""
500+
@range*("2010-01-01T00:00:00.000Z",
501+
"2010-12-31T23:59:59.999Z")
502+
#time* #array
503+
""";
504+
var json =
505+
"""
506+
[
507+
"2010-01-01T00:00:00.000Z",
508+
"2010-02-01T01:30:45.1Z",
509+
"2010-06-30T12:01:07.999999Z",
510+
"2009-12-31T22:39:50.0-04:00",
511+
"2011-01-01T02:10:00.0+06:00",
512+
"2009-12-31T22:59:59.000Z",
513+
"2011-01-01T00:00:00.000Z"
514+
]
515+
""";
516+
JsonSchema.IsValid(schema, json);
517+
var exception = Assert.ThrowsException<JsonSchemaException>(
518+
() => JsonAssert.IsValid(schema, json));
519+
Assert.AreEqual(DRNG02, exception.Code);
520+
Console.WriteLine(exception);
521+
}
522+
523+
[TestMethod]
524+
public void When_JsonDateNotValidWithStart_ExceptionThrown()
525+
{
526+
var schema =
527+
"""
528+
@start*("2010-01-01") #date* #array
529+
""";
530+
var json =
531+
"""
532+
[
533+
"2010-01-01",
534+
"2010-02-01",
535+
"2011-06-30",
536+
"2050-11-01",
537+
"2009-12-31"
538+
]
539+
""";
540+
JsonSchema.IsValid(schema, json);
541+
var exception = Assert.ThrowsException<JsonSchemaException>(
542+
() => JsonAssert.IsValid(schema, json));
543+
Assert.AreEqual(STRT01, exception.Code);
544+
Console.WriteLine(exception);
545+
}
546+
547+
[TestMethod]
548+
public void When_JsonDateNotValidWithEnd_ExceptionThrown()
549+
{
550+
var schema =
551+
"""
552+
@end*("2010-12-31") #date* #array
553+
""";
554+
var json =
555+
"""
556+
[
557+
"1930-01-01",
558+
"2000-02-01",
559+
"2005-06-30",
560+
"2010-12-31",
561+
"2011-01-01"
562+
]
563+
""";
564+
JsonSchema.IsValid(schema, json);
565+
var exception = Assert.ThrowsException<JsonSchemaException>(
566+
() => JsonAssert.IsValid(schema, json));
567+
Assert.AreEqual(ENDE01, exception.Code);
568+
Console.WriteLine(exception);
569+
}
570+
571+
[TestMethod]
572+
public void When_JsonDateNotValidWithBefore_ExceptionThrown()
573+
{
574+
var schema =
575+
"""
576+
@before*("2011-01-01") #date* #array
577+
""";
578+
var json =
579+
"""
580+
[
581+
"2010-01-01",
582+
"2010-06-30",
583+
"2010-12-31",
584+
"2011-01-01",
585+
"2023-11-15"
586+
]
587+
""";
588+
JsonSchema.IsValid(schema, json);
589+
var exception = Assert.ThrowsException<JsonSchemaException>(
590+
() => JsonAssert.IsValid(schema, json));
591+
Assert.AreEqual(BFOR01, exception.Code);
592+
Console.WriteLine(exception);
593+
}
594+
595+
[TestMethod]
596+
public void When_JsonDateNotValidWithAfter_ExceptionThrown()
597+
{
598+
var schema =
599+
"""
600+
@after*("2009-12-31") #date* #array
601+
""";
602+
var json =
603+
"""
604+
[
605+
"2010-01-01",
606+
"2010-02-10",
607+
"2010-12-31",
608+
"2009-12-31",
609+
"1980-11-19"
610+
]
611+
""";
612+
JsonSchema.IsValid(schema, json);
613+
var exception = Assert.ThrowsException<JsonSchemaException>(
614+
() => JsonAssert.IsValid(schema, json));
615+
Assert.AreEqual(AFTR01, exception.Code);
616+
Console.WriteLine(exception);
617+
}
618+
619+
[TestMethod]
620+
public void When_JsonTimeNotValidWithEndInFraction_ExceptionThrown()
621+
{
622+
var schema =
623+
"""
624+
@end*("1939-09-02T02:12:12.555Z") #time* #array
625+
""";
626+
var json =
627+
"""
628+
[
629+
"1939-09-02T02:12:12.554Z",
630+
"1939-09-02T02:12:12.555Z",
631+
"1939-09-02T02:12:12.556Z"
632+
]
633+
""";
634+
JsonSchema.IsValid(schema, json);
635+
var exception = Assert.ThrowsException<JsonSchemaException>(
636+
() => JsonAssert.IsValid(schema, json));
637+
Assert.AreEqual(ENDE02, exception.Code);
638+
Console.WriteLine(exception);
639+
}
640+
641+
[TestMethod]
642+
public void When_SchemaDateNotValidWithBefore_ExceptionThrown()
643+
{
644+
var schema =
645+
"""
646+
@before*("01-01-2011") #date* #array
647+
""";
648+
var json =
649+
"""
650+
[
651+
"1900-01-01",
652+
"2010-06-30",
653+
"2010-12-31"
654+
]
655+
""";
656+
JsonSchema.IsValid(schema, json);
657+
var exception = Assert.ThrowsException<JsonSchemaException>(
658+
() => JsonAssert.IsValid(schema, json));
659+
Assert.AreEqual(DYAR01, exception.Code);
660+
Console.WriteLine(exception);
661+
}
662+
663+
[TestMethod]
664+
public void When_SchemaDateNotValidWithAfter_ExceptionThrown()
665+
{
666+
var schema =
667+
"""
668+
@after*("12-31-2009") #date* #array
669+
""";
670+
var json =
671+
"""
672+
[
673+
"2010-01-01",
674+
"2010-02-10",
675+
"2050-12-31"
676+
]
677+
""";
678+
JsonSchema.IsValid(schema, json);
679+
var exception = Assert.ThrowsException<JsonSchemaException>(
680+
() => JsonAssert.IsValid(schema, json));
681+
Assert.AreEqual(DYAR01, exception.Code);
682+
Console.WriteLine(exception);
683+
}
470684
}

JsonSchema.Tests/RelogicLabs/JsonSchema/Tests/Negative/IntegerTests.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public void When_NestedRangeWithJsonWrongIntegerInObject_ExceptionThrown()
186186
}
187187

188188
[TestMethod]
189-
public void When_NestedRangeWithUndefinedAndWrongIntegerInArray_ExceptionThrown()
189+
public void When_NestedRangeWithMinUndefinedAndWrongIntegerInArray_ExceptionThrown()
190190
{
191191
var schema =
192192
"""
@@ -204,6 +204,25 @@ public void When_NestedRangeWithUndefinedAndWrongIntegerInArray_ExceptionThrown(
204204
Console.WriteLine(exception);
205205
}
206206

207+
[TestMethod]
208+
public void When_NestedRangeWithMaxUndefinedAndWrongIntegerInArray_ExceptionThrown()
209+
{
210+
var schema =
211+
"""
212+
@range*(1000, !) #integer*
213+
""";
214+
var json =
215+
"""
216+
[2000, 1000, 900]
217+
""";
218+
219+
JsonSchema.IsValid(schema, json);
220+
var exception = Assert.ThrowsException<JsonSchemaException>(
221+
() => JsonAssert.IsValid(schema, json));
222+
Assert.AreEqual(RANG03, exception.Code);
223+
Console.WriteLine(exception);
224+
}
225+
207226
[TestMethod]
208227
public void When_NestedPositiveWithWrongIntegerInArray_ExceptionThrown()
209228
{

0 commit comments

Comments
 (0)