-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for getting samples data in CSV format
- Implement test for retrieving samples in CSV format - Update resources.py to support CSV format in response Signed-off-by: Osman Hadzic <[email protected]>
- Loading branch information
1 parent
05fe8b6
commit 80aa547
Showing
2 changed files
with
319 additions
and
0 deletions.
There are no files selected for viewing
251 changes: 251 additions & 0 deletions
251
...app_engine/device/app_engine_device_get_samples/test_app_engine_device_get_samples_csv.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,251 @@ | ||
# SPDX-FileCopyrightText: 2024 SECO Mind Srl | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import subprocess | ||
import csv | ||
from io import StringIO | ||
|
||
from resources import ( | ||
expected_output_object_csv, | ||
list_of_params_endpoints, | ||
list_of_nonparams_endpoints, | ||
list_of_keys, | ||
map_of_params_csv_data, | ||
map_of_nonparams_csv_data, | ||
) | ||
|
||
|
||
def test_get_sample_data_for_device_individual_parametric_datastream_csv(astarte_env_vars): | ||
device_id = astarte_env_vars["device_test_1"] | ||
astarte_url = astarte_env_vars["astarte_url"] | ||
realm = astarte_env_vars["realm"] | ||
jwt = astarte_env_vars["jwt"] | ||
|
||
interface_name = "test.astarte-platform.device.individual.parametric.Datastream" | ||
|
||
for path in list_of_params_endpoints: | ||
arg_list = [ | ||
"astartectl", | ||
"appengine", | ||
"devices", | ||
"get-samples", | ||
device_id, | ||
interface_name, | ||
path, | ||
"-t", | ||
jwt, | ||
"-u", | ||
astarte_url, | ||
"-r", | ||
realm, | ||
"-o", | ||
"csv", | ||
] | ||
sample_data_result = subprocess.run(arg_list, capture_output=True, text=True) | ||
|
||
samples_csv = sample_data_result.stdout | ||
csv_reader = csv.DictReader(StringIO(samples_csv)) | ||
|
||
value = _parse_csv_to_sample_value(csv_reader) | ||
element = map_of_params_csv_data[path] | ||
|
||
assert value == element | ||
|
||
|
||
def test_get_sample_data_for_device_individual_nonparametric_datastream_csv(astarte_env_vars): | ||
device_id = astarte_env_vars["device_test_1"] | ||
astarte_url = astarte_env_vars["astarte_url"] | ||
realm = astarte_env_vars["realm"] | ||
jwt = astarte_env_vars["jwt"] | ||
|
||
interface_name = "test.astarte-platform.device.individual.nonparametric.Datastream" | ||
|
||
for path in list_of_nonparams_endpoints: | ||
arg_list = [ | ||
"astartectl", | ||
"appengine", | ||
"devices", | ||
"get-samples", | ||
device_id, | ||
interface_name, | ||
path, | ||
"-t", | ||
jwt, | ||
"-u", | ||
astarte_url, | ||
"-r", | ||
realm, | ||
"-o", | ||
"csv", | ||
] | ||
sample_data_result = subprocess.run(arg_list, capture_output=True, text=True) | ||
|
||
samples_csv = sample_data_result.stdout | ||
csv_reader = csv.DictReader(StringIO(samples_csv)) | ||
|
||
value = _parse_csv_to_sample_value(csv_reader) | ||
|
||
element = map_of_nonparams_csv_data[path] | ||
|
||
assert value == element | ||
|
||
|
||
def test_get_sample_data_for_device_object_parametric_datastream_csv(astarte_env_vars): | ||
device_id = astarte_env_vars["device_test_1"] | ||
astarte_url = astarte_env_vars["astarte_url"] | ||
realm = astarte_env_vars["realm"] | ||
jwt = astarte_env_vars["jwt"] | ||
|
||
interface_name = "test.astarte-platform.device.object.parametric.Datastream" | ||
path = "/a" | ||
|
||
arg_list = [ | ||
"astartectl", | ||
"appengine", | ||
"devices", | ||
"get-samples", | ||
device_id, | ||
interface_name, | ||
path, | ||
"-t", | ||
jwt, | ||
"-u", | ||
astarte_url, | ||
"-r", | ||
realm, | ||
"-o", | ||
"csv", | ||
] | ||
sample_data_result = subprocess.run(arg_list, capture_output=True, text=True) | ||
sample_data = csv.DictReader(StringIO(sample_data_result.stdout)) | ||
|
||
json_object = _map_sample_data_to_json(sample_data) | ||
|
||
assert json_object == expected_output_object_csv | ||
|
||
|
||
def test_get_sample_data_for_device_object_nonparametric_datastream_csv(astarte_env_vars): | ||
device_id = astarte_env_vars["device_test_1"] | ||
astarte_url = astarte_env_vars["astarte_url"] | ||
realm = astarte_env_vars["realm"] | ||
jwt = astarte_env_vars["jwt"] | ||
|
||
interface_name = "test.astarte-platform.device.object.nonparametric.Datastream" | ||
path = "/the" | ||
|
||
arg_list = [ | ||
"astartectl", | ||
"appengine", | ||
"devices", | ||
"get-samples", | ||
device_id, | ||
interface_name, | ||
path, | ||
"-t", | ||
jwt, | ||
"-u", | ||
astarte_url, | ||
"-r", | ||
realm, | ||
"-o", | ||
"csv", | ||
] | ||
sample_data_result = subprocess.run(arg_list, capture_output=True, text=True) | ||
sample_data = csv.DictReader(StringIO(sample_data_result.stdout)) | ||
|
||
json_object = _map_sample_data_to_json(sample_data) | ||
|
||
assert json_object == expected_output_object_csv | ||
|
||
|
||
def test_get_sample_data_for_server_individual_parametric_datastream_csv(astarte_env_vars): | ||
device_id = astarte_env_vars["device_test_1"] | ||
astarte_url = astarte_env_vars["astarte_url"] | ||
realm = astarte_env_vars["realm"] | ||
jwt = astarte_env_vars["jwt"] | ||
|
||
interface_name = "test.astarte-platform.server.individual.parametric.Datastream" | ||
|
||
for path in list_of_params_endpoints: | ||
arg_list = [ | ||
"astartectl", | ||
"appengine", | ||
"devices", | ||
"get-samples", | ||
device_id, | ||
interface_name, | ||
path, | ||
"-t", | ||
jwt, | ||
"-u", | ||
astarte_url, | ||
"-r", | ||
realm, | ||
"-o", | ||
"csv", | ||
] | ||
sample_data_result = subprocess.run(arg_list, capture_output=True, text=True) | ||
|
||
samples_csv = sample_data_result.stdout | ||
csv_reader = csv.DictReader(StringIO(samples_csv)) | ||
|
||
value = _parse_csv_to_sample_value(csv_reader) | ||
element = map_of_params_csv_data[path] | ||
|
||
assert value == element | ||
|
||
|
||
def test_get_sample_data_for_server_individual_nonparametric_datastream_csv(astarte_env_vars): | ||
device_id = astarte_env_vars["device_test_1"] | ||
astarte_url = astarte_env_vars["astarte_url"] | ||
realm = astarte_env_vars["realm"] | ||
jwt = astarte_env_vars["jwt"] | ||
|
||
interface_name = "test.astarte-platform.server.individual.nonparametric.Datastream" | ||
|
||
for path in list_of_nonparams_endpoints: | ||
arg_list = [ | ||
"astartectl", | ||
"appengine", | ||
"devices", | ||
"get-samples", | ||
device_id, | ||
interface_name, | ||
path, | ||
"-t", | ||
jwt, | ||
"-u", | ||
astarte_url, | ||
"-r", | ||
realm, | ||
"-o", | ||
"csv", | ||
] | ||
sample_data_result = subprocess.run(arg_list, capture_output=True, text=True) | ||
|
||
samples_csv = sample_data_result.stdout | ||
csv_reader = csv.DictReader(StringIO(samples_csv)) | ||
|
||
value = _parse_csv_to_sample_value(csv_reader) | ||
|
||
element = map_of_nonparams_csv_data[path] | ||
|
||
assert value == element | ||
|
||
|
||
def _parse_csv_to_sample_value(csv_reader): | ||
samples_json = [] | ||
for row in csv_reader: | ||
samples_json.append({"timestamp": row["Timestamp"], "value": row["Value"]}) | ||
value = samples_json[0]["value"] | ||
return value | ||
|
||
|
||
def _map_sample_data_to_json(sample_data): | ||
samples_json = [] | ||
for row in sample_data: | ||
for key in list_of_keys: | ||
if key in row: | ||
samples_json.append({key: row[key]}) | ||
return samples_json |
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