|
7 | 7 |
|
8 | 8 | from outlines.fsm.json_schema import (
|
9 | 9 | BOOLEAN,
|
| 10 | + DATE, |
| 11 | + DATE_TIME, |
10 | 12 | INTEGER,
|
11 | 13 | NULL,
|
12 | 14 | NUMBER,
|
13 | 15 | STRING,
|
14 | 16 | STRING_INNER,
|
| 17 | + TIME, |
| 18 | + UUID, |
15 | 19 | build_regex_from_object,
|
16 | 20 | get_schema_from_signature,
|
17 | 21 | to_regex,
|
@@ -451,3 +455,75 @@ def test_match(schema, regex, examples):
|
451 | 455 | assert match.span() == (0, len(string))
|
452 | 456 | else:
|
453 | 457 | assert match is None
|
| 458 | + |
| 459 | + |
| 460 | +@pytest.mark.parametrize( |
| 461 | + "schema,regex,examples", |
| 462 | + [ |
| 463 | + # UUID |
| 464 | + ( |
| 465 | + {"title": "Foo", "type": "string", "format": "uuid"}, |
| 466 | + UUID, |
| 467 | + [ |
| 468 | + ("123e4567-e89b-12d3-a456-426614174000", True), |
| 469 | + ("123e4567-e89b-12d3-a456-42661417400", False), |
| 470 | + ("123e4567-e89b-12d3-a456-42661417400g", False), |
| 471 | + ("123e4567-e89b-12d3-a456-42661417400-", False), |
| 472 | + ("", False), |
| 473 | + ], |
| 474 | + ), |
| 475 | + # DATE-TIME |
| 476 | + ( |
| 477 | + {"title": "Foo", "type": "string", "format": "date-time"}, |
| 478 | + DATE_TIME, |
| 479 | + [ |
| 480 | + ("2018-11-13T20:20:39Z", True), |
| 481 | + ("2016-09-18T17:34:02.666Z", True), |
| 482 | + ("2008-05-11T15:30:00Z", True), |
| 483 | + ("2021-01-01T00:00:00", True), |
| 484 | + ("2022-01-10 07:19:30", False), # missing T |
| 485 | + ("2022-12-10T10-04-29", False), # incorrect separator |
| 486 | + ("2023-01-01", False), |
| 487 | + ], |
| 488 | + ), |
| 489 | + # DATE |
| 490 | + ( |
| 491 | + {"title": "Foo", "type": "string", "format": "date"}, |
| 492 | + DATE, |
| 493 | + [ |
| 494 | + ("2018-11-13", True), |
| 495 | + ("2016-09-18", True), |
| 496 | + ("2008-05-11", True), |
| 497 | + ("2015-13-01", False), # incorrect month |
| 498 | + ("2022-01", False), # missing day |
| 499 | + ("2022/12/01", False), # incorrect separator" |
| 500 | + ], |
| 501 | + ), |
| 502 | + # TIME |
| 503 | + ( |
| 504 | + {"title": "Foo", "type": "string", "format": "time"}, |
| 505 | + TIME, |
| 506 | + [ |
| 507 | + ("20:20:39Z", True), |
| 508 | + ("15:30:00Z", True), |
| 509 | + ("25:30:00", False), # incorrect hour |
| 510 | + ("15:30", False), # missing seconds |
| 511 | + ("15:30:00.000", False), # missing Z |
| 512 | + ("15-30-00", False), # incorrect separator |
| 513 | + ("15:30:00+01:00", False), # incorrect separator |
| 514 | + ], |
| 515 | + ), |
| 516 | + ], |
| 517 | +) |
| 518 | +def test_format(schema, regex, examples): |
| 519 | + schema = json.dumps(schema) |
| 520 | + test_regex = build_regex_from_object(schema) |
| 521 | + assert test_regex == regex |
| 522 | + |
| 523 | + for string, does_match in examples: |
| 524 | + match = re.fullmatch(test_regex, string) |
| 525 | + if does_match: |
| 526 | + assert match[0] == string |
| 527 | + assert match.span() == (0, len(string)) |
| 528 | + else: |
| 529 | + assert match is None |
0 commit comments