-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplexTupleTests.cs
More file actions
232 lines (212 loc) · 8.44 KB
/
ComplexTupleTests.cs
File metadata and controls
232 lines (212 loc) · 8.44 KB
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
using Amazon.DynamoDBv2.Model;
using AutoFixture;
using DynamoDBGenerator.Attributes;
using DynamoDBGenerator.Exceptions;
using DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Asserters;
namespace DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests.Marshaller.Tuples;
[DynamoDBMarshaller(EntityType =
typeof((string FirstName, int Age,
IReadOnlyCollection<((string Address, string? ZipCode) Address, (string Email, string Phone)? Mediums)>
PhoneAndEmail )))]
public partial class ComplexTupleTests : MarshalAsserter<(string FirstName, int Age,
IReadOnlyCollection<((string Address, string? ZipCode) Address, (string Email, string Phone)? Mediums)>
PhoneAndEmail)>
{
private static readonly Fixture Fixture = new();
public static IEnumerable<object[]> InvalidData
{
get
{
yield return new object[]
{
"FirstName",
(
null as string,
2,
Array.Empty<((string Address, string? ZipCode), (string Email, string Phone)?)>()
)
};
yield return new object[]
{
"Address",
(
"Bob",
2,
new ((string? Address, string? ZipCode), (string Email, string Phone)?)[]
{
((null, "ZIP"), ("EMAIL", "+PHONE"))
}
)
};
yield return new object[]
{
"FirstName",
(
null as string,
2,
new ((string? Address, string? ZipCode), (string Email, string Phone)?)[]
{
((null, "ZIP"), ("EMAIL", "+PHONE"))
}
)
};
yield return new object[]
{
"Email",
(
"Bob",
2,
new ((string Address, string? ZipCode), (string? Email, string Phone)?)[]
{
(("ADDRESS", "ZIP"), (null, "PHONE"))
}
)
};
yield return new object[]
{
"Phone",
(
"Bob",
2,
new ((string Address, string? ZipCode), (string Email, string? Phone)?)[]
{
(("ADDRESS", "ZIP"), ("EMAIL", null))
}
)
};
}
}
private static ((string FirstName, int Age,
IReadOnlyCollection<((string Address, string? ZipCode) Address, (string Email, string Phone)? Mediums)>
PhoneAndEmail)
tuple, Dictionary<string, AttributeValue> dict) Av(
(string FirstName, int Age,
IReadOnlyCollection<((string Address, string? ZipCode) Address, (string Email, string Phone)? Mediums)>
PhoneAndEmail) tuple
)
{
var dict = new Dictionary<string, AttributeValue>
{
{ nameof(tuple.FirstName), new AttributeValue { S = tuple.FirstName } },
{ nameof(tuple.Age), new AttributeValue { N = tuple.Age.ToString() } },
{
nameof(tuple.PhoneAndEmail),
new AttributeValue { L = tuple.PhoneAndEmail.Select(BuildPhoneAndMail).ToList() }
}
};
return (tuple, dict);
static AttributeValue BuildPhoneAndMail(
((string Address, string? ZipCode) Address, (string Email, string Phone)? Mediums) valueTuple)
{
var attributeValue = new AttributeValue
{
M = new Dictionary<string, AttributeValue>
{
{
nameof(valueTuple.Address), new AttributeValue
{
M = valueTuple.Address.ZipCode is null
? new Dictionary<string, AttributeValue>
{
{
nameof(valueTuple.Address.Address),
new AttributeValue { S = valueTuple.Address.Address }
}
}
: new Dictionary<string, AttributeValue>
{
{
nameof(valueTuple.Address.Address),
new AttributeValue { S = valueTuple.Address.Address }
},
{
nameof(valueTuple.Address.ZipCode),
new AttributeValue { S = valueTuple.Address.ZipCode }
}
}
}
}
}
};
if (valueTuple.Mediums is { } mediums)
attributeValue.M.Add(nameof(valueTuple.Mediums), new AttributeValue
{
M = new Dictionary<string, AttributeValue>
{
{ nameof(mediums.Email), new AttributeValue { S = mediums.Email } },
{ nameof(mediums.Phone), new AttributeValue { S = mediums.Phone } }
}
});
return attributeValue;
}
}
protected override IEnumerable<((string FirstName, int Age,
IReadOnlyCollection<((string Address, string? ZipCode) Address, (string Email, string Phone)? Mediums)>
PhoneAndEmail)
element, Dictionary<string, AttributeValue>
attributeValues)> Arguments()
{
var customizationComposer =
Fixture
.Build<(string FirstName, int Age,
IReadOnlyCollection<((string Address, string? ZipCode) Address, (string Email, string Phone)?
Mediums)>
PhoneAndEmail)>();
yield return Av(customizationComposer.Create());
yield return Av(
customizationComposer.Create() switch
{
var a => a with { PhoneAndEmail = a.PhoneAndEmail.Select(y => y with { Mediums = null }).ToArray() }
}
);
yield return Av(
customizationComposer.Create() switch
{
var a => a with
{
PhoneAndEmail = a.PhoneAndEmail.Select(x => x with { Address = x.Address with { ZipCode = null } })
.ToArray()
}
}
);
}
[Theory]
[MemberData(nameof(InvalidData))]
public void Unmarshall_NullField_Throws(
string dataMember,
(string FirstName, int Age,
((string Address, string? ZipCode) Address, (string Email, string Phone)? Mediums)[] PhoneAndEmail
) arg)
{
var (_, dict) = Av(arg);
var act = () => ValueTupleMarshaller.Unmarshall(dict);
act.Should().Throw<DynamoDBMarshallingException>().Which.MemberName.Should().Be(dataMember);
}
[Theory]
[MemberData(nameof(InvalidData))]
public void Marshall_NullField_Throws(
string dataMember,
(string FirstName, int Age,
((string Address, string? ZipCode) Address, (string Email, string Phone)? Mediums)[] PhoneAndEmail
) arg)
{
var (e, _) = Av(arg);
var act = () => ValueTupleMarshaller.Marshall(e);
act.Should().Throw<DynamoDBMarshallingException>().Which.MemberName.Should().Be(dataMember);
}
protected override (string FirstName, int Age,
IReadOnlyCollection<((string Address, string? ZipCode) Address, (string Email, string Phone)? Mediums)>
PhoneAndEmail)
UnmarshallImplementation(
Dictionary<string, AttributeValue> attributeValues)
{
return ValueTupleMarshaller.Unmarshall(attributeValues);
}
protected override Dictionary<string, AttributeValue> MarshallImplementation(
(string FirstName, int Age,
IReadOnlyCollection<((string Address, string? ZipCode) Address, (string Email, string Phone)? Mediums)>
PhoneAndEmail) element)
{
return ValueTupleMarshaller.Marshall(element);
}
}