> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mangrovesystems.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 5.2: Testing Patterns

> Create comprehensive test datasets, validate expected outputs, and systematically test edge cases and boundary conditions.

<Panel>
  <Card title="Accounting Academy" className="accounting-academy-card">
    <div className="accounting-academy-content">
      <Steps className="my-0">
        <Step title="MODULE 1: Fundamentals" icon="bookmark" iconType="solid" stepNumber={1} titleSize="p">
          * [1.1: Core Concepts & the Data Lifecycle](/accounting-academy/module-1/1-1-core-concepts-data-lifecycle)
          * [1.2: Translating Methodologies to Mangrove](/accounting-academy/module-1/1-2-translating-methodologies-to-mangrove)
          * [1.3: Events and Datapoints](/accounting-academy/module-1/1-3-events-and-datapoints)
          * [Exercise: Design and Build](/accounting-academy/module-1/1-exercise-design-and-build)
        </Step>

        <Step title="MODULE 2: Calculations" icon="bookmark" iconType="solid" stepNumber={2} titleSize="p">
          * [2.1: Calculation Architecture](/accounting-academy/module-2/2-1-calculation-architecture)
          * [2.2: Expanding Your Data Model](/accounting-academy/module-2/2-2-expanding-your-data-model)
          * [Exercise: Multi-Step Calculations](/accounting-academy/module-2/2-exercise-multi-step-calculations)
        </Step>

        <Step title="MODULE 3: Batch Partitioning & LCA" icon="bookmark" iconType="solid" stepNumber={3} titleSize="p">
          * [3.1: What Is a Batch?](/accounting-academy/module-3/3-1-what-is-a-batch)
          * [3.2: Batch Partitioning Strategies](/accounting-academy/module-3/3-2-batch-partitioning)
          * [3.3: Life Cycle Assessment & Emissions](/accounting-academy/module-3/3-3-lca-and-emissions)
          * [3.4: Allocating Emissions to Batches](/accounting-academy/module-3/3-4-allocating-emissions-to-batches)
          * [Exercise: Batch Partitioning & LCA](/accounting-academy/module-3/3-exercise-batch-partitioning-and-lca)
        </Step>

        <Step title="MODULE 4: Mass Balance with Ledgers" icon="bookmark" iconType="solid" stepNumber={4} titleSize="p">
          * [4.1: Introduction to Mass Balance](/accounting-academy/module-4/4-1-intro-to-mass-balance)
          * [4.2: Designing the Ledger Structure](/accounting-academy/module-4/4-2-designing-the-ledger-structure)
          * [4.3: Multiple Batch Types for Different Stages](/accounting-academy/module-4/4-3-multiple-batch-types)
          * [Exercise: Design & Build Mass Balance](/accounting-academy/module-4/4-exercise-design-and-build-mass-balance)
        </Step>

        <Step title="MODULE 5: Integration Testing" icon="bookmark" iconType="solid" stepNumber={5} titleSize="p">
          * [5.1: The Complete Model Assembly](/accounting-academy/module-5/5-1-the-complete-model-assembly)
          * [5.2: Testing Patterns](/accounting-academy/module-5/5-2-testing-patterns)
          * [5.3: Common Integration Pitfalls](/accounting-academy/module-5/5-3-common-integration-pitfalls)
          * [Exercise: Integration Testing](/accounting-academy/module-5/5-exercise-integration-testing)
        </Step>

        <Step title="MODULE 6: Advanced Patterns & Production" icon="bookmark" iconType="solid" stepNumber={6} titleSize="p">
          * [6.1: Model Design Best Practices](/accounting-academy/module-6/6-1-model-design-best-practices)
          * [6.2: Advanced Techniques](/accounting-academy/module-6/6-2-advanced-techniques)
          * [6.3: Production Readiness](/accounting-academy/module-6/6-3-production-readiness)
          * [Exercise: Production Deployment](/accounting-academy/module-6/6-exercise-production-deployment)
        </Step>
      </Steps>
    </div>
  </Card>
</Panel>

<Check>
  **What you'll learn in this lesson:**

  * Design comprehensive test datasets for your project
  * Calculate expected outputs before running tests
  * Test edge cases: missing data, extreme values, and boundary conditions
  * Use Analytics and Data Inputs views to validate results
</Check>

Testing a production accounting project isn't just about running the pipeline and seeing if it works. You need **pre-calculated expected outputs**, **systematic edge case coverage**, and a clear process for validating every result.

## Designing test datasets

A good test dataset covers three scenarios: happy path, edge cases, and error cases.

<Tabs>
  <Tab title="Happy path">
    Normal operations with typical values. This is your baseline — if this doesn't work, nothing will.

    **Example for Mangrove Biochar (January 2024):**

    | Event Type         | Count | Typical Values                   |
    | ------------------ | ----- | -------------------------------- |
    | Feedstock Receipt  | 5     | 8-15 t wet mass, 10-20% moisture |
    | Lab Analysis       | 3     | 75-85% carbon, 0.2-0.5 H:C ratio |
    | Biochar Delivery   | 4     | 2-6 t biochar                    |
    | Energy Consumption | 1     | 10,000 kWh, 500 gal propane      |
    | Transportation     | 4     | 100-250 km per delivery          |
  </Tab>

  <Tab title="Edge cases">
    Unusual but valid values that test the boundaries of your model.

    **Examples:**

    * Very high moisture (50%) — dry mass drops to half
    * High H:C ratio (0.75) — lowest permanence tier
    * Very small delivery (0.5 t) — emissions may exceed gross carbon
    * Zero distance transport — should produce zero transport emissions
  </Tab>

  <Tab title="Error cases">
    Invalid data that should be caught by validation.

    **Examples:**

    * Missing required datapoints — should fail validation
    * Negative mass values — should be rejected
    * Moisture > 100% — physically impossible
    * Missing events for a required type — model should flag it
  </Tab>
