Files
FCI_WaterMaker_API/tests/test_data_conversion.py
2025-06-08 15:53:25 +00:00

130 lines
4.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
Tests for data conversion utilities.
"""
import pytest
import struct
import sys
import os
# Add the parent directory to the path so we can import the package
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from watermaker_plc_api.utils.data_conversion import (
scale_value,
convert_ieee754_float,
convert_gallon_counter,
get_descriptive_value,
validate_register_value,
format_binary_string
)
class TestDataConversion:
"""Test cases for data conversion utilities"""
def test_scale_value_direct(self):
"""Test direct scaling (no change)"""
assert scale_value(100, "direct") == 100
assert scale_value(0, "direct") == 0
def test_scale_value_divide(self):
"""Test division scaling"""
assert scale_value(100, "÷10") == 10.0
assert scale_value(250, "÷10") == 25.0
assert scale_value(1000, "÷100") == 10.0
def test_scale_value_multiply(self):
"""Test multiplication scaling"""
assert scale_value(10, "×10") == 100.0
assert scale_value(5, "×2") == 10.0
def test_scale_value_invalid(self):
"""Test invalid scaling types"""
# Should return original value for invalid scale types
assert scale_value(100, "invalid") == 100
assert scale_value(100, "÷0") == 100 # Division by zero
assert scale_value(100, "×abc") == 100 # Invalid multiplier
def test_convert_ieee754_float(self):
"""Test IEEE 754 float conversion"""
# Test known values
# 1.0 in IEEE 754: 0x3F800000
high = 0x3F80
low = 0x0000
result = convert_ieee754_float(high, low)
assert result == 1.0
# Test another known value
# 3.14159 in IEEE 754: approximately 0x40490FD0
high = 0x4049
low = 0x0FD0
result = convert_ieee754_float(high, low)
assert abs(result - 3.14) < 0.01 # Allow small floating point differences
def test_convert_gallon_counter(self):
"""Test gallon counter conversion (same as IEEE 754)"""
high = 0x3F80
low = 0x0000
result = convert_gallon_counter(high, low)
assert result == 1.0
def test_get_descriptive_value_with_mapping(self):
"""Test getting descriptive value with value mapping"""
config = {
"values": {
"0": "Standby",
"5": "Running",
"7": "Service"
}
}
assert get_descriptive_value(0, config) == "Standby"
assert get_descriptive_value(5, config) == "Running"
assert get_descriptive_value(7, config) == "Service"
assert get_descriptive_value(99, config) == "Unknown (99)"
def test_get_descriptive_value_without_mapping(self):
"""Test getting descriptive value without value mapping"""
config = {}
assert get_descriptive_value(100, config) == 100
def test_validate_register_value(self):
"""Test register value validation"""
# Valid values
assert validate_register_value(0) is True
assert validate_register_value(1000) is True
assert validate_register_value(65533) is True
# Invalid values
assert validate_register_value(None) is False
assert validate_register_value(-1) is False
assert validate_register_value(65534) is False
assert validate_register_value(65535) is False
assert validate_register_value("string") is False
def test_validate_register_value_custom_max(self):
"""Test register value validation with custom maximum"""
assert validate_register_value(100, max_value=1000) is True
assert validate_register_value(1000, max_value=1000) is False
assert validate_register_value(999, max_value=1000) is True
def test_format_binary_string(self):
"""Test binary string formatting"""
assert format_binary_string(5) == "0000000000000101"
assert format_binary_string(255) == "0000000011111111"
assert format_binary_string(0) == "0000000000000000"
# Test custom width
assert format_binary_string(5, width=8) == "00000101"
assert format_binary_string(15, width=4) == "1111"
def test_ieee754_edge_cases(self):
"""Test IEEE 754 conversion edge cases"""
# Test with None return on error
result = convert_ieee754_float(None, 0)
assert result is None
# Test zero
result = convert_ieee754_float(0, 0)
assert result == 0.0