4
4
using Feature . Manager . Api . FeatureRuns ;
5
5
using Feature . Manager . Api . FeatureRuns . Exceptions ;
6
6
using Feature . Manager . Api . FeatureRuns . ViewModels ;
7
- using Feature . Manager . Api . Features . ViewModels ;
7
+ using Feature . Manager . Api . Features ;
8
+ using Feature . Manager . Api . Features . Exceptions ;
8
9
using Feature . Manager . Api . Uuid ;
9
10
using Moq ;
10
11
using NUnit . Framework ;
11
12
12
13
namespace Feature . Manager . UnitTest . FeatureRuns
13
14
{
14
- public class FeatureRunCreateService
15
+ public class FeatureRunCreateServiceTest
15
16
{
16
- private Mock < IFeatureRunRepository > _mock ;
17
17
private FeatureRunService _featureRunService ;
18
+ private Mock < IFeatureRepository > _featureRepository ;
19
+ private Mock < IFeatureRunRepository > _mock ;
18
20
private UuidService _uuidService ;
19
21
20
22
private FeatureRun MakeFeatureRun ( StopResult ? stopResult , string featId , DateTime ? endAt )
@@ -25,17 +27,11 @@ private FeatureRun MakeFeatureRun(StopResult? stopResult, string featId, DateTim
25
27
Id = _uuidService . GenerateUuId ( ) ,
26
28
StartAt = DateTime . Now . Subtract ( TimeSpan . FromDays ( 5 ) ) ,
27
29
RunToken = _uuidService . GenerateUuId ( ) ,
28
- FeatId = featId ,
30
+ FeatId = featId
29
31
} ;
30
- if ( stopResult . HasValue )
31
- {
32
- run . StopResult = stopResult . Value ;
33
- }
32
+ if ( stopResult . HasValue ) run . StopResult = stopResult . Value ;
34
33
35
- if ( endAt . HasValue )
36
- {
37
- run . EndAt = endAt ;
38
- }
34
+ if ( endAt . HasValue ) run . EndAt = endAt ;
39
35
40
36
return run ;
41
37
}
@@ -51,7 +47,7 @@ public void Setup()
51
47
Id = "rand" ,
52
48
FeatId = "APP-2" ,
53
49
RunToken = "run-token" ,
54
- StartAt = DateTime . Now . Subtract ( TimeSpan . FromHours ( 2 ) ) ,
50
+ StartAt = DateTime . Now . Subtract ( TimeSpan . FromHours ( 2 ) )
55
51
} ) ;
56
52
57
53
// APP-1 will have 1 item with no end date, (NO STOP RESULT)
@@ -61,45 +57,65 @@ public void Setup()
61
57
// APP-5 will have 1 item with end date BUT with stop result of all B (to show that even if you set it to B, you can still create new runs)
62
58
mock . Setup ( x => x . GetRunsForFeatureByFeatId ( "APP-1" ) ) . ReturnsAsync ( new List < FeatureRun >
63
59
{
64
- MakeFeatureRun ( null , "APP-1" , null ) ,
60
+ MakeFeatureRun ( null , "APP-1" , null )
65
61
} ) ;
66
62
mock . Setup ( x => x . GetRunsForFeatureByFeatId ( "APP-2" ) ) . ReturnsAsync ( new List < FeatureRun >
67
63
{
68
64
MakeFeatureRun ( StopResult . ChangeSettings , "APP-2" , DateTime . Now . Subtract ( TimeSpan . FromDays ( 2 ) ) ) ,
69
- MakeFeatureRun ( null , "APP-2" , null ) ,
65
+ MakeFeatureRun ( null , "APP-2" , null )
70
66
} ) ;
71
67
mock . Setup ( x => x . GetRunsForFeatureByFeatId ( "APP-3" ) ) . ReturnsAsync ( new List < FeatureRun >
72
68
{
73
69
MakeFeatureRun ( StopResult . ChangeSettings , "APP-3" , DateTime . Now . Subtract ( TimeSpan . FromDays ( 2 ) ) ) ,
74
70
MakeFeatureRun ( StopResult . ChangeSettings , "APP-3" , DateTime . Now . Subtract ( TimeSpan . FromDays ( 1 ) ) ) ,
75
- MakeFeatureRun ( StopResult . ChangeSettings , "APP-3" , DateTime . Now . Subtract ( TimeSpan . FromHours ( 12 ) ) ) ,
71
+ MakeFeatureRun ( StopResult . ChangeSettings , "APP-3" , DateTime . Now . Subtract ( TimeSpan . FromHours ( 12 ) ) )
76
72
} ) ;
77
73
mock . Setup ( x => x . GetRunsForFeatureByFeatId ( "APP-4" ) ) . ReturnsAsync ( new List < FeatureRun > ( ) ) ;
78
74
mock . Setup ( x => x . GetRunsForFeatureByFeatId ( "APP-5" ) ) . ReturnsAsync ( new List < FeatureRun >
79
75
{
80
- MakeFeatureRun ( StopResult . AllB , "APP-5" , DateTime . Now . Subtract ( TimeSpan . FromHours ( 12 ) ) ) ,
76
+ MakeFeatureRun ( StopResult . AllB , "APP-5" , DateTime . Now . Subtract ( TimeSpan . FromHours ( 12 ) ) )
81
77
} ) ;
78
+ mock . Setup ( x => x . GetRunsForFeatureByFeatId ( "APP-19" ) ) . ReturnsAsync ( new List < FeatureRun > ( ) ) ;
82
79
_mock = mock ;
83
- _featureRunService = new FeatureRunService ( _mock . Object ) ;
80
+ var featureRepository = new Mock < IFeatureRepository > ( ) ;
81
+ featureRepository . Setup ( x => x . FindByFeatId ( It . IsAny < string > ( ) ) ) . ReturnsAsync ( ( string featId ) =>
82
+ {
83
+ if ( featId == "APP-19" )
84
+ {
85
+ return null ;
86
+ }
87
+ return new Api . Features . Feature
88
+ {
89
+ Description = "asdfasdfasdf" ,
90
+ Hypothesis = "asdfasdfsd" ,
91
+ Id = "asdfasdf" ,
92
+ FeatId = featId ,
93
+ FeatureToken = "asldf"
94
+ } ;
95
+ } ) ;
96
+ _featureRepository = featureRepository ;
97
+ _featureRunService = new FeatureRunService ( _mock . Object , _featureRepository . Object ) ;
84
98
}
85
99
86
100
[ Test ]
87
101
public async Task TestCannotCreateNewRunIfARunIsAlreadyRunning ( )
88
102
{
89
- Assert . ThrowsAsync < FeatureAlreadyRunningException > ( ( ) => _featureRunService . CreateFeatureRun ( new CreateFeatureRunRequest
90
- {
91
- Allocation = 100 ,
92
- EndAt = DateTime . Now . Add ( TimeSpan . FromDays ( 20 ) ) ,
93
- StartAt = DateTime . Now ,
94
- FeatId = "APP-1"
95
- } ) ) ;
96
- Assert . ThrowsAsync < FeatureAlreadyRunningException > ( ( ) => _featureRunService . CreateFeatureRun ( new CreateFeatureRunRequest
97
- {
98
- Allocation = 100 ,
99
- EndAt = DateTime . Now . Add ( TimeSpan . FromDays ( 20 ) ) ,
100
- StartAt = DateTime . Now ,
101
- FeatId = "APP-2"
102
- } ) ) ;
103
+ Assert . ThrowsAsync < FeatureAlreadyRunningException > ( ( ) => _featureRunService . CreateFeatureRun (
104
+ new CreateFeatureRunRequest
105
+ {
106
+ Allocation = 100 ,
107
+ EndAt = DateTime . Now . Add ( TimeSpan . FromDays ( 20 ) ) ,
108
+ StartAt = DateTime . Now ,
109
+ FeatId = "APP-1"
110
+ } ) ) ;
111
+ Assert . ThrowsAsync < FeatureAlreadyRunningException > ( ( ) => _featureRunService . CreateFeatureRun (
112
+ new CreateFeatureRunRequest
113
+ {
114
+ Allocation = 100 ,
115
+ EndAt = DateTime . Now . Add ( TimeSpan . FromDays ( 20 ) ) ,
116
+ StartAt = DateTime . Now ,
117
+ FeatId = "APP-2"
118
+ } ) ) ;
103
119
}
104
120
105
121
[ Test ]
@@ -114,5 +130,18 @@ public async Task TestCreatesNewRunWhenNoFeaturesAreRunning()
114
130
} ) ;
115
131
Assert . NotNull ( result ) ;
116
132
}
133
+
134
+ [ Test ]
135
+ public async Task TestCreateFailsIfFeatureDoesNotExist ( )
136
+ {
137
+ Assert . ThrowsAsync < FeatureNotFoundException > ( ( ) => _featureRunService . CreateFeatureRun (
138
+ new CreateFeatureRunRequest
139
+ {
140
+ Allocation = 100 ,
141
+ EndAt = DateTime . Now . Add ( TimeSpan . FromDays ( 20 ) ) ,
142
+ StartAt = DateTime . Now ,
143
+ FeatId = "APP-19"
144
+ } ) ) ;
145
+ }
117
146
}
118
- }
147
+ }
0 commit comments