-
-
Notifications
You must be signed in to change notification settings - Fork 804
Expand file tree
/
Copy pathOperationDocumentHelperTests.cs
More file actions
98 lines (80 loc) · 2.91 KB
/
OperationDocumentHelperTests.cs
File metadata and controls
98 lines (80 loc) · 2.91 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
using HotChocolate.Language;
using static CookieCrumble.FileResource;
using static HotChocolate.Language.Utf8GraphQLParser;
using static StrawberryShake.CodeGeneration.Utilities.OperationDocumentHelper;
namespace StrawberryShake.CodeGeneration.Utilities;
public class OperationDocumentHelperTests
{
// This test ensures that each operation becomes one document that
// only has the fragments needed by the extracted operation.
[Fact]
public void Extract_Operation_Documents()
{
// arrange
var query = Parse(Open("simple.query1.graphql"));
List<DocumentNode> queries = [query];
// act
var operations = CreateOperationDocuments(queries);
// assert
Assert.Collection(
operations.Operations,
t => Assert.Equal("GetBookTitles", t.Key),
t => Assert.Equal("GetBooksAndAuthor", t.Key));
operations.Operations.Select(t => t.Value.ToString()).ToArray().MatchSnapshot();
}
[Fact]
public void Merge_Multiple_Documents()
{
// arrange
var query1 = Parse(Open("simple.query1.graphql"));
var query2 = Parse(Open("simple.query2.graphql"));
List<DocumentNode> queries = [query1, query2];
// act
var operations = CreateOperationDocuments(queries);
// assert
Assert.Collection(
operations.Operations,
t => Assert.Equal("GetBookTitles", t.Key),
t => Assert.Equal("GetBooksAndAuthor", t.Key),
t => Assert.Equal("GetAuthorsAndBooks", t.Key));
operations.Operations.Select(t => t.Value.ToString()).ToArray().MatchSnapshot();
}
[Fact]
public void No_Operation()
{
// arrange
DocumentNode query = new(new List<IDefinitionNode>());
List<DocumentNode> queries = [query];
// act
void Error() => CreateOperationDocuments(queries);
// assert
var error = Assert.Throws<ArgumentException>(Error);
error.Message.MatchSnapshot();
}
[Fact]
public void Duplicate_Operation()
{
// arrange
var query1 = Parse(Open("simple.query2.graphql"));
var query2 = Parse(Open("simple.query2.graphql"));
List<DocumentNode> queries = [query1, query2];
// act
void Error() => CreateOperationDocuments(queries);
// assert
var error = Assert.Throws<CodeGeneratorException>(Error);
error.Message.MatchSnapshot();
}
[Fact]
public void Duplicate_Fragment()
{
// arrange
var query1 = Parse(Open("simple.query1.graphql"));
var query2 = query1.WithDefinitions(query1.Definitions.Skip(2).ToArray());
List<DocumentNode> queries = [query1, query2];
// act
void Error() => CreateOperationDocuments(queries);
// assert
var error = Assert.Throws<CodeGeneratorException>(Error);
error.Message.MatchSnapshot();
}
}