-
Notifications
You must be signed in to change notification settings - Fork 5
/
UserPreviouslyActiveIsPartOfTheRecentlyInactiveCohort.cs
115 lines (97 loc) · 3.31 KB
/
UserPreviouslyActiveIsPartOfTheRecentlyInactiveCohort.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
using System;
using NUnit.Framework;
using Specification;
using Specification.SpecificationTree;
namespace SpecificationTests
{
internal class UserPreviouslyActiveIsPartOfTheRecentlyInactiveCohort
{
private readonly ISpecification<UserContext> _inactiveCohort;
private UserContext _userContext;
public UserPreviouslyActiveIsPartOfTheRecentlyInactiveCohort()
{
_inactiveCohort = new UserIsInactiveForOverTwoWeeks()
.And(new UserHasViewedSomeThing())
.And(new UserHasUploadedSomeThing())
.And(new UserHasNotReceivedAReportThisMonth());
}
[SetUp]
public void Setup()
{
_userContext = new UserContext
{
LastActiveDate = DateTime.UtcNow.AddDays(-15),
LastReportSent = DateTime.UtcNow.AddMonths(-1),
TotalUploads = 10,
TotalViews = 50
};
}
[Test]
public void UserIsPartOfTheCohort()
{
Console.WriteLine(_inactiveCohort.PrettyPrint());
Assert.IsTrue(_inactiveCohort.IsSatisfiedBy(_userContext));
}
[Test]
public void UserHasBeenActiveInTheLastTwoWeeks()
{
_userContext.LastActiveDate = DateTime.UtcNow.AddDays(-10);
Assert.IsFalse(_inactiveCohort.IsSatisfiedBy(_userContext));
}
[Test]
public void UserHasNeverViewedAnyThing()
{
_userContext.TotalViews = 0;
Assert.IsFalse(_inactiveCohort.IsSatisfiedBy(_userContext));
}
[Test]
public void UserHasNeverUploadedAnyThing()
{
_userContext.TotalUploads = 0;
Assert.IsFalse(_inactiveCohort.IsSatisfiedBy(_userContext));
}
[Test]
public void UserHasReceivedAReportThisMonth()
{
_userContext.LastReportSent = DateTime.UtcNow;
Assert.IsFalse(_inactiveCohort.IsSatisfiedBy(_userContext));
}
}
internal class UserHasUploadedSomeThing : LeafSpecification<UserContext>
{
public override bool IsSatisfiedBy(UserContext context)
{
return context.TotalUploads > 0;
}
}
internal class UserHasNotReceivedAReportThisMonth : LeafSpecification<UserContext>
{
public override bool IsSatisfiedBy(UserContext context)
{
var now = DateTime.UtcNow;
return context.LastReportSent.Year == now.Year && context.LastReportSent.Month != now.Month;
}
}
internal class UserHasViewedSomeThing : LeafSpecification<UserContext>
{
public override bool IsSatisfiedBy(UserContext context)
{
return context.TotalViews > 0;
}
}
internal class UserIsInactiveForOverTwoWeeks : LeafSpecification<UserContext>
{
public override bool IsSatisfiedBy(UserContext context)
{
var timeSpan = DateTime.UtcNow - context.LastActiveDate.ToUniversalTime();
return timeSpan.Days > 14;
}
}
internal class UserContext
{
public DateTime LastActiveDate { get; set; }
public int TotalViews { get; set; }
public int TotalUploads { get; set; }
public DateTime LastReportSent { get; set; }
}
}