forked from nozzlegear/ShopifySharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomCollection_Tests.cs
More file actions
150 lines (121 loc) · 4.26 KB
/
CustomCollection_Tests.cs
File metadata and controls
150 lines (121 loc) · 4.26 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Xunit;
using EmptyAssert = ShopifySharp.Tests.Extensions.EmptyExtensions;
namespace ShopifySharp.Tests
{
[Trait("Category", "CustomCollection")]
public class CustomCollection_Tests : IClassFixture<CustomCollection_Tests_Fixture>
{
private CustomCollection_Tests_Fixture Fixture { get; }
public CustomCollection_Tests(CustomCollection_Tests_Fixture fixture)
{
this.Fixture = fixture;
}
[Fact]
public async Task Counts_CustomCollections()
{
var count = await Fixture.Service.CountAsync();
Assert.True(count > 0);
}
[Fact]
public async Task Lists_CustomCollections()
{
var list = await Fixture.Service.ListAsync();
Assert.True(list.Items.Count() > 0);
}
[Fact]
public async Task Gets_CustomCollections()
{
var collection = await Fixture.Service.GetAsync(Fixture.Created.First().Id.Value);
Assert.NotNull(collection);
Assert.True(collection.Id.HasValue);
Assert.Equal(Fixture.Title, collection.Title);
}
[Fact]
public async Task Deletes_CustomCollections()
{
var created = await Fixture.Create(true);
bool threw = false;
try
{
await Fixture.Service.DeleteAsync(created.Id.Value);
}
catch (ShopifyException ex)
{
Console.WriteLine($"{nameof(Deletes_CustomCollections)} failed. {ex.Message}");
threw = true;
}
Assert.False(threw);
}
[Fact]
public async Task Creates_CustomCollections()
{
var collection = await Fixture.Create();
Assert.NotNull(collection);
Assert.True(collection.Id.HasValue);
Assert.Equal(Fixture.Title, collection.Title);
}
[Fact]
public async Task Updates_CustomCollections()
{
string newTitle = "New Title";
var created = await Fixture.Create();
long id = created.Id.Value;
created.Title = newTitle;
created.Id = null;
var updated = await Fixture.Service.UpdateAsync(id, created);
// Reset the id so the Fixture can properly delete this object.
created.Id = id;
Assert.Equal(newTitle, updated.Title);
}
}
public class CustomCollection_Tests_Fixture : IAsyncLifetime
{
public CustomCollectionService Service { get; } = new CustomCollectionService(Utils.MyShopifyUrl, Utils.AccessToken);
public List<CustomCollection> Created { get; } = new List<CustomCollection>();
public string Title => "Things";
public async Task InitializeAsync()
{
Service.SetExecutionPolicy(new LeakyBucketExecutionPolicy());
// Create one collection for use with count, list, get, etc. tests.
await Create();
}
public async Task DisposeAsync()
{
foreach (var obj in Created)
{
try
{
await Service.DeleteAsync(obj.Id.Value);
}
catch (ShopifyException ex)
{
if (ex.HttpStatusCode != HttpStatusCode.NotFound)
{
Console.WriteLine($"Failed to delete created CustomCollection with id {obj.Id.Value}. {ex.Message}");
}
}
}
}
/// <summary>
/// Convenience function for running tests. Creates an object and automatically adds it to the queue for deleting after tests finish.
/// </summary>
public async Task<CustomCollection> Create(bool skipAddToCreatedList = false)
{
var obj = await Service.CreateAsync(new CustomCollection()
{
Title = Title,
Published = false
});
if (!skipAddToCreatedList)
{
Created.Add(obj);
}
return obj;
}
}
}