-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for handling of RegularExpression definitions for string …
…based properties Signed-off-by: Kostadin Ivanov (BD/TBC-BG) <[email protected]>
- Loading branch information
1 parent
8105691
commit 7f57a87
Showing
11 changed files
with
226 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
tests/vspec/test_datatypes_pattern/test_datatypes_pattern.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# Copyright (c) 2024 Contributors to COVESA | ||
# | ||
# This program and the accompanying materials are made available under the | ||
# terms of the Mozilla Public License 2.0 which is available at | ||
# https://www.mozilla.org/en-US/MPL/2.0/ | ||
# | ||
# SPDX-License-Identifier: MPL-2.0 | ||
|
||
import subprocess | ||
from pathlib import Path | ||
|
||
HERE = Path(__file__).resolve().parent | ||
TEST_UNITS = HERE / ".." / "test_units.yaml" | ||
TEST_QUANT = HERE / ".." / "test_quantities.yaml" | ||
|
||
|
||
# TODO-Kosta: | ||
# Test that: | ||
# | ||
# FAILS when other than string and string[] datatypes have defined pattern | ||
# | ||
# FAILS when pattern is defined and default is defined and default value does not match pattern | ||
# - there should be test for default and allowed values, | ||
# where allowed is mainly for string[] datatypes | ||
# | ||
# Add above three fail tests and 1 success test | ||
|
||
|
||
def test_datatype_pattern_wrong_type(tmp_path): | ||
spec = HERE / "test_pattern_wrong_datatype.vspec" | ||
output = tmp_path / "out.json" | ||
log = tmp_path / "log.txt" | ||
cmd = ( | ||
f"vspec --log-file {log} export json --pretty -u {TEST_UNITS} -q {TEST_QUANT} --vspec {spec} --output {output}" | ||
) | ||
process = subprocess.run(cmd.split()) | ||
assert process.returncode != 0 | ||
|
||
print(process.stdout) | ||
assert "Field 'pattern' is not allowed for type: 'uint16'. Allowed types: ['string', 'string[]']" in log.read_text() | ||
|
||
|
||
def test_datatype_pattern_no_match(tmp_path): | ||
def get_log_msg(value_type: str, value: str): | ||
return f"Specified '{value_type}' value: '{value}' must match defined pattern: '^[a-z]+$'" | ||
|
||
no_pattern_match_tests_to_run = { | ||
# Test #1: test no match of defined DEFAULT value | ||
"test_pattern_no_default_match.vspec": get_log_msg("default", "WrongLabel1"), | ||
# Test #2: test no match of defined DEFAULT list of values | ||
"test_pattern_no_default_array_match.vspec": get_log_msg("default", "Red"), | ||
# Test #3: test no match of defined ALLOWED value | ||
"test_pattern_no_allowed_match.vspec": get_log_msg("allowed", "Red"), | ||
# Test #4: test no match of defined ALLOWED list of values | ||
"test_pattern_no_allowed_array_match.vspec": get_log_msg("allowed", "Red"), | ||
} | ||
|
||
def run_no_match_pattern_test(test_name: str, log_message: str): | ||
spec = HERE / test_name | ||
output = tmp_path / "out.json" | ||
log = tmp_path / "log.txt" | ||
cmd = f"vspec --log-file {log} export json --pretty -u {TEST_UNITS} -q {TEST_QUANT}" | ||
cmd += f" --vspec {spec} --output {output} --strict" | ||
process = subprocess.run(cmd.split()) | ||
|
||
# Tested command should fail | ||
assert process.returncode != 0 | ||
# Check logged error message | ||
assert log_message in log.read_text() | ||
|
||
# Run no_pattern_match_tests_to_run | ||
for tst_name, tst_msg in no_pattern_match_tests_to_run.items(): | ||
run_no_match_pattern_test(tst_name, tst_msg) | ||
|
||
|
||
def test_datatype_pattern_ok(tmp_path): | ||
# Test #1: test no match of defined DEFAULT value | ||
spec = HERE / "test_pattern_ok.vspec" | ||
output = tmp_path / "out.json" | ||
log = tmp_path / "log.txt" | ||
cmd = f"vspec --log-file {log} export json --pretty -u {TEST_UNITS} -q {TEST_QUANT}" | ||
cmd += f" --vspec {spec} --output {output} --strict" | ||
process = subprocess.run(cmd.split()) | ||
assert process.returncode == 0 |
13 changes: 13 additions & 0 deletions
13
tests/vspec/test_datatypes_pattern/test_pattern_no_allowed_array_match.vspec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# | ||
A: | ||
type: branch | ||
description: Branch A - used to test no match of 'allowed' values with specified 'pattern' field. | ||
|
||
A.Colors: | ||
datatype: string | ||
type: attribute | ||
description: Simple Label for colors, which should be a simple collection of all lower case strings. | ||
Should fail on the 'allowed' 'Red' color label. | ||
pattern: ^[a-z]+$ | ||
allowed: [white, green, Red] | ||
default: white |
12 changes: 12 additions & 0 deletions
12
tests/vspec/test_datatypes_pattern/test_pattern_no_allowed_match.vspec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# | ||
A: | ||
type: branch | ||
description: Branch A - used to test no match of 'allowed' value with specified 'pattern' field. | ||
|
||
A.ErrorColors: | ||
datatype: string[] | ||
type: attribute | ||
description: Simple Label for ErrorColors, which should be a simple all lower case string. | ||
Should fail on the 'allowed' (only) 'Red' color label. | ||
pattern: ^[a-z]+$ | ||
allowed: [Red] |
12 changes: 12 additions & 0 deletions
12
tests/vspec/test_datatypes_pattern/test_pattern_no_default_array_match.vspec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# | ||
A: | ||
type: branch | ||
description: Branch A - used to test no match of 'default' values with specified 'pattern' field. | ||
|
||
A.Colors: | ||
datatype: string[] | ||
type: attribute | ||
description: Simple Label for colors, which should be a simple collection of all lower case strings. | ||
Should fail on the 'Red' color label. | ||
pattern: ^[a-z]+$ | ||
default: [white, green, Red] |
11 changes: 11 additions & 0 deletions
11
tests/vspec/test_datatypes_pattern/test_pattern_no_default_match.vspec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# | ||
A: | ||
type: branch | ||
description: Branch A - used to test no match of 'default' value with specified 'pattern' field. | ||
|
||
A.Label: | ||
datatype: string | ||
type: attribute | ||
description: Simple Label, which should be a simple all lower case string. | ||
pattern: ^[a-z]+$ | ||
default: WrongLabel1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# | ||
A: | ||
type: branch | ||
description: Branch A - used to test correct attribute (property) with defined 'pattern' field. | ||
|
||
A.Label: | ||
datatype: string | ||
type: attribute | ||
description: Simple Label, which should be a simple all lower case string with a number. | ||
pattern: ^[a-z0-9]+$ | ||
default: label1 | ||
|
||
A.Colors: | ||
datatype: string[] | ||
type: attribute | ||
description: Simple collection with colors, where each color should be a simple lower case string. | ||
pattern: ^[a-z]+$ | ||
allowed: [white, green, red, black, yellow, blue] | ||
default: [white, green, red] |
11 changes: 11 additions & 0 deletions
11
tests/vspec/test_datatypes_pattern/test_pattern_wrong_datatype.vspec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# | ||
A: | ||
type: branch | ||
description: Branch A - used to test wrong usage of 'pattern' property for other than a string datatype. | ||
|
||
A.Year: | ||
datatype: uint16 | ||
type: attribute | ||
description: Unsigned Integer (number) for an Year attribute. | ||
Should fail for defined 'pattern' field because 'pattern' is allowed only for string types. | ||
pattern: ^[0-9]+$ |