forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataUsageCalculatorTest.php
More file actions
134 lines (103 loc) · 4.11 KB
/
DataUsageCalculatorTest.php
File metadata and controls
134 lines (103 loc) · 4.11 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
<?php
use App\Models\Result;
use App\Services\DataUsageCalculator;
use Carbon\Carbon;
it('calculates total data usage for results within the period', function () {
$startDate = Carbon::parse('2024-01-01 00:00:00');
$endDate = Carbon::parse('2024-01-31 23:59:59');
// Create results within the period
Result::factory()->create([
'download_bytes' => 1000000,
'upload_bytes' => 500000,
'created_at' => Carbon::parse('2024-01-15 12:00:00'),
]);
Result::factory()->create([
'download_bytes' => 2000000,
'upload_bytes' => 1000000,
'created_at' => Carbon::parse('2024-01-20 15:30:00'),
]);
$result = DataUsageCalculator::calculate($startDate, $endDate);
expect($result)->toBeArray()
->and($result['download_bytes'])->toBe(3000000)
->and($result['upload_bytes'])->toBe(1500000)
->and($result['total_bytes'])->toBe(4500000);
});
it('returns zero when no results exist in the period', function () {
$startDate = Carbon::parse('2024-01-01 00:00:00');
$endDate = Carbon::parse('2024-01-31 23:59:59');
// Create a result outside the period
Result::factory()->create([
'download_bytes' => 1000000,
'upload_bytes' => 500000,
'created_at' => Carbon::parse('2024-02-15 12:00:00'),
]);
$result = DataUsageCalculator::calculate($startDate, $endDate);
expect($result)->toBeArray()
->and($result['download_bytes'])->toBe(0)
->and($result['upload_bytes'])->toBe(0)
->and($result['total_bytes'])->toBe(0);
});
it('handles null byte values correctly', function () {
$startDate = Carbon::parse('2024-01-01 00:00:00');
$endDate = Carbon::parse('2024-01-31 23:59:59');
// Create results with null bytes
Result::factory()->create([
'download_bytes' => null,
'upload_bytes' => null,
'created_at' => Carbon::parse('2024-01-15 12:00:00'),
]);
Result::factory()->create([
'download_bytes' => 1000000,
'upload_bytes' => 500000,
'created_at' => Carbon::parse('2024-01-20 15:30:00'),
]);
$result = DataUsageCalculator::calculate($startDate, $endDate);
expect($result)->toBeArray()
->and($result['download_bytes'])->toBe(1000000)
->and($result['upload_bytes'])->toBe(500000)
->and($result['total_bytes'])->toBe(1500000);
});
it('includes results at the exact start boundary', function () {
$startDate = Carbon::parse('2024-01-01 00:00:00');
$endDate = Carbon::parse('2024-01-31 23:59:59');
Result::factory()->create([
'download_bytes' => 1000000,
'upload_bytes' => 500000,
'created_at' => $startDate,
]);
$result = DataUsageCalculator::calculate($startDate, $endDate);
expect($result['total_bytes'])->toBe(1500000);
});
it('includes results at the exact end boundary', function () {
$startDate = Carbon::parse('2024-01-01 00:00:00');
$endDate = Carbon::parse('2024-01-31 23:59:59');
Result::factory()->create([
'download_bytes' => 1000000,
'upload_bytes' => 500000,
'created_at' => $endDate,
]);
$result = DataUsageCalculator::calculate($startDate, $endDate);
expect($result['total_bytes'])->toBe(1500000);
});
it('excludes results before the start date', function () {
$startDate = Carbon::parse('2024-01-01 00:00:00');
$endDate = Carbon::parse('2024-01-31 23:59:59');
Result::factory()->create([
'download_bytes' => 1000000,
'upload_bytes' => 500000,
'created_at' => Carbon::parse('2023-12-31 23:59:59'),
]);
$result = DataUsageCalculator::calculate($startDate, $endDate);
expect($result['total_bytes'])->toBe(0);
});
it('excludes results after the end date', function () {
$startDate = Carbon::parse('2024-01-01 00:00:00');
$endDate = Carbon::parse('2024-01-31 23:59:59');
Result::factory()->create([
'download_bytes' => 1000000,
'upload_bytes' => 500000,
'created_at' => Carbon::parse('2024-02-01 00:00:01'),
]);
$result = DataUsageCalculator::calculate($startDate, $endDate);
expect($result['total_bytes'])->toBe(0);
});