datasynth-generators 2.4.0

50+ data generators covering GL, P2P, O2C, S2C, HR, manufacturing, audit, tax, treasury, and ESG
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//! Regression tests for document FK population in P2P and O2C chains.
//!
//! DS-GEP-004: Ensures top-level FK fields (`purchase_order_id`,
//! `goods_receipt_id`, `sales_order_id`, `delivery_id`) are
//! consistently populated when documents are generated as part of a
//! chain. This removes the need for expensive FK lookups in the
//! graph-export pipeline's edge synthesizers.

use chrono::NaiveDate;
use rust_decimal::Decimal;

use datasynth_core::models::{
    CreditRating, Customer, CustomerPaymentBehavior, CustomerType, Material, MaterialType, Vendor,
    VendorType,
};
use datasynth_generators::document_flow::{
    O2CGenerator, O2CGeneratorConfig, P2PGenerator, P2PGeneratorConfig,
};

// ---------------------------------------------------------------------------
// Helper factories
// ---------------------------------------------------------------------------

fn test_vendor() -> Vendor {
    Vendor::new("V-000001", "Test Vendor Inc.", VendorType::Supplier)
}

fn test_customer() -> Customer {
    let mut customer = Customer::new("C-000001", "Test Customer Inc.", CustomerType::Corporate);
    customer.credit_rating = CreditRating::A;
    customer.credit_limit = Decimal::from(1_000_000);
    customer.payment_behavior = CustomerPaymentBehavior::OnTime;
    customer
}

fn test_materials() -> Vec<Material> {
    vec![
        Material::new("MAT-001", "Material A", MaterialType::RawMaterial)
            .with_standard_cost(Decimal::from(100)),
        Material::new("MAT-002", "Material B", MaterialType::FinishedGood)
            .with_standard_cost(Decimal::from(50)),
    ]
}

fn test_materials_o2c() -> Vec<Material> {
    let mut mat1 = Material::new("MAT-001", "Product A", MaterialType::FinishedGood);
    mat1.list_price = Decimal::from(100);
    mat1.standard_cost = Decimal::from(60);

    let mut mat2 = Material::new("MAT-002", "Product B", MaterialType::FinishedGood);
    mat2.list_price = Decimal::from(200);
    mat2.standard_cost = Decimal::from(120);

    vec![mat1, mat2]
}

// ---------------------------------------------------------------------------
// P2P FK tests
// ---------------------------------------------------------------------------

#[test]
fn p2p_vendor_invoice_has_purchase_order_id() {
    let mut gen = P2PGenerator::new(42);
    let vendor = test_vendor();
    let materials = test_materials();
    let refs: Vec<&Material> = materials.iter().collect();

    let chain = gen.generate_chain(
        "1000",
        &vendor,
        &refs,
        NaiveDate::from_ymd_opt(2024, 6, 1).unwrap(),
        2024,
        6,
        "TESTUSER",
    );

    let invoice = chain
        .vendor_invoice
        .as_ref()
        .expect("chain should have vendor invoice");

    assert!(
        invoice.purchase_order_id.is_some(),
        "VendorInvoice.purchase_order_id must be populated in chain"
    );
    assert_eq!(
        invoice.purchase_order_id.as_deref().unwrap(),
        chain.purchase_order.header.document_id,
        "VendorInvoice.purchase_order_id must match the PO document_id"
    );
}

#[test]
fn p2p_vendor_invoice_has_goods_receipt_id() {
    let mut gen = P2PGenerator::new(42);
    let vendor = test_vendor();
    let materials = test_materials();
    let refs: Vec<&Material> = materials.iter().collect();

    let chain = gen.generate_chain(
        "1000",
        &vendor,
        &refs,
        NaiveDate::from_ymd_opt(2024, 6, 1).unwrap(),
        2024,
        6,
        "TESTUSER",
    );

    let invoice = chain
        .vendor_invoice
        .as_ref()
        .expect("chain should have vendor invoice");

    assert!(
        invoice.goods_receipt_id.is_some(),
        "VendorInvoice.goods_receipt_id must be populated in chain"
    );
    assert_eq!(
        invoice.goods_receipt_id.as_deref().unwrap(),
        chain.goods_receipts[0].header.document_id,
        "VendorInvoice.goods_receipt_id must match the first GR document_id"
    );
}

