From 817ff9b42b87ecd337cb70bcf2ee89c14c82b817 Mon Sep 17 00:00:00 2001 From: ginsstaahh Date: Mon, 30 Jul 2018 15:03:34 -0700 Subject: [PATCH] second commit --- bounce/db/__init__.py | 2 ++ bounce/db/club.py | 14 +++++++++----- bounce/server/api/__init__.py | 2 +- bounce/server/api/clubs.py | 14 +++++++------- bounce/server/resource/club.py | 7 +++---- bounce/server/resource/user.py | 8 -------- container/dev/Dockerfile | 4 ---- requirements.txt | 5 ----- test-requirements.txt | 21 --------------------- tests/conftest.py | 10 ++-------- tests/test_api.py | 15 ++++++++------- 11 files changed, 32 insertions(+), 70 deletions(-) diff --git a/bounce/db/__init__.py b/bounce/db/__init__.py index d773a6a..23a5ed1 100644 --- a/bounce/db/__init__.py +++ b/bounce/db/__init__.py @@ -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. diff --git a/bounce/db/club.py b/bounce/db/club.py index 4506a78..4cd9223 100644 --- a/bounce/db/club.py +++ b/bounce/db/club.py @@ -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 @@ -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, @@ -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() \ No newline at end of file diff --git a/bounce/server/api/__init__.py b/bounce/server/api/__init__.py index 7d2850c..9923f48 100644 --- a/bounce/server/api/__init__.py +++ b/bounce/server/api/__init__.py @@ -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. """ diff --git a/bounce/server/api/clubs.py b/bounce/server/api/clubs.py index 6666e43..2466949 100644 --- a/bounce/server/api/clubs.py +++ b/bounce/server/api/clubs.py @@ -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/ request by updating the club with @@ -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), @@ -43,7 +43,7 @@ async def put(self, request, name): async def delete(self, _, name): """Handles a DELETE /clubs/ request by deleting the club with - the given name. """ + the given name.""" club.delete(self.server.db_session, name) return response.text('', status=204) @@ -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) diff --git a/bounce/server/resource/club.py b/bounce/server/resource/club.py index 3f976f4..dc5c45d 100644 --- a/bounce/server/resource/club.py +++ b/bounce/server/resource/club.py @@ -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, @@ -39,7 +38,7 @@ class PostClubsRequest(metaclass=ResourceMeta): class PutClubRequest(metaclass=ResourceMeta): """Defines the schema for a PUT /clubs/ request.""" - __request__ = { + __body__ = { 'type': 'object', 'required': ['name'], 'additionalProperties': False, diff --git a/bounce/server/resource/user.py b/bounce/server/resource/user.py index 1f52703..11467b1 100644 --- a/bounce/server/resource/user.py +++ b/bounce/server/resource/user.py @@ -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, @@ -30,11 +26,7 @@ class PostUsersRequest(metaclass=ResourceMeta): class PutUserRequest(metaclass=ResourceMeta): """Defines the schema for a PUT /users/ request.""" -<<<<<<< 58ddae63f22bf2f54edd5143cbf322eef552066b __body__ = { -======= - __body__ = { #shouldn't it be __params__? ->>>>>>> first commit 'type': 'object', 'additionalProperties': False, 'properties': { diff --git a/container/dev/Dockerfile b/container/dev/Dockerfile index 493e79c..8cad3d8 100644 --- a/container/dev/Dockerfile +++ b/container/dev/Dockerfile @@ -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 diff --git a/requirements.txt b/requirements.txt index 5c7307e..3a2f1ba 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/test-requirements.txt b/test-requirements.txt index 405ed16..651ad9f 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -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 @@ -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 diff --git a/tests/conftest.py b/tests/conftest.py index 1f1bd9d..ad6d86c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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 @@ -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 diff --git a/tests/test_api.py b/tests/test_api.py index 4453bba..c408a75 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -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', @@ -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)