This test suite covers the core functionality of the MarketstackClient
class, which interacts with the Marketstack API and manages data caching.
Tests the retrieval of stock data from the API when the data is not in the cache.
Verifies the fetching of exchange tickers from the API.
Ensures that cached stock data is correctly retrieved without making an API call.
In unit testing, simulated (mock) database and API calls are performed for several important reasons:
-
Isolation: By mocking external dependencies, we're testing only the
MarketstackClient
logic, not the behaviour of the database or the Marketstack API. -
Consistency: API responses can vary, and databases can be in different states. Mocking ensures our tests always run with the same data.
-
Speed: Real API calls and database operations can be slow. Mocks make tests run faster.
-
Reliability: Tests don't fail due to network issues, API downtime, or database connection problems.
-
Control: We can easily simulate various scenarios (e.g., API errors, cache hits/misses) that might be difficult to reproduce with real systems.
-
Cost: Many APIs have usage limits or costs associated with calls. Mocking prevents unnecessary API usage during testing.
- Database Simulation: We use a mock
DatabaseManager
to simulate cache operations without interacting with a real database. - API Simulation: The
httpx.get
method is patched to return predefined responses, simulating API calls without actually contacting the Marketstack server.
This approach allows us to comprehensively test the client's behaviour in various scenarios while maintaining fast, reliable, and isolated tests.
(In progress)
We cache some real data for use in testing using src/populate_test_database.py
to first populate the test data.
The paramters for generating the test data are in test_data.toml
.
Then use test/test_cached_data.py
to run the tests.