</Tabs>

## Calculating expected outputs

**Before running the pipeline**, calculate the expected output for each test scenario by hand or in a spreadsheet. This is critical — without expected values, you can't tell if the output is correct.

<Steps>
  <Step title="Set up a spreadsheet">
    Create a spreadsheet with columns for each input value and each intermediate/final calculation. Include all 5 steps from your Module 2 model plus the LCA calculations from Module 3.
  </Step>

  <Step title="Enter your test data">
    Fill in the test values for each event. Calculate each intermediate value step by step, exactly as your model should.
  </Step>

  <Step title="Calculate batch-level outputs">
    For each batch (delivery), compute: gross carbon, allocated emissions (proportional + direct), and net carbon.
  </Step>

  <Step title="Compute ledger totals">
    Sum all batch outputs per ledger. Verify that ledger balances reconcile (inputs = outputs + inventory changes).
  </Step>
</Steps>

**Example spreadsheet layout:**

| Delivery  | Mass (t) | Carbon % | Gross tCO2e | Alloc. Elec | Alloc. Propane | Transport | Net tCO2e |
| --------- | -------- | -------- | ----------- | ----------- | -------------- | --------- | --------- |
| DEL-001   | 3        | 80%      | 8.81        | 1.25        | 0.61           | 0.11      | 6.84      |
| DEL-002   | 5        | 78%      | 14.31       | 2.09        | 1.02           | 0.14      | 11.06     |
| DEL-003   | 4        | 82%      | 12.04       | 1.67        | 0.82           | 0.13      | 9.42      |
| DEL-004   | 2        | 79%      | 5.80        | 0.83        | 0.41           | 0.09      | 4.47      |
| **Total** | **14**   |          | **40.96**   | **5.84**    | **2.86**       | **0.47**  | **31.79** |

<Note>
  The numbers above are illustrative — your actual values will depend on your model structure and emission factors. The point is to have a **reference table** before you run the pipeline, so you can compare against the results.
</Note>

## Validating results in Mangrove

After running <a href="/production-accounting/generate-batches" target="_blank" rel="noopener noreferrer">Generate Batches</a>, use these views to validate:

### Data Inputs view

Check that all events were created correctly:

* Event count matches expected
* Datapoint values are correct
* Evidence is attached where required
* Date ranges are within the accounting period

### Batch outputs

For each batch, verify:

* **Gross carbon** matches your spreadsheet
* **Allocated emissions** are proportional to mass
* **Transport emissions** are assigned to the correct batch
* **Net carbon** = gross - total emissions

### Ledger balances

Check that each ledger reconciles:

* Credits (incoming batches) match expected values
* Debits (allocations to downstream ledgers) are correct
* Running balance makes physical sense

## Systematic edge case testing

For each edge case, follow this pattern:

```mermaid theme={null}
flowchart LR
    INPUT["Create<br/>Edge Case<br/>Event"] --> RUN["Run<br/>Pipeline"]
    RUN --> CHECK{"Output<br/>Matches<br/>Expected?"}
    CHECK -->|Yes| PASS["Pass ✓"]
    CHECK -->|No| DEBUG["Debug:<br/>Check model<br/>nodes"]
    DEBUG --> FIX["Fix &<br/>Re-run"]
    FIX --> CHECK

    style INPUT fill:#42a5f5,stroke:#1976d2,stroke-width:2px,color:#000
    style RUN fill:#66bb6a,stroke:#388e3c,stroke-width:2px,color:#000
    style CHECK fill:#ffa726,stroke:#f57c00,stroke-width:2px,color:#000
    style PASS fill:#66bb6a,stroke:#388e3c,stroke-width:2px,color:#000
    style DEBUG fill:#ef5350,stroke:#c62828,stroke-width:2px,color:#000
    style FIX fill:#ef5350,stroke:#c62828,stroke-width:2px,color:#000
```

**Edge cases to test for Mangrove Biochar:**

| Edge Case            | Expected Behavior                                                     |
| -------------------- | --------------------------------------------------------------------- |
| Moisture = 50%       | Dry mass is halved; all downstream values proportionally reduced      |
| H:C ratio = 0.75     | Permanence factor = 0.7 (lowest tier); net carbon is lower            |
| Mass = 0.5 t         | Very small batch; emissions may be a large proportion of gross carbon |
| Distance = 0 km      | Transport emissions = 0; only shared emissions allocated              |
| Carbon content = 95% | High carbon; verify no overflow or rounding errors                    |

<Tip>
  Document each edge case test and its result. This becomes your test suite — when you modify the model later, you can re-run these tests to catch regressions.
</Tip>

***

### Check your understanding

<Accordion title="Why should you calculate expected outputs before running the pipeline?" icon="circle-question">
  Without pre-calculated expected values, you have no way to verify if the pipeline output is **correct** — only that it runs without errors. A pipeline can produce wrong numbers silently. Pre-calculated values give you a reference to compare against, catching formula errors, unit mismatches, and configuration problems.
</Accordion>

<Accordion title="What are the 3 types of test scenarios you should create?" icon="circle-question">
  **Happy path** (normal operations with typical values), **edge cases** (unusual but valid values that test boundaries), and **error cases** (invalid data that should be caught by validation). Together these cover the range of inputs your project might encounter in production.
</Accordion>

***

Next, learn about common problems and how to fix them in [Lesson 5.3: Common Integration Pitfalls](/accounting-academy/module-5/5-3-common-integration-pitfalls).
