def test_ensure_connected_does_not_reconnect_pointlessly(self): db.ensure_connected() conn1 = db.conn db.ensure_connected() conn2 = db.conn assert conn1 == conn2
def _ticker_symbols(self): """Get all the tickers to poll from the database.""" db.ensure_connected() cur = db.conn.execute(""" SELECT ticker AS name FROM assets WHERE class != 'Domestic Cash';""") return [ticker.name for ticker in cur.fetchall()]
def determine_accounts(accounts): """Parse `accounts` parameter to see which accounts to include.""" if accounts is None: db.ensure_connected() cur = db.conn.execute("""SELECT name FROM accounts;""") return [x.name for x in cur.fetchall()] if type(accounts) is str: return accounts.split(",") if "," in accounts else [accounts] return accounts
def test_no_switching_db_environments(self): db.ensure_connected() assert db._env == "test" with pytest.raises(Exception): db.ensure_connected("diff") assert db._env == "test" assert db.is_alive()
def simple_fixture_teardown(): db.ensure_connected("test") db.conn.execute("""DELETE FROM transactions;""") db.conn.execute("""DELETE FROM distributions;""") db.conn.execute("""DELETE FROM assetprices;""") db.conn.execute("""DELETE FROM marketdays;""") db.conn.execute("""DELETE FROM assets;""") db.conn.execute("""DELETE FROM assetclasses;""") db.conn.execute("""DELETE FROM accounts;""") db.conn.execute("""DELETE FROM investors;""") db.conn.execute("""DELETE FROM accounttypes;""") db.conn.close()
def _get_start_date(self, accounts): db.ensure_connected() cur = db.conn.execute( """SELECT MIN(datecreated) AS datecreated FROM accounts WHERE name = ANY(%(accounts)s);""", {"accounts": accounts}, ) date_created = cur.fetchone().datecreated if date_created is None: return None return date_created - timedelta(days=1)
def _get_ticker_names(self, accounts, from_day): """Get ticker names for which there are prices available.""" db.ensure_connected() cur = db.conn.execute( """SELECT DISTINCT(ticker) AS name FROM assetprices WHERE (%(from_day)s IS NULL OR day >= %(from_day)s) AND day <= %(today)s ORDER BY name ASC;""", {"from_day": from_day, "today": date.today()}, ) priced = [x.name for x in cur.fetchall()] cur = db.conn.execute( """SELECT DISTINCT(target) AS name FROM transactions WHERE (%(from_day)s IS NULL OR day >= %(from_day)s) AND day <= %(today)s AND account = ANY(%(accounts)s);""", {"from_day": from_day, "today": date.today(), "accounts": accounts}, ) bought = [x.name for x in cur.fetchall()] return sorted(set(priced).intersection(bought))
def setup_function(): db._env = "test" db.ensure_connected()
def test_ensure_connected_connects(self): assert db.ensure_connected() assert db.is_alive()
def simple_fixture(): db._env = "test" db.ensure_connected("test") db.conn.execute("""DELETE FROM transactions;""") db.conn.execute("""DELETE FROM distributions;""") db.conn.execute("""DELETE FROM assetprices;""") db.conn.execute("""DELETE FROM marketdays;""") db.conn.execute("""DELETE FROM assets;""") db.conn.execute("""DELETE FROM assetclasses;""") db.conn.execute("""DELETE FROM accounts;""") db.conn.execute("""DELETE FROM investors;""") db.conn.execute("""DELETE FROM accounttypes;""") db.conn.execute(""" INSERT INTO accounttypes (name, tax, margin) VALUES ('RRSP', 'deferred', false);""") db.conn.execute(""" INSERT INTO investors (name) VALUES ('Someone');""") db.conn.execute(""" INSERT INTO accounts (name, accounttype, investor, datecreated) VALUES ('RRSP1', 'RRSP', 'Someone', '2017-03-02');""") db.conn.execute(""" INSERT INTO marketdays (day, open) VALUES ('2017-03-02', true), ('2017-03-03', true), ('2017-03-04', false), ('2017-03-05', false), ('2017-03-06', true), ('2017-03-07', true), ('2017-03-08', true);""") db.conn.execute(""" INSERT INTO assetclasses (name, domesticcurrency) VALUES ('Cash', true), ('Domestic Equity', true), ('Emergent Markets Equity', false);""") db.conn.execute(""" INSERT INTO assets (ticker, class) VALUES ('Cash', 'Cash'), ('VCN.TO', 'Domestic Equity'), ('VEE.TO', 'Emergent Markets Equity');""") db.conn.execute(""" INSERT INTO assetprices (ticker, day, close) VALUES ('VCN.TO', '2017-03-02', 30.00), ('VCN.TO', '2017-03-03', 30.10), ('VCN.TO', '2017-03-06', 29.85), ('VCN.TO', '2017-03-07', 29.85), ('VCN.TO', '2017-03-08', 30.15), ('VEE.TO', '2017-03-02', 29.00), ('VEE.TO', '2017-03-03', 28.00), ('VEE.TO', '2017-03-06', 32.00), ('VEE.TO', '2017-03-07', 32.00), ('VEE.TO', '2017-03-08', 31.00);""") db.conn.execute(""" INSERT INTO distributions (ticker, day, amount) VALUES ('VCN.TO', '2017-03-03', 0.1010), ('VCN.TO', '2017-03-06', 0.0990);""") db.conn.execute(""" INSERT INTO transactions (day, txtype, account, source, target, units, unitprice, commission, total) VALUES ('2017-03-02', 'deposit', 'RRSP1', null, 'Cash', null, null, null, 10000), ('2017-03-03', 'buy', 'RRSP1', 'Cash', 'VCN.TO', 100, 30.10, 0.35, 3010.35), ('2017-03-03', 'buy', 'RRSP1', 'Cash', 'VEE.TO', 100, 28.00, 0.35, 2800.35), ('2017-03-03', 'dividend', 'RRSP1', 'VCN.TO', 'Cash', 100, 0.1010, 0, 10.10), ('2017-03-06', 'dividend', 'RRSP1', 'VCN.TO', 'Cash', 100, 0.0990, 0, 9.90);""" ) db.conn.close() db.ensure_connected("test")
def __init__(self): db.ensure_connected() self._conn = db.conn