Skip to content

Commit f3a9e30

Browse files
authored
Merge pull request #6 from DirectoryTree/fix-between-dates
Fix tuple comparison on MySQL
2 parents 31f2a6b + 50572c3 commit f3a9e30

File tree

4 files changed

+38
-28
lines changed

4 files changed

+38
-28
lines changed

.github/workflows/run-tests.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ jobs:
3030

3131
name: ${{ matrix.os }} - P${{ matrix.php }} - L${{ matrix.laravel }}
3232

33+
services:
34+
mysql:
35+
image: mysql:8.0
36+
env:
37+
MYSQL_ROOT_PASSWORD: password
38+
MYSQL_DATABASE: testing
39+
ports:
40+
- 3306:3306
41+
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
42+
3343
steps:
3444
- name: Checkout code
3545
uses: actions/checkout@v4
@@ -44,6 +54,7 @@ jobs:
4454
uses: shivammathur/setup-php@v2
4555
with:
4656
php-version: ${{ matrix.php }}
57+
extensions: pdo, pdo_mysql
4758

4859
- name: Install dependencies
4960
run: |
@@ -52,3 +63,10 @@ jobs:
5263
5364
- name: Execute tests
5465
run: vendor/bin/pest
66+
env:
67+
DB_CONNECTION: mysql
68+
DB_HOST: 127.0.0.1
69+
DB_PORT: 3306
70+
DB_DATABASE: testing
71+
DB_USERNAME: root
72+
DB_PASSWORD: password

