googleads-rs 23.2.0

A gRPC client library for Google Ads API, generated automatically from the API definition files.
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
// Unit tests for GoogleAdsRow::get() method - Error Conditions and Edge Cases
//
// This module tests error handling, unimplemented paths, and edge cases

#![allow(unused_imports)]

mod test_helpers;

use test_helpers::{
    AdGroupBuilder, CampaignBuilder, GoogleAdsRowBuilder, MetricsBuilder, SegmentsBuilder,
};

// ============================================================================
// Unimplemented Field Paths
// ============================================================================

#[test]
fn test_unimplemented_field_path_returns_not_implemented() {
    let row = GoogleAdsRowBuilder::new().build();

    assert_eq!(
        row.get("unknown.field.path"),
        "not implemented by googleads-rs"
    );
}

#[test]
fn test_unimplemented_campaign_field() {
    let campaign = CampaignBuilder::new().id(123).build();

    let row = GoogleAdsRowBuilder::new().with_campaign(campaign).build();

    // Test a field that exists in the proto but isn't implemented in get()
    assert_eq!(
        row.get("campaign.nonexistent_field"),
        "not implemented by googleads-rs"
    );
}

#[test]
fn test_unimplemented_resource() {
    let row = GoogleAdsRowBuilder::new().build();

    assert_eq!(
        row.get("unimplemented_resource.field"),
        "not implemented by googleads-rs"
    );
}

#[test]
fn test_partial_field_path() {
    let campaign = CampaignBuilder::new().id(123).build();

    let row = GoogleAdsRowBuilder::new().with_campaign(campaign).build();

    // Incomplete path
    assert_eq!(row.get("campaign"), "not implemented by googleads-rs");
}

#[test]
fn test_typo_in_field_name() {
    let campaign = CampaignBuilder::new().id(123).build();

    let row = GoogleAdsRowBuilder::new().with_campaign(campaign).build();

    // Common typo: "campaing" instead of "campaign"
    assert_eq!(row.get("campaing.id"), "not implemented by googleads-rs");
}

#[test]
fn test_case_sensitive_field_names() {
    let campaign = CampaignBuilder::new().id(123).build();

    let row = GoogleAdsRowBuilder::new().with_campaign(campaign).build();

    // Field names are case-sensitive
    assert_eq!(row.get("Campaign.ID"), "not implemented by googleads-rs");

    assert_eq!(row.get("CAMPAIGN.ID"), "not implemented by googleads-rs");
}

#[test]
fn test_extra_dots_in_path() {
    let campaign = CampaignBuilder::new().id(123).build();

    let row = GoogleAdsRowBuilder::new().with_campaign(campaign).build();

    assert_eq!(row.get("campaign..id"), "not implemented by googleads-rs");

    assert_eq!(row.get("campaign.id."), "not implemented by googleads-rs");
}

#[test]
fn test_empty_string_field_path() {
    let row = GoogleAdsRowBuilder::new().build();

    assert_eq!(row.get(""), "not implemented by googleads-rs");
}

#[test]
fn test_whitespace_in_field_path() {
    let campaign = CampaignBuilder::new().id(123).build();

    let row = GoogleAdsRowBuilder::new().with_campaign(campaign).build();

    assert_eq!(row.get(" campaign.id"), "not implemented by googleads-rs");

    assert_eq!(row.get("campaign.id "), "not implemented by googleads-rs");

    assert_eq!(row.get("campaign .id"), "not implemented by googleads-rs");
}

// ============================================================================
// Missing Optional Parents
// ============================================================================

#[test]
fn test_accessing_field_when_parent_absent() {
    // Create row without campaign
    let row = GoogleAdsRowBuilder::new().build();

    // This should not panic - optional_attr_str! handles missing parent
    assert_eq!(row.get("campaign_budget.amount_micros"), "");
}

#[test]
fn test_accessing_multiple_fields_with_missing_parent() {
    let row = GoogleAdsRowBuilder::new().build();

    // Test multiple optional fields with missing parent
    assert_eq!(row.get("campaign_criterion.status"), "");
    assert_eq!(row.get("campaign_criterion.type"), "");
    assert_eq!(row.get("campaign_criterion.criterion_id"), "");
    assert_eq!(row.get("campaign_criterion.display_name"), "");
}

