-
Notifications
You must be signed in to change notification settings - Fork 277
/
Copy pathScheduleTests.cs
179 lines (136 loc) · 6.4 KB
/
ScheduleTests.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
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
using System;
using Common;
using CompressImagesFunction;
using LibGit2Sharp;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NSubstitute;
namespace Test
{
[TestClass]
public class ScheduleTests
{
private readonly RepoConfiguration _dailyConfiguration = new RepoConfiguration
{
Schedule = KnownScheduleSettings.Daily,
};
private readonly RepoConfiguration _weeklyConfiguration = new RepoConfiguration
{
Schedule = KnownScheduleSettings.Weekly,
};
private readonly RepoConfiguration _monthlyConfiguration = new RepoConfiguration
{
Schedule = KnownScheduleSettings.Monthly,
};
[TestMethod]
public void GivenDefaultConfiguration_ShouldOptimizeImages()
{
var repository = Substitute.For<IRepository>();
var shouldOptimize = Schedule.ShouldOptimizeImages(new RepoConfiguration(), repository);
Assert.IsTrue(shouldOptimize);
}
[TestMethod]
public void GivenImgBotNeverCommited_ShouldOptimizeImages()
{
var repository = Substitute.For<IRepository>();
var commits = new SimpleCommitLog(new[]
{
OneRandoCommit(new DateTime(2017, 10, 31)),
OneRandoCommit(new DateTime(2017, 10, 30)),
OneRandoCommit(new DateTime(2017, 10, 29)),
});
repository.Commits.Returns(commits);
Assert.IsTrue(Schedule.ShouldOptimizeImages(_dailyConfiguration, repository));
Assert.IsTrue(Schedule.ShouldOptimizeImages(_weeklyConfiguration, repository));
Assert.IsTrue(Schedule.ShouldOptimizeImages(_monthlyConfiguration, repository));
Assert.IsTrue(Schedule.ShouldOptimizeImages(new RepoConfiguration(), repository));
}
[TestMethod]
public void GivenImgBotCommittedYesterday_ShouldOptimizeImagesForDaily()
{
var repository = Substitute.For<IRepository>();
var commits = new SimpleCommitLog(new[]
{
OneRandoCommit(DateTime.Now),
OneImgbotCommit(DateTime.Now - TimeSpan.FromHours(35)),
OneRandoCommit(new DateTime(2017, 10, 29)),
OneImgbotCommit(new DateTime(2017, 10, 27)),
OneImgbotCommit(new DateTime(2017, 10, 25)),
OneRandoCommit(new DateTime(2017, 10, 20)),
});
repository.Commits.Returns(commits);
Assert.IsTrue(Schedule.ShouldOptimizeImages(_dailyConfiguration, repository));
Assert.IsFalse(Schedule.ShouldOptimizeImages(_weeklyConfiguration, repository));
Assert.IsFalse(Schedule.ShouldOptimizeImages(_monthlyConfiguration, repository));
Assert.IsTrue(Schedule.ShouldOptimizeImages(new RepoConfiguration(), repository));
}
[TestMethod]
public void GivenImgBotCommittedLastWeek_ShouldOptimizeImagesForDailyWeekly()
{
var repository = Substitute.For<IRepository>();
var commits = new SimpleCommitLog(new[]
{
OneRandoCommit(DateTime.Now),
OneImgbotCommit(DateTime.Now - TimeSpan.FromDays(9)),
OneRandoCommit(new DateTime(2017, 8, 29)),
OneImgbotCommit(new DateTime(2017, 8, 27)),
OneImgbotCommit(new DateTime(2017, 8, 25)),
OneRandoCommit(new DateTime(2017, 8, 20)),
});
repository.Commits.Returns(commits);
Assert.IsTrue(Schedule.ShouldOptimizeImages(_dailyConfiguration, repository));
Assert.IsTrue(Schedule.ShouldOptimizeImages(_weeklyConfiguration, repository));
Assert.IsFalse(Schedule.ShouldOptimizeImages(_monthlyConfiguration, repository));
Assert.IsTrue(Schedule.ShouldOptimizeImages(new RepoConfiguration(), repository));
}
[TestMethod]
public void GivenImgBotCommittedLastMonth_ShouldOptimizeImagesForDailyWeeklyMonthly()
{
var repository = Substitute.For<IRepository>();
var commits = new SimpleCommitLog(new[]
{
OneRandoCommit(DateTime.Now),
OneRandoCommit(DateTime.Now - TimeSpan.FromDays(3)),
OneImgbotCommit(DateTime.Now - TimeSpan.FromDays(40)),
OneRandoCommit(new DateTime(2017, 8, 29)),
OneImgbotCommit(new DateTime(2017, 8, 27)),
OneImgbotCommit(new DateTime(2017, 8, 25)),
OneRandoCommit(new DateTime(2017, 8, 20)),
});
repository.Commits.Returns(commits);
Assert.IsTrue(Schedule.ShouldOptimizeImages(_dailyConfiguration, repository));
Assert.IsTrue(Schedule.ShouldOptimizeImages(_weeklyConfiguration, repository));
Assert.IsTrue(Schedule.ShouldOptimizeImages(_monthlyConfiguration, repository));
Assert.IsTrue(Schedule.ShouldOptimizeImages(new RepoConfiguration(), repository));
}
[TestMethod]
public void GivenImgBotCommittedLastYear_ShouldOptimizeImages()
{
var repository = Substitute.For<IRepository>();
var commits = new SimpleCommitLog(new[]
{
OneRandoCommit(new DateTime(2017, 10, 31)),
OneImgbotCommit(new DateTime(2016, 10, 30)),
OneRandoCommit(new DateTime(2015, 10, 29)),
});
repository.Commits.Returns(commits);
Assert.IsTrue(Schedule.ShouldOptimizeImages(_dailyConfiguration, repository));
Assert.IsTrue(Schedule.ShouldOptimizeImages(_weeklyConfiguration, repository));
Assert.IsTrue(Schedule.ShouldOptimizeImages(_monthlyConfiguration, repository));
Assert.IsTrue(Schedule.ShouldOptimizeImages(new RepoConfiguration(), repository));
}
private Commit OneImgbotCommit(DateTime when)
{
var author = new Signature(KnownGitHubs.ImgBotLogin, KnownGitHubs.ImgBotEmail, when);
var commit = Substitute.For<Commit>();
commit.Author.Returns(author);
return commit;
}
private Commit OneRandoCommit(DateTime when)
{
var author = new Signature("random", "random@gmail.com", when);
var commit = Substitute.For<Commit>();
commit.Author.Returns(author);
return commit;
}
}
}