Skip to content

Commit

Permalink
migrate some unittests from pybitshares to increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
xeroc committed Aug 8, 2018
1 parent 1c7cb84 commit cfbc228
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 13 deletions.
3 changes: 2 additions & 1 deletion graphenebase/objecttypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
object_type["null"] = 0
object_type["base"] = 1
object_type["account"] = 2
object_type["OBJECT_TYPE_COUNT"] = 3
object_type["asset"] = 3
object_type["OBJECT_TYPE_COUNT"] = 4
4 changes: 4 additions & 0 deletions graphenebase/operationids.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
ops = [
"demooepration",
"newdemooepration",
"nonexisting",
"nonexisting2",
"nonexisting3",
"account_create",
]
operations = {o: ops.index(o) for o in ops}

Expand Down
73 changes: 71 additions & 2 deletions graphenebase/operations.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from collections import OrderedDict
import json
from .types import (
Uint8, Int16, Uint16, Uint32, Uint64,
Varint32, Int64, String, Bytes, Void,
Expand All @@ -10,7 +9,6 @@
from .objects import GrapheneObject, isArgsThisClass
from .account import PublicKey
from .chains import default_prefix
from .objects import Operation
from .operationids import operations


Expand All @@ -36,3 +34,74 @@ def detail(self, *args, **kwargs):
('optional', Optional(String(kwargs.get("optional")))),
('extensions', Set([])),
])


class Asset(GrapheneObject):
def detail(self, *args, **kwargs):
return OrderedDict([
('amount', Int64(kwargs["amount"])),
('asset_id', ObjectId(kwargs["asset_id"], "asset"))
])


class Permission(GrapheneObject):
def detail(self, *args, **kwargs):
prefix = kwargs.pop("prefix", default_prefix)
# Sort keys (FIXME: ideally, the sorting is part of Public
# Key and not located here)
kwargs["key_auths"] = sorted(
kwargs["key_auths"],
key=lambda x: repr(PublicKey(x[0], prefix=prefix).address),
reverse=False,
)
accountAuths = Map([
[ObjectId(e[0], "account"), Uint16(e[1])]
for e in kwargs["account_auths"]
])
keyAuths = Map([
[PublicKey(e[0], prefix=prefix), Uint16(e[1])]
for e in kwargs["key_auths"]
])
return OrderedDict([
('weight_threshold', Uint32(int(kwargs["weight_threshold"]))),
('account_auths', accountAuths),
('key_auths', keyAuths),
('extensions', Set([])),
])


class AccountOptions(GrapheneObject):
def detail(self, *args, **kwargs):
prefix = kwargs.pop("prefix", default_prefix)
# remove dublicates
kwargs["votes"] = list(set(kwargs["votes"]))
# Sort votes
kwargs["votes"] = sorted(
kwargs["votes"],
key=lambda x: float(x.split(":")[1]),
)
return OrderedDict([
('memo_key', PublicKey(kwargs["memo_key"], prefix=prefix)),
('voting_account', ObjectId(kwargs["voting_account"], "account")),
('num_witness', Uint16(kwargs["num_witness"])),
('num_committee', Uint16(kwargs["num_committee"])),
('votes', Array([VoteId(o) for o in kwargs["votes"]])),
('extensions', Set([])),
])


# For more detailed unit testing
class Account_create(GrapheneObject):
def detail(self, *args, **kwargs):
prefix = kwargs.get("prefix", default_prefix)
return OrderedDict([
('fee', Asset(kwargs["fee"])),
('registrar', ObjectId(kwargs["registrar"], "account")),
('referrer', ObjectId(kwargs["referrer"], "account")),
('referrer_percent', Uint16(kwargs["referrer_percent"])),
('name', String(kwargs["name"])),
('owner', Permission(kwargs["owner"], prefix=prefix)),
('active', Permission(kwargs["active"], prefix=prefix)),
('options', AccountOptions(kwargs["options"], prefix=prefix)),
('extensions', Set([])),
])
3 changes: 1 addition & 2 deletions graphenebase/signedtransactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
Uint16,
Uint32,
)
from .objects import GrapheneObject, isArgsThisClass
from .operations import Operation
from .objects import GrapheneObject, isArgsThisClass, Operation
from .chains import known_chains
log = logging.getLogger(__name__)

Expand Down
10 changes: 5 additions & 5 deletions graphenebase/transactions.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import json
import struct
import time

from collections import OrderedDict
from binascii import hexlify, unhexlify
from calendar import timegm
from datetime import datetime
import json
import struct
import time

from .account import PublicKey
from .chains import known_chains
from .signedtransactions import Signed_Transaction
from .operations import Operation
from .objects import GrapheneObject, isArgsThisClass
from .objects import GrapheneObject, isArgsThisClass, Operation

timeformat = '%Y-%m-%dT%H:%M:%S%Z'

Expand Down
2 changes: 2 additions & 0 deletions graphenebase/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,8 @@ def __init__(self, object_str, type_verify=None):
self.instance = Id(int(id))
self.Id = object_str
if type_verify:
assert type_verify in self.object_types,\
"Type {} is not defined!".format(type_verify)
assert self.object_types[type_verify] == int(type),\
"Object id does not match object type! " +\
"Excpected %d, got %d" %\
Expand Down
4 changes: 2 additions & 2 deletions graphenestorage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
]


def get_default_config_store():
return SqliteConfigurationStore()
def get_default_config_store(*args, **kwargs):
return SqliteConfigurationStore(*args, **kwargs)
11 changes: 10 additions & 1 deletion graphenestorage/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,16 @@ def __getitem__(self, key):
def __iter__(self):
""" Iterates through the store
"""
return iter(self.items())
return iter(self.keys())

def keys(self):
query = ("SELECT {} from {}".format(
self.__key__,
self.__tablename__))
connection = sqlite3.connect(self.sqlDataBaseFile)
cursor = connection.cursor()
cursor.execute(query)
return [x[0] for x in cursor.fetchall()]

def __len__(self):
""" return lenght of store
Expand Down

0 comments on commit cfbc228

Please sign in to comment.