#[test]
fn test_empty_row_optional_fields_safe() {
    let row = GoogleAdsRowBuilder::new().build();

    // Test that accessing OPTIONAL fields on empty row doesn't panic
    // Note: Required fields (like campaign.id) would panic with unwrap()
    // which is expected behavior - they should only be queried when present
    let test_paths = vec![
        "campaign_budget.amount_micros",
        "campaign_criterion.status",
        "campaign_criterion.type",
        "campaign_criterion.keyword.text",
    ];

    for path in test_paths {
        let result = row.get(path);
        // Optional fields should return empty string when parent is absent
        assert_eq!(
            result, "",
            "Path: {} should return empty string when parent absent",
            path
        );
    }
}

// ============================================================================
// Boundary Conditions
// ============================================================================

#[test]
fn test_very_long_field_path() {
    let row = GoogleAdsRowBuilder::new().build();

    let long_path = "a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z";
    assert_eq!(row.get(long_path), "not implemented by googleads-rs");
}

#[test]
fn test_field_path_with_numbers() {
    let row = GoogleAdsRowBuilder::new().build();

    assert_eq!(
        row.get("campaign123.field456"),
        "not implemented by googleads-rs"
    );
}

#[test]
fn test_field_path_with_underscores() {
    let campaign = CampaignBuilder::new().id(123).build();

    let row = GoogleAdsRowBuilder::new().with_campaign(campaign).build();

    // Valid path with underscores
    assert_eq!(row.get("campaign.campaign_budget"), "");

    // Invalid path with wrong underscores
    assert_eq!(
        row.get("campaign.campaign_budget_wrong"),
        "not implemented by googleads-rs"
    );
}

#[test]
fn test_field_path_with_special_characters() {
    let row = GoogleAdsRowBuilder::new().build();

    let special_paths = vec![
        "campaign@field",
        "campaign#field",
        "campaign$field",
        "campaign%field",
        "campaign&field",
        "campaign*field",
    ];

    for path in special_paths {
        assert_eq!(
            row.get(path),
            "not implemented by googleads-rs",
            "Failed for path: {}",
            path
        );
    }
}

// ============================================================================
// Default Values
// ============================================================================

#[test]
fn test_default_numeric_values() {
    let campaign = CampaignBuilder::new().build();
    let row = GoogleAdsRowBuilder::new().with_campaign(campaign).build();

    // Default i64 values (None) return empty string with prost-reflect
    assert_eq!(row.get("campaign.id"), "");
}

#[test]
fn test_default_string_values() {
    let campaign = CampaignBuilder::new().build();
    let row = GoogleAdsRowBuilder::new().with_campaign(campaign).build();

    // Default string values should be empty
    assert_eq!(row.get("campaign.name"), "");
    assert_eq!(row.get("campaign.end_date_time"), "");
}

#[test]
fn test_default_bool_values() {
    let campaign = CampaignBuilder::new()
        .with_network_settings(false, false, false, false)
        .build();

    let row = GoogleAdsRowBuilder::new().with_campaign(campaign).build();

    assert_eq!(
        row.get("campaign.network_settings.target_search_network"),
        "false"
    );
}

#[test]
fn test_default_enum_values() {
    use googleads_rs::google::ads::googleads::v23::enums::campaign_status_enum::CampaignStatus;

    let campaign = CampaignBuilder::new()
        .status(CampaignStatus::Unspecified)
        .build();

    let row = GoogleAdsRowBuilder::new().with_campaign(campaign).build();

    // Enum values are now returned in SCREAMING_SNAKE_CASE
    assert_eq!(row.get("campaign.status"), "UNSPECIFIED");
}

// ============================================================================
// Multiple Resources in Row
// ============================================================================

#[test]
fn test_accessing_missing_resource_in_populated_row() {
    let campaign = CampaignBuilder::new().id(111).build();

    let metrics = MetricsBuilder::new().impressions(1000).build();

    let row = GoogleAdsRowBuilder::new()
        .with_campaign(campaign)
        .with_metrics(metrics)
        .build();

    // Campaign and metrics are present
    assert_eq!(row.get("campaign.id"), "111");
    assert_eq!(row.get("metrics.impressions"), "1000");

    // Optional fields like campaign_budget should return empty when absent
    assert_eq!(row.get("campaign_budget.amount_micros"), "");
}

#[test]
fn test_mixed_present_and_absent_resources() {
    let campaign = CampaignBuilder::new().id(222).name("Test Campaign").build();

    let segments = SegmentsBuilder::new().date("2024-10-10").build();

    let row = GoogleAdsRowBuilder::new()
        .with_campaign(campaign)
        .with_segments(segments)
        .build();

    // Present resources
    assert_eq!(row.get("campaign.id"), "222");
    assert_eq!(row.get("segments.date"), "2024-10-10");

    // Optional absent resources return empty string
    assert_eq!(row.get("campaign_criterion.status"), "");
    assert_eq!(row.get("campaign_budget.amount_micros"), "");
}

