import unittest
import sys, os

sys.path.insert(0, os.path.abspath(__file__ + "/../../cli"))
sys.path.insert(0, os.path.abspath(__file__ + "/../../schema"))
from iroha_cli.built_in_commands import BuildInCommand
from iroha_cli.exception import CliException

class Sample:
    def __init__(self):
        self.sample_account_id_1 = "sample_account_id_1"
        self.sample_account_id_2 = "sample_account_id_2"
        self.sample_account_id_3 = "sample_account_id_3"

        self.sample_asset_id_1 = "sample_asset_id_1"
        self.sample_asset_id_2 = "sample_asset_id_2"
        self.sample_asset_id_3 = "sample_asset_id_3"

        self.sample_account_name = "sonoko_mizuki"

        self.sample_domain_id = "japan"
        self.sample_account_name = "mizuki"
        self.sample_keypair_path = "mizuki_key"

class TestKeygen(unittest.TestCase):

    def setUp(self):
        self.commands = BuildInCommand().commands
        self.sample = Sample()

    def tearDown(self):
        base = self.sample.sample_account_name + "@" + self.sample.sample_domain_id
        base_key = self.sample.sample_keypair_path
        if os.path.exists(base + ".pub"):
            os.remove(base + ".pub")
            os.remove(base + ".pri")
        if os.path.exists(base_key + ".pub"):
            os.remove(base_key + ".pub")
            os.remove(base_key + ".pri")

    def test_normal_with_account_name(self):
        command = self.commands["keygen"]["function"](
            {"account_name": self.sample.sample_account_name}
        )
        self.assertTrue(os.path.exists(self.sample.sample_account_name+".pub") and os.path.exists(self.sample.sample_account_name+".pri"))

    def test_normal_with_keypair_name(self):
        command = self.commands["keygen"]["function"](
            {"account_name": self.sample.sample_account_name,
            "keypair_name": self.sample.sample_keypair_path}
        )
        self.assertTrue(os.path.exists(self.sample.sample_keypair_path+".pub") and os.path.exists(self.sample.sample_keypair_path+".pri"))

    def test_error_no_account_name(self):
        try:
            self.commands["keygen"]["function"]({
                "keypair_name": self.sample.sample_keypair_path
            })
        except CliException:
            pass
        else:
            self.fail()

@@ -806,16 +806,367 @@
 i_key%22%0A%0A
+%0Adef deleteKeyIfExists(account_id):%0A    if os.path.exists(%22%7B%7D/.irohac/%7B%7D.pub%22.format(os.environ%5B'HOME'%5D, account_id)):%0A        os.remove(%22%7B%7D/.irohac/%7B%7D.pub%22.format(os.environ%5B'HOME'%5D, account_id))%0A    if os.path.exists(%22%7B%7D/.irohac/%7B%7D%22.format(os.environ%5B'HOME'%5D, account_id)):%0A        os.remove(%22%7B%7D/.irohac/%7B%7D%22.format(os.environ%5B'HOME'%5D, account_id))%0A%0A
 class Te
@@ -1469,242 +1469,125 @@
     
-if os.path.exists(base + %22.pub%22):%0A            os.remove(base + %22.pub%22)%0A            os.remove(base + %22.pri%22)%0A        if os.path.exists(base_key + %22.pub%22):%0A            os.remove(base_key + %22.pub%22)%0A            os.remove(base_key + %22.pri%22
+deleteKeyIfExists(base)%0A        deleteKeyIfExists(base_key)%0A        deleteKeyIfExists(self.sample.sample_account_name
 )%0A%0A 
@@ -1699,36 +1699,34 @@
       %7B%22account_
-name
+id
 %22: self.sample.s
@@ -1771,32 +1771,45 @@
 self.assertTrue(
+%0A            
 os.path.exists(s
@@ -1811,67 +1811,165 @@
 sts(
-self.sample.sample_account_name+%22.pub%22) and os.path.exists(
+%22%7B%7D/.irohac/%7B%7D.pub%22.format(os.environ%5B'HOME'%5D, self.sample.sample_account_name)) and %5C%0A            os.path.exists(%22%7B%7D/.irohac/%7B%7D%22.format(os.environ%5B'HOME'%5D, 
 self
@@ -1995,24 +1995,27 @@
 unt_name
-+%22.pri%22)
+))%0A        
 )%0A%0A    d
@@ -2131,20 +2131,18 @@
 account_
-name
+id
 %22: self.
@@ -2264,16 +2264,29 @@
 ertTrue(
+%0A            
 os.path.
@@ -2296,106 +2296,207 @@
 sts(
-self.sample.sample_keypair_path+%22.pub%22) and os.path.exists(self.sample.sample_keypair_path+%22.pri%22)
+%22%7B%7D/.irohac/%7B%7D.pub%22.format(os.environ%5B'HOME'%5D, self.sample.sample_account_name)) and %5C%0A            os.path.exists(%22%7B%7D/.irohac/%7B%7D%22.format(os.environ%5B'HOME'%5D, self.sample.sample_account_name))%0A        
 )%0A%0A 

Update test code