-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor array handling and add tests
- Loading branch information
Showing
2 changed files
with
157 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# /******************************************************************************** | ||
# * Copyright (c) 2023 Contributors to the Eclipse Foundation | ||
# * | ||
# * See the NOTICE file(s) distributed with this work for additional | ||
# * information regarding copyright ownership. | ||
# * | ||
# * This program and the accompanying materials are made available under the | ||
# * terms of the Apache License 2.0 which is available at | ||
# * http://www.apache.org/licenses/LICENSE-2.0 | ||
# * | ||
# * SPDX-License-Identifier: Apache-2.0 | ||
# ********************************************************************************/ | ||
|
||
import pytest | ||
from kuksa_client.grpc import Datapoint | ||
|
||
# | ||
# Client rules: | ||
# For simple strings like abd it is optional to quote them ("abc") or not (abc) | ||
# Quotes are needed if you have commas ("ab, c") | ||
# If you have duoble quotes in strings you must escape them | ||
|
||
def test_array_parse_no_quote(): | ||
test_str = r'[say hello, abc]' | ||
my_array = list(Datapoint.cast_array_values(Datapoint.cast_str,test_str)) | ||
assert len(my_array) == 2 | ||
assert my_array[0] == "say hello" | ||
assert my_array[1] == "abc" | ||
|
||
def test_array_parse_no_inside_quote(): | ||
test_str = r'["say hello","abc"]' | ||
my_array = list(Datapoint.cast_array_values(Datapoint.cast_str,test_str)) | ||
assert len(my_array) == 2 | ||
assert my_array[0] == "say hello" | ||
assert my_array[1] == "abc" | ||
|
||
def test_array_parse_double_quote(): | ||
test_str = r'["say \"hello\"","abc"]' | ||
my_array = list(Datapoint.cast_array_values(Datapoint.cast_str,test_str)) | ||
assert len(my_array) == 2 | ||
assert my_array[0] == "say \"hello\"" | ||
assert my_array[1] == "abc" | ||
|
||
def test_array_parse_double_quote_2(): | ||
test_str = r'[say \"hello\",abc]' | ||
my_array = list(Datapoint.cast_array_values(Datapoint.cast_str,test_str)) | ||
assert len(my_array) == 2 | ||
assert my_array[0] == "say \"hello\"" | ||
assert my_array[1] == "abc" | ||
|
||
def test_array_parse_comma(): | ||
test_str = r'["say, hello","abc"]' | ||
my_array = list(Datapoint.cast_array_values(Datapoint.cast_str,test_str)) | ||
assert len(my_array) == 2 | ||
assert my_array[0] == "say, hello" | ||
assert my_array[1] == "abc" | ||
|
||
def test_array_square(): | ||
"""No problem having square brackets as part of strings""" | ||
test_str = r'[say hello[], abc]' | ||
my_array = list(Datapoint.cast_array_values(Datapoint.cast_str,test_str)) | ||
assert len(my_array) == 2 | ||
assert my_array[0] == "say hello[]" | ||
assert my_array[1] == "abc" | ||
|
||
def test_int_no_quote(): | ||
test_str = r'[123,456]' | ||
my_array = list(Datapoint.cast_array_values(int,test_str)) | ||
assert len(my_array) == 2 | ||
assert my_array[0] == 123 | ||
assert my_array[1] == 456 | ||
|
||
def test_int_quote(): | ||
"""Quoting individual int values is not allowed""" | ||
test_str = r'["123","456"]' | ||
with pytest.raises(ValueError): | ||
my_array = list(Datapoint.cast_array_values(int,test_str)) | ||
|
||
|
||
def test_float_no_quote(): | ||
test_str = r'[123,456.23]' | ||
my_array = list(Datapoint.cast_array_values(float,test_str)) | ||
assert len(my_array) == 2 | ||
assert my_array[0] == 123 | ||
assert my_array[1] == 456.23 | ||
|
||
def test_cast_str(): | ||
"""Unquoted quotation marks shall be removed, quoted kept without quotes""" | ||
test_str = r'"say hello"' | ||
assert Datapoint.cast_str(test_str) == r'say hello' | ||
test_str = r'"say \"hello\""' | ||
assert Datapoint.cast_str(test_str) == r'say "hello"' | ||
test_str = r'say "hello"' | ||
assert Datapoint.cast_str(test_str) == r'say hello' | ||
|
||
def test_cast_bool(): | ||
assert Datapoint.cast_bool("true") is True | ||
assert Datapoint.cast_bool("True") is True | ||
assert Datapoint.cast_bool("T") is True | ||
assert Datapoint.cast_bool("t") is True | ||
assert Datapoint.cast_bool("false") is False | ||
assert Datapoint.cast_bool("False") is False | ||
assert Datapoint.cast_bool("F") is False | ||
assert Datapoint.cast_bool("f") is False | ||
|
||
# And then some other, treated as true for now | ||
assert Datapoint.cast_bool("Ja") is True | ||
assert Datapoint.cast_bool("Nein") is True | ||
assert Datapoint.cast_bool("Doch") is True | ||
|