// ============================================================================
// Real-world Error Scenarios
// ============================================================================

#[test]
fn test_common_field_name_mistakes() {
    let campaign = CampaignBuilder::new().id(123).name("Test").build();

    let row = GoogleAdsRowBuilder::new().with_campaign(campaign).build();

    // Common mistakes developers might make
    let mistakes = vec![
        ("campaign.campaignid", "campaign.id"),  // Missing underscore
        ("campaign.campaign_id", "campaign.id"), // Extra word
        ("campaign.name_", "campaign.name"),     // Trailing underscore
        ("campaign.status_", "campaign.status"), // Trailing underscore
    ];

    for (wrong, _correct) in mistakes {
        assert_eq!(
            row.get(wrong),
            "not implemented by googleads-rs",
            "Wrong path '{}' should return not implemented",
            wrong
        );
    }
}

#[test]
fn test_sql_injection_like_input() {
    let row = GoogleAdsRowBuilder::new().build();

    // Test that weird inputs don't cause issues
    let weird_inputs = vec![
        "'; DROP TABLE campaigns; --",
        "campaign.id; DELETE FROM ads;",
        "../../../etc/passwd",
        "campaign.id OR 1=1",
    ];

    for input in weird_inputs {
        let result = row.get(input);
        assert_eq!(
            result, "not implemented by googleads-rs",
            "Weird input '{}' should return not implemented",
            input
        );
    }
}

#[test]
fn test_unicode_in_field_paths() {
    let row = GoogleAdsRowBuilder::new().build();

    let unicode_paths = vec![
        "campaign.名前", // Japanese
        "campaign.名字", // Chinese
        "campaign.имя",  // Russian
        "campaign.🎯",   // Emoji
    ];

    for path in unicode_paths {
        assert_eq!(
            row.get(path),
            "not implemented by googleads-rs",
            "Unicode path '{}' should return not implemented",
            path
        );
    }
}

// ============================================================================
// Performance Edge Cases
// ============================================================================

#[test]
fn test_many_sequential_gets() {
    let campaign = CampaignBuilder::new().id(999).name("Test").build();

    let row = GoogleAdsRowBuilder::new().with_campaign(campaign).build();

    // Call get() many times to ensure no memory issues
    for _ in 0..1000 {
        assert_eq!(row.get("campaign.id"), "999");
        assert_eq!(row.get("campaign.name"), "Test");
    }
}

#[test]
fn test_many_different_unimplemented_paths() {
    let row = GoogleAdsRowBuilder::new().build();

    // Test many different unimplemented paths
    for i in 0..100 {
        let path = format!("resource{}.field{}", i, i);
        assert_eq!(row.get(&path), "not implemented by googleads-rs");
    }
}

// ============================================================================
// Empty Segment Handling Tests
// ============================================================================

#[test]
fn test_empty_segment_trailing_dot() {
    let campaign = CampaignBuilder::new().id(123).build();
    let row = GoogleAdsRowBuilder::new().with_campaign(campaign).build();

    // Trailing dot creates an empty segment
    assert_eq!(row.get("campaign.id."), "not implemented by googleads-rs");
}

#[test]
fn test_empty_segment_double_dot() {
    let campaign = CampaignBuilder::new().id(123).build();
    let row = GoogleAdsRowBuilder::new().with_campaign(campaign).build();

    // Double dot creates an empty segment in the middle
    assert_eq!(row.get("campaign..id"), "not implemented by googleads-rs");
}

#[test]
fn test_empty_segment_leading_dot() {
    let campaign = CampaignBuilder::new().id(123).build();
    let row = GoogleAdsRowBuilder::new().with_campaign(campaign).build();

    // Leading dot creates an empty first segment
    assert_eq!(row.get(".campaign.id"), "not implemented by googleads-rs");
}

#[test]
fn test_empty_segment_multiple_dots() {
    let campaign = CampaignBuilder::new().id(123).build();
    let row = GoogleAdsRowBuilder::new().with_campaign(campaign).build();

    // Multiple consecutive dots create multiple empty segments
    assert_eq!(row.get("campaign...id"), "not implemented by googleads-rs");
}

#[test]
fn test_empty_segment_only_dot() {
    let row = GoogleAdsRowBuilder::new().build();

    // Just a single dot
    assert_eq!(row.get("."), "not implemented by googleads-rs");
}

