Skip to content

Commit

Permalink
more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kylehoehns committed Feb 20, 2025
1 parent 4134a40 commit 6d35144
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/database/__tests__/database.test.ts
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');
});
});
});
79 changes: 79 additions & 0 deletions src/utils/__tests__/s3.test.ts
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',
);
});
});
});

0 comments on commit 6d35144

Please sign in to comment.