Skip to content

Commit 854ce61

Browse files
committed
feat(Oracle): Implement Oracle Test's
Fix `oracle-get` command
1 parent c5e89ee commit 854ce61

File tree

2 files changed

+88
-2
lines changed

2 files changed

+88
-2
lines changed

bin/commands/oracle.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,12 @@ async function queryOracle (oracleId, options) {
172172
await handleApiError(async () => {
173173
const oracle = await client.getOracle(oracleId)
174174
const { oracleQueries: queries } = await client.getOracleQueries(oracleId)
175-
printOracle(oracle, options.json)
176-
printQueries(queries, options.json)
175+
if (options.json) {
176+
console.log(JSON.stringify({ ...oracle, queries }))
177+
} else {
178+
printOracle(oracle, options.json)
179+
printQueries(queries, options.json)
180+
}
177181
exit()
178182
})
179183
} catch (e) {

test/cli/oracle.test.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* ISC License (ISC)
3+
* Copyright (c) 2018 aeternity developers
4+
*
5+
* Permission to use, copy, modify, and/or distribute this software for any
6+
* purpose with or without fee is hereby granted, provided that the above
7+
* copyright notice and this permission notice appear in all copies.
8+
*
9+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10+
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11+
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12+
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13+
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14+
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15+
* PERFORMANCE OF THIS SOFTWARE.
16+
*/
17+
18+
import { before, describe, it } from 'mocha'
19+
20+
import { configure, plan, ready, execute as exec, WALLET_NAME } from './index'
21+
import { generateKeyPair } from '@aeternity/aepp-sdk/es/utils/crypto';
22+
23+
plan(10000000000000)
24+
25+
const execute = (arg) => exec(arg, { withNetworkId: true })
26+
27+
describe('CLI Oracle Module', function () {
28+
configure(this)
29+
const oracleFormat = 'string'
30+
const responseFormat = 'string'
31+
let wallet
32+
let oracleId
33+
let queryId
34+
35+
before(async function () {
36+
// Spend tokens for wallet
37+
wallet = await ready(this)
38+
})
39+
40+
it('Oracle create', async () => {
41+
const oracleCreate = JSON.parse(await execute(['oracle', 'create', WALLET_NAME, '--password', 'test', oracleFormat, responseFormat, '--json']))
42+
oracleCreate.blockHeight.should.be.gt(0)
43+
oracleCreate.queryFormat.should.be.equal(oracleFormat)
44+
oracleCreate.responseFormat.should.be.equal(responseFormat)
45+
oracleId = oracleCreate.id
46+
})
47+
it('Oracle extend', async () => {
48+
const oracle = await wallet.getOracleObject(oracleId)
49+
const oracleExtend = JSON.parse(await execute(['oracle', 'extend', WALLET_NAME, '--password', 'test', oracleId, 100, '--json']))
50+
oracleExtend.blockHeight.should.be.gt(0)
51+
oracleExtend.ttl.should.be.gte(oracle.ttl + 100)
52+
})
53+
it('Oracle create query', async () => {
54+
const oracleQuery = JSON.parse(await execute(['oracle', 'create-query', WALLET_NAME, '--password', 'test', oracleId, 'Hello?', '--json']))
55+
oracleQuery.blockHeight.should.be.gt(0)
56+
oracleQuery.decodedQuery.should.be.equal('Hello?')
57+
oracleQuery.decodedQuery.should.be.equal('Hello?')
58+
oracleQuery.id.split('_')[0].should.be.equal('oq')
59+
queryId = oracleQuery.id
60+
const oracle = await wallet.getOracleObject(oracleId)
61+
oracle.queries.length.should.be.equal(1)
62+
})
63+
it('Oracle respond to query', async () => {
64+
const oracleQueryResponse = JSON.parse(await execute(['oracle', 'respond-query', WALLET_NAME, '--password', 'test', oracleId, queryId, 'Hi!', '--json']))
65+
oracleQueryResponse.blockHeight.should.be.gt(0)
66+
const oracle = await wallet.getOracleObject(oracleId)
67+
const query = await oracle.getQuery(queryId)
68+
query.decodedResponse.should.be.equal('Hi!')
69+
})
70+
it('Get non existed Oracle', async () => {
71+
const fakeOracleId = generateKeyPair().publicKey.replace('ak_', 'ok_')
72+
const oracleNotFound = await execute(['oracle', 'get', fakeOracleId, '--json'])
73+
const oracleInvalid = await execute(['oracle', 'get', 'oq_d1sadasdasda', '--json'])
74+
oracleNotFound.should.be.equal('API ERROR: Oracle not found\n')
75+
oracleInvalid.should.be.equal('Invalid oracleId \n')
76+
})
77+
it('Get existed Oracle', async () => {
78+
const oracle = JSON.parse(await execute(['oracle', 'get', oracleId, '--json']))
79+
oracle.id.should.be.a('string')
80+
oracle.id.split('_')[0].should.be.equal('ok')
81+
})
82+
})

0 commit comments

Comments
 (0)