#[test]
fn p2p_goods_receipt_has_purchase_order_id() {
    let mut gen = P2PGenerator::new(42);
    let vendor = test_vendor();
    let materials = test_materials();
    let refs: Vec<&Material> = materials.iter().collect();

    let chain = gen.generate_chain(
        "1000",
        &vendor,
        &refs,
        NaiveDate::from_ymd_opt(2024, 6, 1).unwrap(),
        2024,
        6,
        "TESTUSER",
    );

    for (i, gr) in chain.goods_receipts.iter().enumerate() {
        assert!(
            gr.purchase_order_id.is_some(),
            "GoodsReceipt[{i}].purchase_order_id must be populated in chain"
        );
        assert_eq!(
            gr.purchase_order_id.as_deref().unwrap(),
            chain.purchase_order.header.document_id,
            "GoodsReceipt[{i}].purchase_order_id must match the PO document_id"
        );
    }
}

#[test]
fn p2p_partial_delivery_all_grs_have_po_fk() {
    let config = P2PGeneratorConfig {
        partial_delivery_rate: 1.0, // Force partial delivery → 2 GRs
        ..Default::default()
    };

    let mut gen = P2PGenerator::with_config(42, config);
    let vendor = test_vendor();
    let materials = test_materials();
    let refs: Vec<&Material> = materials.iter().collect();

    let chain = gen.generate_chain(
        "1000",
        &vendor,
        &refs,
        NaiveDate::from_ymd_opt(2024, 6, 1).unwrap(),
        2024,
        6,
        "TESTUSER",
    );

    assert!(
        chain.goods_receipts.len() >= 2,
        "partial delivery must produce >= 2 GRs"
    );

    for (i, gr) in chain.goods_receipts.iter().enumerate() {
        assert!(
            gr.purchase_order_id.is_some(),
            "GoodsReceipt[{i}] (partial delivery) must have purchase_order_id"
        );
    }

    // Invoice should still have the FK
    if let Some(ref invoice) = chain.vendor_invoice {
        assert!(
            invoice.purchase_order_id.is_some(),
            "VendorInvoice (after partial delivery) must have purchase_order_id"
        );
    }
}

// ---------------------------------------------------------------------------
// O2C FK tests
// ---------------------------------------------------------------------------

#[test]
fn o2c_customer_invoice_has_sales_order_id() {
    let mut gen = O2CGenerator::new(42);
    let customer = test_customer();
    let materials = test_materials_o2c();
    let refs: Vec<&Material> = materials.iter().collect();

    let chain = gen.generate_chain(
        "1000",
        &customer,
        &refs,
        NaiveDate::from_ymd_opt(2024, 6, 1).unwrap(),
        2024,
        6,
        "TESTUSER",
    );

    assert!(chain.credit_check_passed, "credit check must pass");

    let invoice = chain
        .customer_invoice
        .as_ref()
        .expect("chain should have customer invoice");

    assert!(
        invoice.sales_order_id.is_some(),
        "CustomerInvoice.sales_order_id must be populated in chain"
    );
    assert_eq!(
        invoice.sales_order_id.as_deref().unwrap(),
        chain.sales_order.header.document_id,
        "CustomerInvoice.sales_order_id must match the SO document_id"
    );
}

#[test]
fn o2c_customer_invoice_has_delivery_id() {
    let mut gen = O2CGenerator::new(42);
    let customer = test_customer();
    let materials = test_materials_o2c();
    let refs: Vec<&Material> = materials.iter().collect();

    let chain = gen.generate_chain(
        "1000",
        &customer,
        &refs,
        NaiveDate::from_ymd_opt(2024, 6, 1).unwrap(),
        2024,
        6,
        "TESTUSER",
    );

    let invoice = chain
        .customer_invoice
        .as_ref()
        .expect("chain should have customer invoice");

    assert!(
        invoice.delivery_id.is_some(),
        "CustomerInvoice.delivery_id must be populated in chain"
    );
    assert_eq!(
        invoice.delivery_id.as_deref().unwrap(),
        chain.deliveries[0].header.document_id,
        "CustomerInvoice.delivery_id must match the first Delivery document_id"
    );
}

