Skip to content

Commit

Permalink
Add Perk dbapi along with the required tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sayanchowdhury committed Jul 19, 2016
1 parent 5d2c8ae commit 418ba64
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 2 deletions.
84 changes: 83 additions & 1 deletion tahrir_api/dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from utils import autocommit, convert_name_to_id
from model import Badge, Invitation, Issuer, Assertion, Person, Authorization
from model import Team, Series
from model import Team, Series, Perk
from sqlalchemy import create_engine, func, and_, not_
from sqlalchemy.orm import sessionmaker, scoped_session
from datetime import (
Expand Down Expand Up @@ -160,6 +160,88 @@ def get_all_series(self):

return self.session.query(Series)

def perk_exists(self, perk_id):
"""
Check to see if this perk already exists in the database
:type perk_id: str
:param perk_id: The ID of a Perk
"""
return self.session.query(Perk).filter(Perk.id == perk_id).count() != 0

def perk_exists_for_badge_series(self, badge_id, series_id):
"""
Check if the perk with the given series and badge id exists
:type badge_id: str
:param badge_id: The ID of the badge
:type series_id: str
:param series_id: The ID of the series
"""
return self.get_perk_from_badge_series(badge_id,
series_id).count() != 0

def get_perk_from_badge_series(self, badge_id, series_id):
"""
Return the perk with the given series and badge id
:type badge_id: str
:param badge_id: The ID of the badge
:type series_id: str
:param series_id: The ID of the series
"""
return self.session.query(Perk).filter(
and_(Perk.series_id == func.lower(series_id),
Perk.badge_id == func.lower(badge_id)))

def get_perk(self, perk_id):
"""
Return the matching perk from the database
:type perk_id: str
:param perk_id: The ID of a Perk
"""
return self.session.query(Perk).filter(Perk.id == perk_id)

def get_all_perks(self, series_id):
"""
Returns all the perks for the series
:type series_id: str
:param series_id: The id of the Series
"""
return self.session.query(Perk).filter(
Perk.series_id == series_id).all()

@autocommit
def create_perk(self, position, badge_id, series_id):
"""
Adds a new perk to the database
:type name: int
:param name: position of the perk in the series
:type badge_id: str
:param badge_id: Badge ID for the Perk
:type series_id: str
:param series_id: ID of the Series
"""
if self.perk_exists_for_badge_series(badge_id, series_id):
perk = self.get_perk_from_badge_series(badge_id, series_id).one()
else:
perk = Perk(position=position,
badge_id=badge_id,
series_id=series_id)

self.session.add(perk)
self.session.flush()
perk_id = perk.id

return perk_id

def badge_exists(self, badge_id):
"""
Check to see if this badge already exists in the database
Expand Down
6 changes: 5 additions & 1 deletion tahrir_api/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
DateTime,
Unicode,
ForeignKey,
UniqueConstraint,
)
from sqlalchemy.types import (
Integer,
Expand Down Expand Up @@ -131,7 +132,10 @@ class Series(DeclarativeBase):


class Perk(DeclarativeBase):
__tablename__ = 'badgeseries'
__tablename__ = 'perk'
__table_args__ = (
UniqueConstraint('position', 'badge_id', 'series_id'),
)
id = Column(Integer, unique=True, primary_key=True)
position = Column(Integer, default=None)
badge_id = Column(Unicode(128), ForeignKey('badges.id'), nullable=False)
Expand Down
35 changes: 35 additions & 0 deletions tests/test_dbai.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,41 @@ def test_add_series(self):

assert self.api.series_exists("testseries") is True

def test_add_perk(self):
team_id = self.api.create_team("TestTeam")

series_id = self.api.create_series("TestSeries",
"A test series",
team_id,
"test, series")

badge_id_1 = self.api.add_badge(
"TestBadge-1",
"TestImage-2",
"A test badge for doing 10 unit tests",
"TestCriteria",
1337
)

badge_id_2 = self.api.add_badge(
"TestBadge-2",
"TestImage-2",
"A test badge for doing 100 unit tests",
"TestCriteria",
1337
)

perk_id_1 = self.api.create_perk(1,
badge_id_1,
series_id)

perk_id_2 = self.api.create_perk(2,
badge_id_2,
series_id)

assert self.api.perk_exists(perk_id_1) is True
assert self.api.perk_exists(perk_id_2) is True

def test_add_person(self):
self.api.add_person("test@tester.com")
assert self.api.person_exists("test@tester.com") is True
Expand Down

0 comments on commit 418ba64

Please sign in to comment.