45 lines
2.5 KiB
Python
45 lines
2.5 KiB
Python
from pathlib import Path
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'app'))
|
|
from database import connect, ensure_asset, initialize
|
|
|
|
|
|
class PositionMigrationTests(unittest.TestCase):
|
|
def test_existing_history_and_one_time_migration(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
path = Path(directory) / 'finance.db'
|
|
initialize(path)
|
|
with connect(path) as db:
|
|
# Simulate the previous release: no migration marker, old active positions.
|
|
db.execute('DELETE FROM data_migrations')
|
|
for name in ['Zinsen', 'Steuerrückzahlung']:
|
|
asset_id = ensure_asset(db, name)
|
|
db.execute("INSERT INTO income_entries (date,asset_id,category,amount) VALUES ('2026-09-01',?,'interest',367)", (asset_id,))
|
|
before = [tuple(row) for row in db.execute('SELECT * FROM income_entries ORDER BY id')]
|
|
initialize(path)
|
|
with connect(path) as db:
|
|
positions = {row['name']: row['active'] for row in db.execute('SELECT * FROM assets')}
|
|
self.assertEqual(positions['Anleihezinsen'], 1)
|
|
self.assertEqual(positions['Zinsen NG'], 1)
|
|
self.assertEqual(positions['Zinsen'], 0)
|
|
self.assertEqual(positions['Steuerrückzahlung'], 0)
|
|
self.assertEqual(before, [tuple(row) for row in db.execute('SELECT * FROM income_entries ORDER BY id')])
|
|
db.execute("UPDATE assets SET active=0 WHERE name='Zinsen NG'")
|
|
initialize(path)
|
|
with connect(path) as db:
|
|
self.assertEqual(db.execute("SELECT active FROM assets WHERE name='Zinsen NG'").fetchone()[0], 0)
|
|
self.assertEqual(db.execute('SELECT COUNT(*) FROM data_migrations WHERE name="2026-09-09-interest-positions"').fetchone()[0], 1)
|
|
self.assertEqual(db.execute("SELECT COUNT(*) FROM assets WHERE name='Anleihezinsen'").fetchone()[0], 1)
|
|
|
|
def test_fresh_database(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
path = Path(directory) / 'finance.db'
|
|
initialize(path)
|
|
with connect(path) as db:
|
|
self.assertEqual(db.execute("SELECT asset_type FROM assets WHERE name='Anleihezinsen'").fetchone()[0], 'bond')
|
|
self.assertEqual(db.execute("SELECT asset_type FROM assets WHERE name='Zinsen NG'").fetchone()[0], 'interest')
|
|
self.assertEqual(db.execute('PRAGMA user_version').fetchone()[0], 3)
|