Add Python SDK unit tests and fix get_host_top_protocols TypeError#10227
Open
Add Python SDK unit tests and fix get_host_top_protocols TypeError#10227
get_host_top_protocols TypeError#10227Conversation
…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>
Copilot created this pull request from a session on behalf of
lucaderi
March 26, 2026 22:03
View session
| import sys | ||
| import os | ||
| import unittest | ||
| from unittest.mock import MagicMock, patch, call |
Check notice
Code scanning / CodeQL
Unused import Note test
Show autofix suggestion
Hide autofix suggestion
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
| @@ -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.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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, leavingcalculateMinMax,calculateStatistics,ninetififthPercentile, and edge cases ofupsampleSerieentirely untested.Python SDK — 86 new unit tests (
python/tests/test_ntopng_sdk.py)Uses
unittest.mockto test all four SDK classes offline (no ntopng instance needed):Ntopng— constructor auth modes (token vs user+pass), connection/content-type failure,request/post_requestsuccess and HTTP-error paths, all getter methods, URL constructionInterface— all data, alert, flow, L4/L7 endpoints;get_host/get_historicalfactory methods;get_all_alertswith and without IP filterHost—get_host_data,get_l7_stats,get_dscp_stats(both directions),get_active_flows_paginated, VLAN propagationHistorical— all nine alert-family delegate methods, timeseries schema/query building,timeseries_to_pandas(valid and missing-key responses),get_flows(POST), top conversations/protocolsBug fix —
historical.pyget_host_top_protocolsTests uncovered a runtime
TypeError:self.ifid(anint) was concatenated directly with strings:Lua — 11 new
ts_commontest cases (ts_common_test.lua)Covers previously untested
ts_common.luafunctions:upsampleSerie: no-op when target ≤ source length, empty seriescalculateMinMax: ascending sequence, single element, NaN-skippingcalculateStatistics: counter type (total×step), gauge+keep_total, gauge without total, NaN-skippingninetififthPercentile: single element, all-NaN seriesThe new module is registered in
run.lua.