electrum-bitcoinprivate/lib/tests/test_wallet.py

70 lines
1.7 KiB
Python
Raw Normal View History

import shutil
import tempfile
import sys
import unittest
import os
2014-08-25 06:11:52 -07:00
import json
2017-10-24 05:04:16 -07:00
from io import StringIO
2017-01-22 10:25:24 -08:00
from lib.storage import WalletStorage, FINAL_SEED_VERSION
class FakeSynchronizer(object):
def __init__(self):
self.store = []
def add(self, address):
self.store.append(address)
class WalletTestCase(unittest.TestCase):
def setUp(self):
super(WalletTestCase, self).setUp()
self.user_dir = tempfile.mkdtemp()
self.wallet_path = os.path.join(self.user_dir, "somewallet")
self._saved_stdout = sys.stdout
self._stdout_buffer = StringIO()
sys.stdout = self._stdout_buffer
def tearDown(self):
super(WalletTestCase, self).tearDown()
shutil.rmtree(self.user_dir)
# Restore the "real" stdout
sys.stdout = self._saved_stdout
class TestWalletStorage(WalletTestCase):
2017-10-12 20:52:58 -07:00
def test_read_dictionary_from_file(self):
some_dict = {"a":"b", "c":"d"}
2017-03-05 23:44:38 -08:00
contents = json.dumps(some_dict)
with open(self.wallet_path, "w") as f:
contents = f.write(contents)
2017-10-12 20:52:58 -07:00
storage = WalletStorage(self.wallet_path, manual_upgrades=True)
self.assertEqual("b", storage.get("a"))
self.assertEqual("d", storage.get("c"))
2017-10-12 20:52:58 -07:00
def test_write_dictionary_to_file(self):
storage = WalletStorage(self.wallet_path)
2016-10-16 04:23:43 -07:00
some_dict = {
2016-10-16 13:17:52 -07:00
u"a": u"b",
u"c": u"d",
u"seed_version": FINAL_SEED_VERSION}
2015-09-11 04:17:20 -07:00
for key, value in some_dict.items():
storage.put(key, value)
storage.write()
contents = ""
with open(self.wallet_path, "r") as f:
contents = f.read()
2014-08-25 06:11:52 -07:00
self.assertEqual(some_dict, json.loads(contents))