Skip to content

Add Python SDK unit tests and fix get_host_top_protocols TypeError#10227

Open
Copilot wants to merge 1 commit intodevfrom
copilot/improve-test-coverage
Open

Add Python SDK unit tests and fix get_host_top_protocols TypeError#10227
Copilot wants to merge 1 commit intodevfrom
copilot/improve-test-coverage

Conversation

Copy link
Copy Markdown

Copilot AI commented Mar 26, 2026

The Python SDK (python/ntopng/) had zero unit tests — only a manual integration script requiring a live server. The Lua timeseries test suite covered just two cases, leaving calculateMinMax, calculateStatistics, ninetififthPercentile, and edge cases of upsampleSerie entirely untested.

Python SDK — 86 new unit tests (python/tests/test_ntopng_sdk.py)

Uses unittest.mock to test all four SDK classes offline (no ntopng instance needed):

  • Ntopng — constructor auth modes (token vs user+pass), connection/content-type failure, request/post_request success and HTTP-error paths, all getter methods, URL construction
  • Interface — all data, alert, flow, L4/L7 endpoints; get_host/get_historical factory methods; get_all_alerts with and without IP filter
  • Hostget_host_data, get_l7_stats, get_dscp_stats (both directions), get_active_flows_paginated, VLAN propagation
  • Historical — all nine alert-family delegate methods, timeseries schema/query building, timeseries_to_pandas (valid and missing-key responses), get_flows (POST), top conversations/protocols

Bug fix — historical.py get_host_top_protocols

Tests uncovered a runtime TypeError: self.ifid (an int) was concatenated directly with strings:

# Before — crashes at runtime
"ts_query": "ifid:" + self.ifid + ",host:" + host

# After
"ts_query": "ifid:" + str(self.ifid) + ",host:" + host

Lua — 11 new ts_common test cases (ts_common_test.lua)

Covers previously untested ts_common.lua functions:

  • upsampleSerie: no-op when target ≤ source length, empty series
  • calculateMinMax: ascending sequence, single element, NaN-skipping
  • calculateStatistics: counter type (total×step), gauge+keep_total, gauge without total, NaN-skipping
  • ninetififthPercentile: single element, all-NaN series

The new module is registered in run.lua.

…mon tests

- Add python/tests/test_ntopng_sdk.py with 86 unit tests covering:
  - Ntopng: constructor (auth_token / user+pass), request/post_request wrappers,
    all getter methods, debug flag, URL building
  - Interface: all data, alert, flow, L7/L4 and host methods
  - Host: get_host_data, get_l7_stats, get_dscp_stats (both directions), VLAN handling
  - Historical: all alert family delegates, timeseries, flows, conversations
- Fix bug in historical.py get_host_top_protocols(): self.ifid (int) was
  concatenated with strings without str() conversion, causing TypeError at runtime
- Add scripts/lua/modules/timeseries/tests/ts_common_test.lua with 11 test cases:
  - upsampleSerie: no-op when target <= source length, empty serie
  - calculateMinMax: basic, single element, NaN-skipping
  - calculateStatistics: counter (total*step), gauge+keep_total, gauge no-total, NaN-skipping
  - ninetififthPercentile: single element, all-NaN
- Register ts_common_test in the Lua test runner (run.lua)

Agent-Logs-Url: https://github.com/ntop/ntopng/sessions/32da23c3-306c-4100-9bbe-64b91191653b

Co-authored-by: lucaderi <4493366+lucaderi@users.noreply.github.com>
import sys
import os
import unittest
from unittest.mock import MagicMock, patch, call

Check notice

Code scanning / CodeQL

Unused import Note test

Import of 'call' is not used.

Copilot Autofix

AI about 1 month ago

To fix an unused import, remove only the unused name from the import statement while preserving the used ones. This eliminates the unnecessary dependency and resolves the static analysis warning without changing behavior.

In python/tests/test_ntopng_sdk.py, on line 11, change from unittest.mock import MagicMock, patch, call to import only MagicMock and patch, since those are used in _make_mock_response and _make_ntopng, while call is not referenced. No additional methods or imports are needed; we simply adjust the existing import line.

Suggested changeset 1
python/tests/test_ntopng_sdk.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/python/tests/test_ntopng_sdk.py b/python/tests/test_ntopng_sdk.py
--- a/python/tests/test_ntopng_sdk.py
+++ b/python/tests/test_ntopng_sdk.py
@@ -8,7 +8,7 @@
 import sys
 import os
 import unittest
-from unittest.mock import MagicMock, patch, call
+from unittest.mock import MagicMock, patch
 
 # Add the python directory to the path so we can import ntopng modules
 sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
EOF
@@ -8,7 +8,7 @@
import sys
import os
import unittest
from unittest.mock import MagicMock, patch, call
from unittest.mock import MagicMock, patch

# Add the python directory to the path so we can import ntopng modules
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
Copilot is powered by AI and may make mistakes. Always verify output.
@lucaderi lucaderi marked this pull request as ready for review March 26, 2026 22:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants