Skip to content

Commit

Permalink
Add series dbapi along with the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sayanchowdhury committed Jul 19, 2016
1 parent 00295a0 commit 5d2c8ae
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
72 changes: 69 additions & 3 deletions 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
from model import Team, Series
from sqlalchemy import create_engine, func, and_, not_
from sqlalchemy.orm import sessionmaker, scoped_session
from datetime import (
Expand Down Expand Up @@ -52,7 +52,7 @@ def __init__(self, dburi=None, session=None, autocommit=True,

def team_exists(self, team_id):
"""
Check to see if this badge already exists in the database
Check to see if this team already exists in the database
:type team_id: str
:param team_id: The ID of a Team
Expand All @@ -66,7 +66,7 @@ def get_team(self, team_id):
Return the team with the given ID
:type team_id: str
:param team_id: The ID of the badge to return
:param team_id: The ID of the team to return
"""

if self.team_exists(team_id):
Expand Down Expand Up @@ -94,6 +94,72 @@ def create_team(self, name, team_id=None):
self.session.flush()
return team_id

def series_exists(self, series_id):
"""
Check to see if this series already exists in the database
:type series_id: str
:param series_id: The ID of a Series
"""

return self.session.query(Series).filter(
func.lower(Series.id) == func.lower(series_id)).count() != 0

def get_series(self, series_id):
"""
Return the series with the given ID
:type series_id: str
:param series_id: The ID of the series to return
"""

if self.team_exists(series_id):
return self.session.query(Series).filter(
func.lower(Series.id) == func.lower(series_id)).one()
return None

@autocommit
def create_series(self, name, desc, team_id, tags=None, series_id=None):
"""
Adds a new series to the database
:type name: str
:param name: Name of the Series
:type desc: str
:param desc: Description of the Series
:type team_id: str
:param team_id: Team Id to which this Series belongs to
:type tags: str
:param tags: Tags for a Series
:type series_id: str
:param series_id: ID of the Series
"""

if not series_id:
series_id = convert_name_to_id(name)

if not self.series_exists(series_id):
new_series = Series(id=series_id,
name=name,
description=desc,
tags=tags,
team_id=team_id)

self.session.add(new_series)
self.session.flush()
return series_id

def get_all_series(self):
"""
Get all series in the db.
"""

return self.session.query(Series)

def badge_exists(self, badge_id):
"""
Check to see if this badge already exists in the database
Expand Down
10 changes: 10 additions & 0 deletions tests/test_dbai.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ def test_add_team(self):

assert self.api.team_exists("testteam") is True

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

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

assert self.api.series_exists("testseries") 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 5d2c8ae

Please sign in to comment.