From 9472893e5742fb7f892b9b8b2a52eeeddca564d1 Mon Sep 17 00:00:00 2001 From: 1F616EMO Date: Mon, 25 Dec 2023 10:08:23 +0800 Subject: [PATCH] Remove attitudes.DOWNVOTE As suggested by TAs --- bchosttrust/attitudes.py | 2 -- tests/analysis_search.py | 27 +++++++++++++++++---------- tests/analysis_tree.py | 4 ++-- tests/consensus_limitations.py | 4 ++-- tests/import_block.py | 10 +++++----- tests/storage_dummy.py | 11 +++++------ tests/storage_leveldb.py | 11 +++++------ 7 files changed, 36 insertions(+), 33 deletions(-) diff --git a/bchosttrust/attitudes.py b/bchosttrust/attitudes.py index 992c436..91c4cca 100644 --- a/bchosttrust/attitudes.py +++ b/bchosttrust/attitudes.py @@ -4,8 +4,6 @@ from collections import defaultdict UPVOTE = 0 -DOWNVOTE = 1 WEIGHTS = defaultdict(lambda: 0) WEIGHTS[UPVOTE] = 1 -WEIGHTS[DOWNVOTE] = -1 diff --git a/tests/analysis_search.py b/tests/analysis_search.py index 7f5f7ba..2de0937 100644 --- a/tests/analysis_search.py +++ b/tests/analysis_search.py @@ -18,15 +18,13 @@ def setUp(self): # Not caring about satisfying PoW here self.block1 = BCHTBlock(1, b"\x00" * 32, 0, 4, ( BCHTEntry("www.example.com", attitudes.UPVOTE), - BCHTEntry("www.example.net", attitudes.DOWNVOTE) + BCHTEntry("www.example.net", attitudes.UPVOTE) )) self.block2 = BCHTBlock(1, self.block1.hash, 0, 4, ( BCHTEntry("www.example.com", attitudes.UPVOTE), - BCHTEntry("www.example.net", attitudes.UPVOTE) )) self.block3 = BCHTBlock(1, self.block2.hash, 0, 4, ( - BCHTEntry("www.example.com", attitudes.UPVOTE), - BCHTEntry("www.example.net", attitudes.DOWNVOTE) + BCHTEntry("www.example.org", attitudes.UPVOTE), )) self.db.put(self.block1) @@ -49,22 +47,31 @@ def testGetVotes(self): self.assertDictEqual(votes, { "www.example.com": { - attitudes.UPVOTE: 3 + attitudes.UPVOTE: 2 }, "www.example.net": { - attitudes.UPVOTE: 1, - attitudes.DOWNVOTE: 2 - } + attitudes.UPVOTE: 1 + }, + "www.example.org": { + attitudes.UPVOTE: 1 + }, }) def testRating(self): ratings = search.get_website_rating(self.db, self.block3.hash) self.assertDictEqual(ratings, { - "www.example.com": 3, - "www.example.net": -1 # (-1) + 1 + (-1) + "www.example.com": 2, + "www.example.net": 1, + "www.example.org": 1 }) + def testSpecificRating(self): + specific_rating = search.get_specific_website_rating( + self.db, self.block3.hash, "www.example.com") + + self.assertEqual(specific_rating, 2) + if __name__ == '__main__': unittest.main() diff --git a/tests/analysis_tree.py b/tests/analysis_tree.py index 6dcf431..46e26f8 100644 --- a/tests/analysis_tree.py +++ b/tests/analysis_tree.py @@ -18,7 +18,7 @@ def setUp(self): # Not caring about satisfying PoW here self.block1 = BCHTBlock(1, b"\x00" * 32, 0, 4, ( BCHTEntry("www.example.com", attitudes.UPVOTE), - BCHTEntry("www.example.net", attitudes.DOWNVOTE) + BCHTEntry("www.example.net", attitudes.UPVOTE) )) self.block2 = BCHTBlock(1, self.block1.hash, 0, 4, ( BCHTEntry("www.example.com", attitudes.UPVOTE), @@ -26,7 +26,7 @@ def setUp(self): )) self.block3 = BCHTBlock(1, self.block2.hash, 0, 4, ( BCHTEntry("www.example.com", attitudes.UPVOTE), - BCHTEntry("www.example.net", attitudes.DOWNVOTE) + BCHTEntry("www.example.net", attitudes.UPVOTE) )) self.db.put(self.block1) diff --git a/tests/consensus_limitations.py b/tests/consensus_limitations.py index 0d9b958..3074500 100644 --- a/tests/consensus_limitations.py +++ b/tests/consensus_limitations.py @@ -14,7 +14,7 @@ class BCHTConsensusTestCase(unittest.TestCase): def test_success(self): block1 = BCHTBlock(1, b"\x00" * 32, 0, 4, ( BCHTEntry("www.example.com", attitudes.UPVOTE), - BCHTEntry("www.example.net", attitudes.DOWNVOTE) + BCHTEntry("www.example.net", attitudes.UPVOTE) )) self.assertTrue(limitations.validate_block_limitations(block1)) @@ -34,7 +34,7 @@ def test_failed_empty(self): def test_failed_duplicate(self): block1 = BCHTBlock(1, b"\x00" * 32, 0, 4, ( BCHTEntry("www.example.com", attitudes.UPVOTE), - BCHTEntry("www.example.com", attitudes.DOWNVOTE) + BCHTEntry("www.example.com", attitudes.UPVOTE) )) self.assertFalse(limitations.validate_block_limitations(block1)) diff --git a/tests/import_block.py b/tests/import_block.py index 93d0cbd..138834d 100644 --- a/tests/import_block.py +++ b/tests/import_block.py @@ -21,15 +21,15 @@ def setUp(self): self.blocks = [] self.blocks.append(BCHTBlock(1, b"\x00" * 32, 0, 4, ( BCHTEntry("www.example.com", attitudes.UPVOTE), - BCHTEntry("www.example.net", attitudes.DOWNVOTE) + BCHTEntry("www.example.net", attitudes.UPVOTE) ))) self.blocks.append(BCHTBlock(1, self.blocks[0].hash, 1, 4, ( BCHTEntry("www.example.com", attitudes.UPVOTE), - BCHTEntry("www.example.net", attitudes.DOWNVOTE) + BCHTEntry("www.example.net", attitudes.UPVOTE) ))) self.blocks.append(BCHTBlock(1, self.blocks[0].hash, 2, 4, ( BCHTEntry("www.example.com", attitudes.UPVOTE), - BCHTEntry("www.example.net", attitudes.DOWNVOTE) + BCHTEntry("www.example.net", attitudes.UPVOTE) ))) for block in self.blocks: @@ -70,7 +70,7 @@ def test_import_block_build_on(self): # We build a block on top of blocks[1] new_block, _ = attempt(1, self.blocks[1].hash, 5, ( BCHTEntry("www.example.com", attitudes.UPVOTE), - BCHTEntry("www.example.net", attitudes.DOWNVOTE) + BCHTEntry("www.example.net", attitudes.UPVOTE) )) # By definition of attempt, we are now getting a block @@ -97,7 +97,7 @@ def test_import_block_fork(self): new_block, _ = attempt(1, self.blocks[0].hash, 5, ( BCHTEntry("www.example.com", attitudes.UPVOTE), - BCHTEntry("www.example.net", attitudes.DOWNVOTE) + BCHTEntry("www.example.net", attitudes.UPVOTE) )) # By definition of attempt, we are now getting a block diff --git a/tests/storage_dummy.py b/tests/storage_dummy.py index 4109d20..ce0ad1d 100644 --- a/tests/storage_dummy.py +++ b/tests/storage_dummy.py @@ -68,7 +68,7 @@ def testIteration(self): block1 = BCHTBlock(1, b"\x00" * 32, 0, 4, ( BCHTEntry("www.example.com", attitudes.UPVOTE), - BCHTEntry("www.example.net", attitudes.DOWNVOTE) + BCHTEntry("www.example.net", attitudes.UPVOTE) )) block2 = BCHTBlock(1, block1.hash, 0, 4, ( BCHTEntry("www.example.com", attitudes.UPVOTE), @@ -76,7 +76,7 @@ def testIteration(self): )) block3 = BCHTBlock(1, block2.hash, 0, 4, ( BCHTEntry("www.example.com", attitudes.UPVOTE), - BCHTEntry("www.example.net", attitudes.DOWNVOTE) + BCHTEntry("www.example.net", attitudes.UPVOTE) )) backend.put(block1) @@ -93,7 +93,7 @@ def testIterationDict(self): block1 = BCHTBlock(1, b"\x00" * 32, 0, 4, ( BCHTEntry("www.example.com", attitudes.UPVOTE), - BCHTEntry("www.example.net", attitudes.DOWNVOTE) + BCHTEntry("www.example.net", attitudes.UPVOTE) )) block2 = BCHTBlock(1, block1.hash, 0, 4, ( BCHTEntry("www.example.com", attitudes.UPVOTE), @@ -101,15 +101,14 @@ def testIterationDict(self): )) block3 = BCHTBlock(1, block2.hash, 0, 4, ( BCHTEntry("www.example.com", attitudes.UPVOTE), - BCHTEntry("www.example.net", attitudes.DOWNVOTE) + BCHTEntry("www.example.net", attitudes.UPVOTE) )) backend.put(block1) backend.put(block2) backend.put(block3) - dict_blocks = {key: value for key, - value in backend.iter_blocks_with_key()} + dict_blocks = dict(backend.iter_blocks_with_key()) for block in (block1, block2, block3): self.assertTrue(block.hash in dict_blocks) diff --git a/tests/storage_leveldb.py b/tests/storage_leveldb.py index 45be5d6..ee1cddc 100644 --- a/tests/storage_leveldb.py +++ b/tests/storage_leveldb.py @@ -86,7 +86,7 @@ def testIteration(self): block1 = BCHTBlock(1, b"\x00" * 32, 0, 4, ( BCHTEntry("www.example.com", attitudes.UPVOTE), - BCHTEntry("www.example.net", attitudes.DOWNVOTE) + BCHTEntry("www.example.net", attitudes.UPVOTE) )) block2 = BCHTBlock(1, block1.hash, 0, 4, ( BCHTEntry("www.example.com", attitudes.UPVOTE), @@ -94,7 +94,7 @@ def testIteration(self): )) block3 = BCHTBlock(1, block2.hash, 0, 4, ( BCHTEntry("www.example.com", attitudes.UPVOTE), - BCHTEntry("www.example.net", attitudes.DOWNVOTE) + BCHTEntry("www.example.net", attitudes.UPVOTE) )) backend.put(block1) @@ -113,7 +113,7 @@ def testIterationDict(self): block1 = BCHTBlock(1, b"\x00" * 32, 0, 4, ( BCHTEntry("www.example.com", attitudes.UPVOTE), - BCHTEntry("www.example.net", attitudes.DOWNVOTE) + BCHTEntry("www.example.net", attitudes.UPVOTE) )) block2 = BCHTBlock(1, block1.hash, 0, 4, ( BCHTEntry("www.example.com", attitudes.UPVOTE), @@ -121,15 +121,14 @@ def testIterationDict(self): )) block3 = BCHTBlock(1, block2.hash, 0, 4, ( BCHTEntry("www.example.com", attitudes.UPVOTE), - BCHTEntry("www.example.net", attitudes.DOWNVOTE) + BCHTEntry("www.example.net", attitudes.UPVOTE) )) backend.put(block1) backend.put(block2) backend.put(block3) - dict_blocks = {key: value for key, - value in backend.iter_blocks_with_key()} + dict_blocks = dict(backend.iter_blocks_with_key()) for block in (block1, block2, block3): self.assertTrue(block.hash in dict_blocks)