def test_get_invoices_should_return_invoices_with_page_parameter(self):
        query = 'select document, description, amount, reference_month, reference_year, is_active, created_at, ' \
                'deactive_at from invoice where is_active is true order by id limit 7 offset 7'
        database = Mock()
        connection = Mock()
        cursor = Mock()
        database.get_connection.return_value = connection
        connection.cursor.return_value = cursor
        invoice_repository = InvoiceRepository(database)
        params = {'page_size': 7, 'page_number': 2}

        invoice_repository.get_invoices(params)

        cursor.execute.assert_called_once_with(query)
        cursor.fetchall.assert_called_once()
    def test_delete_invoice_should_delete_invoice(self):
        query = 'update invoice set is_active = false, deactive_at = %s where document = %s'
        param = (ANY, '123')
        database = Mock()
        connection = Mock()
        cursor = Mock()
        database.get_connection.return_value = connection
        connection.cursor.return_value = cursor
        cursor.execute.return_value = None
        invoice_repository = InvoiceRepository(database)

        invoice_repository.delete_invoice('123')

        cursor.execute.assert_called_once_with(query, param)
        cursor.fetchall.assert_not_called()
    def test_count_invoice_should_return_count_found_invoices(self):
        expected = {'count': 1}
        database = Mock()
        connection = Mock()
        cursor = Mock()
        database.get_connection.return_value = connection
        connection.cursor.return_value = cursor
        cursor.execute.return_value = None
        cursor.fetchall.return_value = [{'count': 1}]
        invoice_repository = InvoiceRepository(database)
        params = {'param': 'value'}

        actual = invoice_repository.count_invoices(params)

        cursor.execute.assert_called_once()
        cursor.fetchall.assert_called_once()
        self.assertEqual(expected, actual)
    def test_save_invoice_should_save_invoice(self):
        query = "insert into invoice ( document, description, amount, reference_month, reference_year, is_active, " \
                "created_at ) values ('123', 'Descrição', '123.12', 4, 2020, True, '2020-04-01')"
        database = Mock()
        connection = Mock()
        cursor = Mock()
        database.get_connection.return_value = connection
        connection.cursor.return_value = cursor
        cursor.execute.return_value = None
        invoice_repository = InvoiceRepository(database)
        invoice = Invoice('123', 'Descrição', 123.12, 4, 2020, True,
                          '2020-04-01')

        invoice_repository.save_invoice(invoice)

        cursor.execute.assert_called_once_with(query)
        cursor.fetchall.assert_not_called()
    def test_get_invoice_should_return_invoice(self):
        query = 'select document, description, amount, reference_month, reference_year, is_active, created_at,' \
                'deactive_at from invoice where document = %s and is_active is true'
        param = ('123', )
        expected = [{'any': 'any'}]
        database = Mock()
        connection = Mock()
        cursor = Mock()
        database.get_connection.return_value = connection
        connection.cursor.return_value = cursor
        cursor.fetchall.return_value = [{'any': 'any'}]
        invoice_repository = InvoiceRepository(database)

        actual = invoice_repository.get_invoice('123')

        cursor.execute.assert_called_once_with(query, param)
        cursor.fetchall.assert_called_once()
        self.assertEqual(expected, actual)
Esempio n. 6
0
from flask import Flask, request
from flask_restful import Resource, Api, reqparse, marshal_with, fields

from invoice_app.exceptions.invoice import InvoiceNotFoundException, InvalidQueryParameterException
from invoice_app.extensions import database, configuration
from invoice_app.repositories.invoice import InvoiceRepository
from invoice_app.services.auth import requires_authentication
from invoice_app.services.invoice import InvoiceService

app = Flask(__name__)
configuration.init_app(app)
database.init_db(app)
api_v1 = Api(app, '/api/v1')

db = database.Database(app.config)
rep = InvoiceRepository(db)
invoice_service = InvoiceService(rep)


class InvoiceResource(Resource):
    params = reqparse.RequestParser()
    params.add_argument('document', type=str, required=True, help='Missing required parameter')
    params.add_argument('description', type=str, required=True, help='Missing required parameter')
    params.add_argument('amount', type=float, required=True, help='Missing required parameter')
    params.add_argument('reference_month', type=int, required=True, help='Missing required parameter')
    params.add_argument('reference_year', type=int, required=True, help='Missing required parameter')

    m_fields = {
        'document': fields.String,
        'description': fields.String,
        'amount': fields.Float,