def test_validate_tickets(self):
        customer = requests.post("http://localhost:8080/api/customers", params=self.customer1).json()
        show1 = data.create_show("Tony Carreira", "31/10/2014", 22.50, 100)
        product1 = data.create_product("Cafe Expresso", "coffee", 2.3)
        product2 = data.create_product("Pipocas", "popcorn", 5.3)
        payload = {
            "customerID": customer["id"],
            "showID": show1["id"],
            "pin": int(customer["pin"]),
            "quantity": 3
        }
        helpers.format_date(datetime.date.today())
        requests.post("http://localhost:8080/api/tickets", params=payload)

        # START

        # Get purchased tickets
        tickets = requests.get("http://localhost:8080/api/tickets", params={"customerID": customer["id"]}).json()

        tickets_ids = []
        for tick in tickets[:2]:
            tickets_ids.append(tick["id"])
        tickets_string = ",".join(tickets_ids)

        # Test that the tickets are valid
        validation_params = {"customerID": customer["id"], "tickets": tickets_string}
        answer = requests.get("http://localhost:8080/api/validateTickets", params=validation_params)
        self.assertEqual(answer.status_code, 200)

        # Test that the status changed to used
        tickets = requests.get("http://localhost:8080/api/tickets", params={"customerID": customer["id"]}).json()
        results = list(filter(lambda ticket: ticket["id"] in tickets_ids and ticket["status"] == "used", tickets))
        self.assertTrue(len(results) == 2)

        # Test that used tickets can't be validated again
        answer = requests.get("http://localhost:8080/api/validateTickets", params=validation_params)
        self.assertEqual(answer.status_code, 400)


        # END

        data.delete_tickets(show1["id"], customer["id"])
        data.delete_customer(customer["id"])
        data.delete_show(show1["id"])
        data.delete_product(product1["id"])
        data.delete_product(product2["id"])
        data.delete_transactions(customer["id"])
        data.delete_vouchers(customer["id"])
    def test_buy_tickets(self):
        customer = requests.post("http://localhost:8080/api/customers", params=self.customer1).json()
        show1 = data.create_show("Tony Carreira", "31/10/2014", 22.50, 100)
        product1 = data.create_product("Cafe Expresso", "coffee", 2.3)
        product2 = data.create_product("Pipocas", "popcorn", 5.3)
        payload = {
            "customerID" : customer["id"],
            "showID" : show1["id"],
            "pin" : int(customer["pin"]),
            "quantity" : 3
        }
        today_date = helpers.format_date(datetime.date.today())

        # Buy tickets valid
        answer = requests.post("http://localhost:8080/api/tickets", params=payload)
        self.assertEqual(answer.status_code, 200)

        #Buy tickets invalid
        payload["quantity"] = 99
        answer = requests.post("http://localhost:8080/api/tickets", params=payload)
        self.assertEqual(answer.status_code, 400)

        # Check if available tickets are updated
        answer = requests.get("http://localhost:8080/api/shows").json()
        results = list(filter(lambda show: show["name"] == "Tony Carreira" and show["price"] == 22.50
                                           and show["seats"] == 100 and show["date"] == "31/10/2014"
                                            and show["available"] == 97, answer))
        self.assertTrue(len(results) == 1)

        # Get purchased tickets
        tickets = requests.get("http://localhost:8080/api/tickets", params={"customerID": customer["id"]}).json()
        results = list(filter(lambda ticket: "id" in ticket and "seat" in ticket
                                             and ticket["status"] == "unused" and ticket["date"] == today_date
                                             and ticket["showID"] == show1["id"]
                                             and ticket["name"] == "Tony Carreira"
                                             and ticket["date_show"] == "31/10/2014", tickets))
        self.assertTrue(len(results) == 3)

        # Get vouchers
        vouchers = requests.get("http://localhost:8080/api/vouchers", params={"customerID": customer["id"]}).json()
        results = list(filter(lambda voucher: "id" in voucher and (voucher["product"] == "coffee" or voucher["product"] == "popcorn") and voucher["discount"] == 1 and voucher["status"] == "unused", vouchers))
        self.assertTrue(len(results) == 3)

        # Check that the transactions were recorded
        get_params = {"customerID": customer["id"]}
        transactions = requests.get("http://localhost:8080/api/transactions", params=get_params).json()
        results = list(filter(lambda transaction: transaction["date"] == today_date and transaction["amount"] == 67.5
                                                  and "3" in transaction["description"]
                                                  and "Tony Carreira" in transaction["description"], transactions))
        self.assertTrue(len(results) == 1)

        #Buy tickets for free discount vouchers
        payload["quantity"] = 5
        answer = requests.post("http://localhost:8080/api/tickets", params=payload)
        self.assertEqual(answer.status_code, 200)
        vouchers = requests.get("http://localhost:8080/api/vouchers", params={"customerID": customer["id"]}).json()
        results = list(filter(lambda voucher: "id" in voucher and (voucher["product"] == "all" or voucher["product"] == "popcorn" or voucher["product"] == "coffee")
                                              and (voucher["discount"] == 1 or voucher["discount"] == 0.05) and voucher["status"] == "unused", vouchers))
        self.assertTrue(len(vouchers) == 9)

        data.delete_tickets(show1["id"], customer["id"])
        data.delete_customer(customer["id"])
        data.delete_show(show1["id"])
        data.delete_product(product1["id"])
        data.delete_product(product2["id"])
        data.delete_transactions(customer["id"])
        data.delete_vouchers(customer["id"])