|
| 1 | +/** |
| 2 | + * @athenna/database |
| 3 | + * |
| 4 | + * (c) João Lenon <lenon@athenna.io> |
| 5 | + * |
| 6 | + * For the full copyright and license information, please view the LICENSE |
| 7 | + * file that was distributed with this source code. |
| 8 | + */ |
| 9 | + |
| 10 | +import { Path } from '@athenna/common' |
| 11 | +import { AfterEach, Test, type Context } from '@athenna/test' |
| 12 | +import { BaseCommandTest } from '#tests/helpers/BaseCommandTest' |
| 13 | + |
| 14 | +export default class DbQueryCommandTest extends BaseCommandTest { |
| 15 | + @AfterEach() |
| 16 | + public async afterEachQueryTest() { |
| 17 | + delete process.env.MOCK_RAW_TYPE |
| 18 | + } |
| 19 | + |
| 20 | + @Test() |
| 21 | + public async shouldBeAbleToRunARawQueryAndPrintObjectResultAsJson({ command }: Context) { |
| 22 | + const output = await command.run('db:query SELECT * from users --connection=fake', { |
| 23 | + path: Path.fixtures('consoles/db-query-console.ts') |
| 24 | + }) |
| 25 | + |
| 26 | + output.assertSucceeded() |
| 27 | + output.assertLogged('[ RUNNING QUERY ]') |
| 28 | + output.assertLogged('{}') |
| 29 | + } |
| 30 | + |
| 31 | + @Test() |
| 32 | + public async shouldPrintArrayResultAsJson({ command }: Context) { |
| 33 | + process.env.MOCK_RAW_TYPE = 'array' |
| 34 | + |
| 35 | + const output = await command.run('db:query SELECT * from users --connection=fake', { |
| 36 | + path: Path.fixtures('consoles/db-query-console.ts') |
| 37 | + }) |
| 38 | + |
| 39 | + output.assertSucceeded() |
| 40 | + output.assertLogged('[ RUNNING QUERY ]') |
| 41 | + output.assertLogged('[{"id":1,"name":"Lenon"}]') |
| 42 | + } |
| 43 | + |
| 44 | + @Test() |
| 45 | + public async shouldPrintNumberResultAsString({ command }: Context) { |
| 46 | + process.env.MOCK_RAW_TYPE = 'number' |
| 47 | + |
| 48 | + const output = await command.run('db:query SELECT COUNT(*) from users --connection=fake', { |
| 49 | + path: Path.fixtures('consoles/db-query-console.ts') |
| 50 | + }) |
| 51 | + |
| 52 | + output.assertSucceeded() |
| 53 | + output.assertLogged('[ RUNNING QUERY ]') |
| 54 | + output.assertLogged('42') |
| 55 | + output.assertNotLogged('{') |
| 56 | + } |
| 57 | + |
| 58 | + @Test() |
| 59 | + public async shouldPrintStringResultAsString({ command }: Context) { |
| 60 | + process.env.MOCK_RAW_TYPE = 'string' |
| 61 | + |
| 62 | + const output = await command.run('db:query SELECT version --connection=fake', { |
| 63 | + path: Path.fixtures('consoles/db-query-console.ts') |
| 64 | + }) |
| 65 | + |
| 66 | + output.assertSucceeded() |
| 67 | + output.assertLogged('[ RUNNING QUERY ]') |
| 68 | + output.assertLogged('hello') |
| 69 | + } |
| 70 | + |
| 71 | + @Test() |
| 72 | + public async shouldPrintBooleanResultAsString({ command }: Context) { |
| 73 | + process.env.MOCK_RAW_TYPE = 'boolean' |
| 74 | + |
| 75 | + const output = await command.run('db:query SELECT 1 --connection=fake', { |
| 76 | + path: Path.fixtures('consoles/db-query-console.ts') |
| 77 | + }) |
| 78 | + |
| 79 | + output.assertSucceeded() |
| 80 | + output.assertLogged('[ RUNNING QUERY ]') |
| 81 | + output.assertLogged('true') |
| 82 | + } |
| 83 | + |
| 84 | + @Test() |
| 85 | + public async shouldNotPrintAnyResultWhenQueryReturnsUndefined({ command }: Context) { |
| 86 | + process.env.MOCK_RAW_TYPE = 'undefined' |
| 87 | + |
| 88 | + const output = await command.run('db:query INSERT INTO users VALUES (1) --connection=fake', { |
| 89 | + path: Path.fixtures('consoles/db-query-console.ts') |
| 90 | + }) |
| 91 | + |
| 92 | + output.assertSucceeded() |
| 93 | + output.assertLogged('[ RUNNING QUERY ]') |
| 94 | + output.assertNotLogged('undefined') |
| 95 | + output.assertNotLogged('null') |
| 96 | + } |
| 97 | + |
| 98 | + @Test() |
| 99 | + public async shouldJoinMultipleTokensIntoTheRawQueryString({ command }: Context) { |
| 100 | + const output = await command.run('db:query SELECT id, name FROM users WHERE id = 1 --connection=fake', { |
| 101 | + path: Path.fixtures('consoles/db-query-console.ts') |
| 102 | + }) |
| 103 | + |
| 104 | + output.assertSucceeded() |
| 105 | + output.assertLogged('[ RUNNING QUERY ]') |
| 106 | + output.assertLogged('{}') |
| 107 | + } |
| 108 | +} |
0 commit comments