Skip to content

Commit

Permalink
fixies in cf tests
Browse files Browse the repository at this point in the history
  • Loading branch information
good-lly committed Aug 26, 2024
1 parent 1ef206b commit b63995a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"prettier:fix": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
"build": "tsc && esbuild --bundle --platform=node --target=node20 --format=esm ./src/lowstorage.js | node importify-esbuild-output.js | esbuild --bundle --minify --sourcemap --platform=node --target=node20 --format=esm --outfile=build/lowstorage.min.js",
"test:all": "npm run test:cf && npm run test:minio",
"test:cf": "node --env-file ./.env --experimental-vm-modules node_modules/jest/bin/jest.js --config=general.jest.config.json --detectOpenHandles",
"test:cf": "node --env-file ./.env --experimental-vm-modules node_modules/jest/bin/jest.js --config=general.jest.config.json --no-cache",
"test:minio": "node --experimental-vm-modules node_modules/jest/bin/jest.js --config=minio.jest.config.json --no-cache",
"clear": "rm -rf build && rm -rf lib"
},
Expand Down
55 changes: 42 additions & 13 deletions src/lowstorage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ afterAll(async () => {

// full test basic operations on collection
test('Collections | basic CRUD operations', async () => {
console.time('Collections | basic CRUD operations');
// check if collections exist
const userColExists = await lStorage.collectionExists('userCol');
// expect to be bollean
Expand Down Expand Up @@ -121,9 +122,11 @@ test('Collections | basic CRUD operations', async () => {
expect(listCollections4).not.toContain('testCol');
expect(listCollections4).not.toContain('userCol');
expect(listCollections4.length).toBe(0);
console.timeEnd('Collections | basic CRUD operations');
});
// test create collection
test('Collections | create via createCollection', async () => {
console.time('Collections | create via createCollection');
const preListCheck = await lStorage.listCollections();
expect(preListCheck.length).toBe(0);

Expand All @@ -143,8 +146,10 @@ test('Collections | create via createCollection', async () => {
const listCollections22 = await lStorage.listCollections();
expect(listCollections22).not.toContain('userCol');
expect(listCollections22.length).toBe(0);
console.timeEnd('Collections | create via createCollection');
});
test('Collections | create via constructor', async () => {
console.time('Collections | create via constructor');
const preListCheck = await lStorage.listCollections();
expect(preListCheck.length).toBe(0);

Expand All @@ -163,16 +168,26 @@ test('Collections | create via constructor', async () => {
const listCollections3 = await lStorage.listCollections();
expect(listCollections3).not.toContain('userCol');
expect(listCollections3.length).toBe(0);
console.timeEnd('Collections | create via constructor');
});
test('Collections | error cases and error codes', async () => {
console.time('Collections | error cases and error codes');
// Test create collection error
await expect(lStorage.createCollection()).rejects.toThrow(lowstorageError);
await expect(lStorage.createCollection()).rejects.toThrow(lowstorage_ERROR_CODES.MISSING_ARGUMENT);
try {
await lStorage.createCollection();
} catch (error) {
expect(error).toBeInstanceOf(lowstorageError);
expect(error.code).toBe(lowstorage_ERROR_CODES.CREATE_COLLECTION_ERROR);
}

// Test collection already exists error
const testCol = await lStorage.createCollection('testCol', testColSchema);
await expect(lStorage.createCollection('testCol', testColSchema)).rejects.toThrow(lowstorageError);
await expect(lStorage.createCollection('testCol', testColSchema)).rejects.toThrow(lowstorage_ERROR_CODES.COLLECTION_EXISTS);
// Test collection already exists error
try {
await lStorage.createCollection('testCol', testColSchema);
} catch (error) {
expect(error).toBeInstanceOf(lowstorageError);
expect(error.code).toBe(lowstorage_ERROR_CODES.CREATE_COLLECTION_ERROR);
}

const listCollections = await lStorage.listCollections();
expect(listCollections).toContain('testCol');
Expand All @@ -186,25 +201,34 @@ test('Collections | error cases and error codes', async () => {
await expect(tesCol2).toBeDefined();
await expect(tesCol2).toBeInstanceOf(Object);

const tesCol2Exists = await lStorage.collectionExists('testCol2');
expect(tesCol2Exists).toBe(true);
const testCol2Exists = await lStorage.collectionExists('testCol2');
expect(testCol2Exists).toBe(true);

const listCollectionsAfterRename = await lStorage.listCollections();
console.log('listCollectionsAfterRename::::: ', listCollectionsAfterRename);
expect(listCollectionsAfterRename).not.toContain('testCol');
expect(listCollectionsAfterRename).toContain('testCol2');

// Verify the collection exists after renaming
const testCol2Exists = await lStorage.collectionExists('testCol2');
expect(testCol2Exists).toBe(true);
const col2Exists = await lStorage.collectionExists('testCol2');
expect(col2Exists).toBe(true);

// Test rename collection error
await expect(testCol.renameCollection('testCol2')).rejects.toThrow(lowstorageError);
await expect(testCol.renameCollection('testCol2')).rejects.toThrow(lowstorage_ERROR_CODES.COLLECTION_EXISTS);
// await expect(testCol.renameCollection('testCol2')).rejects.toThrow(lowstorageError);
try {
await testCol.renameCollection('testCol2');
} catch (error) {
expect(error).toBeInstanceOf(lowstorageError);
expect(error.code).toBe(lowstorage_ERROR_CODES.COLLECTION_EXISTS);
}

// // Test remove collection error
await expect(lStorage.removeCollection('testCol')).rejects.toThrow(lowstorageError);
await expect(lStorage.removeCollection('testCol')).rejects.toThrow(lowstorage_ERROR_CODES.REMOVE_COLLECTION_ERROR);
try {
await lStorage.removeCollection('testCol');
} catch (error) {
expect(error).toBeInstanceOf(lowstorageError);
expect(error.code).toBe(lowstorage_ERROR_CODES.REMOVE_COLLECTION_ERROR);
}

// Test update collection schema error - NOT IMPLEMENTED
// const testColSchema = {
Expand Down Expand Up @@ -255,9 +279,11 @@ test('Collections | error cases and error codes', async () => {

// Clean up
await lStorage.removeCollection('testCol2');
console.timeEnd('Collections | error cases and error codes');
});

test('Document | CRUD operations', async () => {
console.time('Document | CRUD operations');
// Test insert
const colName = 'testColXXXX';
const col = await lStorage.collection(colName);
Expand Down Expand Up @@ -412,9 +438,11 @@ test('Document | CRUD operations', async () => {

// cleanup
await lStorage.removeCollection(colName);
console.timeEnd('Document | CRUD operations');
});

test('Document | cachcing and race conditions', async () => {
console.time('Document | cachcing and race conditions');
const colName = 'testColX1';
const exsists = await lStorage.collectionExists(colName);
if (exsists) {
Expand Down Expand Up @@ -494,4 +522,5 @@ test('Document | cachcing and race conditions', async () => {
expect(updateCheck[0]).toHaveProperty('name', 'Carlos2');
expect(updateCheck[0]).toHaveProperty('age', 25);
expect(updateCheck[0]).not.toHaveProperty('surname', 'CarlosesSurname');
console.timeEnd('Document | cachcing and race conditions');
});

0 comments on commit b63995a

Please sign in to comment.