-
Notifications
You must be signed in to change notification settings - Fork 0
/
AssertMaker.cs
148 lines (128 loc) · 5.53 KB
/
AssertMaker.cs
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
public class AssertMaker
{
private readonly BaseGenerator baseGenerator;
private readonly Liner liner;
private readonly string target;
public AssertMaker(BaseGenerator baseGenerator, Liner liner, string target)
{
this.baseGenerator = baseGenerator;
this.liner = liner;
this.target = target;
}
public void EntityField(GeneratorConfig.ModelConfig m, string errorMessage)
{
foreach (var f in m.Fields)
{
EqualsTestEntity(m, f, errorMessage);
}
var foreignProperties = baseGenerator.GetForeignProperties(m);
foreach (var f in foreignProperties)
{
if (!f.IsSelfReference)
{
ForeignIdEquals(m, f, errorMessage);
}
}
}
public void EqualsTestEntity(GeneratorConfig.ModelConfig m, GeneratorConfig.ModelField f, string errorMessage)
{
var target = m.Name + "1." + f.Name;
AddAssertEquals(m, f, target, errorMessage);
}
public void IdEquals(GeneratorConfig.ModelConfig m, string errorMessage)
{
liner.Add("Assert.That(" + target + ".Id, Is.EqualTo(TestData." + m.Name + "1.Id)," + FormatErrorMessage(m, "Id", errorMessage) + ");");
}
public void EqualsTestScalar(GeneratorConfig.ModelConfig m, GeneratorConfig.ModelField f, string errorMessage)
{
var target = f.Type.FirstToUpper();
AddAssertEquals(m, f, target, errorMessage);
}
public void ForeignIdEquals(GeneratorConfig.ModelConfig m, ForeignProperty f, string errorMessage)
{
liner.Add("Assert.That(" + target + "." + f.WithId + ", Is.EqualTo(TestData." + f.Type + "1.Id)," + FormatErrorMessage(m, f, errorMessage) + ");");
}
public void CollectionOne(GeneratorConfig.ModelConfig m, string collectionName)
{
liner.Add("Assert.That(" + collectionName + ".Count, Is.EqualTo(1), \"Expected only 1 " + m.Name + "\");");
}
public void CollectionEmpty(GeneratorConfig.ModelConfig m, string collectionName)
{
liner.Add("CollectionAssert.IsEmpty(" + collectionName + ", \"Expected 0 " + m.Name + ".\");");
}
public void ErrorMessage(GeneratorConfig.ModelConfig m, string idTag)
{
var expectedErrorMessage = "Unable to find '" + m.Name + "' by Id: '\" + " + idTag + " + \"'";
liner.Add("Assert.That(errors[0].Message, Is.EqualTo(\"" + expectedErrorMessage + "\"), \"Unexpected error message.\");");
}
public void FailedToFindMutationResponse(GeneratorConfig.ModelConfig m, string mutation)
{
if (baseGenerator.IsFailedToFindStrategyErrorCode())
{
CollectionOne(m, "errors");
ErrorMessage(m, "TestData." + m.Name + "1.Id");
}
if (baseGenerator.IsFailedToFindStrategyNullObject())
{
NoErrors(mutation);
NullReturned(m, mutation);
}
}
public void FailedToFindQueryResponse(GeneratorConfig.ModelConfig m)
{
if (baseGenerator.IsFailedToFindStrategyErrorCode())
{
CollectionOne(m, "errors");
ErrorMessage(m, "TestData.Test" + baseGenerator.Config.IdType.FirstToUpper());
}
if (baseGenerator.IsFailedToFindStrategyNullObject())
{
NoErrors("query");
NullReturned(m, "");
}
}
public void NoErrors(string queryOrMutation)
{
liner.Add("CollectionAssert.IsEmpty(errors, \"Expected " + queryOrMutation + " to not return errors.\");");
}
public void NullReturned(GeneratorConfig.ModelConfig m, string mutation)
{
var field = mutation + m.Name;
liner.Add("Assert.That(gqlData.Data." + field + ", Is.Null, \"Expected null object to be returned.\");");
}
public void NoErrors()
{
liner.Add("gqlData.AssertNoErrors();");
}
public void DeleteResponse(GeneratorConfig.ModelConfig m)
{
var field = baseGenerator.Config.GraphQl.GqlMutationsDeleteMethod + m.Name;
if (baseGenerator.IsFailedToFindStrategyNullObject())
{
liner.Add("if (response.Data." + field + " == null) throw new AssertionException(\"Unexpected null returned by " +
baseGenerator.Config.GraphQl.GqlMutationsDeleteMethod + " mutation.\");");
}
liner.Add("Assert.That(response.Data." + field + ", Is.EqualTo(TestData." + m.Name + "1.Id), \"Incorrect Id returned by " +
baseGenerator.Config.GraphQl.GqlMutationsDeleteMethod + " mutation.\");");
}
public void EntityNotNull(string operation)
{
liner.Add("if (" + target + " == null) throw new AssertionException(\"Unexpected null returned by '" + operation + "'.\");");
}
private string FormatErrorMessage(GeneratorConfig.ModelConfig m, GeneratorConfig.ModelField f, string errorMessage)
{
return FormatErrorMessage(m, f.Name, errorMessage);
}
private string FormatErrorMessage(GeneratorConfig.ModelConfig m, ForeignProperty f, string errorMessage)
{
return FormatErrorMessage(m, f.Name, errorMessage);
}
private string FormatErrorMessage(GeneratorConfig.ModelConfig m, string f, string errorMessage)
{
return " \"" + errorMessage + " (" + m.Name + "." + f + ")\"";
}
private void AddAssertEquals(GeneratorConfig.ModelConfig m, GeneratorConfig.ModelField f, string testTarget, string errorMessage)
{
liner.Add("Assert.That(" + target + "." + f.Name + ", Is.EqualTo(TestData." + testTarget + ")" + TypeUtils.GetAssertPostfix(f.Type) + "," + FormatErrorMessage(m, f, errorMessage) + ");");
}
}