Esempio n. 1
0
    def test_filter_transactions_by_isbn(self):
        with self.app.app_context():
            self.basic_setup()
            self.add_transactions_user1()  # has book with isbn = 1
            self.add_transactions_user2()  # has book with isbn = 1

            res = self.client.get(
                "/api/allTransactions",
                data={'isbn': self.book.isbn},
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(200, res.status_code)

            # the transactions with the books with isbn 1 are transactions 1 and 2
            ids_transactions = [1, 2]
            transactions = TransactionsModel.query.filter(
                TransactionsModel.id_transaction.in_(ids_transactions))
            expected_transactions = TransactionsModel.group_transactions_by_id(
                transactions)
            for i, transactions in enumerate(expected_transactions):
                for j, transaction in enumerate(transactions):
                    self.assertEqual(
                        transaction,
                        json.loads(res.data)['transactions'][i][j])
Esempio n. 2
0
    def test_filter_transactions_by_user_id_and_date_desc(self):
        with self.app.app_context():
            self.basic_setup()
            self.add_transactions_user1()
            self.add_transactions_user2()

            res = self.client.get(
                "/api/allTransactions",
                data={
                    'user_id': self.user2.id,
                    'date': 'desc'
                },
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(200, res.status_code)

            transactions = TransactionsModel.query.filter_by(
                user_id=self.user2.id).order_by(desc('date')).all()
            expected_transactions = TransactionsModel.group_transactions_by_id(
                transactions)
            for i, transactions in enumerate(expected_transactions):
                for j, transaction in enumerate(transactions):
                    self.assertEqual(
                        transaction,
                        json.loads(res.data)['transactions'][i][j])
Esempio n. 3
0
 def get(self, email):
     with lock:
         user = UsersModel.find_by_email(email)
         if user is None:
             return {"message": "User with ['email': " + email + "] Not Found"}, 404
         if g.user != user:
             return {"message": "Invalid user, can only be yourself"}, 401
         transactions = TransactionsModel.query.filter_by(user_id=user.id).all()
         grouped_transactions = TransactionsModel.group_transactions_by_id(transactions)
         return {'transactions': grouped_transactions}, 200
Esempio n. 4
0
    def get(self):
        with lock:
            parser = reqparse.RequestParser(bundle_errors=True)

            parser.add_argument('isbn', type=int, required=False,
                                help="In this field goes the isbn of the transactions")
            # parser.add_argument('titulo', type=str, required=False,
            # help="In this field goes the tittle of the book")
            parser.add_argument('user_id', type=int, required=False,
                                help="In this field goes the user_id of the transactions")
            parser.add_argument('date', type=str, required=False,
                                help="In this field goes the date order (asc, desc) of the transactions")
            data = parser.parse_args()

            transactions = TransactionsModel.query
            if not any(v is not None for k, v in data.items()):  # no filter asked
                grouped_transactions = TransactionsModel.group_transactions_by_id(transactions)
                return {'transactions': grouped_transactions}, 200
            else:
                if data['isbn'] is not None:
                    # Get transactions with isbn = isbn
                    transactions_containing_isbn = transactions.filter_by(isbn=data['isbn'])
                    # Ids of the transactions that have the isbn
                    ids_transactions = [t.id_transaction for t in transactions_containing_isbn]

                    # All transactions with the ids
                    transactions = TransactionsModel.query.filter(
                        TransactionsModel.id_transaction.in_(ids_transactions))
                if data['user_id'] is not None:
                    transactions = transactions.filter_by(user_id=data['user_id'])
                if data['date'] == str('asc'):
                    transactions = transactions.order_by(asc('date'))
                elif data['date'] == str('desc'):
                    transactions = transactions.order_by(desc('date'))

            grouped_transactions = TransactionsModel.group_transactions_by_id(transactions)
            return {'transactions': grouped_transactions}, 200
Esempio n. 5
0
    def test_get_all_transactions_no_filter(self):
        with self.app.app_context():
            self.basic_setup()
            self.add_transactions_user1()
            self.add_transactions_user2()

            res = self.client.get(
                "/api/allTransactions",
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(200, res.status_code)

            transactions = TransactionsModel.query.all()
            expected_transactions = TransactionsModel.group_transactions_by_id(
                transactions)
            for i, transactions in enumerate(expected_transactions):
                for j, transaction in enumerate(transactions):
                    self.assertEqual(
                        transaction,
                        json.loads(res.data)['transactions'][i][j])