Repository navigation
test(files): add unit tests for Shallow Scan - #181
Merged
Merged
Conversation
Adds 36 fixture-free unit tests for the OneDrive and SharePoint Shallow
Scan path. All test data is defined inline, so the suite runs in about
two seconds from a clean checkout with no fixture generation step.
The tests are organised in four layers, each mocking at a different seam
so a failure points at one thing:
1. _split_library_endpoint pure, no I/O. Endpoint string correctness
including OneDrive parity with the
pre-refactor implementation, encoded
spaces, subsite webs and OData escaping.
2. SpRestConnector mocks the session so the real retry,
throttle and status handling executes.
Covers 200, 403, 404, 401, 429 and 500.
3. _scan_document_libraries patches _execute_get so aggregation and
threshold logic runs without sleeping.
Covers multi-library roll-up, subsite
roll-up, DL/Subsite/Site thresholds and
failure isolation.
4. calculate_resource_metrics full orchestration over the Graph mock.
Failure scenarios are covered alongside success, including the
all-or-nothing invariant: if ItemCount succeeds but StorageMetrics
fails, the library must contribute nothing rather than partial counts.
Parametrised cases use subTest to keep the file compact.
Shared fakes are added to tests/files/mocks.py, following the existing
convention of centralising mocks for the files suites:
- MockSpSession, a thread-safe session keyed by URL fragment that
supports response sequences so retry paths can be exercised.
- MockCertTokenManager, a domain-keyed token manager that records
refresh calls.
- MockResponse gains optional text and headers, which SpRestConnector
reads on its 403, 429 and 500 paths. Both are lazy and optional, so
existing callers are unaffected.
Empty tests/__init__.py and tests/files/__init__.py are added so
`python3 -m unittest discover` can import the package. Discovery
previously failed with ImportError for every pattern.
Run with:
python3 -m unittest tests.files.shallow_estimator_test -v
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.
Summary of Changes
Adds 36 fixture-free unit tests for the OneDrive and SharePoint Shallow Scan path introduced in #180. All test data is defined inline, so the suite runs in about two seconds from a clean checkout with no fixture generation step.
Test architecture
Organised in four layers, each mocking at a different seam so a failure points at exactly one thing:
_split_library_endpointSpRestConnectorcert_token_manager.get_session()_build_library_targets,_scan_document_librariesSpRestConnector._execute_getcalculate_resource_metricsMockUrlInvoker+_execute_getLayer 2 deliberately mocks the session rather than the connector, so the real retry, throttle and HTTP status handling executes. Those tests construct
SpRestConnector(mgr, max_retries=1)so the backoff loop never actually sleeps.Success coverage
_api, OData apostrophe escaping, system libraries, trailing slashes.folderCount = ItemCount - TotalFileCount,resourceCount = max(item_count, folders + files), OData string integers.driveMetricsholding each library's own numbers.N/Apreservation,isShallowScan, site classification.Failure coverage
Retry-Afterand 401 triggering an inline token refresh.ItemCountsucceeds butStorageMetricsfails, the library contributes nothing rather than partial counts. This encodes the behaviour chosen during design and is the case most likely to be accidentally "optimised" into partial credit later.{}.Parametrised cases use
subTestto keep the file compact — for example all six endpoint shapes are a single test, as are the three failure-type mappings.Shared fakes
Added to
tests/files/mocks.py, following the existing convention of centralising mocks for the files suites:MockSpSession— thread-safe session keyed by URL fragment, supporting response sequences so retry paths can be exercised. Thread safety matters because_scan_document_librariesdrives it from aThreadPoolExecutor.MockCertTokenManager— domain-keyed token manager that records refresh calls.MockResponse— gains optionaltextandheaders, whichSpRestConnectorreads on its 403, 429 and 500 paths.textis a lazy property, so bodies are only serialised if a caller actually reads them and existing callers are completely unaffected.Test discovery
Empty
tests/__init__.pyandtests/files/__init__.pyare added. Discovery previously failed outright for every pattern:It now works for
tests/files. Bringing the remaining suites into discovery is intentionally left out of scope.Validation
python3 -m unittest tests.files.shallow_estimator_test -v→ 36 tests, OK, 2.0spython3 -m unittest discover -s tests -t . -p "*_test.py"→ 36 tests, OKpython3 -m compileall -q tests→ cleanPassing tests prove little on their own, so the suite was validated by mutating the production code and confirming each mutation is caught:
shallow_file_estimator.pys_meta["fileCount"] +=changed to=>changed to>=owning == top_levelsubsite guardThe file was restored identical afterwards and the suite returned to OK.
Out of scope
This PR only adds tests and does not change any production code. Two pre-existing test problems were found while verifying and are deliberately left for a follow-up:
tests/files/load_tests.pyfails onlargeResourceCountby exactly one.data_state_creator.pyappends the SITE COLLECTION entry totenantLevelLargeResourcesbut never increments the site's ownlargeResourceCount, whereasfile_estimator.pydoes. Reproduces on a freshly generated fixture, so it is a genuine generator/estimator disagreement rather than a stale fixture.tests/eo_in_place_archives/has seven failures, all the same0 != Nsignature incalculate_resource_count. Unrelated to files.Neither is caused by this change: both were reproduced with the committed
mocks.pyswapped back in and with the new__init__.pyfiles removed.