Add skeleton app with namespaces
This commit is contained in:
		
							
								
								
									
										2
									
								
								app/fizz/fizzbar/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								app/fizz/fizzbar/__init__.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,2 @@
 | 
			
		||||
from .model import Fizzbar  # noqa
 | 
			
		||||
from .schema import FizzbarSchema  # noqa
 | 
			
		||||
							
								
								
									
										56
									
								
								app/fizz/fizzbar/controller.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								app/fizz/fizzbar/controller.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,56 @@
 | 
			
		||||
from flask import request
 | 
			
		||||
from flask_accepts import accepts, responds
 | 
			
		||||
from flask_restplus import Namespace, Resource
 | 
			
		||||
from flask.wrappers import Response
 | 
			
		||||
from typing import List
 | 
			
		||||
 | 
			
		||||
from .schema import FizzbarSchema
 | 
			
		||||
from .service import FizzbarService
 | 
			
		||||
from .model import Fizzbar
 | 
			
		||||
from .interface import FizzbarInterface
 | 
			
		||||
 | 
			
		||||
api = Namespace('Fizzbar', description='A modular namespace within fizz')  # noqa
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@api.route('/')
 | 
			
		||||
class FizzbarResource(Resource):
 | 
			
		||||
    '''Fizzbars'''
 | 
			
		||||
 | 
			
		||||
    @responds(schema=FizzbarSchema, many=True)
 | 
			
		||||
    def get(self) -> List[Fizzbar]:
 | 
			
		||||
        '''Get all Fizzbars'''
 | 
			
		||||
 | 
			
		||||
        return FizzbarService.get_all()
 | 
			
		||||
 | 
			
		||||
    @accepts(schema=FizzbarSchema, api=api)
 | 
			
		||||
    @responds(schema=FizzbarSchema)
 | 
			
		||||
    def post(self) -> Fizzbar:
 | 
			
		||||
        '''Create a Single Fizzbar'''
 | 
			
		||||
 | 
			
		||||
        return FizzbarService.create(request.parsed_obj)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@api.route('/<int:fizzbarId>')
 | 
			
		||||
@api.param('fizzbarId', 'Fizzbar database ID')
 | 
			
		||||
class FizzbarIdResource(Resource):
 | 
			
		||||
    @responds(schema=FizzbarSchema)
 | 
			
		||||
    def get(self, fizzbarId: int) -> Fizzbar:
 | 
			
		||||
        '''Get Single Fizzbar'''
 | 
			
		||||
 | 
			
		||||
        return FizzbarService.get_by_id(fizzbarId)
 | 
			
		||||
 | 
			
		||||
    def delete(self, fizzbarId: int) -> Response:
 | 
			
		||||
        '''Delete Single Fizzbar'''
 | 
			
		||||
        from flask import jsonify
 | 
			
		||||
        print('fizzbarId = ', fizzbarId)
 | 
			
		||||
        id = FizzbarService.delete_by_id(fizzbarId)
 | 
			
		||||
        return jsonify(dict(status='Success', id=id))
 | 
			
		||||
 | 
			
		||||
    @accepts(schema=FizzbarSchema, api=api)
 | 
			
		||||
    @responds(schema=FizzbarSchema)
 | 
			
		||||
    def put(self, fizzbarId: int) -> Fizzbar:
 | 
			
		||||
        '''Update Single Fizzbar'''
 | 
			
		||||
 | 
			
		||||
        changes: FizzbarInterface = request.parsed_obj
 | 
			
		||||
        Fizzbar = FizzbarService.get_by_id(fizzbarId)
 | 
			
		||||
        return FizzbarService.update(Fizzbar, changes)
 | 
			
		||||
							
								
								
									
										84
									
								
								app/fizz/fizzbar/controller_test.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										84
									
								
								app/fizz/fizzbar/controller_test.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,84 @@
 | 
			
		||||
 | 
			
		||||
from unittest.mock import patch
 | 
			
		||||
from flask.testing import FlaskClient
 | 
			
		||||
 | 
			
		||||
from app.test.fixtures import client, app  # noqa
 | 
			
		||||
from .service import FizzbarService
 | 
			
		||||
from .schema import FizzbarSchema
 | 
			
		||||
from .model import Fizzbar
 | 
			
		||||
from .interface import FizzbarInterface
 | 
			
		||||
