Skip to content

Commit 283d259

Browse files
committed
tests: boolean columns
1 parent 2eb9891 commit 283d259

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

src/SQLLoader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ protected function buildDefaultColumns(string $table, array $columns): array
116116
}
117117

118118
if (in_array($column, $booleans)) {
119-
$default = $schemaColumns->where('name', $column)->first()['default'];
120-
$columns[$key] = "{$escapedColumn} \"DECODE(:$column, '', $default, :$column)\"";
119+
$default = trim((string) $schemaColumns->where('name', $column)->first()['default']);
120+
$columns[$key] = "{$escapedColumn} \"DECODE(:{$column}, '', {$default}, :{$column})\"";
121121

122122
continue;
123123
}

tests/Feature/SQLLoaderTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,21 @@
139139
assertStringContainsString('"PHONE" FILLER', $controlFile);
140140
assertStringContainsString('"CREATED_AT" DATE', $controlFile);
141141
});
142+
143+
test('it can detect BOOLEAN columns and set the default value if empty', function () {
144+
Process::fake();
145+
146+
$loader = new SQLLoader();
147+
$loader->inFile(__DIR__.'/../data/filler.dat')
148+
->as('users.ctl')
149+
->withHeaders()
150+
->into('users')
151+
->execute();
152+
153+
$controlFile = $loader->buildControlFile();
154+
assertStringContainsString("\"NAME\",\n", $controlFile);
155+
assertStringContainsString("\"EMAIL\",\n", $controlFile);
156+
assertStringContainsString('"PHONE" FILLER', $controlFile);
157+
assertStringContainsString('"CREATED_AT" DATE', $controlFile);
158+
assertStringContainsString("\"IS_ACTIVE\" \"DECODE(:is_active, '', '1', :is_active)\"\n", $controlFile);
159+
});

tests/TestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ protected function migrateDatabase(): void
2727
$table->increments('id');
2828
$table->string('name');
2929
$table->string('email');
30+
$table->boolean('is_active')->default(true);
3031
$table->timestamps();
3132
});
3233
}

tests/data/filler.dat

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
NAME,EMAIL,PHONE,CREATED_AT
2-
John Doe,[email protected],123-456-7890,2016-01-01T00:00:00.000000Z
3-
Jane Doe,[email protected],123-456-7890,2016-01-01T00:00:00.000000Z
4-
"Mr. ""John"" Doe",[email protected],123-456-7890,2016-01-01T00:00:00.000000Z
5-
"Ms. Doe, Jane", [email protected],123-456-7890,2016-01-01T00:00:00.000000Z
1+
NAME,EMAIL,PHONE,CREATED_AT,IS_ACTIVE
2+
John Doe,[email protected],123-456-7890,2016-01-01T00:00:00.000000Z,1
3+
Jane Doe,[email protected],123-456-7890,2016-01-01T00:00:00.000000Z,1
4+
"Mr. ""John"" Doe",[email protected],123-456-7890,2016-01-01T00:00:00.000000Z,0
5+
"Ms. Doe, Jane", [email protected],123-456-7890,2016-01-01T00:00:00.000000Z,0

0 commit comments

Comments
 (0)