generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathProjectableTest.php
307 lines (236 loc) · 10.5 KB
/
ProjectableTest.php
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?php
namespace TimothePearce\TimeSeries\Tests;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Queue;
use TimothePearce\TimeSeries\Jobs\ComputeProjection;
use TimothePearce\TimeSeries\Models\Projection;
use TimothePearce\TimeSeries\Tests\Models\Log;
use TimothePearce\TimeSeries\Tests\Models\Message;
use TimothePearce\TimeSeries\Tests\Models\Projections\GlobalPeriodProjection;
use TimothePearce\TimeSeries\Tests\Models\Projections\MultiplePeriodsProjection;
use TimothePearce\TimeSeries\Tests\Models\Projections\SinglePeriodKeyedProjection;
use TimothePearce\TimeSeries\Tests\Models\Projections\SinglePeriodProjection;
use TimothePearce\TimeSeries\Tests\Models\Projections\SinglePeriodProjectionWithCallable;
use TimothePearce\TimeSeries\Tests\Models\Projections\SinglePeriodProjectionWithUniqueKey;
use TimothePearce\TimeSeries\Tests\Models\Projections\WeeklyPeriodProjection;
class ProjectableTest extends TestCase
{
use ProjectableFactory;
/** @test */
public function it_creates_the_projection_on_model_created_event()
{
$this->assertDatabaseCount('time_series_projections', 0);
$log = Log::factory()->create();
$projection = $log->firstProjection();
$this->assertEquals($projection->content['created_count'], 1);
}
/** @test */
public function it_updates_the_projection_on_model_updating_event()
{
$log = Log::factory()->create();
$projection = $log->firstProjection();
$this->assertEquals($projection->content['updating_count'], 0);
$log->update(['message' => 'message updating']);
$this->assertEquals($projection->refresh()->content['updating_count'], 1);
}
/** @test */
public function it_updates_the_projection_on_model_updated_event()
{
$log = Log::factory()->create();
$projection = $log->firstProjection();
$this->assertEquals($projection->content['updated_count'], 0);
$log->update(['message' => 'message updated']);
$this->assertEquals($projection->refresh()->content['updated_count'], 1);
}
/** @test */
public function it_updates_the_projection_on_model_deleting_event()
{
$log = Log::factory()->create();
$projection = $log->firstProjection();
$this->assertEquals($projection->content['deleting_count'], 0);
$log->delete();
$this->assertEquals($projection->refresh()->content['deleting_count'], 1);
}
/** @test */
public function it_updates_the_projection_on_model_deleted_event()
{
$log = Log::factory()->create();
$projection = $log->firstProjection();
$this->assertEquals($projection->content['deleted_count'], 0);
$log->delete();
$this->assertEquals($projection->refresh()->content['deleted_count'], 1);
}
/** @test */
public function it_creates_a_projection_for_each_period_when_a_model_with_projections_is_created()
{
$this->createModelWithProjections(Log::class, [MultiplePeriodsProjection::class]);
$this->assertDatabaseCount('time_series_projections', 8);
}
/** @test */
public function it_gets_the_existing_projection_when_the_period_is_in_completion()
{
$this->travelTo(Carbon::today());
Log::factory()->create();
$this->travel(3)->minutes();
Log::factory()->create();
$this->assertDatabaseCount('time_series_projections', 1);
}
/** @test */
public function it_creates_a_new_projection_when_the_period_is_ended()
{
$this->travelTo(Carbon::today());
Log::factory()->create();
$this->travel(6)->minutes();
Log::factory()->create();
$this->assertDatabaseCount('time_series_projections', 2);
}
/** @test */
public function it_dispatch_a_job_when_the_queue_config_is_enabled()
{
Queue::fake();
config(['time-series.queue' => true]);
Log::factory()->create();
Queue::assertPushed(ComputeProjection::class);
}
/** @test */
public function it_dispatch_a_job_to_the_named_queue()
{
Queue::fake();
config(['time-series.queue' => true, 'time-series.queue_name' => 'named']);
Log::factory()->create();
Queue::assertPushedOn('named', ComputeProjection::class);
}
/** @test */
public function it_has_a_relationship_with_the_projection()
{
$log = Log::factory()->create();
$this->assertNotEmpty($log->projections);
}
/** @test */
public function it_projects_the_data_with_a_start_date_equals_to_the_beginning_of_the_week()
{
$this->travelTo(new Carbon('2021-12-17 00:00:00')); // wednesday
$log = $this->createModelWithProjections(Log::class, [
WeeklyPeriodProjection::class,
]);
$projection = $log->projections(WeeklyPeriodProjection::class)->first();
$this->assertEquals(
Carbon::createFromDate(2021, 12, 13), // monday
$projection->start_date,
);
}
/** @test */
public function it_get_the_projections_from_a_single_type()
{
$log = $this->createModelWithProjections(Log::class, [
SinglePeriodProjection::class,
MultiplePeriodsProjection::class,
]);
$projections = $log->projections(MultiplePeriodsProjection::class)->get();
$this->assertCount(8, $projections);
$projections->each(function (Projection $projection) {
$this->assertEquals(MultiplePeriodsProjection::class, $projection->projection_name);
});
}
/** @test */
public function it_get_the_projections_from_a_single_type_and_period()
{
$log = $this->createModelWithProjections(Log::class, [
SinglePeriodProjection::class,
MultiplePeriodsProjection::class,
]);
$projections = $log->projections(MultiplePeriodsProjection::class, '5 minutes')->get();
$this->assertCount(1, $projections);
$this->assertEquals('5 minutes', $projections->first()->period);
}
/** @test */
public function it_get_the_projections_from_a_single_type_and_multiple_periods()
{
$log = $this->createModelWithProjections(Log::class, [
SinglePeriodProjection::class,
MultiplePeriodsProjection::class,
]);
$projections = $log->projections(MultiplePeriodsProjection::class, ['5 minutes', '1 hour'])->get();
$this->assertCount(2, $projections);
$projections->each(function (Projection $projection) {
$this->assertTrue(collect(['5 minutes', '1 hour'])->contains($projection->period));
});
}
/** @test */
public function it_creates_a_single_projection_for_models_with_the_same_projection()
{
$this->createModelWithProjections(Log::class, [SinglePeriodProjection::class]);
$this->createModelWithProjections(Message::class, [SinglePeriodProjection::class]);
$this->assertEquals(1, Projection::count());
}
/** @test */
public function it_updates_a_projection_for_a_single_projectable_type_and_period()
{
$log = $this->createModelWithProjections(Log::class, [SinglePeriodProjection::class]);
$message = $this->createModelWithProjections(Message::class, [MultiplePeriodsProjection::class]);
$this->createModelWithProjections(Log::class, [SinglePeriodProjection::class]);
$logProjection = $log->projections(SinglePeriodProjection::class, '5 minutes')->first();
$messageProjection = $message->projections(MultiplePeriodsProjection::class, '5 minutes')->first();
$this->assertEquals(2, $logProjection->content['created_count']);
$this->assertEquals(1, $messageProjection->content['created_count']);
}
/** @test */
public function it_creates_a_global_projection()
{
$this->createModelWithProjections(Log::class, [GlobalPeriodProjection::class]);
$this->assertEquals(1, Projection::count());
$this->assertEquals(1, Projection::first()->content['created_count']);
}
/** @test */
public function it_creates_a_projection_for_each_different_key()
{
$this->createModelWithProjections(Log::class, [SinglePeriodProjectionWithUniqueKey::class]);
$this->createModelWithProjections(Log::class, [SinglePeriodProjectionWithUniqueKey::class]);
$this->assertEquals(2, Projection::count());
}
/** @test */
public function it_creates_a_single_projection_for_a_similar_key()
{
$this->createModelWithProjections(Log::class, [SinglePeriodKeyedProjection::class]);
$this->createModelWithProjections(Log::class, [SinglePeriodKeyedProjection::class]);
$this->assertEquals(1, Projection::count());
}
/** @test */
public function it_creates_a_projection_with_the_model_name_created_hook()
{
$log = $this->createModelWithProjections(Log::class, [SinglePeriodProjectionWithCallable::class]);
$logProjection = $log->projections(SinglePeriodProjectionWithCallable::class, '5 minutes')->first();
$this->assertEquals(1, $logProjection->content['created_count']);
}
/** @test */
public function it_updates_a_global_projection()
{
$log = $this->createModelWithProjections(Log::class, [GlobalPeriodProjection::class]);
$log->update(['message' => 'updated message']);
$this->assertEquals(1, Projection::count());
$this->assertEquals(1, Projection::first()->content['updated_count']);
}
/** @test */
public function it_updates_a_projection_with_the_model_name_updated_hook()
{
$log = $this->createModelWithProjections(Log::class, [SinglePeriodProjectionWithCallable::class]);
$log->update(['message' => 'updated message']);
$logProjection = $log->projections(SinglePeriodProjectionWithCallable::class, '5 minutes')->first();
$this->assertEquals(1, $logProjection->content['updated_count']);
}
/** @test */
public function it_updates_a_projection_with_the_model_name_deleted_hook()
{
$log = $this->createModelWithProjections(Log::class, [SinglePeriodProjectionWithCallable::class]);
$log->delete();
$logProjection = $log->projections(SinglePeriodProjectionWithCallable::class, '5 minutes')->first();
$this->assertEquals(1, $logProjection->content['deleted_count']);
}
/** @test */
public function it_get_the_first_projections()
{
$log = Log::factory()->create();
$firstProjection = $log->firstProjection();
$this->assertEquals($firstProjection->id, Projection::first()->id);
}
}