Пример #1
0
    def test_order_mail(self):
        with self.app.app_context():
            self.basic_setup()
            book2 = BooksModel(2, 2, 13.1, "book2")
            book2.save_to_db()

            isbns = [self.book.isbn, book2.isbn]
            prices = [self.book.precio, book2.precio]
            quantities = [1, 1]
            dataTransaction = {
                "isbns": isbns,
                'prices': prices,
                'quantities': quantities,
                "email": self.user.email,
            }
            res = self.client.post(
                "/api/transaction",
                data=dataTransaction,
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(201, res.status_code)
            # hi ha dues transaccions amb id 1
            transactions = TransactionsModel.find_by_id(1)
            self.assertEqual(len(transactions), 2)

            # les dues transaccions equivalen als llibres que acabem de posar
            for i, isbn in enumerate(isbns):
                self.assertEqual(
                    TransactionsModel.find_by_id_and_isbn(1, isbn).json(),
                    json.loads(res.data)['transactions'][i])
Пример #2
0
 def get(self, id_transaction):
     with lock:
         transaction = TransactionsModel.find_by_id(id_transaction)
         if not transaction:
             return {"message": f"Transaction with ['id_transaction':{id_transaction}] not found"}, 404
         if UsersModel.find_by_id(transaction.user_id) != g.user:
             return {"message": "Invalid transaction, can only be yours"}, 401
     return {"transaction": transaction.json()}, 200
Пример #3
0
    def test_get_transactions_user(self):
        with self.app.app_context():
            self.basic_setup()
            book2 = BooksModel(2, 2, 13.1, "book2")
            book2.save_to_db()

            isbns = [self.book.isbn, book2.isbn]
            prices = [self.book.precio, book2.precio]
            quantities = [1, 1]
            dataTransaction = {
                "isbns": isbns,
                'prices': prices,
                'quantities': quantities,
                "email": self.user.email,
            }

            res = self.client.post(
                "/api/transaction",
                data=dataTransaction,
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(201, res.status_code)

            res = self.client.get(
                f"/api/transactions/{self.user.email}",
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(200, res.status_code)
            # l'usuari te dues transactions
            self.assertEqual(2, len(self.user.transactions))

            # comprovem que son les que hem acabat d'afegir
            expected = [
                transaction.json()
                for transaction in TransactionsModel.find_by_id(1)
            ]
            real_output = [
                transaction.json() for transaction in self.user.transactions
            ]
            self.assertEqual(expected, real_output)
Пример #4
0
    def test_get_best_sellers(self):
        with self.app.app_context():
            self.basic_setup()

            isbns = [self.book.isbn, self.book2.isbn]
            prices = [self.book.precio, self.book2.precio]
            quantities = [1, 50]
            dataTransaction = {
                "isbns": isbns,
                'prices': prices,
                'quantities': quantities,
                "email": self.user.email,
            }
            res = self.client.post(
                "/api/transaction",
                data=dataTransaction,
                headers={
                    "Authorization":
                    'Basic ' + base64.b64encode(
                        (self.token + ":").encode('ascii')).decode('ascii')
                })
            self.assertEqual(201, res.status_code)
            # hi ha dues transaccions amb id 1
            transactions = TransactionsModel.find_by_id(1)
            self.assertEqual(len(transactions), 2)

            args = {
                "numBooks": 2,
            }
            res = self.client.get('/api/trending', data=args)
            self.assertEqual(200, res.status_code)

            self.assertEqual(
                2, len(json.loads(res.data)
                       ['books']))  # veiem que n'hi ha dos com li hem demanat
            self.assertEqual(self.book2.isbn,
                             json.loads(res.data)['books'][0]
                             ['isbn'])  # el més venut és el llibre amb isbn 2
Пример #5
0
    def test_model_update(self):
        with self.app.app_context():
            self.basic_setup()

            TransactionsModel.save_transaction(self.user.id, [self.book.isbn],
                                               [self.book.precio], [1])
            self.assertEqual(1, len(TransactionsModel.query.all()))

            data = {"id_transaction": 10}  # id = 10
            TransactionsModel.query.filter_by(
                id_transaction=1).first().update_from_db(data)
            expected_output = {
                'date': dt.datetime.today().strftime("%d-%m-%Y"),
                'id_transaction': 10,
                'user_id': 1,
                'isbn': self.book.isbn,
                'price': self.book.precio,
                'book': self.book.json(),
                'quantity': 1
            }

            self.assertEqual(
                expected_output,
                TransactionsModel.find_by_id(data["id_transaction"])[0].json())