#[test]
fn test_empty_segment_only_dots() {
    let row = GoogleAdsRowBuilder::new().build();

    // Multiple dots only
    assert_eq!(row.get("..."), "not implemented by googleads-rs");
}

// ============================================================================
// Responsive Search Ad Special Case Tests
// ============================================================================

#[test]
fn test_responsive_search_ad_headlines_special_case() {
    // This tests the special case in get_field_from_dynamic() for headlines
    use test_helpers::{AdBuilder, AdGroupAdBuilder};

    let ad = AdBuilder::new()
        .with_responsive_search_ad(
            vec!["Headline 1", "Headline 2"],
            vec!["Description"],
            None,
            None,
        )
        .build();

    let ad_group_ad = AdGroupAdBuilder::new().with_ad(ad).build();
    let row = GoogleAdsRowBuilder::new()
        .with_ad_group_ad(ad_group_ad)
        .build();

    // The special case should extract .text from each headline
    let result = row.get("ad_group_ad.ad.responsive_search_ad.headlines");
    assert_eq!(result, "Headline 1, Headline 2");
}

#[test]
fn test_responsive_search_ad_descriptions_special_case() {
    // This tests the special case in get_field_from_dynamic() for descriptions
    use test_helpers::{AdBuilder, AdGroupAdBuilder};

    let ad = AdBuilder::new()
        .with_responsive_search_ad(
            vec!["Headline"],
            vec!["Description 1", "Description 2", "Description 3"],
            None,
            None,
        )
        .build();

    let ad_group_ad = AdGroupAdBuilder::new().with_ad(ad).build();
    let row = GoogleAdsRowBuilder::new()
        .with_ad_group_ad(ad_group_ad)
        .build();

    // The special case should extract .text from each description
    let result = row.get("ad_group_ad.ad.responsive_search_ad.descriptions");
    assert_eq!(result, "Description 1, Description 2, Description 3");
}

#[test]
fn test_responsive_search_ad_empty_headlines() {
    use test_helpers::{AdBuilder, AdGroupAdBuilder};

    let ad = AdBuilder::new()
        .with_responsive_search_ad(vec![], vec!["Description"], None, None)
        .build();

    let ad_group_ad = AdGroupAdBuilder::new().with_ad(ad).build();
    let row = GoogleAdsRowBuilder::new()
        .with_ad_group_ad(ad_group_ad)
        .build();

    let result = row.get("ad_group_ad.ad.responsive_search_ad.headlines");
    assert_eq!(result, "");
}

#[test]
fn test_responsive_search_ad_empty_descriptions() {
    use test_helpers::{AdBuilder, AdGroupAdBuilder};

    let ad = AdBuilder::new()
        .with_responsive_search_ad(vec!["Headline"], vec![], None, None)
        .build();

    let ad_group_ad = AdGroupAdBuilder::new().with_ad(ad).build();
    let row = GoogleAdsRowBuilder::new()
        .with_ad_group_ad(ad_group_ad)
        .build();

    let result = row.get("ad_group_ad.ad.responsive_search_ad.descriptions");
    assert_eq!(result, "");
}

#[test]
fn test_responsive_search_ad_single_headline() {
    use test_helpers::{AdBuilder, AdGroupAdBuilder};

    let ad = AdBuilder::new()
        .with_responsive_search_ad(vec!["Only Headline"], vec!["Description"], None, None)
        .build();

    let ad_group_ad = AdGroupAdBuilder::new().with_ad(ad).build();
    let row = GoogleAdsRowBuilder::new()
        .with_ad_group_ad(ad_group_ad)
        .build();

    let result = row.get("ad_group_ad.ad.responsive_search_ad.headlines");
    assert_eq!(result, "Only Headline");
}

#[test]
fn test_responsive_search_ad_headlines_with_special_chars() {
    use test_helpers::{AdBuilder, AdGroupAdBuilder};

    let ad = AdBuilder::new()
        .with_responsive_search_ad(
            vec!["50% Off Sale!", "Buy Now & Save", "Limited \"Time\" Offer"],
            vec!["Description"],
            None,
            None,
        )
        .build();

    let ad_group_ad = AdGroupAdBuilder::new().with_ad(ad).build();
    let row = GoogleAdsRowBuilder::new()
        .with_ad_group_ad(ad_group_ad)
        .build();

    let result = row.get("ad_group_ad.ad.responsive_search_ad.headlines");
    assert!(result.contains("50% Off Sale!"));
    assert!(result.contains("Buy Now & Save"));
    assert!(result.contains("Limited \"Time\" Offer"));
}