add first pytests

This commit is contained in:
Markus Clauß 2023-02-08 15:33:26 +01:00
parent 04fe4e3a83
commit 663c78d8ff
3 changed files with 166 additions and 0 deletions

49
tests/io/minio_test.py Normal file
View File

@ -0,0 +1,49 @@
import os
import random
import tempfile
from string import ascii_letters
from dotenv import dotenv_values
from pytestpavement.helper.filehasher import calc_hash_of_file
from pytestpavement.io.minio import MinioClient
class TestMinio():
#read .env file
config = dotenv_values(".env")
bucket = 'pytest'
client = MinioClient(url=config['S3_HOST'],
access_key=config['S3_ACCESS_KEY'],
secret_key=config['S3_PASSWD'],
secure=False,
bucket=bucket)
def test_init_minio(self) -> None:
return
def test_upload_file(self) -> None:
"""
generate dummy file and upload to s3 bucket
"""
with tempfile.TemporaryDirectory() as tmpdirname:
#generate testfile
n = 1024**2
n = 1000
file_orig = os.path.join(tmpdirname, 'textfile.txt')
with open(file_orig, 'w') as f:
f.write(''.join(
[random.choice(ascii_letters) for l in range(n)]))
filehash_orig = calc_hash_of_file(file_orig)
self.client.compress_and_upload_file(file_orig,
'test',
metadata={'lab': '123'})

View File

117
tests/labtests/sine_test.py Normal file
View File

@ -0,0 +1,117 @@
from random import uniform
import numpy as np
from pytestpavement.analysis.regression import fit_cos, fit_cos_eval
def fit(freq: float = 10,
ampl: float = 100.0,
offset: float = 20.0,
slope: float = 0.1,
phase: float = 0.05,
error: float = 0.001) -> None:
N: int = 5
num_samples_per_cycle: int = 50
t = np.linspace(0, N / freq, N * num_samples_per_cycle)
y = ampl * np.cos(2 * np.pi * freq * t + phase) + slope * t + offset
r = fit_cos(t, y)
error_min = (1 - error)
error_max = (1 + error)
# ampltude
rel_error = (r['amp'] / ampl)
assert error_min <= rel_error <= error_max
# offset
rel_error = (r['offset'] / offset)
assert error_min <= rel_error <= error_max
# slope
rel_error = (r['slope'] / slope)
assert error_min <= rel_error <= error_max
# phase
rel_error = (r['phase'] / phase)
assert error_min <= rel_error <= error_max
# freq
rel_error = (r['freq'] / freq)
assert error_min <= rel_error <= error_max
def test_fit_simple_sine(ntest: int = 50) -> None:
"""
fit a simple sine signal and evaluate amplitude
error: percentage error of ampl, Error max 0.1 %
"""
fit()
#run multiple tests with random parameters
for i in range(ntest):
fit(
ampl=uniform(1e-3, 1000),
offset=uniform(1e-3, 1),
slope=uniform(1e-5, 1),
phase=uniform(1e-5, 1),
)
def fit_noise(freq: float = 10,
ampl: float = 100.0,
offset: float = 20.0,
slope: float = 0.1,
phase: float = 0.05,
noise_level: float = 0.01,
error: float = 0.01) -> None:
N: int = 5
num_samples_per_cycle: int = 50
t = np.linspace(0, N / freq, N * num_samples_per_cycle)
y = ampl * np.cos(2 * np.pi * freq * t + phase) + slope * t + offset
y_noise = np.random.normal(0, noise_level * ampl, len(t))
y = y + y_noise
r = fit_cos(t, y)
error_min = (1 - error)
error_max = (1 + error)
# ampltude
rel_error = (r['amp'] / ampl)
assert error_min <= rel_error <= error_max
# freq
rel_error = (r['freq'] / freq)
assert error_min <= rel_error <= error_max
def test_fit_simple_sine_with_noise(ntest: int = 50) -> None:
"""
fit a simple sine signal and evaluate amplitude
error: percentage error of ampl, Error max 0.1 %
"""
fit_noise()
#run multiple tests with random parameters
for i in range(ntest):
fit_noise(
ampl=uniform(1e-3, 1000),
offset=uniform(1e-3, 1),
slope=uniform(1e-5, 1),
phase=uniform(1e-5, 1),
noise_level=uniform(0.01, 0.1),
error=0.02,
)