-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from HubSpot/br-unit-tests-3
Unit tests for hubdb utils
- Loading branch information
Showing
10 changed files
with
284 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"status": "COMPLETE", | ||
"results": [ | ||
{ | ||
"name": "My Event", | ||
"id": "string", | ||
"createdAt": "string", | ||
"updatedAt": "string", | ||
"publishedAt": null, | ||
"values": { "text_column": "a" }, | ||
"path": null | ||
}, | ||
{ | ||
"name": "My Better Event", | ||
"id": "string", | ||
"createdAt": "string", | ||
"updatedAt": "string", | ||
"publishedAt": null, | ||
"values": { "text_column": "b" }, | ||
"path": null | ||
}, | ||
{ | ||
"name": "My Best Event", | ||
"id": "string", | ||
"createdAt": "string", | ||
"updatedAt": "string", | ||
"publishedAt": null, | ||
"values": { "text_column": "c" }, | ||
"path": null | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"total": 3, | ||
"results": [ | ||
{ | ||
"name": "My Event", | ||
"id": "string", | ||
"createdAt": "string", | ||
"updatedAt": "string", | ||
"publishedAt": null, | ||
"values": { "text_column": "a" }, | ||
"path": null | ||
}, | ||
{ | ||
"name": "My Better Event", | ||
"id": "string", | ||
"createdAt": "string", | ||
"updatedAt": "string", | ||
"publishedAt": null, | ||
"values": { "text_column": "b" }, | ||
"path": null | ||
}, | ||
{ | ||
"name": "My Best Event", | ||
"id": "string", | ||
"createdAt": "string", | ||
"updatedAt": "string", | ||
"publishedAt": null, | ||
"values": { "text_column": "c" }, | ||
"path": null | ||
} | ||
], | ||
"paging": null | ||
} |
37 changes: 37 additions & 0 deletions
37
lib/__tests__/fixtures/hubdb/fetchRowsResponseWithPaging.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
"total": 3, | ||
"results": [ | ||
{ | ||
"name": "Paging 1", | ||
"id": "string", | ||
"createdAt": "string", | ||
"updatedAt": "string", | ||
"publishedAt": null, | ||
"values": { "text_column": "a" }, | ||
"path": null | ||
}, | ||
{ | ||
"name": "Paging 2", | ||
"id": "string", | ||
"createdAt": "string", | ||
"updatedAt": "string", | ||
"publishedAt": null, | ||
"values": { "text_column": "b" }, | ||
"path": null | ||
}, | ||
{ | ||
"name": "Paging 3", | ||
"id": "string", | ||
"createdAt": "string", | ||
"updatedAt": "string", | ||
"publishedAt": null, | ||
"values": { "text_column": "c" }, | ||
"path": null | ||
} | ||
], | ||
"paging": { | ||
"next": { | ||
"after": "testing" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"id": "cool-table-id", | ||
"name": "cool-table-name", | ||
"createdAt": "string", | ||
"publishedAt": "string", | ||
"updatedAt": "string", | ||
"label": "string", | ||
"allowChildTables": false, | ||
"allowPublicApiAccess": false, | ||
"columns": [ | ||
{ | ||
"name": "first_col", | ||
"label": "first_col", | ||
"id": "col_1", | ||
"type": "TEXT" | ||
}, | ||
{ | ||
"name": "second_col", | ||
"label": "first_col", | ||
"id": "col_2", | ||
"type": "TEXT" | ||
} | ||
], | ||
"rowCount": 3, | ||
"useForPages": false, | ||
"enableChildTablePages": false, | ||
"archived": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import fs from 'fs-extra'; | ||
import { | ||
downloadHubDbTable, | ||
createHubDbTable, | ||
clearHubDbTableRows, | ||
} from '../hubdb'; | ||
import { | ||
createRows as __createRows, | ||
createTable as __createTable, | ||
fetchRows as __fetchRows, | ||
fetchTable as __fetchTable, | ||
publishTable as __publishTable, | ||
} from '../../api/hubdb'; | ||
import { getCwd as __getCwd } from '../path'; | ||
import hubdbFetchRowResponse from './fixtures/hubdb/fetchRowsResponse.json'; | ||
import hubdbFetchRowsResponseWithPaging from './fixtures/hubdb/fetchRowsResponseWithPaging.json'; | ||
import hubdbTableResponse from './fixtures/hubdb/tableResponse.json'; | ||
import hubdbCreateRowsResponse from './fixtures/hubdb/createRowsResponse.json'; | ||
import { Table } from '../../types/Hubdb'; | ||
|
||
jest.mock('fs-extra'); | ||
jest.mock('../path'); | ||
jest.mock('../../api/hubdb'); | ||
|
||
const mockedFS = fs as jest.Mocked<typeof fs>; | ||
const getCwd = __getCwd as jest.MockedFunction<typeof __getCwd>; | ||
const createRows = __createRows as jest.MockedFunction<typeof __createRows>; | ||
const createTable = __createTable as jest.MockedFunction<typeof __createTable>; | ||
const fetchRows = __fetchRows as jest.MockedFunction<typeof __fetchRows>; | ||
const fetchTable = __fetchTable as jest.MockedFunction<typeof __fetchTable>; | ||
const publishTable = __publishTable as jest.MockedFunction< | ||
typeof __publishTable | ||
>; | ||
|
||
describe('hubdb', () => { | ||
describe('downloadHubDbTable', () => { | ||
const accountId = 123; | ||
const tableId = '456'; | ||
const destPath = 'tmp.json'; | ||
const projectCwd = '/home/tom/projects'; | ||
|
||
beforeEach(() => { | ||
mockedFS.outputFile.mockClear(); | ||
getCwd.mockReturnValue(projectCwd); | ||
fetchRows.mockReturnValue(Promise.resolve(hubdbFetchRowResponse)); | ||
fetchTable.mockReturnValue(Promise.resolve(hubdbTableResponse)); | ||
}); | ||
|
||
it('fetches all results', async () => { | ||
const { filePath } = await downloadHubDbTable( | ||
accountId, | ||
tableId, | ||
destPath | ||
); | ||
const outputFile = JSON.parse( | ||
mockedFS.outputFile.mock.calls[0][1] as string | ||
) as unknown as Table; | ||
|
||
if (!outputFile.rows) { | ||
throw new Error('No rows found on the table object'); | ||
} | ||
|
||
expect(outputFile.rows.length).toBe(3); | ||
expect(outputFile.rows[1].name).toBe('My Better Event'); | ||
expect(outputFile.rows[0].values['text_column']).toBe('a'); | ||
expect(outputFile.name).toBe('cool-table-name'); | ||
expect(filePath).toEqual(`${projectCwd}/${destPath}`); | ||
}); | ||
|
||
it('fetches all results with paging', async () => { | ||
fetchRows.mockReturnValueOnce( | ||
Promise.resolve(hubdbFetchRowsResponseWithPaging) | ||
); | ||
await downloadHubDbTable(accountId, tableId, destPath); | ||
const outputFile = JSON.parse( | ||
mockedFS.outputFile.mock.calls[0][1] as string | ||
) as unknown as Table; | ||
|
||
if (!outputFile.rows) { | ||
throw new Error('No rows found on the table object'); | ||
} | ||
|
||
expect(outputFile.rows.length).toEqual(6); | ||
expect(outputFile.rows[0].name).toMatchInlineSnapshot(`"Paging 1"`); | ||
expect(outputFile.rows[5].name).toMatchInlineSnapshot(`"My Best Event"`); | ||
}); | ||
}); | ||
|
||
describe('createHubDbTable', () => { | ||
it('creates a table', async () => { | ||
const accountId = 123; | ||
const srcPath = 'tmp.json'; | ||
const projectCwd = '/home/tom/projects'; | ||
|
||
mockedFS.statSync.mockReturnValue({ isFile: () => true } as fs.Stats); | ||
mockedFS.readJsonSync.mockReturnValue(hubdbTableResponse); | ||
|
||
createTable.mockReturnValue(Promise.resolve(hubdbTableResponse)); | ||
createRows.mockReturnValue(Promise.resolve(hubdbCreateRowsResponse)); | ||
publishTable.mockReturnValue(Promise.resolve(hubdbTableResponse)); | ||
|
||
const table = await createHubDbTable( | ||
accountId, | ||
`${projectCwd}/${srcPath}` | ||
); | ||
|
||
expect(table.rowCount).toEqual(3); | ||
expect(table.tableId).toEqual('cool-table-id'); | ||
expect(publishTable).toBeCalled(); | ||
}); | ||
}); | ||
|
||
describe('clearHubDbTableRows', () => { | ||
it('clears all of the hubdb table rows', async () => { | ||
fetchRows.mockReturnValue(Promise.resolve(hubdbFetchRowResponse)); | ||
const result = await clearHubDbTableRows(123, '456'); | ||
|
||
expect(result.deletedRowCount).toBe(3); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters