This repository has been archived by the owner on Jun 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
94 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import abc | ||
|
||
|
||
class DbInterface(abc.ABC): | ||
|
||
@abc.abstractmethod | ||
def get_block(self, block_number): | ||
return NotImplemented | ||
|
||
@abc.abstractmethod | ||
def save_block(self, block, block_number): | ||
return NotImplemented |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class BlockAlreadyExistsException(Exception): | ||
"""block already exists""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from .db_interface import DbInterface | ||
from .exceptions import BlockAlreadyExistsException | ||
|
||
|
||
class MemoryDb(DbInterface): | ||
|
||
def __init__(self): | ||
self.blocks = {} | ||
|
||
def get_block(self, block_number): | ||
return self.blocks.get(block_number) | ||
|
||
def save_block(self, block, block_number): | ||
if block_number in self.blocks: | ||
raise BlockAlreadyExistsException('should not save block with same id again.') | ||
self.blocks[block_number] = block |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import pytest | ||
|
||
from plasma_cash.utils.db.exceptions import BlockAlreadyExistsException | ||
from plasma_cash.utils.db.memory_db import MemoryDb | ||
|
||
|
||
class TestMemoryDb(object): | ||
@pytest.fixture(scope='function') | ||
def db(self): | ||
return MemoryDb() | ||
|
||
def test_block_normal_case(self, db): | ||
DUMMY_BLOCK = 'dummy block' | ||
DUMMY_BLK_NUM = 1 | ||
db.save_block(DUMMY_BLOCK, DUMMY_BLK_NUM) | ||
assert db.get_block(DUMMY_BLK_NUM) == DUMMY_BLOCK | ||
|
||
def test_get_block_none(self, db): | ||
NON_EXIST_BLK_NUM = -1 | ||
assert db.get_block(NON_EXIST_BLK_NUM) is None | ||
|
||
def test_save_block_already_exists(self, db): | ||
DUMMY_BLK_NUM = 1 | ||
db.save_block('first block', DUMMY_BLK_NUM) | ||
with pytest.raises(BlockAlreadyExistsException): | ||
db.save_block('second block should fail', DUMMY_BLK_NUM) |