def test_find_by_availability(self): """ Find Payments by availability """ self._add_two_test_payments() payments = Payment.find_by_availability(True) self.assertIsNot(payments, None) self._assert_equal_test_payment_1(payments[0]) payments = Payment.find_by_availability(False) self.assertIsNot(payments, None) self._assert_equal_test_payment_2(payments[0])
def test_find_by_customer(self): """ Find Payments by customer id """ self._add_two_test_payments() payments = Payment.find_by_customer(1) self.assertIsNot(payments, None) self._assert_equal_test_payment_1(payments[0]) payments = Payment.find_by_customer(2) self.assertIsNot(payments, None) self._assert_equal_test_payment_2(payments[0])
def test_find_by_type(self): """ Find Payments by type""" self._add_two_test_payments() payments = Payment.find_by_type("credit card") self.assertIsNot(payments, None) self._assert_equal_test_payment_1(payments[0]) payments = Payment.find_by_type("paypal") self.assertIsNot(payments, None) self._assert_equal_test_payment_2(payments[0])
def test_serial(self): """ Convert a payment to JSON""" payment = Payment(order_id="1", customer_id="1", available=True, payments_type="credit card") payment_json = payment.serialize() self.assertEqual(payment_json['order_id'], "1") self.assertEqual(payment_json['customer_id'], "1") self.assertEqual(payment_json['available'], True) self.assertEqual(payment_json['payments_type'], "credit card")
def _add_two_test_payments(self): Payment( order_id="1", customer_id="1", available=True, type="credit card", info=self._test_credit_card_info).save() Payment( order_id="2", customer_id="2", available=False, type="paypal", info=self._test_paypal_info).save()
def test_deserial(self): """ Convert a JSON to payment object""" data = { "order_id": "1", "customer_id": "1", "available": True, "payments_type": "credit card" } payment = Payment() payment.deserialize(data) self.assertEqual(data['order_id'], payment.order_id) self.assertEqual(data['customer_id'], payment.customer_id) self.assertEqual(data['available'], payment.available) self.assertEqual(data['payments_type'], payment.payments_type)
def test_serialize(self): """ Convert a payment to JSON """ payment = Payment( order_id="1", customer_id="1", available=True, type="credit card", info=self._test_credit_card_info) payment_json = payment.serialize() self.assertEqual(payment_json['order_id'], "1") self.assertEqual(payment_json['customer_id'], "1") self.assertEqual(payment_json['available'], True) self.assertEqual(payment_json['type'], "credit card") self.assertEqual(payment_json['info'], self._test_credit_card_info)
def test_deserialize(self): """ Convert JSON to a payment object """ data = { "order_id": "1", "customer_id": "1", "available": True, "type": "credit card", "info": DUMMY } payment = Payment() payment.deserialize(data) self.assertEqual(data['order_id'], payment.order_id) self.assertEqual(data['customer_id'], payment.customer_id) self.assertEqual(data['available'], payment.available) self.assertEqual(data['type'], payment.type) self.assertEqual(data['info'], payment.info)
def list_payments(): """ Returns all of the Payments """ app.logger.info('Request for payments list') payments = [] customer_id = request.args.get('customer_id') order_id = request.args.get('order_id') if customer_id: app.logger.info('Request for payments list with customer_id : %s', customer_id) payments = Payment.find_by_customer(customer_id) elif order_id: app.logger.info('Request for payments list with order_id : %s', order_id) payments = Payment.find_by_order(order_id) else: payments = Payment.all() results = [payment.serialize() for payment in payments] return make_response(jsonify(results), status.HTTP_200_OK)
def test_delete_a_payment(self): """ Delete a Payment """ payment = Payment(order_id="1", customer_id="1", available=True, payments_type="credit card") payment.save() self.assertEqual(len(Payment.all()), 1) # delete the pet and make sure it isn't in the database payment.delete() self.assertEqual(len(Payment.all()), 0)
def delete_payment(payments_id): """ Delete a payment This endpoint will delete a Payment based the id specified in the path """ app.logger.info('Request to delete payment with id: %s', payments_id) payment = Payment.find(payments_id) if payment: payment.delete() return make_response('', status.HTTP_204_NO_CONTENT)
def delete(self, payment_id): """Delete a payment. This endpoint will delete a payment based the id specified in the path """ app.logger.info('Request to Delete a payment with id [%s]', payment_id) # pylint: disable=no-member payment = Payment.find(payment_id) if payment: payment.delete() return '', status.HTTP_204_NO_CONTENT
def get_payments(payments_id): """ Retrieve a single Payment This endpoint will return a payment based on it's id """ app.logger.info('Request for payment with id: %s', payments_id) payment = Payment.find(payments_id) if not payment: raise NotFound("Payment with id '{}' was not found.".format(payments_id)) return make_response(jsonify(payment.serialize()), status.HTTP_200_OK)
def test_create_a_payment(self): """ Create a payment and assert that it exists """ payment = Payment(order_id="1", customer_id="1", available=True, payments_type="credit card") self.assertTrue(payment is not None) self.assertEqual(payment.id, None) self.assertEqual(payment.order_id, "1") self.assertEqual(payment.customer_id, "1") self.assertEqual(payment.available, True) self.assertEqual(payment.payments_type, "credit card")
def toggle_payments_availability(payments_id): """ Toggle payment availability This toggles whether or not a payment is currently available """ app.logger.info('Request to toggle payment availability with id: %s', payments_id) payment = Payment.find(payments_id) if not payment: raise NotFound("Payment with id '{}' was not found.".format(payments_id)) payment.available = not payment.available payment.save() return make_response(jsonify(payment.serialize()), status.HTTP_200_OK)
def get(self, payment_id): """Retrieve a single payment. This endpoint will return a payment based on its id. """ app.logger.info( # pylint: disable=no-member "Request to Retrieve a payment with id [%s]", payment_id) payment = Payment.find(payment_id) if not payment: api.abort(status.HTTP_404_NOT_FOUND, "Payment with id '{}' was not found.".format(payment_id)) return payment.serialize(), status.HTTP_200_OK
def test_remove_all(self): """ Test dropping and recreating all tables in the database """ self._add_two_test_payments() self.assertEqual(len(Payment.all()), 2) Payment.disconnect() Payment.remove_all() self.assertEqual(len(Payment.all()), 0)
def test_find_by_customer(self): """ Find Payments by customer id""" Payment(order_id="1", customer_id="2", available=True, payments_type="credit card").save() Payment(order_id="3", customer_id="4", available=False, payments_type="paypal").save() payment_in_db = Payment.find_by_customer(2) self.assertIsNot(payment_in_db, None) self.assertEqual(payment_in_db[0].order_id, 1) self.assertEqual(payment_in_db[0].customer_id, 2) self.assertEqual(payment_in_db[0].available, True) self.assertEqual(payment_in_db[0].payments_type, "credit card") payment_in_db = Payment.find_by_customer(4) self.assertIsNot(payment_in_db, None) self.assertEqual(payment_in_db[0].order_id, 3) self.assertEqual(payment_in_db[0].customer_id, 4) self.assertEqual(payment_in_db[0].available, False) self.assertEqual(payment_in_db[0].payments_type, "paypal")
def get(self): """Returns all of the Payments.""" app.logger.info('Request to list Payments...') # pylint: disable=no-member args = payment_args.parse_args() customer_id = args['customer_id'] order_id = args['order_id'] available = args['available'] payment_type = args['type'] payments = Payment.find_by(customer_id, order_id, available, payment_type) results = [payment.serialize() for payment in payments] return results, status.HTTP_200_OK
def update_payments(payments_id): """ Update a Payment This endpoint will update a Payment based the body that is posted """ app.logger.info('Request to update payment with id: %s', payments_id) check_content_type('application/json') payment = Payment.find(payments_id) if not payment: raise NotFound("Payment with id '{}' was not found.".format(payments_id)) payment.deserialize(request.get_json()) payment.id = payments_id payment.save() return make_response(jsonify(payment.serialize()), status.HTTP_200_OK)
def patch(self, payments_id): """Toggle payment availability. This toggles whether or not a payment is currently available. """ app.logger.info( # pylint: disable=no-member 'Request to toggle payment availability with id: %s', payments_id) payment = Payment.find(payments_id) if not payment: api.abort( status.HTTP_404_NOT_FOUND, 'Payment with id [{}] was not found.'.format(payments_id)) payment.available = not payment.available payment.save() return payment.serialize(), status.HTTP_200_OK
def create_payments(): """ Creates a Payment This endpoint will create a Payment based the data in the body that is posted """ app.logger.info('Request to create a payments') check_content_type('application/json') payment = Payment() payment.deserialize(request.get_json()) payment.save() message = payment.serialize() location_url = url_for('get_payments', payments_id=payment.id, _external=True) return make_response(jsonify(message), status.HTTP_201_CREATED, { 'Location': location_url })
def put(self, payment_id): """Update a payment. This endpoint will update a payment with the body that is posted. """ app.logger.info('Request to Update a payment with id [%s]', payment_id) # pylint: disable=no-member check_content_type('application/json') payment = Payment.find(payment_id) if not payment: api.abort(status.HTTP_404_NOT_FOUND, "Payment with id '{}' was not found.".format(payment_id)) app.logger.debug('Payload = %s', api.payload) # pylint: disable=no-member data = api.payload # Still use jsonschema to validate data. jsonschema.validate(data, PAYMENT_SCHEMA) payment.deserialize(data) payment.id = payment_id payment.save() return payment.serialize(), status.HTTP_200_OK
def test_add_a_payment(self): """ Create a payment and add it to the database """ payments = Payment.all() self.assertEqual(payments, []) payment = Payment(order_id="1", customer_id="1", available=True, payments_type="credit card") self.assertTrue(payment is not None) self.assertEqual(payment.id, None) payment.save() # Asert that it was assigned an id and shows up in the database self.assertEqual(payment.id, 1) payments = Payment.all() self.assertEqual(len(payments), 1)
def test_find_a_payment(self): """ Find a payment by ID""" payment = Payment(order_id="1", customer_id="1", available=True, payments_type="credit card") payment.save() #adding extra row in case the find method return something randomly Payment(order_id="2", customer_id="2", available=False, payments_type="paypal").save() self.assertTrue(payment is not None) self.assertIsNot(payment.id, None) new_payment = Payment.find(payment.id) self.assertEqual(new_payment.id, payment.id)
def post(self): """Creates a payment. This endpoint will create a payment based on the data posted in the body. """ app.logger.info('Request to Create a Payment') # pylint: disable=no-member check_content_type('application/json') payment = Payment() app.logger.debug('Payload = %s', api.payload) # pylint: disable=no-member data = api.payload # Still use jsonschema to validate data. jsonschema.validate(data, PAYMENT_SCHEMA) payment.deserialize(data) payment.save() app.logger.info('Payment with new id [%s] saved!', payment.id) # pylint: disable=no-member location_url = api.url_for(PaymentResource, payment_id=payment.id, _external=True) return payment.serialize(), status.HTTP_201_CREATED, { 'Location': location_url }
def test_find_a_payment(self): """ Find a payment by ID """ saved_payment = Payment( order_id="1", customer_id="1", available=True, type="credit card", info=self._test_credit_card_info) saved_payment.save() # adding extra row in case the find method return something randomly Payment( order_id="2", customer_id="2", available=False, type="paypal", info=self._test_paypal_info).save() payment = Payment.find(saved_payment.id) self.assertIsNot(payment, None) self.assertEqual(payment.id, saved_payment.id) self.assertEqual(payment.order_id, saved_payment.order_id) self.assertEqual(payment.customer_id, saved_payment.customer_id) self.assertEqual(payment.available, saved_payment.available) self.assertEqual(payment.type, saved_payment.type) self.assertEqual(payment.info, saved_payment.info)
def init_db(): """ Initialies the SQLAlchemy app """ global app Payment.init_db(app)
def test_deserialize_with_type_error(self): """ Test deserialization with TypeError """ data = "this is not a dictionary" payment = Payment() self.assertRaises(DataValidationError, payment.deserialize, data)
def setUpClass(cls): """ These run once per Test suite """ app.debug = False # Set up the test database app.config['SQLALCHEMY_DATABASE_URI'] = DATABASE_URI Payment.init_db(app)