-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from autonomys/test/backend
Setup tests on backend
- Loading branch information
Showing
44 changed files
with
2,484 additions
and
163 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
Binary file not shown.
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,131 @@ | ||
import { v4 } from 'uuid' | ||
import { NodesUseCases } from '../../../src/useCases/index.js' | ||
import { | ||
blake3HashFromCid, | ||
cidOfNode, | ||
cidToString, | ||
createFileChunkIpldNode, | ||
createNode, | ||
createSingleFileIpldNode, | ||
encodeNode, | ||
MetadataType, | ||
} from '@autonomys/auto-dag-data' | ||
import { dbMigration } from '../../utils/dbMigrate.js' | ||
import { nodesRepository } from '../../../src/repositories/index.js' | ||
import { ObjectMappingListEntry } from '../../../src/models/objects/objectMappings.js' | ||
|
||
describe('Nodes', () => { | ||
const id = v4() | ||
|
||
const expectNode = { | ||
cid: id, | ||
headCid: id, | ||
rootCid: id, | ||
type: MetadataType.File, | ||
encodedNode: '', | ||
} | ||
|
||
beforeAll(async () => { | ||
await dbMigration.up() | ||
}) | ||
afterAll(async () => { | ||
await dbMigration.down() | ||
}) | ||
|
||
it('should be able to save node', async () => { | ||
await expect( | ||
NodesUseCases.saveNode( | ||
expectNode.cid, | ||
expectNode.headCid, | ||
expectNode.rootCid, | ||
expectNode.type, | ||
expectNode.encodedNode, | ||
), | ||
).resolves.not.toThrow() | ||
}) | ||
|
||
it('should be able to get node', async () => { | ||
const node = await nodesRepository.getNode(id) | ||
expect(node).toMatchObject({ | ||
cid: expectNode.cid, | ||
head_cid: expectNode.headCid, | ||
root_cid: expectNode.rootCid, | ||
type: expectNode.type, | ||
encoded_node: expectNode.encodedNode, | ||
}) | ||
}) | ||
|
||
it('should be able to get chunk data', async () => { | ||
const buffer = Buffer.from('test') | ||
await NodesUseCases.saveNode( | ||
id, | ||
id, | ||
id, | ||
MetadataType.File, | ||
Buffer.from(encodeNode(createFileChunkIpldNode(buffer))).toString( | ||
'base64', | ||
), | ||
) | ||
const data = await NodesUseCases.getChunkData(id) | ||
expect(data).toEqual(buffer) | ||
}) | ||
|
||
it('should be able to get node with wrong cid returns undefined', async () => { | ||
const data = await NodesUseCases.getNode(v4()) | ||
expect(data).toBeUndefined() | ||
}) | ||
|
||
it('should be able to get blockstore', async () => { | ||
const node = createNode(Buffer.from('test'), []) | ||
const randomCID = cidOfNode(node) | ||
await NodesUseCases.saveNode( | ||
randomCID, | ||
randomCID, | ||
randomCID, | ||
MetadataType.File, | ||
Buffer.from( | ||
encodeNode(createFileChunkIpldNode(Buffer.from(encodeNode(node)))), | ||
).toString('base64'), | ||
) | ||
|
||
const blockstore = await nodesRepository.getNode(cidToString(randomCID)) | ||
|
||
expect(blockstore).toMatchObject({ | ||
cid: cidToString(randomCID), | ||
head_cid: cidToString(randomCID), | ||
root_cid: cidToString(randomCID), | ||
type: MetadataType.File, | ||
encoded_node: expect.any(String), | ||
}) | ||
}) | ||
|
||
it('should be able to save nodes', async () => { | ||
const nodes = Array.from({ length: 10 }, (_, index) => | ||
createSingleFileIpldNode(Buffer.from(`test-${index}`), `test-${index}`), | ||
) | ||
await NodesUseCases.saveNodes(id, id, nodes) | ||
}) | ||
|
||
it('process node archived', async () => { | ||
const node = createSingleFileIpldNode(Buffer.from('test'), 'test') | ||
const cid = cidOfNode(node) | ||
const hash = Buffer.from(blake3HashFromCid(cid)).toString('hex') | ||
const objectMappings: ObjectMappingListEntry = { | ||
blockNumber: 1, | ||
v0: { | ||
objects: [[hash, 1, 1]], | ||
}, | ||
} | ||
|
||
await NodesUseCases.saveNodes(cid, cid, [node]) | ||
|
||
await NodesUseCases.processNodeArchived(objectMappings) | ||
|
||
const savedNode = await nodesRepository.getNode(cidToString(cid)) | ||
expect(savedNode).toMatchObject({ | ||
cid: cidToString(cid), | ||
piece_index: 1, | ||
piece_offset: 1, | ||
}) | ||
}) | ||
}) |
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,218 @@ | ||
import { User } from '../../../src/models/users' | ||
import { ObjectUseCases, UsersUseCases } from '../../../src/useCases' | ||
import { dbMigration } from '../../utils/dbMigrate' | ||
import { PreconditionError } from '../../utils/error' | ||
import { createMockUser, MOCK_UNONBOARDED_USER } from '../../utils/mocks' | ||
import { uploadFile } from '../../utils/uploads' | ||
|
||
describe('Object', () => { | ||
let user: User | ||
let fileCid: string | ||
|
||
beforeAll(async () => { | ||
await dbMigration.up() | ||
const result = await UsersUseCases.onboardUser(MOCK_UNONBOARDED_USER) | ||
if (!result) { | ||
throw new PreconditionError('Failed to onboard user') | ||
} | ||
user = result | ||
fileCid = await uploadFile(user, 'test.txt', 'test', 'text/plain') | ||
}) | ||
|
||
afterAll(async () => { | ||
await dbMigration.down() | ||
}) | ||
|
||
it('should get object summary by cid', async () => { | ||
const summary = await ObjectUseCases.getObjectSummaryByCID(fileCid) | ||
expect(summary).toBeDefined() | ||
}) | ||
|
||
it('should not get listed in deleted objects', async () => { | ||
const summary = await ObjectUseCases.getMarkedAsDeletedRoots(user) | ||
expect(summary.rows.length).toBe(0) | ||
}) | ||
|
||
it('should get listed in user objects', async () => { | ||
const summary = await ObjectUseCases.getRootObjects( | ||
{ | ||
scope: 'user', | ||
user, | ||
}, | ||
1, | ||
0, | ||
) | ||
expect(summary.rows).toMatchObject([ | ||
{ | ||
headCid: fileCid, | ||
}, | ||
]) | ||
}) | ||
|
||
it('should get listed in global objects', async () => { | ||
const summary = await ObjectUseCases.getRootObjects( | ||
{ | ||
scope: 'global', | ||
}, | ||
1, | ||
0, | ||
) | ||
expect(summary.rows).toMatchObject([ | ||
{ | ||
headCid: fileCid, | ||
}, | ||
]) | ||
}) | ||
|
||
it('should be able to mark as deleted and get listed in deleted objects', async () => { | ||
await expect( | ||
ObjectUseCases.markAsDeleted(user, fileCid), | ||
).resolves.not.toThrow() | ||
|
||
const deletedSummary = await ObjectUseCases.getMarkedAsDeletedRoots(user) | ||
expect(deletedSummary.rows).toMatchObject([ | ||
{ | ||
headCid: fileCid, | ||
}, | ||
]) | ||
}) | ||
|
||
it('should be able to restore object', async () => { | ||
await expect( | ||
ObjectUseCases.restoreObject(user, fileCid), | ||
).resolves.not.toThrow() | ||
|
||
const deletedSummary = await ObjectUseCases.getMarkedAsDeletedRoots(user) | ||
expect(deletedSummary.rows).toMatchObject([]) | ||
|
||
const summary = await ObjectUseCases.getRootObjects( | ||
{ | ||
scope: 'user', | ||
user, | ||
}, | ||
1, | ||
0, | ||
) | ||
|
||
expect(summary.rows).toMatchObject([ | ||
{ | ||
headCid: fileCid, | ||
}, | ||
]) | ||
|
||
const globalSummary = await ObjectUseCases.getRootObjects( | ||
{ | ||
scope: 'global', | ||
}, | ||
1, | ||
0, | ||
) | ||
|
||
expect(globalSummary.rows).toMatchObject([ | ||
{ | ||
headCid: fileCid, | ||
}, | ||
]) | ||
}) | ||
|
||
it('should be able to search object by name', async () => { | ||
const metadata = await ObjectUseCases.getMetadata(fileCid) | ||
if (!metadata) throw new PreconditionError('Metadata not found') | ||
|
||
const search = await ObjectUseCases.searchMetadataByName( | ||
metadata.name!, | ||
5, | ||
{ | ||
scope: 'user', | ||
user, | ||
}, | ||
) | ||
expect(search).toMatchObject([{ metadata }]) | ||
}) | ||
|
||
it('should be able to search object by cid', async () => { | ||
const metadata = await ObjectUseCases.getMetadata(fileCid) | ||
if (!metadata) throw new PreconditionError('Metadata not found') | ||
|
||
const search = await ObjectUseCases.searchMetadataByCID(fileCid, 5, { | ||
scope: 'user', | ||
user, | ||
}) | ||
expect(search).toMatchObject([{ metadata }]) | ||
}) | ||
|
||
it('should be able to search object by name (using common method)', async () => { | ||
const metadata = await ObjectUseCases.getMetadata(fileCid) | ||
if (!metadata) throw new PreconditionError('Metadata not found') | ||
|
||
const search = await ObjectUseCases.searchByCIDOrName(metadata.name!, 5, { | ||
scope: 'user', | ||
user, | ||
}) | ||
expect(search).toMatchObject([ | ||
{ | ||
cid: fileCid, | ||
name: metadata.name, | ||
}, | ||
]) | ||
}) | ||
|
||
it('should be able to search object by cid (using common method)', async () => { | ||
const metadata = await ObjectUseCases.getMetadata(fileCid) | ||
if (!metadata) throw new PreconditionError('Metadata not found') | ||
|
||
const search = await ObjectUseCases.searchByCIDOrName(fileCid, 5, { | ||
scope: 'user', | ||
user, | ||
}) | ||
expect(search).toMatchObject([ | ||
{ | ||
cid: fileCid, | ||
name: metadata.name, | ||
}, | ||
]) | ||
}) | ||
|
||
describe('Share object', () => { | ||
let randomFile: string | ||
it('should be able to share object', async () => { | ||
const mockUser = await createMockUser() | ||
randomFile = await uploadFile(mockUser, 'test.txt', 'test', 'text/plain') | ||
|
||
await expect( | ||
ObjectUseCases.shareObject(mockUser, randomFile, user.publicId!), | ||
).resolves.not.toThrow() | ||
|
||
const sharedRoots = await ObjectUseCases.getSharedRoots(user) | ||
expect(sharedRoots.rows).toMatchObject([ | ||
{ | ||
headCid: randomFile, | ||
}, | ||
]) | ||
}) | ||
|
||
it('should be able to delete shared object', async () => { | ||
await expect( | ||
ObjectUseCases.markAsDeleted(user, randomFile), | ||
).resolves.not.toThrow() | ||
}) | ||
|
||
it('should not be listed in shared objects', async () => { | ||
const sharedRoots = await ObjectUseCases.getSharedRoots(user) | ||
expect(sharedRoots.rows).toMatchObject([]) | ||
}) | ||
|
||
it('should be able to restore shared object', async () => { | ||
await expect( | ||
ObjectUseCases.restoreObject(user, randomFile), | ||
).resolves.not.toThrow() | ||
|
||
const sharedRoots = await ObjectUseCases.getSharedRoots(user) | ||
expect(sharedRoots.rows).toMatchObject([ | ||
{ | ||
headCid: randomFile, | ||
}, | ||
]) | ||
}) | ||
}) | ||
}) |
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,34 @@ | ||
import { v4 } from 'uuid' | ||
import { TransactionResult } from '../../../src/models/objects' | ||
import { TransactionResultsUseCases } from '../../../src/useCases' | ||
import { dbMigration } from '../../utils/dbMigrate' | ||
|
||
describe('Transaction Results', () => { | ||
beforeAll(async () => { | ||
await dbMigration.up() | ||
}) | ||
|
||
afterAll(async () => { | ||
await dbMigration.down() | ||
}) | ||
|
||
it('should be able to save transaction results', async () => { | ||
const transactionResult: TransactionResult = { | ||
success: true, | ||
batchTxHash: '0x123', | ||
status: 'success', | ||
} | ||
|
||
const cid = v4() | ||
|
||
await TransactionResultsUseCases.setTransactionResults( | ||
cid, | ||
transactionResult, | ||
) | ||
|
||
const savedTransactionResult = | ||
await TransactionResultsUseCases.getNodeTransactionResult(cid) | ||
|
||
expect(savedTransactionResult).toEqual(transactionResult) | ||
}) | ||
}) |
Oops, something went wrong.