Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Refactor most of the files for tests to pass on sqlalchemy2. #232

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions examples/simple_move_split.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
eur,
"transaction {}".format(i),
)
Split(accounts[random.randrange(N)], value=v, transaction=tx)
Split(accounts[random.randrange(N)], value=-v, transaction=tx)
book.add(tx)
book.add(Split(accounts[random.randrange(N)], value=v, transaction=tx))
book.add(Split(accounts[random.randrange(N)], value=-v, transaction=tx))
book.save()

# select two accounts
Expand Down
4 changes: 2 additions & 2 deletions piecash/_declbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from sqlalchemy import Column, VARCHAR, event
from sqlalchemy.ext.declarative import declared_attr
from sqlalchemy.orm import relation, foreign, object_session
from sqlalchemy.orm import relationship, foreign, object_session

from ._common import CallableList
from .kvp import DictWrapper, Slot
Expand All @@ -23,7 +23,7 @@ class DeclarativeBaseGuid(DictWrapper, DeclarativeBase):

@declared_attr
def slots(cls):
rel = relation(
rel = relationship(
"Slot",
primaryjoin=foreign(Slot.obj_guid) == cls.guid,
cascade="all, delete-orphan",
Expand Down
10 changes: 5 additions & 5 deletions piecash/budget.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import uuid

from sqlalchemy import Column, VARCHAR, INTEGER, BIGINT, ForeignKey
from sqlalchemy.orm import relation, foreign
from sqlalchemy.orm import relationship, foreign

from ._common import hybrid_property_gncnumeric, Recurrence, CallableList
from ._declbase import DeclarativeBaseGuid
Expand Down Expand Up @@ -38,14 +38,14 @@ class Budget(DeclarativeBaseGuid):
num_periods = Column("num_periods", INTEGER(), nullable=False)

# # relation definitions
recurrence = relation(
recurrence = relationship(
Recurrence,
primaryjoin=foreign(Recurrence.obj_guid) == guid,
cascade="all, delete-orphan",
uselist=False,
)

amounts = relation(
amounts = relationship(
"BudgetAmount",
back_populates="budget",
cascade="all, delete-orphan",
Expand Down Expand Up @@ -86,8 +86,8 @@ class BudgetAmount(DeclarativeBase):
amount = hybrid_property_gncnumeric(_amount_num, _amount_denom)

# relation definitions
account = relation("Account", back_populates="budget_amounts")
budget = relation("Budget", back_populates="amounts")
account = relationship("Account", back_populates="budget_amounts")
budget = relationship("Budget", back_populates="amounts")

def __str__(self):
return "BudgetAmount<{}={}>".format(self.period_num, self.amount)
24 changes: 12 additions & 12 deletions piecash/business/invoice.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import uuid

from sqlalchemy import Column, INTEGER, BIGINT, VARCHAR, ForeignKey
from sqlalchemy.orm import composite, relation
from sqlalchemy.orm import composite, relationship

from .person import PersonType

Expand Down Expand Up @@ -41,13 +41,13 @@ class Billterm(DeclarativeBaseGuid):
cutoff = Column("cutoff", INTEGER())

# relation definitions
children = relation(
children = relationship(
"Billterm",
back_populates="parent",
cascade="all, delete-orphan",
collection_class=CallableList,
)
parent = relation(
parent = relationship(
"Billterm",
back_populates="children",
remote_side=guid,
Expand Down Expand Up @@ -104,8 +104,8 @@ class Entry(DeclarativeBaseGuid):
order_guid = Column("order_guid", VARCHAR(length=32), ForeignKey("orders.guid"))

# relation definitions
order = relation("Order", back_populates="entries")
invoice = relation("Invoice", back_populates="entries")
order = relationship("Order", back_populates="entries")
invoice = relationship("Invoice", back_populates="entries")

def __str__(self):
return "Entry<{}>".format(self.description)
Expand Down Expand Up @@ -147,13 +147,13 @@ class Invoice(DeclarativeBaseGuid):

# relation definitions
# todo: check all relations and understanding of types...
term = relation("Billterm")
currency = relation("Commodity")
post_account = relation("Account")
post_lot = relation("Lot")
post_txn = relation("Transaction")
term = relationship("Billterm")
currency = relationship("Commodity")
post_account = relationship("Account")
post_lot = relationship("Lot")
post_txn = relationship("Transaction")

entries = relation(
entries = relationship(
"Entry",
back_populates="invoice",
cascade="all, delete-orphan",
Expand Down Expand Up @@ -225,7 +225,7 @@ class Order(DeclarativeBaseGuid):

# relation definitions
# todo: owner_guid/type links to Vendor or Customer
entries = relation(
entries = relationship(
"Entry",
back_populates="order",
cascade="all, delete-orphan",
Expand Down
16 changes: 8 additions & 8 deletions piecash/business/person.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from sqlalchemy import Column, VARCHAR, INTEGER, BIGINT, ForeignKey, and_, event
from sqlalchemy.ext.declarative import declared_attr
from sqlalchemy.orm import composite, relation, foreign
from sqlalchemy.orm import composite, relationship, foreign

from .._common import hybrid_property_gncnumeric, CallableList
from .._declbase import DeclarativeBaseGuid
Expand Down Expand Up @@ -113,7 +113,7 @@ def currency_guid(cls):

@declared_attr
def currency(cls):
return relation("Commodity")
return relationship("Commodity")

# hold the name of the counter to use for id
_counter_name = None
Expand Down Expand Up @@ -149,7 +149,7 @@ def __declare_last__(cls):

owner_type = PersonType.get(cls, None)
if owner_type and not hasattr(cls, "jobs"):
cls.jobs = relation('Job',
cls.jobs = relationship('Job',
primaryjoin=and_(
cls.guid == foreign(Job.owner_guid),
owner_type == Job.owner_type,
Expand Down Expand Up @@ -226,8 +226,8 @@ class Customer(Person, DeclarativeBaseGuid):
taxtable_guid = Column("taxtable", VARCHAR(length=32), ForeignKey("taxtables.guid"))

# relation definitions
taxtable = relation("Taxtable")
term = relation("Billterm")
taxtable = relationship("Taxtable")
term = relationship("Billterm")

def __init__(
self,
Expand Down Expand Up @@ -307,7 +307,7 @@ class Employee(Person, DeclarativeBaseGuid):
rate = hybrid_property_gncnumeric(_rate_num, _rate_denom)

# relation definitions
creditcard_account = relation("Account")
creditcard_account = relationship("Account")

def __init__(
self,
Expand Down Expand Up @@ -384,8 +384,8 @@ class Vendor(Person, DeclarativeBaseGuid):
)

# relation definitions
taxtable = relation("Taxtable")
term = relation("Billterm")
taxtable = relationship("Taxtable")
term = relationship("Billterm")

def __init__(
self,
Expand Down
12 changes: 6 additions & 6 deletions piecash/business/tax.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import uuid
from sqlalchemy import Column, VARCHAR, BIGINT, INTEGER, ForeignKey
from sqlalchemy.orm import relation
from sqlalchemy.orm import relationship

from .._common import hybrid_property_gncnumeric, CallableList
from .._declbase import DeclarativeBaseGuid, DeclarativeBase
Expand All @@ -26,19 +26,19 @@ class Taxtable(DeclarativeBaseGuid):
parent_guid = Column("parent", VARCHAR(length=32), ForeignKey("taxtables.guid"))

# relation definitions
entries = relation(
entries = relationship(
"TaxtableEntry",
back_populates="taxtable",
cascade="all, delete-orphan",
collection_class=CallableList,
)
children = relation(
children = relationship(
"Taxtable",
back_populates="parent",
cascade="all, delete-orphan",
collection_class=CallableList,
)
parent = relation(
parent = relationship(
"Taxtable",
back_populates="children",
remote_side=guid,
Expand Down Expand Up @@ -79,8 +79,8 @@ class TaxtableEntry(DeclarativeBase):
type = Column("type", ChoiceType({1: "value", 2: "percentage"}), nullable=False)

# relation definitions
taxtable = relation("Taxtable", back_populates="entries")
account = relation("Account")
taxtable = relationship("Taxtable", back_populates="entries")
account = relationship("Account")

def __init__(self, type, amount, account, taxtable=None):
self.type = type
Expand Down
16 changes: 8 additions & 8 deletions piecash/core/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from enum import Enum

from sqlalchemy import Column, VARCHAR, ForeignKey, INTEGER
from sqlalchemy.orm import relation, validates
from sqlalchemy.orm import relationship, validates

from .._common import CallableList, GncConversionError
from .._declbase import DeclarativeBaseGuid
Expand Down Expand Up @@ -165,37 +165,37 @@ def commodity_scu(self, value):
)

# relation definitions
commodity = relation("Commodity", back_populates="accounts")
children = relation(
commodity = relationship("Commodity", back_populates="accounts")
children = relationship(
"Account",
back_populates="parent",
cascade="all, delete-orphan",
collection_class=CallableList,
)
parent = relation(
parent = relationship(
"Account",
back_populates="children",
remote_side=guid,
)
splits = relation(
splits = relationship(
"Split",
back_populates="account",
cascade="all, delete-orphan",
collection_class=CallableList,
)
lots = relation(
lots = relationship(
"Lot",
back_populates="account",
cascade="all, delete-orphan",
collection_class=CallableList,
)
budget_amounts = relation(
budget_amounts = relationship(
"BudgetAmount",
back_populates="account",
cascade="all, delete-orphan",
collection_class=CallableList,
)
scheduled_transaction = relation(
scheduled_transaction = relationship(
"ScheduledTransaction",
back_populates="template_account",
cascade="all, delete-orphan",
Expand Down
8 changes: 5 additions & 3 deletions piecash/core/book.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from operator import attrgetter

from sqlalchemy import Column, VARCHAR, ForeignKey, inspect
from sqlalchemy.orm import relation, aliased, joinedload
from sqlalchemy.orm import relationship, aliased, joinedload
from sqlalchemy.orm.base import instance_state
from sqlalchemy.orm.exc import NoResultFound

Expand Down Expand Up @@ -99,12 +99,12 @@ class Book(DeclarativeBaseGuid):
)

# relation definitions
root_account = relation(
root_account = relationship(
"Account",
# back_populates='root_book',
foreign_keys=[root_account_guid],
)
root_template = relation("Account", foreign_keys=[root_template_guid])
root_template = relationship("Account", foreign_keys=[root_template_guid])

uri = None
session = None
Expand Down Expand Up @@ -333,6 +333,8 @@ def close(self):
# # remove the lock
# session.delete_lock()
session.close()
# close the engine
self.session.bind.engine.dispose()

# add general getters for gnucash classes
def get(self, cls, **kwargs):
Expand Down
17 changes: 9 additions & 8 deletions piecash/core/commodity.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from decimal import Decimal

from sqlalchemy import Column, VARCHAR, INTEGER, ForeignKey, BIGINT, Index
from sqlalchemy.orm import relation
from sqlalchemy.orm import relationship
from sqlalchemy.orm.exc import MultipleResultsFound

from ._commodity_helper import quandl_fx
Expand Down Expand Up @@ -67,12 +67,12 @@ class Price(DeclarativeBaseGuid):
value = hybrid_property_gncnumeric(_value_num, _value_denom)

# relation definitions
commodity = relation(
commodity = relationship(
"Commodity",
back_populates="prices",
foreign_keys=[commodity_guid],
)
currency = relation(
currency = relationship(
"Commodity",
foreign_keys=[currency_guid],
)
Expand Down Expand Up @@ -174,19 +174,19 @@ def base_currency(self):
)

# relation definitions
accounts = relation(
accounts = relationship(
"Account",
back_populates="commodity",
cascade="all, delete-orphan",
collection_class=CallableList,
)
transactions = relation(
transactions = relationship(
"Transaction",
back_populates="currency",
cascade="all, delete-orphan",
collection_class=CallableList,
)
prices = relation(
prices = relationship(
"Price",
back_populates="commodity",
foreign_keys=[Price.commodity_guid],
Expand Down Expand Up @@ -304,6 +304,7 @@ def update_prices(self, start_date=None):
date=datetime.datetime.strptime(q.date, "%Y-%m-%d").date(),
value=str(q.rate),
)
self.book.add(p)

else:
symbol = self.mnemonic
Expand All @@ -313,13 +314,13 @@ def update_prices(self, start_date=None):

# get historical data
for q in download_quote(symbol, start_date, datetime.date.today(), tz):
Price(
self.book.add(Price(
commodity=self,
currency=currency,
date=q.date,
value=q.close,
type="last",
)
))

def object_to_validate(self, change):
if change[-1] != "deleted":
Expand Down
Loading