From a68f1febe2dced2aa9188237e273d140d23f1beb Mon Sep 17 00:00:00 2001 From: Holger Nahrstaedt <holger@nahrstaedt.de> Date: Tue, 6 Mar 2018 14:27:53 +0100 Subject: [PATCH] more coverage-reports add unit tests for remove_from_dict --- .circleci/config.yml | 8 +++++++- beem/comment.py | 6 +++--- beem/utils.py | 20 ++++++-------------- tests/test_utils.py | 13 ++++++++++++- 4 files changed, 28 insertions(+), 19 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 6339f0a1..d8b434bd 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -26,7 +26,11 @@ jobs: - v1-dependencies-{{ checksum "requirements.txt" }} # fallback to using the latest cache if no exact match is found - v1-dependencies- - + - run: + name: Setup Code Climate test-reporter + command: | + curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter + chmod +x ./cc-test-reporter - run: name: install dependencies command: | @@ -42,7 +46,9 @@ jobs: - run: name: run tests command: | + ./cc-test-reporter before-build tox -e py36,pylint + ./cc-test-reporter after-build --exit-code $? - deploy: name: Push coverage diff --git a/beem/comment.py b/beem/comment.py index 66ec0a3c..ee800a28 100644 --- a/beem/comment.py +++ b/beem/comment.py @@ -7,7 +7,7 @@ from builtins import str from .instance import shared_steem_instance from .account import Account from .amount import Amount -from .utils import resolve_authorperm, construct_authorperm, derive_permlink, keep_in_dict, make_patch, formatTimeString +from .utils import resolve_authorperm, construct_authorperm, derive_permlink, remove_from_dict, make_patch, formatTimeString from .blockchainobject import BlockchainObject from .exceptions import ContentDoesNotExistsException, VotingInvalidOnArchivedPost from beembase import operations @@ -510,10 +510,10 @@ class Comment(BlockchainObject): # if comment_options are used, add a new op to the transaction if comment_options or beneficiaries: - options = keep_in_dict(comment_options or {}, [ + options = remove_from_dict(comment_options or {}, [ 'max_accepted_payout', 'percent_steem_dollars', 'allow_votes', 'allow_curation_rewards', 'extensions' - ]) + ], keep_keys=True) # override beneficiaries extension if beneficiaries: # validate schema diff --git a/beem/utils.py b/beem/utils.py index cd922920..55fe34e5 100644 --- a/beem/utils.py +++ b/beem/utils.py @@ -217,26 +217,18 @@ def test_proposal_in_buffer(buf, operation_name, id): ) -def keep_in_dict(obj, allowed_keys=list()): - """ Prune a class or dictionary of all but allowed keys. +def remove_from_dict(obj, keys=list(), keep_keys=True): + """ Prune a class or dictionary of all but keys (keep_keys=True). + Prune a class or dictionary of specified keys.(keep_keys=False). """ if type(obj) == dict: items = list(obj.items()) else: items = list(obj.__dict__.items()) - - return {k: v for k, v in items if k in allowed_keys} - - -def remove_from_dict(obj, remove_keys=list()): - """ Prune a class or dictionary of specified keys. - """ - if type(obj) == dict: - items = list(obj.items()) + if keep_keys: + return {k: v for k, v in items if k in keys} else: - items = list(obj.__dict__.items()) - - return {k: v for k, v in items if k not in remove_keys} + return {k: v for k, v in items if k not in keys} def make_patch(a, b, n=3): diff --git a/tests/test_utils.py b/tests/test_utils.py index 52abfb6f..5d92bf60 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -14,7 +14,8 @@ from beem.utils import ( sanitize_permlink, derive_permlink, resolve_root_identifier, - make_patch + make_patch, + remove_from_dict ) @@ -64,3 +65,13 @@ class Testcases(unittest.TestCase): def test_formatTimedelta(self): now = datetime.now() self.assertEqual(formatTimedelta(now-now), '0:00.00') + + def test_remove_from_dict(self): + a = {'a':1, 'b':2} + b = {'b':2} + self.assertEqual(remove_from_dict(a, ['b'], keep_keys=True), {'b':2}) + self.assertEqual(remove_from_dict(a, ['a'], keep_keys=False), {'b':2}) + self.assertEqual(remove_from_dict(b, ['b'], keep_keys=True), {'b':2}) + self.assertEqual(remove_from_dict(b, ['a'], keep_keys=False), {'b':2}) + self.assertEqual(remove_from_dict(b, [], keep_keys=True), {}) + self.assertEqual(remove_from_dict(a, ['a','b'], keep_keys=False), {}) -- GitLab