class TestAssetDefinition(unittest.TestCase): def setUp(self): self.colormap = MockColorMap() d = self.colormap.d self.colorset0 = ColorSet(self.colormap, ['']) self.colorset1 = ColorSet(self.colormap, [d[1], d[2]]) self.colorset2 = ColorSet(self.colormap, [d[3]]) self.def0 = {'monikers': ['bitcoin'], 'color_set': self.colorset0.get_data(), 'unit':100000000} self.def1 = {'monikers': ['test1'], 'color_set': self.colorset1.get_data(), 'unit':10} self.def2 = {'monikers': ['test2','test2alt'], 'color_set': self.colorset2.get_data(), 'unit':1} self.asset0 = AssetDefinition(self.colormap, self.def0) self.asset1 = AssetDefinition(self.colormap, self.def1) self.asset2 = AssetDefinition(self.colormap, self.def2) self.assetvalue0 = AdditiveAssetValue(asset=self.asset0, value=5) self.assetvalue1 = AdditiveAssetValue(asset=self.asset0, value=6) self.assetvalue2 = AdditiveAssetValue(asset=self.asset1, value=7) self.assettarget0 = AssetTarget('address0', self.assetvalue0) self.assettarget1 = AssetTarget('address1', self.assetvalue1) self.assettarget2 = AssetTarget('address2', self.assetvalue2) config = {'asset_definitions': [self.def1, self.def2]} self.adm = AssetDefinitionManager(self.colormap, config) def test_repr(self): self.assertEquals(self.asset0.__repr__(), "['bitcoin']: ['']") self.assertEquals( self.asset1.__repr__(), "['test1']: ['obc:color_desc_1:0:0', 'obc:color_desc_2:0:1']") self.assertEquals(self.asset2.__repr__(), "['test2', 'test2alt']: ['obc:color_desc_3:0:1']") def test_get_monikers(self): self.assertEquals(self.asset0.get_monikers(), ['bitcoin']) self.assertEquals(self.asset1.get_monikers(), ['test1']) self.assertEquals(self.asset2.get_monikers(), ['test2', 'test2alt']) def test_get_color_set(self): self.assertTrue(self.asset0.get_color_set().equals(self.colorset0)) self.assertTrue(self.asset1.get_color_set().equals(self.colorset1)) self.assertTrue(self.asset2.get_color_set().equals(self.colorset2)) def test_get_colorvalue(self): g = {'txhash':'blah', 'height':1, 'outindex':0} cid0 = list(self.colorset0.color_id_set)[0] cdef0 = OBColorDefinition(cid0, g) cid1 = list(self.colorset1.color_id_set)[0] cdef1 = OBColorDefinition(cid1, g) cid2 = list(self.colorset2.color_id_set)[0] cdef2 = OBColorDefinition(cid2, g) cv0 = SimpleColorValue(colordef=cdef0, value=1) cv1 = SimpleColorValue(colordef=cdef1, value=2) cv2 = SimpleColorValue(colordef=cdef2, value=3) utxo = MockUTXO([cv0, cv1, cv2]) self.assertEquals(self.asset0.get_colorvalue(utxo), cv0) self.assertEquals(self.asset1.get_colorvalue(utxo), cv1) self.assertEquals(self.asset2.get_colorvalue(utxo), cv2) utxo = MockUTXO([cv0, cv2]) self.assertRaises(Exception, self.asset1.get_colorvalue, utxo) def test_parse_value(self): self.assertEquals(self.asset0.parse_value(1.25), 125000000) self.assertEquals(self.asset1.parse_value(2), 20) self.assertEquals(self.asset2.parse_value(5), 5) def test_format_value(self): self.assertEquals(self.asset0.format_value(10000),'0.0001') self.assertEquals(self.asset1.format_value(2),'0.2') self.assertEquals(self.asset2.format_value(5),'5') def test_get_data(self): self.assertEquals(self.asset0.get_data(), self.def0) self.assertEquals(self.asset1.get_data(), self.def1) self.assertEquals(self.asset2.get_data(), self.def2) def test_register_asset_definition(self): self.assertRaises(Exception, self.adm.register_asset_definition, self.asset1) def test_add_asset_definition(self): colorset3 = ColorSet(self.colormap, [self.colormap.d[4]]) def4 = {'monikers': ['test3'], 'color_set': colorset3.get_data()} self.adm.add_asset_definition(def4) self.assertTrue(self.adm.get_asset_by_moniker('test3').get_color_set() .equals(colorset3)) def test_all_assets(self): reprs = [asset.__repr__() for asset in self.adm.get_all_assets()] self.assertTrue(self.asset0.__repr__() in reprs) self.assertTrue(self.asset1.__repr__() in reprs) self.assertTrue(self.asset2.__repr__() in reprs) def test_get_asset_and_address(self): ch = self.asset1.get_color_set().get_color_hash() addr = '1CC3X2gu58d6wXUWMffpuzN9JAfTUWu4Kj' coloraddress = "%s@%s" % (ch, addr) asset, address = self.adm.get_asset_and_address(coloraddress) self.assertEquals(asset.__repr__(), self.asset1.__repr__()) self.assertEquals(addr, address) asset, address = self.adm.get_asset_and_address(addr) self.assertEquals(asset.__repr__(), self.asset0.__repr__()) self.assertEquals(addr, address) self.assertRaises(Exception, self.adm.get_asset_and_address, '0@0') def test_add(self): assetvalue3 = self.assetvalue0 + self.assetvalue1 self.assertEqual(assetvalue3.get_value(), 11) assetvalue3 = 0 + self.assetvalue1 self.assertEqual(assetvalue3.get_value(), 6) self.assertRaises(IncompatibleTypesError, self.assetvalue0.__add__, self.assetvalue2) def test_iadd(self): assetvalue = self.assetvalue0.clone() assetvalue += self.assetvalue1 self.assertEqual(assetvalue.get_value(), 11) def test_sub(self): assetvalue = self.assetvalue1 - self.assetvalue0 self.assertEqual(assetvalue.get_value(), 1) assetvalue = self.assetvalue1 - 0 self.assertEqual(assetvalue.get_value(), self.assetvalue1.get_value()) def test_lt(self): self.assertTrue(self.assetvalue0 < self.assetvalue1) self.assertTrue(self.assetvalue1 > self.assetvalue0) self.assertTrue(self.assetvalue1 >= self.assetvalue0) self.assertTrue(self.assetvalue1 > 0) def test_sum(self): assetvalues = [self.assetvalue0, self.assetvalue1, AdditiveAssetValue(asset=self.asset0, value=3)] self.assertEqual(AdditiveAssetValue.sum(assetvalues).get_value(), 14) def test_get_asset(self): self.assertEqual(self.assettarget0.get_asset(), self.asset0) def test_get_value(self): self.assertEqual(self.assettarget0.get_value(), self.assetvalue0.get_value()) def test_sum(self): assettargets = [self.assettarget0, self.assettarget1, AssetTarget('address3',self.assettarget1)] self.assertEqual(AssetTarget.sum(assettargets).get_value(), 17) self.assertEqual(AssetTarget.sum([]), 0) def test_get_address(self): self.assertEqual(self.assettarget0.get_address(), 'address0') def test_repr(self): self.assertEqual(self.assettarget0.__repr__(), 'address0: Asset Value: 5')
class TestTxcons(unittest.TestCase): def setUp(self): self.path = ":memory:" self.pwallet = PersistentWallet(self.path) self.config = { 'dw_master_key': 'test', 'testnet': True, 'ccc': { 'colordb_path': self.path }, 'bip0032': False } self.pwallet.wallet_config = self.config self.pwallet.init_model() self.model = self.pwallet.get_model() self.colormap = self.model.get_color_map() self.colordesc0 = "obc:color0:0:0" self.colordesc1 = "obc:color1:0:0" self.colordesc2 = "obc:color2:0:0" # add some colordescs self.colorid0 = self.colormap.resolve_color_desc(self.colordesc0) self.colorid1 = self.colormap.resolve_color_desc(self.colordesc1) self.colorid2 = self.colormap.resolve_color_desc(self.colordesc2) self.colordef0 = OBColorDefinition(self.colorid0, { 'txhash': 'color0', 'outindex': 0 }) self.colordef1 = OBColorDefinition(self.colorid1, { 'txhash': 'color1', 'outindex': 0 }) self.colordef2 = OBColorDefinition(self.colorid2, { 'txhash': 'color2', 'outindex': 0 }) self.asset_config = { 'monikers': ['blue'], 'color_set': [self.colordesc0], } self.basset_config = { 'monikers': ['bitcoin'], 'color_set': [''], } self.asset = AssetDefinition(self.colormap, self.asset_config) self.basset = AssetDefinition(self.colormap, self.basset_config) self.basic = BasicTxSpec(self.model) self.bbasic = BasicTxSpec(self.model) wam = self.model.get_address_manager() self.address0 = wam.get_new_address(self.asset.get_color_set()) self.addr0 = self.address0.get_address() self.bcolorset = ColorSet(self.colormap, ['']) self.baddress = wam.get_new_address(self.bcolorset) self.baddr = self.baddress.get_address() self.assetvalue0 = AdditiveAssetValue(asset=self.asset, value=5) self.assetvalue1 = AdditiveAssetValue(asset=self.asset, value=6) self.assetvalue2 = AdditiveAssetValue(asset=self.asset, value=7) self.bassetvalue = AdditiveAssetValue(asset=self.basset, value=8) self.assettarget0 = AssetTarget(self.addr0, self.assetvalue0) self.assettarget1 = AssetTarget(self.addr0, self.assetvalue1) self.assettarget2 = AssetTarget(self.addr0, self.assetvalue2) self.bassettarget = AssetTarget(self.baddr, self.bassetvalue) self.atargets = [ self.assettarget0, self.assettarget1, self.assettarget2 ] # add some targets self.colorvalue0 = SimpleColorValue(colordef=self.colordef0, value=5) self.colortarget0 = ColorTarget(self.addr0, self.colorvalue0) self.colorvalue1 = SimpleColorValue(colordef=self.colordef0, value=6) self.colortarget1 = ColorTarget(self.addr0, self.colorvalue1) self.colorvalue2 = SimpleColorValue(colordef=self.colordef0, value=7) self.colortarget2 = ColorTarget(self.addr0, self.colorvalue2) self.bcolorvalue = SimpleColorValue(colordef=UNCOLORED_MARKER, value=8) self.bcolortarget = ColorTarget(self.baddr, self.bcolorvalue) self.targets = [ self.colortarget0, self.colortarget1, self.colortarget2 ] self.transformer = TransactionSpecTransformer(self.model, self.config) self.blockhash = '00000000c927c5d0ee1ca362f912f83c462f644e695337ce3731b9f7c5d1ca8c' self.txhash = '4fe45a5ba31bab1e244114c4555d9070044c73c98636231c77657022d76b87f7' def test_basic(self): self.assertRaises(InvalidTargetError, self.basic.is_monocolor) self.assertRaises(InvalidTargetError, self.basic.add_target, self.colortarget0) self.basic.add_target(self.assettarget0) self.basic.add_target(self.assettarget1) self.basic.add_target(self.assettarget2) self.assertEqual(self.basic.is_monocolor(), True) self.assertEqual(self.basic.is_monoasset(), True) self.assertEqual(self.basic.targets, self.atargets) self.basic.add_target(self.bassettarget) self.assertEqual(self.basic.is_monoasset(), False) self.assertEqual(self.basic.is_monocolor(), False) self.assertRaises(InvalidTransformationError, self.basic.make_operational_tx_spec, self.asset) def add_coins(self): script = tools.compile( "OP_DUP OP_HASH160 {0} OP_EQUALVERIFY OP_CHECKSIG".format( self.address0.rawPubkey().encode("hex"))).encode("hex") self.model.utxo_man.store.add_utxo(self.addr0, self.txhash, 0, 100, script) script = tools.compile( "OP_DUP OP_HASH160 {0} OP_EQUALVERIFY OP_CHECKSIG".format( self.baddress.rawPubkey().encode("hex"))).encode("hex") self.model.utxo_man.store.add_utxo(self.baddr, self.txhash, 1, 20000, script) self.model.ccc.metastore.set_as_scanned(self.colorid0, self.blockhash) self.model.ccc.cdstore.add(self.colorid0, self.txhash, 0, 100, '') def test_operational(self): self.basic.add_target(self.assettarget0) self.basic.add_target(self.assettarget1) self.basic.add_target(self.assettarget2) op = self.transformer.transform_basic(self.basic, 'operational') self.assertTrue(self.transformer.classify_tx_spec(op), 'operational') self.assertRaises(InvalidTargetError, op.add_target, 1) self.assertEqual(ColorTarget.sum(op.get_targets()), ColorTarget.sum(self.targets)) self.assertEqual(op.get_change_addr(self.colordef0), self.addr0) self.assertEqual(op.get_change_addr(UNCOLORED_MARKER), self.baddr) self.assertEqual(op.get_required_fee(1).get_value(), 10000) self.assertRaises(InvalidColorIdError, op.get_change_addr, self.colordef1) cv = SimpleColorValue(colordef=self.colordef0, value=0) self.assertRaises(ZeroSelectError, op.select_coins, cv) cv = SimpleColorValue(colordef=self.colordef0, value=5) self.assertRaises(InsufficientFundsError, op.select_coins, cv) self.add_coins() self.assertEqual(op.select_coins(cv)[1].get_value(), 100) def test_composed(self): self.basic.add_target(self.assettarget0) self.basic.add_target(self.assettarget1) self.basic.add_target(self.assettarget2) self.add_coins() op = self.transformer.transform(self.basic, 'operational') self.assertEqual(op.get_change_addr(self.colordef0), self.addr0) self.assertEqual(op.get_change_addr(UNCOLORED_MARKER), self.baddr) comp = self.transformer.transform(op, 'composed') self.assertTrue(self.transformer.classify_tx_spec(comp), 'composed') signed = self.transformer.transform(comp, 'signed') self.assertTrue(self.transformer.classify_tx_spec(signed), 'signed') self.assertEqual(len(signed.get_hex_txhash()), 64) txdata = signed.get_tx_data() same = RawTxSpec.from_tx_data(self.model, txdata) self.assertEqual(same.get_hex_tx_data(), signed.get_hex_tx_data()) self.assertRaises(InvalidTransformationError, self.transformer.transform, signed, '') def test_other(self): self.assertEqual(self.transformer.classify_tx_spec(1), None) self.assertRaises(InvalidTransformationError, self.transformer.transform_basic, self.basic, '') self.assertRaises(InvalidTransformationError, self.transformer.transform_operational, self.basic, '') self.assertRaises(InvalidTransformationError, self.transformer.transform_composed, self.basic, '') self.assertRaises(InvalidTransformationError, self.transformer.transform_signed, self.basic, '') self.assertRaises(InvalidTransformationError, self.transformer.transform, '', '') self.add_coins() self.bbasic.add_target(self.bassettarget) signed = self.transformer.transform(self.bbasic, 'signed') self.assertEqual(len(signed.get_hex_txhash()), 64)
class TestAssetDefinition(unittest.TestCase): def setUp(self): self.colormap = MockColorMap() d = self.colormap.d self.colorset0 = ColorSet(self.colormap, [d[0]]) self.colorset1 = ColorSet(self.colormap, [d[1], d[2]]) self.colorset2 = ColorSet(self.colormap, [d[3]]) self.def0 = {'monikers': ['bitcoin'], 'color_set': self.colorset0.get_data(), 'unit':100000000} self.def1 = {'monikers': ['test1'], 'color_set': self.colorset1.get_data(), 'unit':10} self.def2 = {'monikers': ['test2','test2alt'], 'color_set': self.colorset2.get_data(), 'unit':1} self.asset0 = AssetDefinition(self.colormap, self.def0) self.asset1 = AssetDefinition(self.colormap, self.def1) self.asset2 = AssetDefinition(self.colormap, self.def2) config = {'asset_definitions': [self.def1, self.def2]} self.adm = AssetDefinitionManager(self.colormap, config) def test_repr(self): self.assertEquals(self.asset0.__repr__(), "['bitcoin']: ['']") self.assertEquals( self.asset1.__repr__(), "['test1']: ['obc:color_desc_1:0:0', 'obc:color_desc_2:0:1']") self.assertEquals(self.asset2.__repr__(), "['test2', 'test2alt']: ['obc:color_desc_3:0:1']") def test_get_monikers(self): self.assertEquals(self.asset0.get_monikers(), ['bitcoin']) self.assertEquals(self.asset1.get_monikers(), ['test1']) self.assertEquals(self.asset2.get_monikers(), ['test2', 'test2alt']) def test_get_color_set(self): self.assertTrue(self.asset0.get_color_set().equals(self.colorset0)) self.assertTrue(self.asset1.get_color_set().equals(self.colorset1)) self.assertTrue(self.asset2.get_color_set().equals(self.colorset2)) def test_get_colorvalue(self): utxo = MockUTXO(5,[[1,2],[2,3],[3,4]]) self.assertEquals(self.asset0.get_colorvalue(utxo), 5) self.assertEquals(self.asset1.get_colorvalue(utxo), 2) self.assertEquals(self.asset2.get_colorvalue(utxo), 4) utxo = MockUTXO(5,[[5,2],[6,3],[3,4]]) self.assertRaises(Exception, self.asset1.get_colorvalue, utxo) def test_parse_value(self): self.assertEquals(self.asset0.parse_value(1.25), 125000000) self.assertEquals(self.asset1.parse_value(2), 20) self.assertEquals(self.asset2.parse_value(5), 5) def test_format_value(self): self.assertEquals(self.asset0.format_value(10000),'0.0001') self.assertEquals(self.asset1.format_value(2),'0.2') self.assertEquals(self.asset2.format_value(5),'5') def test_get_data(self): self.assertEquals(self.asset0.get_data(), self.def0) self.assertEquals(self.asset1.get_data(), self.def1) self.assertEquals(self.asset2.get_data(), self.def2) def test_register_asset_definition(self): self.assertRaises(Exception, self.adm.register_asset_definition, self.asset1) def test_add_asset_definition(self): colorset3 = ColorSet(self.colormap, [self.colormap.d[4]]) def4 = {'monikers': ['test3'], 'color_set': colorset3.get_data()} self.adm.add_asset_definition(def4) self.assertTrue(self.adm.get_asset_by_moniker('test3').get_color_set() .equals(colorset3)) def test_all_assets(self): reprs = [asset.__repr__() for asset in self.adm.get_all_assets()] self.assertTrue(self.asset0.__repr__() in reprs) self.assertTrue(self.asset1.__repr__() in reprs) self.assertTrue(self.asset2.__repr__() in reprs) def test_get_asset_and_address(self): ch = self.asset1.get_color_set().get_color_hash() addr = '1CC3X2gu58d6wXUWMffpuzN9JAfTUWu4Kj' coloraddress = "%s@%s" % (ch, addr) asset, address = self.adm.get_asset_and_address(coloraddress) self.assertEquals(asset.__repr__(), self.asset1.__repr__()) self.assertEquals(addr, address) asset, address = self.adm.get_asset_and_address(addr) self.assertEquals(asset.__repr__(), self.asset0.__repr__()) self.assertEquals(addr, address) self.assertRaises(Exception, self.adm.get_asset_and_address, '0@0')
class TestTxcons(unittest.TestCase): def setUp(self): self.path = ":memory:" self.pwallet = PersistentWallet(self.path) self.config = {'dw_master_key': 'test', 'testnet': True, 'ccc': { 'colordb_path' : self.path}, 'bip0032': False } self.pwallet.wallet_config = self.config self.pwallet.init_model() self.model = self.pwallet.get_model() self.colormap = self.model.get_color_map() self.colordesc0 = "obc:color0:0:0" self.colordesc1 = "obc:color1:0:0" self.colordesc2 = "obc:color2:0:0" # add some colordescs self.colorid0 = self.colormap.resolve_color_desc(self.colordesc0) self.colorid1 = self.colormap.resolve_color_desc(self.colordesc1) self.colorid2 = self.colormap.resolve_color_desc(self.colordesc2) self.colordef0 = OBColorDefinition( self.colorid0, {'txhash': 'color0', 'outindex': 0}) self.colordef1 = OBColorDefinition( self.colorid1, {'txhash': 'color1', 'outindex': 0}) self.colordef2 = OBColorDefinition( self.colorid2, {'txhash': 'color2', 'outindex': 0}) self.asset_config = { 'monikers': ['blue'], 'color_set': [self.colordesc0], } self.basset_config = { 'monikers': ['bitcoin'], 'color_set': [''], } self.asset = AssetDefinition(self.colormap, self.asset_config) self.basset = AssetDefinition(self.colormap, self.basset_config) self.basic = BasicTxSpec(self.model) self.bbasic = BasicTxSpec(self.model) wam = self.model.get_address_manager() self.address0 = wam.get_new_address(self.asset.get_color_set()) self.addr0 = self.address0.get_address() self.bcolorset = ColorSet(self.colormap, ['']) self.baddress = wam.get_new_address(self.bcolorset) self.baddr = self.baddress.get_address() self.assetvalue0 = AdditiveAssetValue(asset=self.asset, value=5) self.assetvalue1 = AdditiveAssetValue(asset=self.asset, value=6) self.assetvalue2 = AdditiveAssetValue(asset=self.asset, value=7) self.bassetvalue = AdditiveAssetValue(asset=self.basset, value=8) self.assettarget0 = AssetTarget(self.addr0, self.assetvalue0) self.assettarget1 = AssetTarget(self.addr0, self.assetvalue1) self.assettarget2 = AssetTarget(self.addr0, self.assetvalue2) self.bassettarget = AssetTarget(self.baddr, self.bassetvalue) self.atargets = [self.assettarget0, self.assettarget1, self.assettarget2] # add some targets self.colorvalue0 = SimpleColorValue(colordef=self.colordef0, value=5) self.colortarget0 = ColorTarget(self.addr0, self.colorvalue0) self.colorvalue1 = SimpleColorValue(colordef=self.colordef0, value=6) self.colortarget1 = ColorTarget(self.addr0, self.colorvalue1) self.colorvalue2 = SimpleColorValue(colordef=self.colordef0, value=7) self.colortarget2 = ColorTarget(self.addr0, self.colorvalue2) self.bcolorvalue = SimpleColorValue(colordef=UNCOLORED_MARKER, value=8) self.bcolortarget = ColorTarget(self.baddr, self.bcolorvalue) self.targets = [self.colortarget0, self.colortarget1, self.colortarget2] self.transformer = TransactionSpecTransformer(self.model, self.config) self.blockhash = '00000000c927c5d0ee1ca362f912f83c462f644e695337ce3731b9f7c5d1ca8c' self.txhash = '4fe45a5ba31bab1e244114c4555d9070044c73c98636231c77657022d76b87f7' def test_basic(self): self.assertRaises(InvalidTargetError, self.basic.is_monocolor) self.assertRaises(InvalidTargetError, self.basic.add_target, self.colortarget0) self.basic.add_target(self.assettarget0) self.basic.add_target(self.assettarget1) self.basic.add_target(self.assettarget2) self.assertEqual(self.basic.is_monocolor(), True) self.assertEqual(self.basic.is_monoasset(), True) self.assertEqual(self.basic.targets, self.atargets) self.basic.add_target(self.bassettarget) self.assertEqual(self.basic.is_monoasset(), False) self.assertEqual(self.basic.is_monocolor(), False) self.assertRaises(InvalidTransformationError, self.basic.make_operational_tx_spec, self.asset) def add_coins(self): script = tools.compile( "OP_DUP OP_HASH160 {0} OP_EQUALVERIFY OP_CHECKSIG".format( self.address0.rawPubkey().encode("hex"))).encode("hex") self.model.utxo_man.store.add_utxo(self.addr0, self.txhash, 0, 100, script) script = tools.compile( "OP_DUP OP_HASH160 {0} OP_EQUALVERIFY OP_CHECKSIG".format( self.baddress.rawPubkey().encode("hex"))).encode("hex") self.model.utxo_man.store.add_utxo(self.baddr, self.txhash, 1, 20000, script) self.model.ccc.metastore.set_as_scanned(self.colorid0, self.blockhash) self.model.ccc.cdstore.add(self.colorid0, self.txhash, 0, 100, '') def test_operational(self): self.basic.add_target(self.assettarget0) self.basic.add_target(self.assettarget1) self.basic.add_target(self.assettarget2) op = self.transformer.transform_basic(self.basic, 'operational') self.assertTrue(self.transformer.classify_tx_spec(op), 'operational') self.assertRaises(InvalidTargetError, op.add_target, 1) self.assertEqual(ColorTarget.sum(op.get_targets()), ColorTarget.sum(self.targets)) self.assertEqual(op.get_change_addr(self.colordef0), self.addr0) self.assertEqual(op.get_change_addr(UNCOLORED_MARKER), self.baddr) self.assertEqual(op.get_required_fee(1).get_value(), 10000) self.assertRaises(InvalidColorIdError, op.get_change_addr, self.colordef1) cv = SimpleColorValue(colordef=self.colordef0, value=0) self.assertRaises(ZeroSelectError, op.select_coins, cv) cv = SimpleColorValue(colordef=self.colordef0, value=5) self.assertRaises(InsufficientFundsError, op.select_coins, cv) self.add_coins() self.assertEqual(op.select_coins(cv)[1].get_value(), 100) def test_composed(self): self.basic.add_target(self.assettarget0) self.basic.add_target(self.assettarget1) self.basic.add_target(self.assettarget2) self.add_coins() op = self.transformer.transform(self.basic, 'operational') self.assertEqual(op.get_change_addr(self.colordef0), self.addr0) self.assertEqual(op.get_change_addr(UNCOLORED_MARKER), self.baddr) comp = self.transformer.transform(op, 'composed') self.assertTrue(self.transformer.classify_tx_spec(comp), 'composed') signed = self.transformer.transform(comp, 'signed') self.assertTrue(self.transformer.classify_tx_spec(signed), 'signed') self.assertEqual(len(signed.get_hex_txhash()), 64) txdata = signed.get_tx_data() same = RawTxSpec.from_tx_data(self.model, txdata) self.assertEqual(same.get_hex_tx_data(), signed.get_hex_tx_data()) self.assertRaises(InvalidTransformationError, self.transformer.transform, signed, '') def test_other(self): self.assertEqual(self.transformer.classify_tx_spec(1), None) self.assertRaises(InvalidTransformationError, self.transformer.transform_basic, self.basic, '') self.assertRaises(InvalidTransformationError, self.transformer.transform_operational, self.basic, '') self.assertRaises(InvalidTransformationError, self.transformer.transform_composed, self.basic, '') self.assertRaises(InvalidTransformationError, self.transformer.transform_signed, self.basic, '') self.assertRaises(InvalidTransformationError, self.transformer.transform, '', '') self.add_coins() self.bbasic.add_target(self.bassettarget) signed = self.transformer.transform(self.bbasic, 'signed') self.assertEqual(len(signed.get_hex_txhash()), 64)
class TestAssetDefinition(unittest.TestCase): def setUp(self): self.colormap = MockColorMap() d = self.colormap.d self.colorset0 = ColorSet(self.colormap, [d[0]]) self.colorset1 = ColorSet(self.colormap, [d[1], d[2]]) self.colorset2 = ColorSet(self.colormap, [d[3]]) self.def0 = { 'monikers': ['bitcoin'], 'color_set': self.colorset0.get_data(), 'unit': 100000000 } self.def1 = { 'monikers': ['test1'], 'color_set': self.colorset1.get_data(), 'unit': 10 } self.def2 = { 'monikers': ['test2', 'test2alt'], 'color_set': self.colorset2.get_data(), 'unit': 1 } self.asset0 = AssetDefinition(self.colormap, self.def0) self.asset1 = AssetDefinition(self.colormap, self.def1) self.asset2 = AssetDefinition(self.colormap, self.def2) config = {'asset_definitions': [self.def1, self.def2]} self.adm = AssetDefinitionManager(self.colormap, config) def test_repr(self): self.assertEquals(self.asset0.__repr__(), "['bitcoin']: ['']") self.assertEquals( self.asset1.__repr__(), "['test1']: ['obc:color_desc_1:0:0', 'obc:color_desc_2:0:1']") self.assertEquals(self.asset2.__repr__(), "['test2', 'test2alt']: ['obc:color_desc_3:0:1']") def test_get_monikers(self): self.assertEquals(self.asset0.get_monikers(), ['bitcoin']) self.assertEquals(self.asset1.get_monikers(), ['test1']) self.assertEquals(self.asset2.get_monikers(), ['test2', 'test2alt']) def test_get_color_set(self): self.assertTrue(self.asset0.get_color_set().equals(self.colorset0)) self.assertTrue(self.asset1.get_color_set().equals(self.colorset1)) self.assertTrue(self.asset2.get_color_set().equals(self.colorset2)) def test_get_colorvalue(self): utxo = MockUTXO(5, [[1, 2], [2, 3], [3, 4]]) self.assertEquals(self.asset0.get_colorvalue(utxo), 5) self.assertEquals(self.asset1.get_colorvalue(utxo), 2) self.assertEquals(self.asset2.get_colorvalue(utxo), 4) utxo = MockUTXO(5, [[5, 2], [6, 3], [3, 4]]) self.assertRaises(Exception, self.asset1.get_colorvalue, utxo) def test_parse_value(self): self.assertEquals(self.asset0.parse_value(1.25), 125000000) self.assertEquals(self.asset1.parse_value(2), 20) self.assertEquals(self.asset2.parse_value(5), 5) def test_format_value(self): self.assertEquals(self.asset0.format_value(10000), '0.0001') self.assertEquals(self.asset1.format_value(2), '0.2') self.assertEquals(self.asset2.format_value(5), '5') def test_get_data(self): self.assertEquals(self.asset0.get_data(), self.def0) self.assertEquals(self.asset1.get_data(), self.def1) self.assertEquals(self.asset2.get_data(), self.def2) def test_register_asset_definition(self): self.assertRaises(Exception, self.adm.register_asset_definition, self.asset1) def test_add_asset_definition(self): colorset3 = ColorSet(self.colormap, [self.colormap.d[4]]) def4 = {'monikers': ['test3'], 'color_set': colorset3.get_data()} self.adm.add_asset_definition(def4) self.assertTrue( self.adm.get_asset_by_moniker('test3').get_color_set().equals( colorset3)) def test_all_assets(self): reprs = [asset.__repr__() for asset in self.adm.get_all_assets()] self.assertTrue(self.asset0.__repr__() in reprs) self.assertTrue(self.asset1.__repr__() in reprs) self.assertTrue(self.asset2.__repr__() in reprs) def test_get_asset_and_address(self): ch = self.asset1.get_color_set().get_color_hash() addr = '1CC3X2gu58d6wXUWMffpuzN9JAfTUWu4Kj' coloraddress = "%s@%s" % (ch, addr) asset, address = self.adm.get_asset_and_address(coloraddress) self.assertEquals(asset.__repr__(), self.asset1.__repr__()) self.assertEquals(addr, address) asset, address = self.adm.get_asset_and_address(addr) self.assertEquals(asset.__repr__(), self.asset0.__repr__()) self.assertEquals(addr, address) self.assertRaises(Exception, self.adm.get_asset_and_address, '0@0')
class TestAssetDefinition(unittest.TestCase): def setUp(self): self.colormap = MockColorMap() d = self.colormap.d self.colorset0 = ColorSet(self.colormap, ['']) self.colorset1 = ColorSet(self.colormap, [d[1], d[2]]) self.colorset2 = ColorSet(self.colormap, [d[3]]) self.def0 = { 'monikers': ['bitcoin'], 'color_set': self.colorset0.get_data(), 'unit': 100000000 } self.def1 = { 'monikers': ['test1'], 'color_set': self.colorset1.get_data(), 'unit': 10 } self.def2 = { 'monikers': ['test2', 'test2alt'], 'color_set': self.colorset2.get_data(), 'unit': 1 } self.asset0 = AssetDefinition(self.colormap, self.def0) self.asset1 = AssetDefinition(self.colormap, self.def1) self.asset2 = AssetDefinition(self.colormap, self.def2) self.assetvalue0 = AdditiveAssetValue(asset=self.asset0, value=5) self.assetvalue1 = AdditiveAssetValue(asset=self.asset0, value=6) self.assetvalue2 = AdditiveAssetValue(asset=self.asset1, value=7) self.assettarget0 = AssetTarget('address0', self.assetvalue0) self.assettarget1 = AssetTarget('address1', self.assetvalue1) self.assettarget2 = AssetTarget('address2', self.assetvalue2) config = {'asset_definitions': [self.def1, self.def2]} self.adm = AssetDefinitionManager(self.colormap, config) def test_repr(self): self.assertEquals(self.asset0.__repr__(), "['bitcoin']: ['']") self.assertEquals( self.asset1.__repr__(), "['test1']: ['obc:color_desc_1:0:0', 'obc:color_desc_2:0:1']") self.assertEquals(self.asset2.__repr__(), "['test2', 'test2alt']: ['obc:color_desc_3:0:1']") def test_get_monikers(self): self.assertEquals(self.asset0.get_monikers(), ['bitcoin']) self.assertEquals(self.asset1.get_monikers(), ['test1']) self.assertEquals(self.asset2.get_monikers(), ['test2', 'test2alt']) def test_get_color_set(self): self.assertTrue(self.asset0.get_color_set().equals(self.colorset0)) self.assertTrue(self.asset1.get_color_set().equals(self.colorset1)) self.assertTrue(self.asset2.get_color_set().equals(self.colorset2)) def test_get_colorvalue(self): g = {'txhash': 'blah', 'height': 1, 'outindex': 0} cid0 = list(self.colorset0.color_id_set)[0] cdef0 = OBColorDefinition(cid0, g) cid1 = list(self.colorset1.color_id_set)[0] cdef1 = OBColorDefinition(cid1, g) cid2 = list(self.colorset2.color_id_set)[0] cdef2 = OBColorDefinition(cid2, g) cv0 = SimpleColorValue(colordef=cdef0, value=1) cv1 = SimpleColorValue(colordef=cdef1, value=2) cv2 = SimpleColorValue(colordef=cdef2, value=3) utxo = MockUTXO([cv0, cv1, cv2]) self.assertEquals(self.asset0.get_colorvalue(utxo), cv0) self.assertEquals(self.asset1.get_colorvalue(utxo), cv1) self.assertEquals(self.asset2.get_colorvalue(utxo), cv2) utxo = MockUTXO([cv0, cv2]) self.assertRaises(Exception, self.asset1.get_colorvalue, utxo) def test_parse_value(self): self.assertEquals(self.asset0.parse_value(1.25), 125000000) self.assertEquals(self.asset1.parse_value(2), 20) self.assertEquals(self.asset2.parse_value(5), 5) def test_format_value(self): self.assertEquals(self.asset0.format_value(10000), '0.0001') self.assertEquals(self.asset1.format_value(2), '0.2') self.assertEquals(self.asset2.format_value(5), '5') def test_get_data(self): self.assertEquals(self.asset0.get_data(), self.def0) self.assertEquals(self.asset1.get_data(), self.def1) self.assertEquals(self.asset2.get_data(), self.def2) def test_register_asset_definition(self): self.assertRaises(Exception, self.adm.register_asset_definition, self.asset1) def test_add_asset_definition(self): colorset3 = ColorSet(self.colormap, [self.colormap.d[4]]) def4 = {'monikers': ['test3'], 'color_set': colorset3.get_data()} self.adm.add_asset_definition(def4) self.assertTrue( self.adm.get_asset_by_moniker('test3').get_color_set().equals( colorset3)) def test_all_assets(self): reprs = [asset.__repr__() for asset in self.adm.get_all_assets()] self.assertTrue(self.asset0.__repr__() in reprs) self.assertTrue(self.asset1.__repr__() in reprs) self.assertTrue(self.asset2.__repr__() in reprs) def test_get_asset_and_address(self): ch = self.asset1.get_color_set().get_color_hash() addr = '1CC3X2gu58d6wXUWMffpuzN9JAfTUWu4Kj' coloraddress = "%s@%s" % (ch, addr) asset, address = self.adm.get_asset_and_address(coloraddress) self.assertEquals(asset.__repr__(), self.asset1.__repr__()) self.assertEquals(addr, address) asset, address = self.adm.get_asset_and_address(addr) self.assertEquals(asset.__repr__(), self.asset0.__repr__()) self.assertEquals(addr, address) self.assertRaises(Exception, self.adm.get_asset_and_address, '0@0') def test_add(self): assetvalue3 = self.assetvalue0 + self.assetvalue1 self.assertEqual(assetvalue3.get_value(), 11) assetvalue3 = 0 + self.assetvalue1 self.assertEqual(assetvalue3.get_value(), 6) self.assertRaises(IncompatibleTypesError, self.assetvalue0.__add__, self.assetvalue2) def test_iadd(self): assetvalue = self.assetvalue0.clone() assetvalue += self.assetvalue1 self.assertEqual(assetvalue.get_value(), 11) def test_sub(self): assetvalue = self.assetvalue1 - self.assetvalue0 self.assertEqual(assetvalue.get_value(), 1) assetvalue = self.assetvalue1 - 0 self.assertEqual(assetvalue.get_value(), self.assetvalue1.get_value()) def test_lt(self): self.assertTrue(self.assetvalue0 < self.assetvalue1) self.assertTrue(self.assetvalue1 > self.assetvalue0) self.assertTrue(self.assetvalue1 >= self.assetvalue0) self.assertTrue(self.assetvalue1 > 0) def test_sum(self): assetvalues = [ self.assetvalue0, self.assetvalue1, AdditiveAssetValue(asset=self.asset0, value=3) ] self.assertEqual(AdditiveAssetValue.sum(assetvalues).get_value(), 14) def test_get_asset(self): self.assertEqual(self.assettarget0.get_asset(), self.asset0) def test_get_value(self): self.assertEqual(self.assettarget0.get_value(), self.assetvalue0.get_value()) def test_sum(self): assettargets = [ self.assettarget0, self.assettarget1, AssetTarget('address3', self.assettarget1) ] self.assertEqual(AssetTarget.sum(assettargets).get_value(), 17) self.assertEqual(AssetTarget.sum([]), 0) def test_get_address(self): self.assertEqual(self.assettarget0.get_address(), 'address0') def test_repr(self): self.assertEqual(self.assettarget0.__repr__(), 'address0: Asset Value: 5')