| 1 |
Sequences of unique items (UniqueSeq): |
| 2 |
docs: compound/sequences-of-unique-items |
| 3 |
based on: strictyaml |
| 4 |
description: | |
| 5 |
UniqueSeq validates sequences which contain no duplicate |
| 6 |
values. |
| 7 |
given: |
| 8 |
yaml_snippet: | |
| 9 |
- A |
| 10 |
- B |
| 11 |
- C |
| 12 |
setup: | |
| 13 |
from strictyaml import UniqueSeq, Str, load, as_document |
| 14 |
from ensure import Ensure |
| 15 |
|
| 16 |
schema = UniqueSeq(Str()) |
| 17 |
variations: |
| 18 |
Valid: |
| 19 |
steps: |
| 20 |
- Run: |
| 21 |
code: | |
| 22 |
Ensure(load(yaml_snippet, schema)).equals(["A", "B", "C", ]) |
| 23 |
|
| 24 |
Parsing with one dupe raises an exception: |
| 25 |
given: |
| 26 |
yaml_snippet: | |
| 27 |
- A |
| 28 |
- B |
| 29 |
- B |
| 30 |
steps: |
| 31 |
- Run: |
| 32 |
code: load(yaml_snippet, schema) |
| 33 |
raises: |
| 34 |
type: strictyaml.exceptions.YAMLValidationError |
| 35 |
message: |- |
| 36 |
while parsing a sequence |
| 37 |
in "<unicode string>", line 1, column 1: |
| 38 |
- A |
| 39 |
^ (line: 1) |
| 40 |
duplicate found |
| 41 |
in "<unicode string>", line 3, column 1: |
| 42 |
- B |
| 43 |
^ (line: 3) |
| 44 |
|
| 45 |
Parsing all dupes raises an exception: |
| 46 |
given: |
| 47 |
yaml_snippet: | |
| 48 |
- 3 |
| 49 |
- 3 |
| 50 |
- 3 |
| 51 |
steps: |
| 52 |
- Run: |
| 53 |
code: load(yaml_snippet, schema) |
| 54 |
raises: |
| 55 |
type: strictyaml.exceptions.YAMLValidationError |
| 56 |
message: |- |
| 57 |
while parsing a sequence |
| 58 |
in "<unicode string>", line 1, column 1: |
| 59 |
- '3' |
| 60 |
^ (line: 1) |
| 61 |
duplicate found |
| 62 |
in "<unicode string>", line 3, column 1: |
| 63 |
- '3' |
| 64 |
^ (line: 3) |
| 65 |
|
| 66 |
Serializing with dupes raises an exception: |
| 67 |
steps: |
| 68 |
- Run: |
| 69 |
code: | |
| 70 |
as_document(["A", "B", "B"], schema) |
| 71 |
raises: |
| 72 |
type: strictyaml.exceptions.YAMLSerializationError |
| 73 |
message: Expecting all unique items, but duplicates were found in '['A', |
| 74 |
'B', 'B']'. |