Skip to content

Commit

Permalink
test: unknown and empty storage and datastore collection
Browse files Browse the repository at this point in the history
Signed-off-by: David Dal Busco <david.dalbusco@outlook.com>
  • Loading branch information
peterpeterparker committed Aug 21, 2024
1 parent 7039a5e commit 85a1b3d
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 34 deletions.
40 changes: 40 additions & 0 deletions src/tests/satellite.datastore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,46 @@ describe.each([{ memory: { Heap: null } }, { memory: { Stable: null } }])(
});
});

describe('collection', () => {
beforeAll(async () => {
actor.setIdentity(controller);
});

it('should not delete not empty collection', async () => {
const { del_rule } = actor;

try {
await del_rule({ Db: null }, TEST_COLLECTION, { version: [1n] });

expect(true).toBe(false);
} catch (error: unknown) {
expect((error as Error).message).toContain(
`The "${TEST_COLLECTION}" collection in Datastore is not empty.`
);
}
});

it('should not set doc in unknown collection', async () => {
const { set_doc } = actor;

const collectionUnknown = 'unknown';

try {
await set_doc(collectionUnknown, nanoid(), {
data,
description: toNullable(),
version: toNullable()
});

expect(true).toBe(false);
} catch (error: unknown) {
expect((error as Error).message).toContain(
`Collection "${collectionUnknown}" not found in Datastore.`
);
}
});
});

describe('config', () => {
const setRule: SetRule = {
memory: toNullable(memory),
Expand Down
118 changes: 84 additions & 34 deletions src/tests/satellite.storage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,45 @@ describe('Satellite storage', () => {
});
});

const HTML = '<html><body>Hello</body></html>';

const blob = new Blob([HTML], {
type: 'text/plain; charset=utf-8'
});

const upload = async ({
full_path,
name,
collection
}: {
full_path: string;
name: string;
collection: string;
}) => {
const { commit_asset_upload, upload_asset_chunk, init_asset_upload } = actor;

const file = await init_asset_upload({
collection,
description: toNullable(),
encoding_type: [],
full_path,
name,
token: toNullable()
});

const chunk = await upload_asset_chunk({
batch_id: file.batch_id,
content: arrayBufferToUint8Array(await blob.arrayBuffer()),
order_id: [0n]
});

await commit_asset_upload({
batch_id: file.batch_id,
chunk_ids: [chunk.chunk_id],
headers: []
});
};

describe('admin', () => {
beforeAll(() => {
actor.setIdentity(controller);
Expand Down Expand Up @@ -250,37 +289,6 @@ describe('Satellite storage', () => {
({ memory }) => {
const collection = `test_${'Heap' in memory ? 'heap' : 'stable'}`;

const HTML = '<html><body>Hello</body></html>';

const blob = new Blob([HTML], {
type: 'text/plain; charset=utf-8'
});

const upload = async ({ full_path, name }: { full_path: string; name: string }) => {
const { commit_asset_upload, upload_asset_chunk, init_asset_upload } = actor;

const file = await init_asset_upload({
collection,
description: toNullable(),
encoding_type: [],
full_path,
name,
token: toNullable()
});

const chunk = await upload_asset_chunk({
batch_id: file.batch_id,
content: arrayBufferToUint8Array(await blob.arrayBuffer()),
order_id: [0n]
});

await commit_asset_upload({
batch_id: file.batch_id,
chunk_ids: [chunk.chunk_id],
headers: []
});
};

it('should create a collection', async () => {
const { set_rule, list_rules } = actor;

Expand Down Expand Up @@ -336,7 +344,7 @@ describe('Satellite storage', () => {
const name = 'hello.html';
const full_path = `/${collection}/${name}`;

await upload({ full_path, name });
await upload({ full_path, name, collection });

const { headers, body } = await http_request({
body: [],
Expand Down Expand Up @@ -388,14 +396,14 @@ describe('Satellite storage', () => {
const name = 'update.html';
const full_path = `/${collection}/${name}`;

await upload({ full_path, name });
await upload({ full_path, name, collection });

const asset = fromNullable(await get_asset(collection, full_path));

expect(asset).not.toBeUndefined();
expect(fromNullable(asset!.version) ?? 0n).toEqual(1n);

await upload({ full_path, name });
await upload({ full_path, name, collection });

const updatedAsset = fromNullable(await get_asset(collection, full_path));

Expand Down Expand Up @@ -897,6 +905,48 @@ describe('Satellite storage', () => {
);
});

describe('collection', () => {
beforeAll(async () => {
actor.setIdentity(controller);
});

it('should not delete not empty collection', async () => {
const { del_rule } = actor;

const collection = 'images_heap';

try {
await del_rule({ Storage: null }, collection, { version: [1n] });

expect(true).toBe(false);
} catch (error: unknown) {
expect((error as Error).message).toContain(
`The "${collection}" collection in Storage is not empty.`
);
}
});

it('should not upload file in existing collection', async () => {
const { set_doc } = actor;

const collectionUnknown = 'unknown';

try {
await upload({
full_path: `/${collectionUnknown}/hello.html`,
name: 'hello.html',
collection: collectionUnknown
});

expect(true).toBe(false);
} catch (error: unknown) {
expect((error as Error).message).toContain(
`Collection "${collectionUnknown}" not found in Storage.`
);
}
});
});

describe('More configuration', () => {
const maxHeapMemorySize = 3_932_160n;
const maxStableMemorySize = 2_000_000n;
Expand Down

0 comments on commit 85a1b3d

Please sign in to comment.