Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Added new flags #1

Merged
merged 2 commits into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
242 changes: 236 additions & 6 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,41 @@ import { inferTable, inferSchema } from '../src/table'
import { SQL as sql } from 'sql-template-strings'
import moment from 'moment'
import pkg from '../src/pkg.json'
import config from '../src/config'

const connectionString = 'mysql://root@localhost:33306/test'
const conn = createConnection(connectionString)

const agreements = sql`
CREATE TABLE IF NOT EXISTS agreements (
const agreements = sql`
CREATE TABLE agreements (
id varbinary(24) NOT NULL,
billing_plan_id varbinary(24) NOT NULL,
category varbinary(24) NOT NULL,
name varbinary(64) NOT NULL,
name varbinary(64) NOT NULL,
PRIMARY KEY (id)
)`

const withNumbers = sql`
CREATE TABLE with_numbers (
id int unsigned NOT NULL,
tiny tinyint NOT NULL,
small smallint NOT NULL,
medium mediumint NOT NULL,
big bigint NOT NULL,
flo float NOT NULL,
dou double NOT NULL,
PRIMARY KEY (id)
)`

const withJSON = sql`
CREATE TABLE IF NOT EXISTS table_with_json (
CREATE TABLE table_with_json (
id varbinary(24) NOT NULL,
data json DEFAULT NULL,
PRIMARY KEY (id)
)`

const requests = sql`
CREATE TABLE IF NOT EXISTS requests (
CREATE TABLE requests (
id int(11) NOT NULL,
name varchar(255) NOT NULL,
url varchar(255) NOT NULL,
Expand All @@ -34,7 +47,7 @@ const requests = sql`
`

const complex = sql`
CREATE TABLE IF NOT EXISTS complex (
CREATE TABLE complex (
id varbinary(255) NOT NULL,
name varchar(255) NOT NULL,
nullable varchar(255),
Expand All @@ -46,10 +59,22 @@ const complex = sql`
const time = moment().format('YYYY-MM-DD')

beforeAll(async () => {
await query(conn, sql`DROP TABLE IF EXISTS agreements`)
await query(conn, sql`DROP TABLE IF EXISTS table_with_json`)
await query(conn, sql`DROP TABLE IF EXISTS requests`)
await query(conn, sql`DROP TABLE IF EXISTS complex`)
await query(conn, sql`DROP TABLE IF EXISTS with_numbers`)
await query(conn, agreements)
await query(conn, requests)
await query(conn, complex)
await query(conn, withJSON)
await query(conn, withNumbers)
})

beforeEach(async () => {
config.binaryAsBuffer = false
config.tinyIntAsBoolean = false
config.nullAsUndefined = false
})

describe('inferTable', () => {
Expand Down Expand Up @@ -212,6 +237,179 @@ describe('inferTable', () => {
"
`)
})

it('works with binaryAsBuffer = true', async () => {
config.binaryAsBuffer = true
const code = await inferTable(connectionString, 'agreements')
expect(code).toMatchInlineSnapshot(`
"/**
* AUTO-GENERATED FILE @ ${time} - DO NOT EDIT!
*
* This file was automatically generated by ${pkg.name} ${pkg.version}
*/

/**
* Exposes all fields present in agreements as a typescript
* interface.
* This is especially useful for SELECT * FROM
*/
export interface Agreements {
id: Buffer
billing_plan_id: Buffer
category: Buffer
name: Buffer
}

/**
* Exposes the same fields as Agreements,
* but makes every field containing a DEFAULT value optional.
*
* This is especially useful when generating inserts, as you
* should be able to ommit these fields if you'd like
*/
export interface AgreementsWithDefaults {
id: Buffer
billing_plan_id: Buffer
category: Buffer
name: Buffer
}
"
`)
})

it('works with tinyIntAsBoolean = false', async () => {
const code = await inferTable(connectionString, 'with_numbers')
expect(code).toMatchInlineSnapshot(`
"/**
* AUTO-GENERATED FILE @ ${time} - DO NOT EDIT!
*
* This file was automatically generated by ${pkg.name} ${pkg.version}
*/

/**
* Exposes all fields present in with_numbers as a typescript
* interface.
* This is especially useful for SELECT * FROM
*/
export interface WithNumbers {
id: number
tiny: number
small: number
medium: number
big: number
flo: number
dou: number
}

/**
* Exposes the same fields as WithNumbers,
* but makes every field containing a DEFAULT value optional.
*
* This is especially useful when generating inserts, as you
* should be able to ommit these fields if you'd like
*/
export interface WithNumbersWithDefaults {
id: number
tiny: number
small: number
medium: number
big: number
flo: number
dou: number
}
"
`)
})

it('works with tinyIntAsBoolean = true', async () => {
config.tinyIntAsBoolean = true
const code = await inferTable(connectionString, 'with_numbers')
expect(code).toMatchInlineSnapshot(`
"/**
* AUTO-GENERATED FILE @ ${time} - DO NOT EDIT!
*
* This file was automatically generated by ${pkg.name} ${pkg.version}
*/

/**
* Exposes all fields present in with_numbers as a typescript
* interface.
* This is especially useful for SELECT * FROM
*/
export interface WithNumbers {
id: number
tiny: boolean
small: number
medium: number
big: number
flo: number
dou: number
}

/**
* Exposes the same fields as WithNumbers,
* but makes every field containing a DEFAULT value optional.
*
* This is especially useful when generating inserts, as you
* should be able to ommit these fields if you'd like
*/
export interface WithNumbersWithDefaults {
id: number
tiny: boolean
small: number
medium: number
big: number
flo: number
dou: number
}
"
`)
})
it('works with nullAsUndefined = true', async () => {
config.nullAsUndefined = true
const code = await inferTable(connectionString, 'complex')
expect(code).toMatchInlineSnapshot(`
"/**
* AUTO-GENERATED FILE @ ${time} - DO NOT EDIT!
*
* This file was automatically generated by ${pkg.name} ${pkg.version}
*/

/**
* Exposes all fields present in complex as a typescript
* interface.
* This is especially useful for SELECT * FROM
*/
export interface Complex {
id: string
name: string
nullable?: string
created_at?: Date
created_on: Date
/** This is an awesome field */
documented_field?: string
}

/**
* Exposes the same fields as Complex,
* but makes every field containing a DEFAULT value optional.
*
* This is especially useful when generating inserts, as you
* should be able to ommit these fields if you'd like
*/
export interface ComplexWithDefaults {
id: string
name: string
nullable?: string | null
/** Defaults to: CURRENT_TIMESTAMP */
created_at?: Date | null
created_on: Date
/** This is an awesome field */
documented_field?: string | null
}
"
`)
})
})

describe('inferSchema', () => {
Expand Down Expand Up @@ -335,6 +533,38 @@ describe('inferSchema', () => {
id: string
data?: JSONValue | null
}

/**
* Exposes all fields present in with_numbers as a typescript
* interface.
* This is especially useful for SELECT * FROM
*/
export interface WithNumbers {
id: number
tiny: number
small: number
medium: number
big: number
flo: number
dou: number
}

/**
* Exposes the same fields as WithNumbers,
* but makes every field containing a DEFAULT value optional.
*
* This is especially useful when generating inserts, as you
* should be able to ommit these fields if you'd like
*/
export interface WithNumbersWithDefaults {
id: number
tiny: number
small: number
medium: number
big: number
flo: number
dou: number
}
"
`)
})
Expand Down
Loading