Refactor authoritative field handling and enhance device update logic

- Updated `get_source_for_field_update_with_value` to determine source values based on new field values, including handling for empty and unknown values.
- Introduced `get_overwrite_sql_clause` to build SQL conditions for authoritative overwrite checks based on plugin settings.
- Enhanced `update_devices_data_from_scan` to utilize new authoritative settings and conditions for updating device fields.
- Added new tests for source value determination and device creation to ensure proper handling of source fields.
- Created in-memory SQLite database fixtures for testing device creation and updates.
This commit is contained in:
Jokob @NetAlertX
2026-01-22 04:33:49 +00:00
parent 422a048806
commit 49e689f022
5 changed files with 931 additions and 143 deletions

View File

@@ -2,11 +2,9 @@
Unit tests for authoritative field update handler.
"""
import pytest
from server.db.authoritative_handler import (
can_overwrite_field,
get_source_for_field_update,
get_source_for_field_update_with_value,
FIELD_SOURCE_MAP,
)
@@ -75,17 +73,52 @@ class TestCanOverwriteField:
)
class TestGetSourceForFieldUpdate:
"""Test source value determination for field updates."""
class TestGetSourceForFieldUpdateWithValue:
"""Test source value determination with value-based normalization."""
def test_user_override_sets_user_source(self):
"""User override should set USER source."""
assert get_source_for_field_update("devName", "UNIFIAPI", is_user_override=True) == "USER"
assert (
get_source_for_field_update_with_value(
"devName", "UNIFIAPI", "Device", is_user_override=True
)
== "USER"
)
def test_plugin_update_sets_plugin_prefix(self):
"""Plugin update should set plugin prefix as source."""
assert get_source_for_field_update("devName", "UNIFIAPI", is_user_override=False) == "UNIFIAPI"
assert get_source_for_field_update("devLastIP", "ARPSCAN", is_user_override=False) == "ARPSCAN"
assert (
get_source_for_field_update_with_value(
"devName", "UNIFIAPI", "Device", is_user_override=False
)
== "UNIFIAPI"
)
assert (
get_source_for_field_update_with_value(
"devLastIP", "ARPSCAN", "192.168.1.1", is_user_override=False
)
== "ARPSCAN"
)
def test_empty_or_unknown_values_return_newdev(self):
assert (
get_source_for_field_update_with_value(
"devName", "ARPSCAN", "", is_user_override=False
)
== "NEWDEV"
)
assert (
get_source_for_field_update_with_value(
"devName", "ARPSCAN", "(unknown)", is_user_override=False
)
== "NEWDEV"
)
def test_non_empty_value_sets_plugin_prefix(self):
assert (
get_source_for_field_update_with_value(
"devVendor", "ARPSCAN", "Acme", is_user_override=False
)
== "ARPSCAN"
)
class TestFieldSourceMapping: