test create tx does not have an asset id

This commit is contained in:
Scott Sadler 2017-03-07 12:58:32 +01:00
parent 294afa123f
commit cb6bd34744
6 changed files with 65 additions and 5 deletions

View File

@ -29,6 +29,8 @@ def _load_schema(name):
TX_SCHEMA_PATH, TX_SCHEMA = _load_schema('transaction') TX_SCHEMA_PATH, TX_SCHEMA = _load_schema('transaction')
_, TX_SCHEMA_CREATE = _load_schema('transaction_create')
_, TX_SCHEMA_TRANSFER = _load_schema('transaction_transfer')
VOTE_SCHEMA_PATH, VOTE_SCHEMA = _load_schema('vote') VOTE_SCHEMA_PATH, VOTE_SCHEMA = _load_schema('vote')
@ -43,6 +45,10 @@ def _validate_schema(schema, body):
def validate_transaction_schema(tx): def validate_transaction_schema(tx):
""" Validate a transaction dict """ """ Validate a transaction dict """
_validate_schema(TX_SCHEMA, tx) _validate_schema(TX_SCHEMA, tx)
if tx['operation'] == 'TRANSFER':
_validate_schema(TX_SCHEMA_TRANSFER, tx)
else:
_validate_schema(TX_SCHEMA_CREATE, tx)
def validate_vote_schema(vote): def validate_vote_schema(vote):

View File

@ -0,0 +1,17 @@
---
"$schema": "http://json-schema.org/draft-04/schema#"
type: object
title: Transaction Schema - CREATE/GENESIS specific properties
required:
- asset
properties:
asset:
additionalProperties: false
properties:
data:
description: |
User provided metadata associated with the asset. May also be ``null``.
anyOf:
- type: object
additionalProperties: true
- type: 'null'

View File

@ -0,0 +1,18 @@
---
"$schema": "http://json-schema.org/draft-04/schema#"
type: object
title: Transaction Schema - TRANSFER specific properties
required:
- asset
properties:
asset:
additionalProperties: false
properties:
id:
"$ref": "#/definitions/sha3_hexdigest"
description: |
ID of the transaction that created the asset.
definitions:
sha3_hexdigest:
pattern: "[0-9a-f]{64}"
type: string

View File

@ -999,7 +999,8 @@ class Transaction(object):
transactions = [transactions] transactions = [transactions]
# create a set of the transactions' asset ids # create a set of the transactions' asset ids
asset_ids = {tx.id if tx.operation == Transaction.CREATE else tx.asset['id'] asset_ids = {tx.id if tx.operation == Transaction.CREATE
else tx.asset['id']
for tx in transactions} for tx in transactions}
# check that all the transasctions have the same asset id # check that all the transasctions have the same asset id

View File

@ -25,9 +25,6 @@ class Transaction(Transaction):
Raises: Raises:
ValidationError: If the transaction is invalid ValidationError: If the transaction is invalid
""" """
if len(self.inputs) == 0:
raise ValidationError('Transaction contains no inputs')
input_conditions = [] input_conditions = []
inputs_defined = all([input_.fulfills for input_ in self.inputs]) inputs_defined = all([input_.fulfills for input_ in self.inputs])
@ -117,8 +114,12 @@ class Transaction(Transaction):
return self return self
@classmethod @classmethod
def from_dict(cls, tx_body): def validate_structure(cls, tx_body):
validate_transaction_schema(tx_body) validate_transaction_schema(tx_body)
@classmethod
def from_dict(cls, tx_body):
cls.validate_structure(tx_body)
return super().from_dict(tx_body) return super().from_dict(tx_body)

17
tests/test_behaviours.py Normal file
View File

@ -0,0 +1,17 @@
import pytest
from bigchaindb.common import exceptions as exc
from bigchaindb.models import Transaction
################################################################################
# 1.1 - The asset ID of a CREATE transaction is the same as it's ID
def test_create_tx_no_asset_id(b):
tx = Transaction.create([b.me], [([b.me], 1)])
# works
Transaction.validate_structure(tx.to_dict())
# broken
tx.asset['id'] = 'b' * 64
with pytest.raises(exc.SchemaValidationError):
Transaction.validate_structure(tx.to_dict())