-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4134a40
commit 6d35144
Showing
2 changed files
with
123 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { database } from '@database'; | ||
import { GoogleSpreadsheet, GoogleSpreadsheetWorksheet } from 'google-spreadsheet'; | ||
|
||
jest.mock('google-spreadsheet', () => { | ||
return { | ||
GoogleSpreadsheet: jest.fn(() => ({ | ||
useServiceAccountAuth: jest.fn(), | ||
loadInfo: jest.fn(), | ||
sheetsByTitle: {} as Record<string, GoogleSpreadsheetWorksheet>, | ||
addSheet: jest.fn(async ({ title }) => ({ title, setHeaderRow: jest.fn() })), | ||
})), | ||
}; | ||
}); | ||
|
||
describe('database', () => { | ||
let mockDocument: GoogleSpreadsheet; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
mockDocument = new GoogleSpreadsheet(); | ||
}); | ||
|
||
describe('openSheet', () => { | ||
it('should return an existing sheet if available', async () => { | ||
const mockSheet: GoogleSpreadsheetWorksheet = { setHeaderRow: jest.fn() } as any; | ||
mockDocument.sheetsByTitle['TestSheet'] = mockSheet; | ||
database.document = mockDocument; | ||
|
||
const sheet = await database.openSheet('TestSheet', ['col1', 'col2']); | ||
expect(sheet).toBe(mockSheet); | ||
expect(mockSheet.setHeaderRow).toHaveBeenCalledWith(['col1', 'col2']); | ||
}); | ||
|
||
it('should create a new sheet if not found', async () => { | ||
database.document = mockDocument; | ||
const newSheet = await database.openSheet('NewSheet', ['colA', 'colB']); | ||
expect(mockDocument.addSheet).toHaveBeenCalledWith({ | ||
title: 'NewSheet', | ||
headerValues: ['colA', 'colB'], | ||
}); | ||
expect(newSheet.title).toBe('NewSheet'); | ||
}); | ||
}); | ||
}); |
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,79 @@ | ||
import { S3Client, PutObjectCommand, ListObjectsV2Command } from '@aws-sdk/client-s3'; | ||
import { uploadFileToS3, listKeysWithPrefixWithinS3 } from '../s3'; | ||
|
||
jest.mock('@aws-sdk/client-s3', () => { | ||
return { | ||
S3Client: jest.fn(), | ||
PutObjectCommand: jest.fn(), | ||
ListObjectsV2Command: jest.fn(), | ||
}; | ||
}); | ||
|
||
describe('S3', () => { | ||
let mockSend: jest.Mock; | ||
|
||
beforeEach(() => { | ||
mockSend = jest.fn(); | ||
(S3Client as jest.Mock).mockImplementation(() => ({ send: mockSend })); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('uploadFileToS3', () => { | ||
it('should upload a file to S3 successfully', async () => { | ||
mockSend.mockResolvedValueOnce({}); | ||
|
||
await expect(uploadFileToS3('test-bucket', 'test-key', 'test-body')).resolves.not.toThrow(); | ||
|
||
expect(PutObjectCommand).toHaveBeenCalledWith({ | ||
Bucket: 'test-bucket', | ||
Key: 'test-key', | ||
Body: 'test-body', | ||
}); | ||
expect(mockSend).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should throw an error if upload fails', async () => { | ||
mockSend.mockRejectedValueOnce(new Error('Upload failed')); | ||
|
||
await expect(uploadFileToS3('test-bucket', 'test-key', 'test-body')).rejects.toThrow( | ||
'Upload failed', | ||
); | ||
}); | ||
}); | ||
|
||
describe('listKeysWithPrefixWithinS3', () => { | ||
it('should return list of keys from S3', async () => { | ||
mockSend.mockResolvedValueOnce({ | ||
Contents: [{ Key: 'test-key-1' }, { Key: 'test-key-2' }], | ||
}); | ||
|
||
await expect(listKeysWithPrefixWithinS3('test-bucket', 'test-prefix')).resolves.toEqual([ | ||
'test-key-1', | ||
'test-key-2', | ||
]); | ||
|
||
expect(ListObjectsV2Command).toHaveBeenCalledWith({ | ||
Bucket: 'test-bucket', | ||
Prefix: 'test-prefix', | ||
}); | ||
expect(mockSend).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should return an empty array if no contents are found', async () => { | ||
mockSend.mockResolvedValueOnce({}); | ||
|
||
await expect(listKeysWithPrefixWithinS3('test-bucket', 'test-prefix')).resolves.toEqual([]); | ||
}); | ||
|
||
it('should throw an error if listing fails', async () => { | ||
mockSend.mockRejectedValueOnce(new Error('Listing failed')); | ||
|
||
await expect(listKeysWithPrefixWithinS3('test-bucket', 'test-prefix')).rejects.toThrow( | ||
'Listing failed', | ||
); | ||
}); | ||
}); | ||
}); |