Repository: rotki/rotki

Issue:
test_upgrade_db_22_to_23_without_frontend_settings fails in OSX tests
## Problem Definition

We noticed this in the nightly runs here: https://github.com/rotki/rotki/runs/1811061566?check_suite_focus=true

It fails only in OSX and has never failed in linux.

```
______________ test_upgrade_db_22_to_23_without_frontend_settings ______________

data_dir = PosixPath('/Users/runner/.cache/.rotkehlchen-test-dir')
user_data_dir = PosixPath('/Users/runner/.cache/.rotkehlchen-test-dir/testuser')

    def test_upgrade_db_22_to_23_without_frontend_settings(data_dir, user_data_dir):
        """Test upgrading the DB from version 22 to version 23.
    
        Tests the case where frontend settings were not populated and also the cache
        file movement and deletion. Also test deleletion of deprecated historical_data_start
        """
        msg_aggregator = MessagesAggregator()
        _use_prepared_db(user_data_dir, 'v22_rotkehlchen_wo_frontend_settings.db')
        db_v22 = _init_db_with_target_version(
            target_version=22,
            user_data_dir=user_data_dir,
            msg_aggregator=msg_aggregator,
        )
        cursor = db_v22.conn.cursor()
    
        # Create cache files under the data directory
        (data_dir / 'forex_history_file.json').touch()
        (data_dir / 'price_history_BTC_EUR.json').touch()
        (data_dir / 'price_history_aDAI_USD.json').touch()
        (data_dir / 'price_history_YFI_USD.json').touch()
        # Also create an innocent json file and a random file
        (data_dir / 'random.json').touch()
        (data_dir / 'random.txt').touch()
        # Check all settings except 'frontend_settings' exist
        assert cursor.execute(
            'SELECT COUNT(*) FROM settings WHERE name = "frontend_settings";',
        ).fetchone()[0] == 0
        assert cursor.execute(
            'SELECT COUNT(*) FROM settings WHERE name IN '
            '("thousand_separator", "decimal_separator", "currency_location");',
        ).fetchone()[0] == 3
        # Check we got a historical data start entry to remove
        assert cursor.execute(
            'SELECT COUNT(*) FROM settings WHERE name = "historical_data_start";',
        ).fetchone()[0] == 1
    
        # Migrate to v23
        db = _init_db_with_target_version(
            target_version=23,
            user_data_dir=user_data_dir,
            msg_aggregator=msg_aggregator,
        )
        cursor = db.conn.cursor()
    
        # Make sure the settings have been removed
        assert cursor.execute(
            'SELECT COUNT(*) FROM settings WHERE name IN '
            '("thousand_separator", "decimal_separator", "currency_location");',
        ).fetchone()[0] == 0
        assert cursor.execute(
            'SELECT COUNT(*) FROM settings WHERE name = "historical_data_start";',
        ).fetchone()[0] == 0
    
        # Make sure the settings have been migrated into 'frontend_settings'
        frontend_settings = cursor.execute(
            'SELECT value FROM settings WHERE name = "frontend_settings";',
        ).fetchone()[0]
        frontend_settings_map = json.loads(frontend_settings)
        assert frontend_settings_map['thousand_separator'] == ','
        assert frontend_settings_map['decimal_separator'] == '.'
        assert frontend_settings_map['currency_location'] == 'after'
    
        # Assure the cache files were deleted
        assert not (data_dir / 'price_history_BTC_EUR.json').is_file()
        assert not (data_dir / 'price_history_aDAI_USD.json').is_file()
        assert not (data_dir / 'price_history_YFI_USD.json').is_file()
        # and that the forex history cache file moved
>       assert (data_dir / 'price_history' / 'forex_history_file.json').is_file()
E       AssertionError: assert False
E        +  where False = <bound method Path.is_file of PosixPath('/Users/runner/.cache/.rotkehlchen-test-dir/price_history/forex_history_file.json')>()
E        +    where <bound method Path.is_file of PosixPath('/Users/runner/.cache/.rotkehlchen-test-dir/price_history/forex_history_file.json')> = ((PosixPath('/Users/runner/.cache/.rotkehlchen-test-dir') / 'price_history') / 'forex_history_file.json').is_file

rotkehlchen/tests/db/test_db_upgrades.py:1311: AssertionError
```

## Task

Figure out why and fix it


Discussion:

Resolving patch:
diff --git a/rotkehlchen/db/upgrades/v22_v23.py b/rotkehlchen/db/upgrades/v22_v23.py
--- a/rotkehlchen/db/upgrades/v22_v23.py
+++ b/rotkehlchen/db/upgrades/v22_v23.py
@@ -51,6 +51,8 @@ def upgrade_v22_to_v23(db: 'DBHandler') -> None:
     db.conn.commit()
 
     # -- Now move forex history to the new directory and remove all old cache files
+    # We botched this. Should have been forex_history_file.json -> price_history_forex.json
+    # and not the other way around
     data_directory = db.user_data_dir.parent
     price_history_dir = get_or_make_price_history_dir(data_directory)
     forex_history_file = data_directory / 'price_history_forex.json'


Test patch:
diff --git a/rotkehlchen/tests/db/test_db_upgrades.py b/rotkehlchen/tests/db/test_db_upgrades.py
--- a/rotkehlchen/tests/db/test_db_upgrades.py
+++ b/rotkehlchen/tests/db/test_db_upgrades.py
@@ -1241,6 +1241,7 @@ def test_upgrade_db_22_to_23_with_frontend_settings(user_data_dir):
     assert db.get_version() == 23
 
 
+@pytest.mark.parametrize('use_clean_caching_directory', [True])
 def test_upgrade_db_22_to_23_without_frontend_settings(data_dir, user_data_dir):
     """Test upgrading the DB from version 22 to version 23.
 
@@ -1257,7 +1258,7 @@ def test_upgrade_db_22_to_23_without_frontend_settings(data_dir, user_data_dir):
     cursor = db_v22.conn.cursor()
 
     # Create cache files under the data directory
-    (data_dir / 'forex_history_file.json').touch()
+    (data_dir / 'price_history_forex.json').touch()  # we botched this (check upgrade function)
     (data_dir / 'price_history_BTC_EUR.json').touch()
     (data_dir / 'price_history_aDAI_USD.json').touch()
     (data_dir / 'price_history_YFI_USD.json').touch()
@@ -1307,7 +1308,10 @@ def test_upgrade_db_22_to_23_without_frontend_settings(data_dir, user_data_dir):
     assert not (data_dir / 'price_history_BTC_EUR.json').is_file()
     assert not (data_dir / 'price_history_aDAI_USD.json').is_file()
     assert not (data_dir / 'price_history_YFI_USD.json').is_file()
-    # and that the forex history cache file moved
+    # and that the forex history cache file moved. The problem here is that
+    # the DB upgrade botched this file movement. Should have been price_history_forex.json
+    # but ended up moving it with the wrong name. So here we just check the wrong
+    # name. Just to satisfy the test
     assert (data_dir / 'price_history' / 'forex_history_file.json').is_file()
     # and that the other files were not touched
     assert (data_dir / 'random.json').is_file()