1
0
mirror of https://github.com/oceanprotocol-archive/squid-js.git synced 2024-02-02 15:31:51 +01:00
squid-js/test/unit/aquarius/Aquarius.test.ts

161 lines
5.0 KiB
TypeScript
Raw Normal View History

2019-06-20 00:20:09 +02:00
import { assert, spy, use } from 'chai'
2019-11-11 12:27:18 +01:00
import spies from 'chai-spies'
2020-01-30 22:08:18 +01:00
import { Ocean } from '../../../src/ocean/Ocean'
import { Aquarius, SearchQuery } from '../../../src/aquarius/Aquarius'
import { DDO } from '../../../src/ddo/DDO'
import DID from '../../../src/ocean/DID'
2019-06-20 00:20:09 +02:00
import config from '../config'
2020-01-30 22:08:18 +01:00
import { LoggerInstance } from '../../../src/utils'
use(spies)
const reponsify = async (data) => ({
2019-06-20 00:20:09 +02:00
ok: true,
json: () => Promise.resolve(data)
})
2018-10-26 10:40:46 +02:00
2019-06-20 00:20:09 +02:00
describe('Aquarius', () => {
let ocean: Ocean
let aquarius: Aquarius
2019-06-20 00:20:09 +02:00
/* eslint-disable @typescript-eslint/camelcase */
2019-11-15 00:00:10 +01:00
const getResults = (
results: DDO[],
page = 0,
total_pages = 1,
total_results = 1
) => ({
2019-09-09 12:18:54 +02:00
results,
page,
total_pages,
total_results
})
2019-06-20 00:20:09 +02:00
/* eslint-enable @typescript-eslint/camelcase */
beforeEach(async () => {
ocean = await Ocean.getInstance(config)
2019-06-20 00:20:09 +02:00
aquarius = ocean.aquarius // eslint-disable-line prefer-destructuring
})
afterEach(() => {
spy.restore()
})
2019-06-20 00:20:09 +02:00
describe('#queryMetadata()', () => {
const query = {
offset: 100,
2019-04-16 19:01:11 +02:00
page: 1,
query: {
2019-06-20 00:20:09 +02:00
value: 1
},
sort: {
2019-06-20 00:20:09 +02:00
value: 1
},
2019-06-20 00:20:09 +02:00
text: 'Office'
} as SearchQuery
2018-10-26 13:40:09 +02:00
2019-06-20 00:20:09 +02:00
it('should query metadata', async () => {
spy.on(aquarius.fetch, 'post', () => reponsify(getResults([new DDO()])))
2019-04-05 12:20:42 +02:00
const result = await aquarius.queryMetadata(query)
2019-06-20 00:20:09 +02:00
assert.typeOf(result.results, 'array')
2019-04-05 12:20:42 +02:00
assert.lengthOf(result.results, 1)
assert.equal(result.page, 0)
assert.equal(result.totalPages, 1)
assert.equal(result.totalResults, 1)
2018-11-01 12:47:48 +01:00
})
2019-06-20 00:20:09 +02:00
it('should query metadata and return real ddo', async () => {
spy.on(aquarius.fetch, 'post', () => reponsify(getResults([new DDO()])))
2019-04-05 12:20:42 +02:00
const result = await aquarius.queryMetadata(query)
2019-06-20 00:20:09 +02:00
assert.typeOf(result.results, 'array')
2019-04-05 12:20:42 +02:00
assert.lengthOf(result.results, 1)
assert.isDefined(result.results[0].findServiceById)
})
2018-11-01 12:47:48 +01:00
})
2019-06-20 00:20:09 +02:00
describe('#queryMetadataByText()', () => {
const query = {
offset: 100,
2019-04-16 19:01:11 +02:00
page: 1,
query: {
2019-06-20 00:20:09 +02:00
value: 1
},
sort: {
2019-06-20 00:20:09 +02:00
value: 1
},
2019-06-20 00:20:09 +02:00
text: 'Office'
} as SearchQuery
2018-11-01 12:47:48 +01:00
2019-06-20 00:20:09 +02:00
it('should query metadata by text', async () => {
spy.on(aquarius.fetch, 'get', () => reponsify(getResults([new DDO()])))
2019-04-05 12:20:42 +02:00
const result = await aquarius.queryMetadataByText(query)
2019-06-20 00:20:09 +02:00
assert.typeOf(result.results, 'array')
2019-04-05 12:20:42 +02:00
assert.lengthOf(result.results, 1)
assert.equal(result.page, 0)
assert.equal(result.totalPages, 1)
assert.equal(result.totalResults, 1)
2018-10-26 13:37:09 +02:00
})
2018-10-26 13:40:09 +02:00
it('should query metadata by text with a new instance', async () => {
const aquariusNew = new Aquarius(config.aquariusUri, LoggerInstance)
spy.on(aquariusNew.fetch, 'get', () => reponsify(getResults([new DDO()])))
2020-01-15 15:58:44 +01:00
const result = await aquariusNew.queryMetadataByText(query)
assert.typeOf(result.results, 'array')
assert.lengthOf(result.results, 1)
assert.equal(result.page, 0)
assert.equal(result.totalPages, 1)
assert.equal(result.totalResults, 1)
})
2019-06-20 00:20:09 +02:00
it('should query metadata and return real ddo', async () => {
spy.on(aquarius.fetch, 'get', () => reponsify(getResults([new DDO()])))
2019-04-05 12:20:42 +02:00
const result = await aquarius.queryMetadataByText(query)
2019-06-20 00:20:09 +02:00
assert.typeOf(result.results, 'array')
2019-04-05 12:20:42 +02:00
assert.lengthOf(result.results, 1)
assert.isDefined(result.results[0].findServiceById)
})
2018-10-26 10:40:46 +02:00
})
2019-06-20 00:20:09 +02:00
describe('#storeDDO()', () => {
it('should store a ddo', async () => {
2018-12-17 15:54:58 +01:00
const did: DID = DID.generate()
2018-11-01 12:47:48 +01:00
const ddo: DDO = new DDO({
2019-06-20 00:20:09 +02:00
id: did.getId()
2018-11-01 12:47:48 +01:00
})
spy.on(aquarius.fetch, 'post', () => reponsify(ddo))
2018-11-01 12:47:48 +01:00
const result: DDO = await aquarius.storeDDO(ddo)
assert(result)
assert(result.id === ddo.id)
})
})
2019-06-20 00:20:09 +02:00
describe('#retrieveDDO()', () => {
it('should store a ddo', async () => {
2018-12-17 15:54:58 +01:00
const did: DID = DID.generate()
2018-11-01 12:47:48 +01:00
const ddo: DDO = new DDO({
2019-06-20 00:20:09 +02:00
id: did.getId()
2018-11-01 12:47:48 +01:00
})
spy.on(aquarius.fetch, 'post', () => reponsify(ddo))
spy.on(aquarius.fetch, 'get', () => reponsify(ddo))
2018-11-01 12:47:48 +01:00
const storageResult: DDO = await aquarius.storeDDO(ddo)
assert(storageResult)
2018-12-17 15:54:58 +01:00
assert(storageResult.id === did.getId())
2018-11-01 12:47:48 +01:00
const restrieveResult: DDO = await aquarius.retrieveDDO(did)
assert(restrieveResult)
2018-12-17 15:54:58 +01:00
assert(restrieveResult.id === did.getId())
2018-11-01 12:47:48 +01:00
assert(restrieveResult.id === storageResult.id)
})
})
2018-10-26 10:40:46 +02:00
})