Skip to content

Commit

Permalink
second commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ginsstaahh committed Jul 30, 2018
1 parent c56b5be commit 817ff9b
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 70 deletions.
2 changes: 2 additions & 0 deletions bounce/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def create_engine(driver, user, password, host, port, db_name):
return sqlalchemy.create_engine(
f'{driver}://{user}:{password}@{host}:{port}/{db_name}', echo=True)

def conf_mappers():
return sqlalchemy.orm.configure_mappers()

def get_sessionmaker(engine):
"""Create a new DB sessionmaker bound to the given engine.
Expand Down
14 changes: 9 additions & 5 deletions bounce/db/club.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"""Defines the schema for the Clubs table in our DB."""
"""
Defines the schema for the Clubs table in our DB.
Also provides methods to access and edit the DB.
"""

from sqlalchemy import Column, Integer, String
from sqlalchemy import Column, Integer, String, func
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.types import TIMESTAMP

Expand All @@ -21,12 +24,13 @@ class Club(Base):
facebook_url = Column('facebook_url', String, nullable=True)
instagram_url = Column('instagram_url', String, nullable=True)
twitter_url = Column('twitter_url', String, nullable=True)
created_at = Column('created_at', TIMESTAMP)
created_at = Column(
'created_at', TIMESTAMP, nullable=False, server_default=func.now())

def to_dict(self):
"""Returns a dict representation of a club."""
return {
'id': self.id,
'id': self.identifier,
'name': self.name,
'description': self.description,
'website_url': self.website_url,
Expand Down Expand Up @@ -84,4 +88,4 @@ def update(session, name, new_name, description, website_url, facebook_url,
def delete(session, name):
"""Deletes the club with the given name."""
session.query(Club).filter(Club.name == name).delete()
session.commit()
session.commit()
2 changes: 1 addition & 1 deletion bounce/server/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def status(self):

class Endpoint:
"""
Represents and endpoint to which requests can be made in order to manage
Represents an endpoint to which requests can be made in order to manage
a REST resource.
"""

Expand Down
14 changes: 7 additions & 7 deletions bounce/server/api/clubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async def get(self, _, name):
# Failed to find a club with that name
raise APIError('No such club', status=404)
return response.json(club_data, status=200)

@validate(PutClubRequest, GetClubResponse)
async def put(self, request, name):
"""Handles a PUT /clubs/<name> request by updating the club with
Expand All @@ -33,7 +33,7 @@ async def put(self, request, name):
updated_club = club.update(
self.server.db_session,
name,
new_name=body.get('new_name', None),
new_name=body.get('name', None),
description=body.get('description', None),
website_url=body.get('website_url', None),
facebook_url=body.get('facebook_url', None),
Expand All @@ -43,7 +43,7 @@ async def put(self, request, name):

async def delete(self, _, name):
"""Handles a DELETE /clubs/<name> request by deleting the club with
the given name. """
the given name."""
club.delete(self.server.db_session, name)
return response.text('', status=204)

Expand All @@ -59,10 +59,10 @@ async def post(self, request):
# Put the club in the DB
body = request.json
try:
club.insert(self.server.db_session, body['name'],
body['description'], body['website_url'],
body['facebook_url'], body['instagram_url'],
body['twitter_url'])
club.insert(self.server.db_session,
body['name'], body['description'],
body['website_url'], body['facebook_url'],
body['instagram_url'], body['twitter_url'])
except IntegrityError:
raise APIError('Club already exists', status=400)
return response.text('', status=201)
7 changes: 3 additions & 4 deletions bounce/server/resource/club.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@

class PostClubsRequest(metaclass=ResourceMeta):
"""Defines the schema for a POST /clubs request."""
__params__ = {
__body__ = {
'type':
'object',
'required': [
'name', 'description', 'website_url', 'facebook_url',
'instagram_url', 'twitter_url'
'name', 'description'
],
'additionalProperties':
False,
Expand Down Expand Up @@ -39,7 +38,7 @@ class PostClubsRequest(metaclass=ResourceMeta):

class PutClubRequest(metaclass=ResourceMeta):
"""Defines the schema for a PUT /clubs/<name> request."""
__request__ = {
__body__ = {
'type': 'object',
'required': ['name'],
'additionalProperties': False,
Expand Down
8 changes: 0 additions & 8 deletions bounce/server/resource/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@

class PostUsersRequest(metaclass=ResourceMeta):
"""Defines the schema for a POST /users request."""
<<<<<<< 58ddae63f22bf2f54edd5143cbf322eef552066b
__body__ = {
=======
__body__ = { #shouldn't it be __params__?
>>>>>>> first commit
'type': 'object',
'required': ['full_name', 'username', 'email'],
'additionalProperties': False,
Expand All @@ -30,11 +26,7 @@ class PostUsersRequest(metaclass=ResourceMeta):

class PutUserRequest(metaclass=ResourceMeta):
"""Defines the schema for a PUT /users/<username> request."""
<<<<<<< 58ddae63f22bf2f54edd5143cbf322eef552066b
__body__ = {
=======
__body__ = { #shouldn't it be __params__?
>>>>>>> first commit
'type': 'object',
'additionalProperties': False,
'properties': {
Expand Down
4 changes: 0 additions & 4 deletions container/dev/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# Use the Python 3 image as our base image
<<<<<<< 58ddae63f22bf2f54edd5143cbf322eef552066b
FROM python:3.6
=======
FROM python:3
>>>>>>> first commit

# Copy project directory to /opt/bounce
ADD . /opt/bounce
Expand Down
5 changes: 0 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,5 @@ psycopg2==2.7.4
sanic==0.7.0
sqlalchemy==1.2.8
ujson==1.35 # via sanic
<<<<<<< 58ddae63f22bf2f54edd5143cbf322eef552066b
uvloop==0.11.0 # via sanic
websockets==6.0 # via sanic
=======
uvloop==0.9.1 # via sanic
websockets==4.0.1 # via sanic
>>>>>>> first commit
21 changes: 0 additions & 21 deletions test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@
# pip-compile --output-file test-requirements.txt test-requirements.in
#
aiohttp==3.3.0
<<<<<<< 58ddae63f22bf2f54edd5143cbf322eef552066b
astroid==1.6.5 # via pylint
=======
astroid==1.6.4 # via pylint
>>>>>>> first commit
async-timeout==3.0.0 # via aiohttp
atomicwrites==1.1.5 # via pytest
attrs==18.1.0 # via aiohttp, pytest
Expand All @@ -19,40 +15,23 @@ coverage==4.5.1 # via coveralls
coveralls==1.3.0
docopt==0.6.2 # via coveralls
flake8==3.5.0
<<<<<<< 58ddae63f22bf2f54edd5143cbf322eef552066b
idna-ssl==1.1.0 # via aiohttp
idna==2.7 # via idna-ssl, requests, yarl
=======
idna-ssl==1.0.1 # via aiohttp
idna==2.6 # via idna-ssl, requests, yarl
>>>>>>> first commit
isort==4.3.4
lazy-object-proxy==1.3.1 # via astroid
mccabe==0.6.1 # via flake8, pylint
more-itertools==4.2.0 # via pytest
multidict==4.3.1 # via aiohttp, yarl
pluggy==0.6.0 # via pytest
<<<<<<< 58ddae63f22bf2f54edd5143cbf322eef552066b
py==1.5.4 # via pytest
=======
py==1.5.3 # via pytest
>>>>>>> first commit
pycodestyle==2.3.1 # via flake8
pyflakes==1.6.0 # via flake8
pylint==1.9.1
pytest-asyncio==0.8.0
<<<<<<< 58ddae63f22bf2f54edd5143cbf322eef552066b
pytest==3.6.3 # via pytest-asyncio
=======
pytest==3.6.0 # via pytest-asyncio
>>>>>>> first commit
requests==2.19.1 # via coveralls
six==1.11.0 # via astroid, more-itertools, pylint, pytest
urllib3==1.23 # via requests
wrapt==1.10.11 # via astroid
yapf==0.22.0
<<<<<<< 58ddae63f22bf2f54edd5143cbf322eef552066b
yarl==1.2.6 # via aiohttp
=======
yarl==1.2.4 # via aiohttp
>>>>>>> first commit
10 changes: 2 additions & 8 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
"""Defines fixtures for use in our tests."""

import pytest
<<<<<<< 58ddae63f22bf2f54edd5143cbf322eef552066b
from bounce.server import Server
from bounce.server.config import ServerConfig
from bounce.server.api.users import UserEndpoint, UsersEndpoint
=======

from bounce.server import Server
from bounce.server.api.users import UserEndpoint, UsersEndpoint
from bounce.server.config import ServerConfig
>>>>>>> first commit
from bounce.server.api.clubs import ClubEndpoint, ClubsEndpoint


@pytest.fixture
Expand All @@ -23,6 +17,6 @@ def config():
@pytest.fixture
def server(config):
"""Returns a test server."""
serv = Server(config, [UserEndpoint, UsersEndpoint])
serv = Server(config, [UserEndpoint, UsersEndpoint, ClubEndpoint, ClubsEndpoint])
serv.start(test=True)
return serv
15 changes: 8 additions & 7 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def test_delete_user__success(server):
_, response = server.app.test_client.delete('/users/test')
assert response.status == 204


def test_post_clubs__success(server):
_, response = server.app.test_client.post(
'/clubs',
Expand Down Expand Up @@ -105,29 +106,29 @@ def test_put_club__success(server):
_, response = server.app.test_client.put(
'/clubs/test',
data=json.dumps({
'name': 'new test',
'name': 'newtest',
'description': 'club called new test',
}))
assert response.status == 200
assert response.json['name'] == 'test'
assert response.json['name'] == 'newtest'
assert response.json['description'] == 'club called new test'
assert response.json['id'] == 1
assert isinstance(response.json['created_at'], int)


def test_put_club__failure(server):
_, response = server.app.test_client.put(
'/clubs/test', data=json.dumps({
'/clubs/newtest', data=json.dumps({
'garbage': True
}))
assert response.status == 400


def test_get_club__success(server):
_, response = server.app.test_client.get('/clubs/test')
_, response = server.app.test_client.get('/clubs/newtest')
assert response.status == 200
assert response.json['username'] == 'test'
assert response.json['full_name'] == 'New Name'
assert response.json['email'] == 'newemail@test.com'
assert response.json['name'] == 'newtest'
assert response.json['description'] == 'club called new test'
assert response.json['id'] == 1
assert isinstance(response.json['created_at'], int)

Expand Down

0 comments on commit 817ff9b

Please sign in to comment.