|
| 1 | +package threemagateway |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestRecipient_UnmarshalText_And_MarshalText(t *testing.T) { |
| 10 | + scenarios := []struct { |
| 11 | + name string |
| 12 | + input string |
| 13 | + expected Recipient |
| 14 | + expectError error |
| 15 | + }{ |
| 16 | + { |
| 17 | + name: "default recipient type", |
| 18 | + input: "individual", |
| 19 | + expected: Recipient{Type: defaultRecipientType, Value: "individual"}, |
| 20 | + expectError: nil, |
| 21 | + }, |
| 22 | + { |
| 23 | + name: "empty input", |
| 24 | + input: "", |
| 25 | + expected: Recipient{Type: defaultRecipientType, Value: ""}, |
| 26 | + expectError: nil, |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "invalid format", |
| 30 | + input: "type:value:extra", |
| 31 | + expectError: ErrInvalidRecipientFormat, |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "invalid recipient type", |
| 35 | + input: "unknown:value", |
| 36 | + expectError: ErrInvalidRecipientType, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "valid phone recipient", |
| 40 | + input: "phone:+1234567890", |
| 41 | + expected: Recipient{Type: RecipientTypePhone, Value: "+1234567890"}, |
| 42 | + expectError: nil, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "valid email recipient", |
| 46 | + |
| 47 | + expected: Recipient{ Type: RecipientTypeEmail, Value: "[email protected]"}, |
| 48 | + expectError: nil, |
| 49 | + }, |
| 50 | + } |
| 51 | + |
| 52 | + for _, scenario := range scenarios { |
| 53 | + t.Run(scenario.name, func(t *testing.T) { |
| 54 | + var recipient Recipient |
| 55 | + err := recipient.UnmarshalText([]byte(scenario.input)) |
| 56 | + |
| 57 | + if !errors.Is(err, scenario.expectError) { |
| 58 | + t.Fatalf("expected error for scenario '%s': %v, got: %v", scenario.name, scenario.expectError, err) |
| 59 | + } |
| 60 | + |
| 61 | + if scenario.expectError == nil && recipient != scenario.expected { |
| 62 | + t.Fatalf("expected recipient for scenario '%s': %+v, got: %+v", scenario.name, scenario.expected, recipient) |
| 63 | + } |
| 64 | + |
| 65 | + if scenario.expectError == nil { |
| 66 | + marshaled, err := recipient.MarshalText() |
| 67 | + if err != nil { |
| 68 | + t.Fatalf("unexpected error during marshaling for scenario '%s': %v", scenario.name, err) |
| 69 | + } |
| 70 | + expectedMarshaled := scenario.input |
| 71 | + if strings.Contains(scenario.input, ":") == false { |
| 72 | + expectedMarshaled = "id:" + scenario.input |
| 73 | + } |
| 74 | + if string(marshaled) != expectedMarshaled { |
| 75 | + t.Fatalf("expected marshaled text for scenario '%s': %s, got: %s", scenario.name, expectedMarshaled, string(marshaled)) |
| 76 | + } |
| 77 | + } |
| 78 | + }) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func TestRecipient_Validate(t *testing.T) { |
| 83 | + scenarios := []struct { |
| 84 | + name string |
| 85 | + input Recipient |
| 86 | + expectError error |
| 87 | + }{ |
| 88 | + { |
| 89 | + name: "empty recipient", |
| 90 | + input: Recipient{Type: defaultRecipientType, Value: ""}, |
| 91 | + expectError: ErrInvalidRecipientFormat, |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "valid id recipient", |
| 95 | + input: Recipient{Type: RecipientTypeId, Value: "ABCDEFGH"}, |
| 96 | + expectError: nil, |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "valid phone recipient", |
| 100 | + input: Recipient{Type: RecipientTypePhone, Value: "+1234567890"}, |
| 101 | + expectError: nil, |
| 102 | + }, |
| 103 | + { |
| 104 | + name: "invalid phone recipient", |
| 105 | + input: Recipient{Type: RecipientTypePhone, Value: "123-456-7890"}, |
| 106 | + expectError: ErrInvalidPhoneNumberFormat, |
| 107 | + }, |
| 108 | + { |
| 109 | + name: "valid email recipient", |
| 110 | + input: Recipient{ Type: RecipientTypeEmail, Value: "[email protected]"}, |
| 111 | + expectError: nil, |
| 112 | + }, |
| 113 | + { |
| 114 | + name: "invalid email recipient", |
| 115 | + input: Recipient{Type: RecipientTypeEmail, Value: "mailtest.com"}, |
| 116 | + expectError: ErrInvalidEmailAddressFormat, |
| 117 | + }, |
| 118 | + { |
| 119 | + name: "invalid recipient type", |
| 120 | + input: Recipient{Type: RecipientTypeInvalid, Value: "value"}, |
| 121 | + expectError: ErrInvalidRecipientType, |
| 122 | + }, |
| 123 | + } |
| 124 | + |
| 125 | + for _, scenario := range scenarios { |
| 126 | + t.Run(scenario.name, func(t *testing.T) { |
| 127 | + err := scenario.input.Validate() |
| 128 | + |
| 129 | + if !errors.Is(err, scenario.expectError) { |
| 130 | + t.Fatalf("expected error for scenario '%s': %v, got: %v", scenario.name, scenario.expectError, err) |
| 131 | + } |
| 132 | + }) |
| 133 | + } |
| 134 | +} |
0 commit comments