1
0
mirror of https://github.com/bigchaindb/js-bigchaindb-driver.git synced 2024-11-23 02:00:20 +01:00

Add integration test for asset endpoint :diamond:

This commit is contained in:
tim 2017-06-20 16:17:43 +02:00
parent 3fdaf40602
commit db10937f9e
5 changed files with 60 additions and 36 deletions

View File

@ -15,7 +15,7 @@ export default class Connection {
'statuses': 'statuses', 'statuses': 'statuses',
'transactions': 'transactions', 'transactions': 'transactions',
'transactionsDetail': 'transactions/%(transactionId)s', 'transactionsDetail': 'transactions/%(transactionId)s',
'searchAssets': 'assets', 'assets': 'assets',
'votes': 'votes' 'votes': 'votes'
}[endpoints] }[endpoints]
} }
@ -161,12 +161,12 @@ export default class Connection {
/** /**
* @public * @public
* *
* @param query * @param search
*/ */
searchAssets(query) { searchAssets(search) {
return this._req(this.getApiUrls('searchAssets'), { return this._req(this.getApiUrls('assets'), {
query: { query: {
text_search: query search
} }
}) })
} }

View File

@ -16,7 +16,7 @@ test('generate API URLS', t => {
'statuses': 'statuses', 'statuses': 'statuses',
'transactions': 'transactions', 'transactions': 'transactions',
'transactionsDetail': 'transactions/%(transactionId)s', 'transactionsDetail': 'transactions/%(transactionId)s',
'searchAssets': 'assets', 'assets': 'assets',
} }
Object.keys(endpoints).forEach((endpointName) => { Object.keys(endpoints).forEach((endpointName) => {
const url = conn.getApiUrls(endpointName) const url = conn.getApiUrls(endpointName)
@ -125,16 +125,16 @@ test('Get votes for a block id', t => {
}) })
test('Get asset for a text', t => { test('Get asset for text', t => {
const expectedPath = 'path' const expectedPath = 'path'
const query = 'abc' const search = 'abc'
conn._req = sinon.spy() conn._req = sinon.spy()
conn.getApiUrls = sinon.stub().returns(expectedPath) conn.getApiUrls = sinon.stub().returns(expectedPath)
conn.searchAssets(query) conn.searchAssets(search)
t.truthy(conn._req.calledWith( t.truthy(conn._req.calledWith(
expectedPath, expectedPath,
{ query: { text_search: query } } { query: { search } }
)) ))
}) })

View File

@ -2,23 +2,23 @@ import test from 'ava'
import { Transaction, Ed25519Keypair } from '../src' import { Transaction, Ed25519Keypair } from '../src'
// TODO: Find out if ava has something like conftest, if so put this there. // TODO: Find out if ava has something like conftest, if so put this there.
// TODO: We're adding `Math.random()` here to never create the same transaction // NOTE: We cast `Math.random()` to a string, as sometimes Javascript simply
// twice. // yields a slightly different float during runtime, lol
export const assetMessage = { assetMessage: Math.random() } export function asset() { return { message: `${Math.random()}` } }
export const metaDataMessage = { metaDataMessage: 'metaDataMessage' } export const metaData = { message: 'metaDataMessage' }
export const alice = new Ed25519Keypair() export const alice = new Ed25519Keypair()
export const aliceCondition = Transaction.makeEd25519Condition(alice.publicKey) export const aliceCondition = Transaction.makeEd25519Condition(alice.publicKey)
export const aliceOutput = Transaction.makeOutput(aliceCondition) export const aliceOutput = Transaction.makeOutput(aliceCondition)
export const createTx = Transaction.makeCreateTransaction( export const createTx = Transaction.makeCreateTransaction(
assetMessage, asset,
metaDataMessage, metaData,
[aliceOutput], [aliceOutput],
alice.publicKey alice.publicKey
) )
export const transferTx = Transaction.makeTransferTransaction( export const transferTx = Transaction.makeTransferTransaction(
createTx, createTx,
metaDataMessage, metaData,
[aliceOutput], [aliceOutput],
0 0
) )

View File

@ -7,8 +7,8 @@ import {
aliceOutput, aliceOutput,
bob, bob,
bobOutput, bobOutput,
assetMessage, asset,
metaDataMessage metaData
} from '../constants' } from '../constants'
const API_PATH = 'http://localhost:9984/api/v1/' const API_PATH = 'http://localhost:9984/api/v1/'
@ -29,15 +29,15 @@ test('Valid CREATE transaction', t => {
const conn = new Connection(API_PATH) const conn = new Connection(API_PATH)
const tx = Transaction.makeCreateTransaction( const tx = Transaction.makeCreateTransaction(
assetMessage, asset(),
metaDataMessage, metaData,
[aliceOutput], [aliceOutput],
alice.publicKey alice.publicKey
) )
const txSigned = Transaction.signTransaction(tx, alice.privateKey) const txSigned = Transaction.signTransaction(tx, alice.privateKey)
return conn.postTransaction(txSigned) return conn.postTransaction(txSigned)
.then(({ 'id': txId }) => conn.pollStatusAndFetchTransaction(txId)) .then(({ id }) => conn.pollStatusAndFetchTransaction(id))
.then(resTx => t.truthy(resTx)) .then(resTx => t.truthy(resTx))
}) })
@ -45,8 +45,8 @@ test('Valid CREATE transaction', t => {
test('Valid TRANSFER transaction with single Ed25519 input', t => { test('Valid TRANSFER transaction with single Ed25519 input', t => {
const conn = new Connection(API_PATH) const conn = new Connection(API_PATH)
const createTx = Transaction.makeCreateTransaction( const createTx = Transaction.makeCreateTransaction(
assetMessage, asset(),
metaDataMessage, metaData,
[aliceOutput], [aliceOutput],
alice.publicKey alice.publicKey
) )
@ -56,11 +56,11 @@ test('Valid TRANSFER transaction with single Ed25519 input', t => {
) )
return conn.postTransaction(createTxSigned) return conn.postTransaction(createTxSigned)
.then(({ 'id': txId }) => conn.pollStatusAndFetchTransaction(txId)) .then(({ id }) => conn.pollStatusAndFetchTransaction(id))
.then(() => { .then(() => {
const transferTx = Transaction.makeTransferTransaction( const transferTx = Transaction.makeTransferTransaction(
createTxSigned, createTxSigned,
metaDataMessage, metaData,
[aliceOutput], [aliceOutput],
0 0
) )
@ -78,8 +78,8 @@ test('Valid TRANSFER transaction with single Ed25519 input', t => {
test('Valid TRANSFER transaction with multiple Ed25519 inputs', t => { test('Valid TRANSFER transaction with multiple Ed25519 inputs', t => {
const conn = new Connection(API_PATH) const conn = new Connection(API_PATH)
const createTx = Transaction.makeCreateTransaction( const createTx = Transaction.makeCreateTransaction(
assetMessage, asset(),
metaDataMessage, metaData,
[aliceOutput, bobOutput], [aliceOutput, bobOutput],
alice.publicKey alice.publicKey
) )
@ -93,7 +93,7 @@ test('Valid TRANSFER transaction with multiple Ed25519 inputs', t => {
.then(() => { .then(() => {
const transferTx = Transaction.makeTransferTransaction( const transferTx = Transaction.makeTransferTransaction(
createTxSigned, createTxSigned,
metaDataMessage, metaData,
[Transaction.makeOutput(aliceCondition, '2')], [Transaction.makeOutput(aliceCondition, '2')],
0, 0,
1 1
@ -108,3 +108,27 @@ test('Valid TRANSFER transaction with multiple Ed25519 inputs', t => {
.then(resTx => t.truthy(resTx)) .then(resTx => t.truthy(resTx))
}) })
}) })
test('Search for an asset', t => {
const conn = new Connection(API_PATH)
const createTx = Transaction.makeCreateTransaction(
asset(),
metaData,
[aliceOutput],
alice.publicKey
)
const createTxSigned = Transaction.signTransaction(
createTx,
alice.privateKey
)
return conn.postTransaction(createTxSigned)
.then(({ id }) => conn.pollStatusAndFetchTransaction(id))
.then(() => conn.searchAssets(createTxSigned.asset.data.message))
.then(assets => t.truthy(
assets.pop(),
createTxSigned.asset.data.message
))
})

View File

@ -1,14 +1,14 @@
import test from 'ava' import test from 'ava'
import sinon from 'sinon' import sinon from 'sinon'
import { Transaction, Ed25519Keypair } from '../../src' import { Transaction } from '../../src'
import * as makeTransaction from '../../src/transaction/makeTransaction' // eslint-disable-line import * as makeTransaction from '../../src/transaction/makeTransaction' // eslint-disable-line
import makeInputTemplate from '../../src/transaction/makeInputTemplate' import makeInputTemplate from '../../src/transaction/makeInputTemplate'
import { import {
alice, alice,
aliceOutput, aliceOutput,
metaDataMessage, metaData,
createTx, createTx,
transferTx transferTx
} from '../constants' } from '../constants'
@ -72,14 +72,14 @@ test('Create TRANSFER transaction based on CREATE transaction', t => {
Transaction.makeTransferTransaction( Transaction.makeTransferTransaction(
createTx, createTx,
metaDataMessage, metaData,
[aliceOutput], [aliceOutput],
0 0
) )
const expected = [ const expected = [
'TRANSFER', 'TRANSFER',
{ id: createTx.id }, { id: createTx.id },
metaDataMessage, metaData,
[aliceOutput], [aliceOutput],
[makeInputTemplate( [makeInputTemplate(
[alice.publicKey], [alice.publicKey],
@ -100,14 +100,14 @@ test('Create TRANSFER transaction based on TRANSFER transaction', t => {
Transaction.makeTransferTransaction( Transaction.makeTransferTransaction(
transferTx, transferTx,
metaDataMessage, metaData,
[aliceOutput], [aliceOutput],
0 0
) )
const expected = [ const expected = [
'TRANSFER', 'TRANSFER',
{ id: transferTx.asset.id }, { id: transferTx.asset.id },
metaDataMessage, metaData,
[aliceOutput], [aliceOutput],
[makeInputTemplate( [makeInputTemplate(
[alice.publicKey], [alice.publicKey],