src/MetricBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public function lastYearNoOverflow(): self
152152
public function betweenDates(CarbonInterface $start, CarbonInterface $end): self
153153
{
154154
return $this->whereRaw(
155-
'(year, month, day) BETWEEN (?, ?, ?) AND (?, ?, ?)',
155+
'(year, month, day) >= (?, ?, ?) AND (year, month, day) <= (?, ?, ?)',
156156
[
157157
$start->year, $start->month, $start->day,
158158
$end->year, $end->month, $end->day,

tests/MetricBuilderTest.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
$metrics = Metric::today()->get();
2020

2121
expect($metrics)->toHaveCount(1)
22-
->and($metrics->first()->day)->toBe(15)
23-
->and($metrics->first()->value)->toBe(100);
22+
->and($metrics->first()->day)->toEqual(15)
23+
->and($metrics->first()->value)->toEqual(100);
2424
});
2525

2626
it('can filter metrics for yesterday', function () {
@@ -31,8 +31,8 @@
3131
$metrics = Metric::yesterday()->get();
3232

3333
expect($metrics)->toHaveCount(1)
34-
->and($metrics->first()->day)->toBe(14)
35-
->and($metrics->first()->value)->toBe(100);
34+
->and($metrics->first()->day)->toEqual(14)
35+
->and($metrics->first()->value)->toEqual(100);
3636
});
3737

3838
it('can filter metrics for this week', function () {
@@ -46,7 +46,7 @@
4646
$metrics = Metric::thisWeek()->get();
4747

4848
expect($metrics)->toHaveCount(3)
49-
->and($metrics->sum('value'))->toBe(60);
49+
->and($metrics->sum('value'))->toEqual(60);
5050
});
5151

5252
it('can filter metrics for last week', function () {
@@ -60,7 +60,7 @@
6060
$metrics = Metric::lastWeek()->get();
6161

6262
expect($metrics)->toHaveCount(3)
63-
->and($metrics->sum('value'))->toBe(60);
63+
->and($metrics->sum('value'))->toEqual(60);
6464
});
6565

6666
it('can filter metrics for this month', function () {
@@ -73,7 +73,7 @@
7373
$metrics = Metric::thisMonth()->get();
7474

7575
expect($metrics)->toHaveCount(3)
76-
->and($metrics->sum('value'))->toBe(60);
76+
->and($metrics->sum('value'))->toEqual(60);
7777
});
7878

7979
it('can filter metrics for last month', function () {
@@ -86,7 +86,7 @@
8686
$metrics = Metric::lastMonth()->get();
8787

8888
expect($metrics)->toHaveCount(3)
89-
->and($metrics->sum('value'))->toBe(60);
89+
->and($metrics->sum('value'))->toEqual(60);
9090
});
9191

9292
it('can filter metrics for this quarter', function () {
@@ -100,7 +100,7 @@
100100
$metrics = Metric::thisQuarter()->get();
101101

102102
expect($metrics)->toHaveCount(3)
103-
->and($metrics->sum('value'))->toBe(60);
103+
->and($metrics->sum('value'))->toEqual(60);
104104
});
105105

106106
it('can filter metrics for last quarter', function () {
@@ -114,7 +114,7 @@
114114
$metrics = Metric::lastQuarter()->get();
115115

116116
expect($metrics)->toHaveCount(3)
117-
->and($metrics->sum('value'))->toBe(60);
117+
->and($metrics->sum('value'))->toEqual(60);
118118
});
119119

120120
it('can filter metrics for this year', function () {
@@ -127,7 +127,7 @@
127127
$metrics = Metric::thisYear()->get();
128128

129129
expect($metrics)->toHaveCount(3)
130-
->and($metrics->sum('value'))->toBe(60);
130+
->and($metrics->sum('value'))->toEqual(60);
131131
});
132132

133133
it('can filter metrics for last year', function () {
@@ -140,7 +140,7 @@
140140
$metrics = Metric::lastYear()->get();
141141

142142
expect($metrics)->toHaveCount(3)
143-
->and($metrics->sum('value'))->toBe(60);
143+
->and($metrics->sum('value'))->toEqual(60);
144144
});
145145

146146
it('can filter metrics between specific dates', function () {
@@ -156,7 +156,7 @@
156156
$metrics = Metric::betweenDates($start, $end)->get();
157157

158158
expect($metrics)->toHaveCount(3)
159-
->and($metrics->sum('value'))->toBe(60);
159+
->and($metrics->sum('value'))->toEqual(60);
160160
});
161161

162162
it('can filter metrics on a specific date', function () {
@@ -170,7 +170,7 @@
170170
$metrics = Metric::onDate($date)->get();
171171

172172
expect($metrics)->toHaveCount(2)
173-
->and($metrics->sum('value'))->toBe(300);
173+
->and($metrics->sum('value'))->toEqual(300);
174174
});
175175

176176
it('can chain builder methods with where clauses', function () {
@@ -182,7 +182,7 @@
182182

183183
expect($metrics)->toHaveCount(1)
184184
->and($metrics->first()->name)->toBe('page_views')
185-
->and($metrics->first()->value)->toBe(100);
185+
->and($metrics->first()->value)->toEqual(100);
186186
});
187187

188188
it('can sum values for a date range', function () {
@@ -193,7 +193,7 @@
193193

194194
$total = Metric::thisWeek()->where('name', 'page_views')->sum('value');
195195

196-
expect($total)->toBe(450);
196+
expect($total)->toEqual(450);
197197
});
198198

199199
it('can count metrics for a date range', function () {
@@ -204,7 +204,7 @@
204204

205205
$count = Metric::today()->count();
206206

207-
expect($count)->toBe(3);
207+
expect($count)->toEqual(3);
208208
});
209209

210210
it('returns empty collection when no metrics match date filter', function () {
@@ -226,7 +226,7 @@
226226
$metrics = Metric::betweenDates($start, $end)->get();
227227

228228
expect($metrics)->toHaveCount(3)
229-
->and($metrics->sum('value'))->toBe(60);
229+
->and($metrics->sum('value'))->toEqual(60);
230230
});
231231

232232
it('can filter across year boundaries', function () {
@@ -240,5 +240,5 @@
240240
$metrics = Metric::betweenDates($start, $end)->get();
241241

242242
expect($metrics)->toHaveCount(3)
243-
->and($metrics->sum('value'))->toBe(60);
243+
->and($metrics->sum('value'))->toEqual(60);
244244
});

tests/TestCase.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@
99

1010
abstract class TestCase extends BaseTestCase
1111
{
12-
/**
13-
* Define environment setup.
14-
*/
15-
protected function defineEnvironment($app): void
16-
{
17-
$app['config']->set('database.default', 'testing');
18-
}
19-
2012
/**
2113
* Define database migrations.
2214
*/

0 commit comments

Comments
 (0)