DEV Community

Cover image for Testing WhatsApp Activity Pipelines: A Contract-First Approach with Fixtures
NumberChecker
NumberChecker

Posted on

Testing WhatsApp Activity Pipelines: A Contract-First Approach with Fixtures

When building data pipelines that process external signals—like checking WhatsApp account activity—the biggest bottleneck isn't the API itself; it's the fragility of your local parsing logic. If your downstream system expects a specific schema from a CSV export, a minor change in the upstream data format can break your entire ingestion pipeline.

To build a robust integration, you should treat the WhatsApp Activity Checker as a contract. By using local test fixtures, you can validate your normalization and mapping logic without hitting the production API repeatedly.

The Asynchronous Challenge

The WhatsApp Activity Checker uses an asynchronous batch workflow. You submit a file to POST /v1/tasks, poll POST /v1/gettasks for status, and eventually download a result file from the result_url once the status is exported.

Because this process involves file I/O and network latency, writing tests that rely on live calls is slow and prone to failure. Instead, decouple your logic into two layers:

  1. The Client Layer: Handles the X-API-Key authentication and polling logic.
  2. The Processor Layer: Handles the CSV parsing and field mapping.

Step 1: Define Your Contract Fixtures

Create a fixtures/ directory in your project. This allows you to simulate the exported state of a task. Create a mock_results.csv file that mirrors the expected output format:

number,activated,activetime,activedays,business
+14155552671,yes,2023-10-01,15,no
+442071838750,yes,2023-09-28,42,yes
Enter fullscreen mode Exit fullscreen mode

Step 2: Implement the Processor Unit Test

Your unit tests should focus on how your application maps these fields. By using the fixture file, you ensure your code handles the activetime and business fields correctly, even if the API structure evolves.

# Example: Testing the mapper logic
def test_csv_parser_mapping():
 # Load your fixture
 data = load_csv_fixture("fixtures/mock_results.csv")

 # Validate mapping logic
 result = map_to_internal_schema(data[0])

 assert result["is_active"] == True
 assert result["is_business"] == False
Enter fullscreen mode Exit fullscreen mode

Step 3: Integration Boundaries

When you move from local testing to the real v1/tasks endpoint, ensure your integration layer is resilient to the asynchronous nature of the service.

  • Poll, don't guess: Only initiate your download logic when the status returned by v1/gettasks is strictly exported.
  • Normalize inputs: Always ensure your input file contains numbers in E.164 format before sending them to the file field in your POST request.
  • Handle failures: The exported file will contain both successful and failed rows. Ensure your parser checks the failure count returned in the status response to log discrepancies early.

Conclusion

By moving your testing focus to local fixtures, you isolate your business logic from the network-heavy polling workflow. This approach allows you to iterate on your data pipeline safely, ensuring that when you do trigger a production task, your system is already prepared to handle the results correctly. For the latest details on endpoints and status codes, always refer to the official documentation.

This article was drafted with AI assistance and reviewed before publishing.

Top comments (0)