mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
add TUVote SQLAlchemy ORM model
Signed-off-by: Kevin Morris <kevr@0cost.org>
This commit is contained in:
parent
8c345a0448
commit
0c1241f8bb
2 changed files with 99 additions and 0 deletions
43
aurweb/models/tu_vote.py
Normal file
43
aurweb/models/tu_vote.py
Normal file
|
@ -0,0 +1,43 @@
|
|||
from sqlalchemy import Column, ForeignKey, Integer
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm import backref, relationship
|
||||
|
||||
import aurweb.models.tu_voteinfo
|
||||
import aurweb.models.user
|
||||
|
||||
from aurweb.models.declarative import Base
|
||||
|
||||
|
||||
class TUVote(Base):
|
||||
__tablename__ = "TU_Votes"
|
||||
|
||||
VoteID = Column(Integer, ForeignKey("TU_VoteInfo.ID", ondelete="CASCADE"),
|
||||
nullable=False)
|
||||
VoteInfo = relationship(
|
||||
"TUVoteInfo", backref=backref("tu_votes", lazy="dynamic"),
|
||||
foreign_keys=[VoteID])
|
||||
|
||||
UserID = Column(Integer, ForeignKey("Users.ID", ondelete="CASCADE"),
|
||||
nullable=False)
|
||||
User = relationship(
|
||||
"User", backref=backref("tu_votes", lazy="dynamic"),
|
||||
foreign_keys=[UserID])
|
||||
|
||||
__mapper_args__ = {"primary_key": [VoteID, UserID]}
|
||||
|
||||
def __init__(self,
|
||||
VoteInfo: aurweb.models.tu_voteinfo.TUVoteInfo = None,
|
||||
User: aurweb.models.user.User = None):
|
||||
self.VoteInfo = VoteInfo
|
||||
if self.VoteInfo is None:
|
||||
raise IntegrityError(
|
||||
statement="Foreign key VoteID cannot be null.",
|
||||
orig="TU_Votes.VoteID",
|
||||
params=("NULL"))
|
||||
|
||||
self.User = User
|
||||
if self.User is None:
|
||||
raise IntegrityError(
|
||||
statement="Foreign key UserID cannot be null.",
|
||||
orig="TU_Votes.UserID",
|
||||
params=("NULL"))
|
56
test/test_tu_vote.py
Normal file
56
test/test_tu_vote.py
Normal file
|
@ -0,0 +1,56 @@
|
|||
from datetime import datetime
|
||||
|
||||
import pytest
|
||||
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
|
||||
from aurweb.db import create, query, rollback
|
||||
from aurweb.models.account_type import AccountType
|
||||
from aurweb.models.tu_vote import TUVote
|
||||
from aurweb.models.tu_voteinfo import TUVoteInfo
|
||||
from aurweb.models.user import User
|
||||
from aurweb.testing import setup_test_db
|
||||
|
||||
user = tu_voteinfo = None
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup():
|
||||
global user, tu_voteinfo
|
||||
|
||||
setup_test_db("Users", "TU_VoteInfo", "TU_Votes")
|
||||
|
||||
tu_type = query(AccountType,
|
||||
AccountType.AccountType == "Trusted User").first()
|
||||
user = create(User, Username="test", Email="test@example.org",
|
||||
RealName="Test User", Passwd="testPassword",
|
||||
AccountType=tu_type)
|
||||
|
||||
ts = int(datetime.utcnow().timestamp())
|
||||
tu_voteinfo = create(TUVoteInfo,
|
||||
Agenda="Blah blah.",
|
||||
User=user.Username,
|
||||
Submitted=ts, End=ts + 5,
|
||||
Quorum=0.5,
|
||||
Submitter=user)
|
||||
|
||||
|
||||
def test_tu_vote_creation():
|
||||
tu_vote = create(TUVote, User=user, VoteInfo=tu_voteinfo)
|
||||
assert tu_vote.VoteInfo == tu_voteinfo
|
||||
assert tu_vote.User == user
|
||||
|
||||
assert tu_vote in user.tu_votes
|
||||
assert tu_vote in tu_voteinfo.tu_votes
|
||||
|
||||
|
||||
def test_tu_vote_null_user_raises_exception():
|
||||
with pytest.raises(IntegrityError):
|
||||
create(TUVote, VoteInfo=tu_voteinfo)
|
||||
rollback()
|
||||
|
||||
|
||||
def test_tu_vote_null_voteinfo_raises_exception():
|
||||
with pytest.raises(IntegrityError):
|
||||
create(TUVote, User=user)
|
||||
rollback()
|
Loading…
Add table
Reference in a new issue