|
6 | 6 | "context" |
7 | 7 | "encoding/json" |
8 | 8 | "fmt" |
| 9 | + "reflect" |
9 | 10 | "slices" |
10 | 11 | "strings" |
11 | 12 | "testing" |
@@ -50,6 +51,13 @@ type RequireInsertedOpts struct { |
50 | 51 | // No assertion is made if left the zero value. |
51 | 52 | MaxAttempts int |
52 | 53 |
|
| 54 | + // Metadata is a subset of job metadata to assert against. Only the keys and |
| 55 | + // values provided are compared, and any extra metadata on the job is |
| 56 | + // ignored. |
| 57 | + // |
| 58 | + // No assertion is made if left nil or empty. |
| 59 | + Metadata map[string]any |
| 60 | + |
53 | 61 | // Priority is the expected priority for the inserted job. |
54 | 62 | // |
55 | 63 | // No assertion is made if left the zero value. |
@@ -501,6 +509,16 @@ func compareJobToInsertOpts(t testingT, jobRow *rivertype.JobRow, expectedOpts * |
501 | 509 | } |
502 | 510 | } |
503 | 511 |
|
| 512 | + if len(expectedOpts.Metadata) > 0 { |
| 513 | + metadataMatches, metadataFailures := compareMetadataSubset(t, jobRow.Metadata, expectedOpts.Metadata, requireNotInserted) |
| 514 | + |
| 515 | + if !metadataMatches && requireNotInserted { |
| 516 | + return true |
| 517 | + } |
| 518 | + |
| 519 | + failures = append(failures, metadataFailures...) |
| 520 | + } |
| 521 | + |
504 | 522 | if expectedOpts.Priority != 0 { |
505 | 523 | if jobRow.Priority == expectedOpts.Priority { |
506 | 524 | if requireNotInserted { |
@@ -594,6 +612,94 @@ func compareJobToInsertOpts(t testingT, jobRow *rivertype.JobRow, expectedOpts * |
594 | 612 | return false |
595 | 613 | } |
596 | 614 |
|
| 615 | +func compareMetadataSubset(t testingT, jobMetadataBytes []byte, expectedMetadata map[string]any, requireNotInserted bool) (bool, []string) { |
| 616 | + t.Helper() |
| 617 | + |
| 618 | + jobMetadata := map[string]any{} |
| 619 | + if len(jobMetadataBytes) > 0 { |
| 620 | + if err := json.Unmarshal(jobMetadataBytes, &jobMetadata); err != nil { |
| 621 | + failuref(t, "Internal failure: error unmarshaling job metadata: %s", err) |
| 622 | + } |
| 623 | + } |
| 624 | + |
| 625 | + keys := make([]string, 0, len(expectedMetadata)) |
| 626 | + for key := range expectedMetadata { |
| 627 | + keys = append(keys, key) |
| 628 | + } |
| 629 | + slices.Sort(keys) |
| 630 | + |
| 631 | + failures := make([]string, 0, len(keys)) |
| 632 | + allMatch := true |
| 633 | + for _, key := range keys { |
| 634 | + expectedValue := expectedMetadata[key] |
| 635 | + |
| 636 | + actualValue, ok := jobMetadata[key] |
| 637 | + if !ok { |
| 638 | + allMatch = false |
| 639 | + if requireNotInserted { |
| 640 | + return false, nil |
| 641 | + } |
| 642 | + failures = append(failures, fmt.Sprintf("metadata missing key '%s'", key)) |
| 643 | + continue |
| 644 | + } |
| 645 | + |
| 646 | + if expectedValue == nil { |
| 647 | + if actualValue == nil { |
| 648 | + if requireNotInserted { |
| 649 | + failures = append(failures, fmt.Sprintf("metadata[%s] equal to excluded null", key)) |
| 650 | + } |
| 651 | + } else { |
| 652 | + allMatch = false |
| 653 | + if requireNotInserted { |
| 654 | + return false, nil |
| 655 | + } |
| 656 | + failures = append(failures, fmt.Sprintf("metadata[%s] %s not equal to expected null", key, formatMetadataValue(actualValue))) |
| 657 | + } |
| 658 | + continue |
| 659 | + } |
| 660 | + |
| 661 | + normalizedExpected, err := normalizeMetadataValue(expectedValue) |
| 662 | + if err != nil { |
| 663 | + failuref(t, "Internal failure: error normalizing metadata for key '%s': %s", key, err) |
| 664 | + } |
| 665 | + |
| 666 | + if reflect.DeepEqual(actualValue, normalizedExpected) { |
| 667 | + if requireNotInserted { |
| 668 | + failures = append(failures, fmt.Sprintf("metadata[%s] equal to excluded %s", key, formatMetadataValue(normalizedExpected))) |
| 669 | + } |
| 670 | + } else { |
| 671 | + allMatch = false |
| 672 | + if requireNotInserted { |
| 673 | + return false, nil |
| 674 | + } |
| 675 | + failures = append(failures, fmt.Sprintf("metadata[%s] %s not equal to expected %s", key, formatMetadataValue(actualValue), formatMetadataValue(normalizedExpected))) |
| 676 | + } |
| 677 | + } |
| 678 | + |
| 679 | + return allMatch, failures |
| 680 | +} |
| 681 | + |
| 682 | +func formatMetadataValue(value any) string { |
| 683 | + encoded, err := json.Marshal(value) |
| 684 | + if err != nil { |
| 685 | + return fmt.Sprintf("%v", value) |
| 686 | + } |
| 687 | + return string(encoded) |
| 688 | +} |
| 689 | + |
| 690 | +func normalizeMetadataValue(value any) (any, error) { |
| 691 | + encoded, err := json.Marshal(value) |
| 692 | + if err != nil { |
| 693 | + return nil, err |
| 694 | + } |
| 695 | + |
| 696 | + var normalized any |
| 697 | + if err := json.Unmarshal(encoded, &normalized); err != nil { |
| 698 | + return nil, err |
| 699 | + } |
| 700 | + return normalized, nil |
| 701 | +} |
| 702 | + |
597 | 703 | // failuref takes a printf-style directive and is a shortcut for failing an |
598 | 704 | // assertion. |
599 | 705 | func failuref(t testingT, format string, a ...any) { |
|
0 commit comments