#[test]
fn o2c_delivery_has_sales_order_id() {
    let mut gen = O2CGenerator::new(42);
    let customer = test_customer();
    let materials = test_materials_o2c();
    let refs: Vec<&Material> = materials.iter().collect();

    let chain = gen.generate_chain(
        "1000",
        &customer,
        &refs,
        NaiveDate::from_ymd_opt(2024, 6, 1).unwrap(),
        2024,
        6,
        "TESTUSER",
    );

    for (i, dlv) in chain.deliveries.iter().enumerate() {
        assert!(
            dlv.sales_order_id.is_some(),
            "Delivery[{i}].sales_order_id must be populated in chain"
        );
        assert_eq!(
            dlv.sales_order_id.as_deref().unwrap(),
            chain.sales_order.header.document_id,
            "Delivery[{i}].sales_order_id must match the SO document_id"
        );
    }
}

#[test]
fn o2c_partial_shipment_all_deliveries_have_so_fk() {
    let config = O2CGeneratorConfig {
        partial_shipment_rate: 1.0, // Force partial → 2 deliveries
        ..Default::default()
    };

    let mut gen = O2CGenerator::with_config(42, config);
    let customer = test_customer();
    let materials = test_materials_o2c();
    let refs: Vec<&Material> = materials.iter().collect();

    let chain = gen.generate_chain(
        "1000",
        &customer,
        &refs,
        NaiveDate::from_ymd_opt(2024, 6, 1).unwrap(),
        2024,
        6,
        "TESTUSER",
    );

    assert!(
        chain.deliveries.len() >= 2,
        "partial shipment must produce >= 2 deliveries"
    );

    for (i, dlv) in chain.deliveries.iter().enumerate() {
        assert!(
            dlv.sales_order_id.is_some(),
            "Delivery[{i}] (partial shipment) must have sales_order_id"
        );
    }

    // Invoice should still have the FK
    if let Some(ref invoice) = chain.customer_invoice {
        assert!(
            invoice.sales_order_id.is_some(),
            "CustomerInvoice (after partial shipment) must have sales_order_id"
        );
        assert!(
            invoice.delivery_id.is_some(),
            "CustomerInvoice (after partial shipment) must have delivery_id"
        );
    }
}

// ---------------------------------------------------------------------------
// created_by_employee_id tests
// ---------------------------------------------------------------------------

#[test]
fn document_header_created_by_employee_id_defaults_to_none() {
    use datasynth_core::models::documents::{DocumentHeader, DocumentType};

    let header = DocumentHeader::new(
        "TEST-001",
        DocumentType::PurchaseOrder,
        "1000",
        2024,
        6,
        NaiveDate::from_ymd_opt(2024, 6, 1).unwrap(),
        "JSMITH",
    );

    assert!(
        header.created_by_employee_id.is_none(),
        "created_by_employee_id should default to None"
    );
}

#[test]
fn document_header_created_by_employee_id_builder() {
    use datasynth_core::models::documents::{DocumentHeader, DocumentType};

    let header = DocumentHeader::new(
        "TEST-001",
        DocumentType::PurchaseOrder,
        "1000",
        2024,
        6,
        NaiveDate::from_ymd_opt(2024, 6, 1).unwrap(),
        "JSMITH",
    )
    .with_created_by_employee_id("E-001234");

    assert_eq!(
        header.created_by_employee_id.as_deref(),
        Some("E-001234"),
        "created_by_employee_id should be set by builder"
    );
    assert_eq!(
        header.created_by, "JSMITH",
        "created_by (user_id) should remain unchanged"
    );
}

#[test]
fn document_header_created_by_employee_id_serializes() {
    use datasynth_core::models::documents::{DocumentHeader, DocumentType};

    let header = DocumentHeader::new(
        "TEST-001",
        DocumentType::PurchaseOrder,
        "1000",
        2024,
        6,
        NaiveDate::from_ymd_opt(2024, 6, 1).unwrap(),
        "JSMITH",
    )
    .with_created_by_employee_id("E-001234");

    let json = serde_json::to_string(&header).expect("should serialize");
    assert!(
        json.contains("created_by_employee_id"),
        "serialized JSON should contain created_by_employee_id"
    );
    assert!(
        json.contains("E-001234"),
        "serialized JSON should contain the employee ID value"
    );

    // Verify skip_serializing_if: when None, field should be absent
    let header_no_eid = DocumentHeader::new(
        "TEST-002",
        DocumentType::PurchaseOrder,
        "1000",
        2024,
        6,
        NaiveDate::from_ymd_opt(2024, 6, 1).unwrap(),
        "JSMITH",
    );

    let json_no_eid = serde_json::to_string(&header_no_eid).expect("should serialize");
    assert!(
        !json_no_eid.contains("created_by_employee_id"),
        "serialized JSON should not contain created_by_employee_id when None"
    );
}