Skip to content

Commit 7f2b2af

Browse files
committed
test: Write first test
1 parent 5146c0b commit 7f2b2af

File tree

2 files changed

+83
-8
lines changed

2 files changed

+83
-8
lines changed

src/lib/TypeScriptTypes.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@ import { PostgresMeta } from "."
33
export default class TypeScriptTypes {
44
pgMeta: PostgresMeta
55

6-
constructor() {
7-
this.pgMeta = new PostgresMeta({
8-
connectionString: "postgres://postgres:postgres@localhost:5432/postgres",
9-
max: 1
10-
})
6+
constructor({ pgMeta }: { pgMeta: PostgresMeta }) {
7+
this.pgMeta = pgMeta;
118
}
129

1310
async dump(): Promise<any> {
@@ -27,6 +24,3 @@ export default class TypeScriptTypes {
2724
}
2825
}
2926
}
30-
31-
32-
new TypeScriptTypes().dump().then(console.log)

test/lib/TypeScriptTypes.spec.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
var assert = require('assert')
2+
var sinon = require('sinon')
3+
4+
import TypeScriptTypes from '../../bin/src/lib/TypeScriptTypes'
5+
import { PostgresMeta } from '../../bin/src/lib'
6+
7+
8+
describe('dump()', () => {
9+
it('returns a string of TypeScript types', async () => {
10+
const pgMeta = new PostgresMeta({ connectionString: '', max: 1 })
11+
const columnsData = [
12+
{
13+
table_id: 16402,
14+
schema: 'public',
15+
table: 'todos',
16+
id: '16402.1',
17+
ordinal_position: 1,
18+
name: 'id',
19+
default_value: null,
20+
data_type: 'bigint',
21+
format: 'int8',
22+
is_identity: true,
23+
identity_generation: 'BY DEFAULT',
24+
is_nullable: false,
25+
is_updatable: true,
26+
enums: [],
27+
comment: null
28+
},
29+
{
30+
table_id: 16402,
31+
schema: 'public',
32+
table: 'todos',
33+
id: '16402.2',
34+
ordinal_position: 2,
35+
name: 'details',
36+
default_value: null,
37+
data_type: 'text',
38+
format: 'text',
39+
is_identity: false,
40+
identity_generation: null,
41+
is_nullable: true,
42+
is_updatable: true,
43+
enums: [],
44+
comment: null
45+
},
46+
{
47+
table_id: 16402,
48+
schema: 'public',
49+
table: 'todos',
50+
id: '16402.3',
51+
ordinal_position: 3,
52+
name: 'user-id',
53+
default_value: null,
54+
data_type: 'bigint',
55+
format: 'int8',
56+
is_identity: false,
57+
identity_generation: null,
58+
is_nullable: false,
59+
is_updatable: true,
60+
enums: [],
61+
comment: null
62+
},
63+
]
64+
sinon
65+
.stub(pgMeta.columns, "list")
66+
.returns(Promise.resolve({ data: columnsData }))
67+
68+
const example = new TypeScriptTypes({ pgMeta: pgMeta });
69+
70+
// TODO: ewww
71+
const expected = `export interface definitions {
72+
todos: {
73+
id: number;
74+
details?: string;
75+
user_id: number;
76+
};
77+
}`
78+
79+
assert.equal(await example.dump(), expected)
80+
})
81+
})

0 commit comments

Comments
 (0)