Skip to content

Commit

Permalink
Fixed Server Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
R1c4rdCo5t4 committed Jun 5, 2024
1 parent 55e6c29 commit 90aa438
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
7 changes: 5 additions & 2 deletions code/server/src/databases/memory/MemoryResourcesDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,16 @@ export class MemoryResourcesDB implements ResourcesRepository {
const parentResource = await this.getResource(resource.parent);

// remove from parent
parentResource.children = parentResource.children.filter((childId: string) => childId !== id);
parentResource.children = parentResource.children.filter(childId => childId !== id);

// do the same for all children recursively
resource.children.forEach((childId: any) => this.deleteResource(childId));
for (const childId of resource.children) {
await this.deleteResource(childId);
}

// delete resource
delete Memory.workspaces[resource.workspace].resources[id];
console.log(Memory.workspaces[resource.workspace].resources[id]);
}

private getResourceById(id: string) {
Expand Down
19 changes: 6 additions & 13 deletions code/server/test/workspaces/resources.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TestDatabases } from '../../src/databases/TestDatabases';
import { Services } from '../../src/services/Services';
import { Resource, ResourceType } from '@notespace/shared/src/workspace/types/resource';
import { ResourceType } from '@notespace/shared/src/workspace/types/resource';
import { excludeRoot } from './utils';

let services: Services;

Expand All @@ -21,8 +22,8 @@ describe('Resource operations', () => {
const wid = await services.workspaces.createWorkspace('test', false);
const id = await services.resources.createResource(wid, 'testDoc', ResourceType.DOCUMENT);
await services.resources.deleteResource(id);
const resources = (await services.resources.getResources(wid)) as Resource[];
expect(resources.filter(r => r.id !== wid)).toEqual([]);
const resources = excludeRoot(wid, await services.workspaces.getResources(wid));
expect(resources).toEqual([]);
});

test('should update a resource', async () => {
Expand All @@ -37,15 +38,7 @@ describe('Resource operations', () => {
const wid = await services.workspaces.createWorkspace('test', false);
await services.resources.createResource(wid, 'testDoc', ResourceType.DOCUMENT);
await services.resources.createResource(wid, 'testDoc2', ResourceType.DOCUMENT);
const resources = (await services.resources.getResources(wid)) as Resource[];
expect(resources.filter(r => r.id !== wid).length).toEqual(2);
});

test('should get all resources of a type', async () => {
const wid = await services.workspaces.createWorkspace('test', false);
await services.resources.createResource(wid, 'testDoc', ResourceType.DOCUMENT);
await services.resources.createResource(wid, 'testFolder', ResourceType.FOLDER);
const resources = (await services.resources.getResources(wid, ResourceType.DOCUMENT)) as Resource[];
expect(resources.filter(r => r.id !== wid).length).toEqual(1);
const resources = excludeRoot(wid, await services.workspaces.getResources(wid));
expect(resources.length).toEqual(2);
});
});
22 changes: 16 additions & 6 deletions code/server/test/workspaces/tree.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TestDatabases } from '../../src/databases/TestDatabases';
import { Services } from '../../src/services/Services';
import { Resource, ResourceType } from '@notespace/shared/src/workspace/types/resource';
import { ResourceType } from '@notespace/shared/src/workspace/types/resource';
import { excludeRoot } from './utils';

let services: Services;

Expand All @@ -11,14 +12,14 @@ beforeEach(() => {
describe('Workspace tree operations', () => {
test('should return an empty tree', async () => {
const id = await services.workspaces.createWorkspace('test', false);
const resources = await services.resources.getResources(id);
const resources = await services.workspaces.getResources(id);
expect(resources.length).toBe(1);
});

test('should return a tree with one document', async () => {
const id = await services.workspaces.createWorkspace('test', false);
const docId = await services.resources.createResource(id, 'testDoc', ResourceType.DOCUMENT);
const resources = (await services.resources.getResources(id)).filter(r => r.id != id);
const resources = excludeRoot(id, await services.workspaces.getResources(id));
expect(resources.length).toBe(1);
expect(resources[0].id).toBe(docId);
expect(resources[0].type).toBe(ResourceType.DOCUMENT);
Expand All @@ -28,7 +29,7 @@ describe('Workspace tree operations', () => {
const id = await services.workspaces.createWorkspace('test', false);
const folderId = await services.resources.createResource(id, 'testFolder', ResourceType.FOLDER);
const docId = await services.resources.createResource(id, 'testDoc', ResourceType.DOCUMENT, folderId);
const resources = (await services.resources.getResources(id)).filter(r => r.id != id);
const resources = excludeRoot(id, await services.workspaces.getResources(id));

expect(resources.length).toBe(2);
expect(resources[0].id).toBe(folderId);
Expand All @@ -44,7 +45,7 @@ describe('Workspace tree operations', () => {
const folderId1 = await services.resources.createResource(id, 'testFolder1', ResourceType.FOLDER);
const folderId2 = await services.resources.createResource(id, 'testFolder2', ResourceType.FOLDER, folderId1);
const docId = await services.resources.createResource(id, 'testDoc', ResourceType.DOCUMENT, folderId2);
const resources = ((await services.resources.getResources(id)) as Resource[]).filter(r => r.id != id);
const resources = excludeRoot(id, await services.workspaces.getResources(id));

expect(resources.length).toBe(3);
expect(resources[0].id).toBe(folderId1);
Expand All @@ -58,13 +59,22 @@ describe('Workspace tree operations', () => {
expect(resources[0].children).toEqual([folderId2]);
});

test('should delete a single resource', async () => {
const wid = await services.workspaces.createWorkspace('test', false);
const docId = await services.resources.createResource(wid, 'doc1', ResourceType.DOCUMENT);
await services.resources.deleteResource(docId);
const resources = excludeRoot(wid, await services.workspaces.getResources(wid));
expect(resources.length).toBe(0);
});

test('should delete a resource and all its descendants', async () => {
const wid = await services.workspaces.createWorkspace('test', false);
const folderId = await services.resources.createResource(wid, 'folder', ResourceType.FOLDER);
const docId = await services.resources.createResource(wid, 'doc1', ResourceType.DOCUMENT, folderId);
await services.resources.createResource(wid, 'doc2', ResourceType.DOCUMENT, docId);
await services.resources.deleteResource(folderId);
const resources = (await services.resources.getResources(wid)).filter(r => r.id != wid);
const resources = excludeRoot(wid, await services.workspaces.getResources(wid));
console.log(resources);
expect(resources.length).toBe(0);
});
});
5 changes: 5 additions & 0 deletions code/server/test/workspaces/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Resource } from '@notespace/shared/src/workspace/types/resource';

export function excludeRoot(wid: string, resources: Resource[]): Resource[] {
return resources.filter(r => r.id != wid);
}

0 comments on commit 90aa438

Please sign in to comment.