from .. import BASE_ROUTE
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def make_fizzbar(id: int = 123, name: str = 'Test fizzbar',
 | 
			
		||||
                 purpose: str = 'Test purpose') -> Fizzbar:
 | 
			
		||||
    return Fizzbar(
 | 
			
		||||
        fizzbar_id=id, name=name, purpose=purpose
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class TestFizzbarResource:
 | 
			
		||||
    @patch.object(FizzbarService, 'get_all',
 | 
			
		||||
                  lambda: [make_fizzbar(123, name='Test Fizzbar 1'),
 | 
			
		||||
                           make_fizzbar(456, name='Test Fizzbar 2')])
 | 
			
		||||
    def test_get(self, client: FlaskClient):  # noqa
 | 
			
		||||
        with client:
 | 
			
		||||
            results = client.get(f'/api/{BASE_ROUTE}/fizzbar',
 | 
			
		||||
                                 follow_redirects=True).get_json()
 | 
			
		||||
            expected = FizzbarSchema(many=True).dump(
 | 
			
		||||
                [make_fizzbar(123, name='Test Fizzbar 1'),
 | 
			
		||||
                 make_fizzbar(456, name='Test Fizzbar 2')]
 | 
			
		||||
            ).data
 | 
			
		||||
            for r in results:
 | 
			
		||||
                assert r in expected
 | 
			
		||||
 | 
			
		||||
    @patch.object(FizzbarService, 'create',
 | 
			
		||||
                  lambda create_request: Fizzbar(**create_request))
 | 
			
		||||
    def test_post(self, client: FlaskClient):  # noqa
 | 
			
		||||
        with client:
 | 
			
		||||
 | 
			
		||||
            payload = dict(name='Test fizzbar', purpose='Test purpose')
 | 
			
		||||
            result = client.post(f'/api/{BASE_ROUTE}/fizzbar/', json=payload).get_json()
 | 
			
		||||
            expected = FizzbarSchema().dump(Fizzbar(
 | 
			
		||||
                name=payload['name'],
 | 
			
		||||
                purpose=payload['purpose'],
 | 
			
		||||
            )).data
 | 
			
		||||
            assert result == expected
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def fake_update(fizzbar: Fizzbar, changes: FizzbarInterface) -> Fizzbar:
 | 
			
		||||
    # To fake an update, just return a new object
 | 
			
		||||
    updated_Fizzbar = Fizzbar(fizzbar_id=fizzbar.fizzbar_id,
 | 
			
		||||
                              name=changes['name'],
 | 
			
		||||
                              purpose=changes['purpose'])
 | 
			
		||||
    return updated_Fizzbar
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class TestFizzbarIdResource:
 | 
			
		||||
    @patch.object(FizzbarService, 'get_by_id',
 | 
			
		||||
                  lambda id: make_fizzbar(id=id))
 | 
			
		||||
    def test_get(self, client: FlaskClient):  # noqa
 | 
			
		||||
        with client:
 | 
			
		||||
            result = client.get(f'/api/{BASE_ROUTE}/fizzbar/123').get_json()
 | 
			
		||||
            expected = Fizzbar(fizzbar_id=123)
 | 
			
		||||
            assert result['fizzbarId'] == expected.fizzbar_id
 | 
			
		||||
 | 
			
		||||
    @patch.object(FizzbarService, 'delete_by_id',
 | 
			
		||||
                  lambda id: [id])
 | 
			
		||||
    def test_delete(self, client: FlaskClient):  # noqa
 | 
			
		||||
        with client:
 | 
			
		||||
            result = client.delete(f'/api/{BASE_ROUTE}/fizzbar/123').get_json()
 | 
			
		||||
            expected = dict(status='Success', id=[123])
 | 
			
		||||
            assert result == expected
 | 
			
		||||
 | 
			
		||||
    @patch.object(FizzbarService, 'get_by_id',
 | 
			
		||||
                  lambda id: make_fizzbar(id=id))
 | 
			
		||||
    @patch.object(FizzbarService, 'update', fake_update)
 | 
			
		||||
    def test_put(self, client: FlaskClient):  # noqa
 | 
			
		||||
        with client:
 | 
			
		||||
            result = client.put(f'/api/{BASE_ROUTE}/fizzbar/123',
 | 
			
		||||
                                json={'name': 'New Fizzbar',
 | 
			
		||||
                                      'purpose': 'New purpose'}).get_json()
 | 
			
		||||
            expected = FizzbarSchema().dump(
 | 
			
		||||
                Fizzbar(fizzbar_id=123, name='New Fizzbar', purpose='New purpose')).data
 | 
			
		||||
            assert result == expected
 | 
			
		||||
							
								
								
									
										7
									
								
								app/fizz/fizzbar/interface.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								app/fizz/fizzbar/interface.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,7 @@
 | 
			
		||||
from mypy_extensions import TypedDict
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class FizzbarInterface(TypedDict, total=False):
 | 
			
		||||
    fizzbar_id: int
 | 
			
		||||
    name: str
 | 
			
		||||
    purpose: str
 | 
			
		||||
							
								
								
									
										19
									
								
								app/fizz/fizzbar/model.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								app/fizz/fizzbar/model.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,19 @@
 | 
			
		||||
from sqlalchemy import Integer, Column, String
 | 
			
		||||
from app import db  # noqa
 | 
			
		||||
from .interface import FizzbarInterface
 | 
			
		||||
from typing import Any
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Fizzbar(db.Model):  # type: ignore
 | 
			
		||||
    '''A snazzy Fizzbar'''
 | 
			
		||||
 | 
			
		||||
    __tablename__ = 'fizzbar'
 | 
			
		||||
 | 
			
		||||
    fizzbar_id = Column(Integer(), primary_key=True)
 | 
			
		||||
    name = Column(String(255))
 | 
			
		||||
    purpose = Column(String(255))
 | 
			
		||||
 | 
			
		||||
    def update(self, changes: FizzbarInterface):
 | 
			
		||||
        for key, val in changes.items():
 | 
			
		||||
            setattr(self, key, val)
 | 
			
		||||
        return self
 | 
			
		||||
							
								
								
									
										22
									
								
								app/fizz/fizzbar/model_test.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								app/fizz/fizzbar/model_test.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,22 @@
 | 
			
		||||
from pytest import fixture
 | 
			
		||||
from flask_sqlalchemy import SQLAlchemy
 | 
			
		||||
from app.test.fixtures import app, db  # noqa
 | 
			
		||||
from .model import Fizzbar
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@fixture
 | 
			
		||||
def fizzbar() -> Fizzbar:
 | 
			
		||||
    return Fizzbar(
 | 
			
		||||
        fizzbar_id=1, name='Test fizzbar', purpose='Test purpose'
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_Fizzbar_create(fizzbar: Fizzbar):
 | 
			
		||||
    assert fizzbar
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_Fizzbar_retrieve(fizzbar: Fizzbar, db: SQLAlchemy):  # noqa
 | 
			
		||||
    db.session.add(fizzbar)
 | 
			
		||||
    db.session.commit()
 | 
			
		||||
    s = Fizzbar.query.first()
 | 
			
		||||
    assert s.__dict__ == fizzbar.__dict__
 | 
			
		||||
							
								
								
									
										9
									
								
								app/fizz/fizzbar/schema.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								app/fizz/fizzbar/schema.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,9 @@
 | 
			
		||||
from marshmallow import fields, Schema
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class FizzbarSchema(Schema):
 | 
			
		||||
    '''Fizzbar schema'''
 | 
			
		||||
 | 
			
		||||
    fizzbarId = fields.Number(attribute='fizzbar_id')
 | 
			
		||||
    name = fields.String(attribute='name')
 | 
			
		||||
    purpose = fields.String(attribute='purpose')
 | 
			
		||||
							
								
								
									
										0
									
								
								app/fizz/fizzbar/schema_test.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								app/fizz/fizzbar/schema_test.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										41
									
								
								app/fizz/fizzbar/service.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								app/fizz/fizzbar/service.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,41 @@
 | 
			
		||||
from app import db
 | 
			
		||||
from typing import List
 | 
			
		||||
from .model import Fizzbar
 | 
			
		||||
from .interface import FizzbarInterface
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class FizzbarService():
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def get_all() -> List[Fizzbar]:
 | 
			
		||||
        return Fizzbar.query.all()
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def get_by_id(fizzbar_id: int) -> Fizzbar:
 | 
			
		||||
        return Fizzbar.query.get(fizzbar_id)
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def update(fizzbar: Fizzbar, Fizzbar_change_updates: FizzbarInterface) -> Fizzbar:
 | 
			
		||||
        fizzbar.update(Fizzbar_change_updates)
 | 
			
		||||
        db.session.commit()
 | 
			
		||||
        return fizzbar
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def delete_by_id(fizzbar_id: int) -> List[int]:
 | 
			
		||||
        fizzbar = Fizzbar.query.filter(Fizzbar.fizzbar_id == fizzbar_id).first()
 | 
			
		||||
        if not fizzbar:
 | 
			
		||||
            return []
 | 
			
		||||
        db.session.delete(fizzbar)
 | 
			
		||||
        db.session.commit()
 | 
			
		||||
        return [fizzbar_id]
 | 
			
		||||
 | 
			
		||||
    @staticmethod
 | 
			
		||||
    def create(new_attrs: FizzbarInterface) -> Fizzbar:
 | 
			
		||||
        new_fizzbar = Fizzbar(
 | 
			
		||||
            name=new_attrs['name'],
 | 
			
		||||
            purpose=new_attrs['purpose']
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        db.session.add(new_fizzbar)
 | 
			
		||||
        db.session.commit()
 | 
			
		||||
 | 
			
		||||
        return new_fizzbar
 | 
			
		||||
							
								
								
									
										60
									
								
								app/fizz/fizzbar/service_test.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								app/fizz/fizzbar/service_test.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,60 @@
 | 
			
		||||
from flask_sqlalchemy import SQLAlchemy
 | 
			
		||||
from typing import List
 | 
			
		||||
from app.test.fixtures import app, db  # noqa
 | 
			
		||||
from .model import Fizzbar
 | 
			
		||||
from .service import FizzbarService  # noqa
 | 
			
		||||
from .interface import FizzbarInterface
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_get_all(db: SQLAlchemy):  # noqa
 | 
			
		||||
    yin: Fizzbar = Fizzbar(fizzbar_id=1, name='Yin', purpose='thing 1')
 | 
			
		||||
    yang: Fizzbar = Fizzbar(fizzbar_id=2, name='Yang', purpose='thing 2')
 | 
			
		||||
    db.session.add(yin)
 | 
			
		||||
    db.session.add(yang)
 | 
			
		||||
    db.session.commit()
 | 
			
		||||
 | 
			
		||||
    results: List[Fizzbar] = FizzbarService.get_all()
 | 
			
		||||
 | 
			
		||||
    assert len(results) == 2
 | 
			
		||||
    assert yin in results and yang in results
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_update(db: SQLAlchemy):  # noqa
 | 
			
		||||
    yin: Fizzbar = Fizzbar(fizzbar_id=1, name='Yin', purpose='thing 1')
 | 
			
		||||
 | 
			
		||||
    db.session.add(yin)
 | 
			
		||||
    db.session.commit()
 | 
			
		||||
    updates: FizzbarInterface = dict(name='New Fizzbar name')
 | 
			
		||||
 | 
			
		||||
    FizzbarService.update(yin, updates)
 | 
			
		||||
 | 
			
		||||
    result: Fizzbar = Fizzbar.query.get(yin.fizzbar_id)
 | 
			
		||||
    assert result.name == 'New Fizzbar name'
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_delete_by_id(db: SQLAlchemy):  # noqa
 | 
			
		||||
    yin: Fizzbar = Fizzbar(fizzbar_id=1, name='Yin', purpose='thing 1')
 | 
			
		||||
    yang: Fizzbar = Fizzbar(fizzbar_id=2, name='Yang', purpose='thing 2')
 | 
			
		||||
    db.session.add(yin)
 | 
			
		||||
    db.session.add(yang)
 | 
			
		||||
    db.session.commit()
 | 
			
		||||
 | 
			
		||||
    FizzbarService.delete_by_id(1)
 | 
			
		||||
    db.session.commit()
 | 
			
		||||
 | 
			
		||||
    results: List[Fizzbar] = Fizzbar.query.all()
 | 
			
		||||
 | 
			
		||||
    assert len(results) == 1
 | 
			
		||||
    assert yin not in results and yang in results
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_create(db: SQLAlchemy):  # noqa
 | 
			
		||||
 | 
			
		||||
    yin: FizzbarInterface = dict(name='Fancy new fizzbar', purpose='Fancy new purpose')
 | 
			
		||||
    FizzbarService.create(yin)
 | 
			
		||||
    results: List[Fizzbar] = Fizzbar.query.all()
 | 
			
		||||
 | 
			
		||||
    assert len(results) == 1
 | 
			
		||||
 | 
			
		||||
    for k in yin.keys():
 | 
			
		||||
        assert getattr(results[0], k) == yin[k]
 | 
			
		||||
		Reference in New Issue
	
	Block a user