factoring appointment get and test

This commit is contained in:
vincent 2021-06-21 22:34:17 +02:00
parent caa98a0908
commit 5ea127e080
2 changed files with 79 additions and 54 deletions

View File

@ -1,7 +1,7 @@
from flask import request, jsonify
from pony.orm import ObjectNotFound
from datetime import datetime
from ITPlanning.db import Appointment
from datetime import datetime, timedelta
from ITPlanning.db import Appointment, Planning, Service, Ticket, Customer
from ITPlanning.api.exception import NotFound, GenericError
from . import api
@ -17,77 +17,47 @@ def datefiltering(query):
return query
def search_appointment(by=None, id=None):
rv = []
if by == None and id is not None:
rv.append(Appointment[id].to_dict())
else:
query = Appointment.select()
if by is not None:
query.filter(lambda a: getattr(a, by).id == id)
query = datefiltering(query)
for f in query:
rv.append(f.to_dict())
if len(rv) == 0:
raise NotFound("Appointment")
return jsonify(rv)
@api.route("/appointment")
@api.route("/appointment/")
@api.route("/appointment/<id>")
def get_appointment(id=None):
rv = None
if id:
rv = Appointment[id]
else:
query = Appointment.select()
query = datefiltering(query)
for f in query:
rv += query
if rv is None:
raise NotFound("Appointment")
return jsonify(rv)
return search_appointment(id=id)
@api.route("/appointment/planning/<id>")
def get_appointment_by_planning(id):
rv = None
if id is None:
raise GenericError("enter an ID")
query = Appointment.select(lambda a: a.planning.id == id)
query = datefiltering(query)
for f in query:
rv += f
if rv is None:
raise NotFound("Appointment")
return jsonify(rv)
def get_appointment_by_planning(id=None):
return search_appointment("planning", id)
@api.route("/appointment/service/<id>")
def get_appointment_by_service(id):
rv = None
if id is None:
raise GenericError("enter an ID")
query = Appointment.select(lambda a: a.service.id == id)
query = datefiltering(query)
for f in query:
rv += f
if rv is None:
raise NotFound("Appointment")
return jsonify(rv)
return search_appointment("service", id)
@api.route("/appointment/customer/<id>")
def get_appointment_by_customer(id):
rv = None
if id is None:
raise GenericError("enter an ID")
query = Appointment.select(lambda a: a.customer.id == id)
query = datefiltering(query)
for f in query:
rv += f
if rv is None:
raise NotFound("Appointment")
return jsonify(rv)
return search_appointment("customer", id)
@api.route("/appointment/ticket/<id>")
def get_appointment_by_ticket(id):
rv = None
if id is None:
raise GenericError("enter an ID")
query = Appointment.select(lambda a: a.ticket.id == id)
query = datefiltering(query)
for f in query:
rv += f
if rv is None:
raise NotFound("Appointment")
return jsonify(rv)
return search_appointment("ticket", id)
@api.route("/appointment", methods=["POST"])

View File

@ -8,3 +8,58 @@ class AppointmentTestCase(APITestBase):
def test_get_appointement(self):
self.make_request("appointment/", 404)
self.make_request("appointment/1", 404)
current_date = datetime.now()
with db_session:
service = filldatabase.create_service()
customer = filldatabase.create_Customer()
planning = filldatabase.create_planning()
planning.services.add(service)
end_datetime = current_date + timedelta(
minutes=service.service_category.duration
)
appointment = Appointment(
book_datetime=current_date,
start_datetime=current_date,
end_datetime=end_datetime,
is_unavaillable=False,
service=service,
planning=planning,
customer=customer,
)
start_datetime2 = current_date + timedelta(days=2)
end_datetime2 = start_datetime2 + timedelta(
minutes=service.service_category.duration
)
appointment2 = Appointment(
book_datetime=current_date,
start_datetime=start_datetime2,
end_datetime=end_datetime2,
is_unavaillable=False,
service=service,
planning=planning,
customer=customer,
)
req, data = self.make_request("appointment/", 200)
self.assertEqual(len(data), 2)
req, data = self.make_request("appointment/1", 200)
self.assertEqual(len(data), 1)
filteringdate = (current_date + timedelta(days=1)).strftime("%Y-%m-%dT%H%M")
req, data = self.make_request("appointment/", 200, {"after": filteringdate})
self.assertEqual(len(data), 1)
req, data = self.make_request("appointment/", 200, {"before": filteringdate})
self.assertEqual(len(data), 1)
req, data = self.make_request("appointment/planning/", 404)
req, data = self.make_request("appointment/planning/1", 200)
req, data = self.make_request("appointment/service/1", 200)
req, data = self.make_request("appointment/customer/1", 200)
req, data = self.make_request("appointment/ticket/1", 200)