Repository: bitcoin/bitcoin
File: test/functional/wallet_migration.py (Python)

Code before:

        # Check that the wallet was migrated, knows the right txid, and has the right balance.
        assert wallet.gettransaction(txid)
        assert_equal(bals, wallet.getbalances())

        # The migrated wallet should not be in the wallet dir, but should be in the list of wallets.
        info = wallet.getwalletinfo()

        walletdirlist = wallet.listwalletdir()
        assert {"name": info["walletname"]} not in walletdirlist["wallets"]

        walletlist = wallet.listwallets()
        assert info["walletname"] in walletlist

        # Check that old node can restore from the backup.
        self.old_node.restorewallet("relative_restored", migrate_res['backup_path'])
        wallet = self.old_node.get_wallet_rpc("relative_restored")
        assert wallet.gettransaction(txid)
        assert_equal(bals, wallet.getbalances())

        info = wallet.getwalletinfo()
        assert_equal(info["descriptors"], False)
        assert_equal(info["format"], "bdb")

    def test_wallet_with_path_ending_in_slash(self):
        self.log.info("Test migrating a wallet with a name/path ending in '/'")

        # The last directory in the wallet's path
        final_dir = "mywallet"
        wallet_name = f"path/to/{final_dir}/"
        wallet = self.create_legacy_wallet(wallet_name)
        default = self.master_node.get_wallet_rpc(self.default_wallet_name)

        addr = wallet.getnewaddress()
        txid = default.sendtoaddress(addr, 1)
        self.generate(self.master_node, 1)
        bals = wallet.getbalances()

        _, wallet = self.migrate_and_get_rpc(wallet_name)

        assert wallet.gettransaction(txid)

        assert_equal(bals, wallet.getbalances())

    def test_wallet_with_path_ending_in_relative_specifier(self):
        self.log.info("Test migrating a wallet with a name/path ending in a relative specifier, '..'")
        wallet_ending_in_relative = "path/that/ends/in/.."
        # the wallet data is actually inside of path/that/ends/
        wallet = self.create_legacy_wallet(wallet_ending_in_relative)
        default = self.master_node.get_wallet_rpc(self.default_wallet_name)


Review comment (suggestion):
(nit, only if you have to retouch) generally I believe we've been preferring to use f-strings for new python code

```suggestion
        self.log.info(f"Test migrating a wallet with the following path/name: {wallet_path}")
```

Code after:

        # Check that the wallet was migrated, knows the right txid, and has the right balance.
        assert wallet.gettransaction(txid)
        assert_equal(bals, wallet.getbalances())

        # The migrated wallet should not be in the wallet dir, but should be in the list of wallets.
        info = wallet.getwalletinfo()

        walletdirlist = wallet.listwalletdir()
        assert {"name": info["walletname"]} not in walletdirlist["wallets"]

        walletlist = wallet.listwallets()
        assert info["walletname"] in walletlist

        # Check that old node can restore from the backup.
        self.old_node.restorewallet("relative_restored", migrate_res['backup_path'])
        wallet = self.old_node.get_wallet_rpc("relative_restored")
        assert wallet.gettransaction(txid)
        assert_equal(bals, wallet.getbalances())

        info = wallet.getwalletinfo()
        assert_equal(info["descriptors"], False)
        assert_equal(info["format"], "bdb")

    def test_wallet_with_path(self, wallet_path):
        self.log.info("Test migrating a wallet with the following path/name: %s", wallet_path)
        # the wallet data is actually inside of path/that/ends/
        wallet = self.create_legacy_wallet(wallet_path)
        default = self.master_node.get_wallet_rpc(self.default_wallet_name)

        addr = wallet.getnewaddress()
        txid = default.sendtoaddress(addr, 1)
        self.generate(self.master_node, 1)
        bals = wallet.getbalances()

        _, wallet = self.migrate_and_get_rpc(wallet_path)

        assert wallet.gettransaction(txid)

        assert_equal(bals, wallet.getbalances())

    def test_default_wallet(self):
        self.log.info("Test migration of the wallet named as the empty string")
        wallet = self.create_legacy_wallet("")

        res, wallet = self.migrate_and_get_rpc("")
        info = wallet.getwalletinfo()
        assert_equal(info["descriptors"], True)
        assert_equal(info["format"], "sqlite")

        walletdir_list = wallet.listwalletdir()