File size: 5,564 Bytes
b110593
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
//                           _       _
// __      _____  __ ___   ___  __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
//  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
//   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
//  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
//  CONTACT: [email protected]
//

package validation

import (
	"context"
	"errors"
	"fmt"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"github.com/weaviate/weaviate/entities/models"
	"github.com/weaviate/weaviate/usecases/config"
)

func TestPropertyOfTypePhoneNumberValidation(t *testing.T) {
	type test struct {
		name           string
		phone          interface{} // "phone" property in schema
		expectedErr    error
		expectedResult *models.PhoneNumber
	}

	tests := []test{
		{
			name:  "phone of wrong type",
			phone: "how about a string",
			expectedErr: errors.New("invalid phoneNumber property 'phone' on class 'Person': " +
				"phoneNumber must be a map, but got: string"),
		},
		{
			name:  "phone map missing all keys",
			phone: map[string]interface{}{},
			expectedErr: errors.New("invalid phoneNumber property 'phone' on class 'Person': " +
				"phoneNumber is missing required field 'input'"),
		},
		{
			name: "input is not a string",
			phone: map[string]interface{}{
				"input": 1234,
			},
			expectedErr: errors.New("invalid phoneNumber property 'phone' on class 'Person': " +
				"phoneNumber.input must be a string"),
		},
		{
			name: "default country is not a string",
			phone: map[string]interface{}{
				"input":          "1234",
				"defaultCountry": 7,
			},
			expectedErr: errors.New("invalid phoneNumber property 'phone' on class 'Person': " +
				"phoneNumber.defaultCountry must be a string"),
		},
		{
			name: "with only input set",
			phone: map[string]interface{}{
				"input": "+491711234567",
			},
			expectedErr: nil,
			expectedResult: &models.PhoneNumber{
				Valid:                  true,
				Input:                  "+491711234567",
				InternationalFormatted: "+49 171 1234567",
				CountryCode:            49,
				National:               1711234567,
				NationalFormatted:      "0171 1234567",
			},
		},
		{
			name: "with national number and country uppercased",
			phone: map[string]interface{}{
				"input":          "01711234567",
				"defaultCountry": "DE",
			},
			expectedErr: nil,
			expectedResult: &models.PhoneNumber{
				Valid:                  true,
				DefaultCountry:         "DE",
				Input:                  "01711234567",
				InternationalFormatted: "+49 171 1234567",
				CountryCode:            49,
				National:               1711234567,
				NationalFormatted:      "0171 1234567",
			},
		},
		{
			name: "with national number, but missing defaultCountry",
			phone: map[string]interface{}{
				"input": "01711234567",
			},
			expectedErr: fmt.Errorf("invalid phoneNumber property 'phone' on class 'Person': " +
				"invalid phone number: invalid or missing defaultCountry - " +
				"this field is optional if the specified number is in the international format, " +
				"but required if the number is in national format, use ISO 3166-1 alpha-2"),
		},
		{
			name: "with national number and country uppercased",
			phone: map[string]interface{}{
				"input":          "01711234567",
				"defaultCountry": "de",
			},
			expectedErr: nil,
			expectedResult: &models.PhoneNumber{
				Valid:                  true,
				DefaultCountry:         "DE",
				Input:                  "01711234567",
				InternationalFormatted: "+49 171 1234567",
				CountryCode:            49,
				National:               1711234567,
				NationalFormatted:      "0171 1234567",
			},
		},
		{
			name: "with national number and various special characters",
			phone: map[string]interface{}{
				"input":          "(0)171-123 456 7",
				"defaultCountry": "de",
			},
			expectedErr: nil,
			expectedResult: &models.PhoneNumber{
				Valid:                  true,
				DefaultCountry:         "DE",
				Input:                  "(0)171-123 456 7",
				InternationalFormatted: "+49 171 1234567",
				CountryCode:            49,
				National:               1711234567,
				NationalFormatted:      "0171 1234567",
			},
		},
		{
			name: "with international number and optional zero after country code",
			phone: map[string]interface{}{
				"input":          "+49 (0) 171 123 456 7",
				"defaultCountry": "de",
			},
			expectedErr: nil,
			expectedResult: &models.PhoneNumber{
				Valid:                  true,
				DefaultCountry:         "DE",
				Input:                  "+49 (0) 171 123 456 7",
				InternationalFormatted: "+49 171 1234567",
				CountryCode:            49,
				National:               1711234567,
				NationalFormatted:      "0171 1234567",
			},
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			config := &config.WeaviateConfig{}
			validator := New(fakeExists, config, nil)

			obj := &models.Object{
				Class: "Person",
				Properties: map[string]interface{}{
					"phone": test.phone,
				},
			}
			schema := testSchema()
			err := validator.properties(context.Background(), schema.Objects.Classes[0], obj, nil)
			assert.Equal(t, test.expectedErr, err)
			if err != nil {
				return
			}
			phone, ok := obj.Properties.(map[string]interface{})["phone"]
			require.True(t, ok)
			assert.Equal(t, test.expectedResult, phone)
		})
	}
}