def create_new_restaurant(form: RestaurantForm, user_id: int, max_sit: int): """ This method contains all logic save inside the a new restaurant :return: """ restaurant = Restaurant() form.populate_obj(restaurant) restaurant.owner_id = user_id restaurant.likes = 0 restaurant.covid_measures = form.covid_measures.data db.session.add(restaurant) db.session.commit() for i in range(int(form.n_tables.data)): new_table = RestaurantTable() new_table.restaurant_id = restaurant.id new_table.max_seats = max_sit new_table.available = True new_table.name = "" db.session.add(new_table) db.session.commit() # inserimento orari di apertura days = form.open_days.data for i in range(len(days)): new_opening = OpeningHours() new_opening.restaurant_id = restaurant.id new_opening.week_day = int(days[i]) new_opening.open_lunch = form.open_lunch.data new_opening.close_lunch = form.close_lunch.data new_opening.open_dinner = form.open_dinner.data new_opening.close_dinner = form.close_dinner.data db.session.add(new_opening) db.session.commit() # inserimento tipi di cucina cuisin_type = form.cuisine.data for i in range(len(cuisin_type)): new_cuisine = Menu() new_cuisine.restaurant_id = restaurant.id new_cuisine.cusine = cuisin_type[i] new_cuisine.description = "" db.session.add(new_cuisine) db.session.commit() return restaurant
def create_app(): app = Flask(__name__) app.config['WTF_CSRF_SECRET_KEY'] = 'A SECRET KEY' app.config['SECRET_KEY'] = 'ANOTHER ONE' app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///gooutsafe.db' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False for bp in blueprints: app.register_blueprint(bp) bp.app = app db.init_app(app) login_manager.init_app(app) db.create_all(app=app) # create a first admin user with app.app_context(): q = db.session.query(User).filter(User.email == '*****@*****.**') user = q.first() if user is None: example = User() example.firstname = 'Admin' example.lastname = 'Admin' example.email = '*****@*****.**' example.dateofbirth = datetime.datetime(2020, 10, 5) example.is_admin = True example.set_password('admin') db.session.add(example) db.session.commit() q = db.session.query(Restaurant).filter(Restaurant.id == 1) restaurant = q.first() if restaurant is None: example = Restaurant() example.name = 'Trial Restaurant' example.likes = 42 example.phone = 555123456 example.lat = 43.720586 example.lon = 10.408347 db.session.add(example) db.session.commit() return app
def test_component_edit_restaurant(test_app): app, test_client = test_app fields = { 'phone': '345', 'dishes-0-dish_name': 'pizza', 'dishes-0-price': 4, 'dishes-0-ingredients': 'pomodoro' } unc_fields = { 'phone': '', 'dishes-0-dish_name': 'pizza', 'dishes-0-price': 4, 'dishes-0-ingredients': 'pomodoro' } assert test_client.get('/edit_restaurant_informations', follow_redirects=True).status_code == 403 insert_ha(db, app) assert user_login_EP(test_client, email='*****@*****.**', password='******').status_code == 200 assert test_client.get('/edit_restaurant_informations', follow_redirects=True).status_code == 403 assert test_client.get('/logout', follow_redirects=True).status_code == 200 assert create_user_EP(test_client, email='*****@*****.**', password='******', role='owner').status_code == 200 # --- COMPONENTS TESTS --- #---------------------------------------------------------- building the starting conditions # log the user assert user_login_EP(test_client, email='*****@*****.**', password='******').status_code == 200 # create a restaurant by the logged user correct_restaurant = { 'name': 'Trial01-EP', 'lat': 22, 'lon': 22, 'phone': '3346734121', 'cuisine_type': [Restaurant.CUISINE_TYPES(1)], 'prec_measures': 'leggeX', 'avg_time_of_stay': 30, 'tables-0-table_name': 'yellow', 'tables-0-capacity': 5, 'dishes-0-dish_name': 'pizza', 'dishes-0-price': 4, 'dishes-0-ingredients': 'pomodoro', 'workingdays-0-day': WorkingDay.WEEK_DAYS(1), 'workingdays-0-work_shifts': "('12:00','15:00'),('19:00','23:00')" } # try owner with no restaurants assert test_client.get('/edit_restaurant_informations', follow_redirects=True).status_code == 403 assert create_restaurant_EP(test_client, correct_restaurant).status_code == 200 with app.app_context(): user = db.session.query(User).filter( User.email == '*****@*****.**').first() assert user is not None restaurant = db.session.query(Restaurant).filter_by( owner_id=user.id).first() assert restaurant is not None # try owner get with success assert test_client.get('/edit_restaurant_informations', follow_redirects=True).status_code == 200 # get with success assert test_client.get('/edit_restaurant_informations/' + str(restaurant.id), follow_redirects=True).status_code == 200 # try with wrong id assert test_client.post('/edit_restaurant_informations/' + str(10), data=fields, follow_redirects=True).status_code == 404 # post with invalid form assert test_client.post('/edit_restaurant_informations/' + str(restaurant.id), data=unc_fields, follow_redirects=True).status_code == 400 # post with success assert test_client.post('/edit_restaurant_informations/' + str(restaurant.id), data=fields, follow_redirects=True).status_code == 200 # try to post with ha assert test_client.get('/logout', follow_redirects=True).status_code == 200 assert user_login_EP(test_client, email='*****@*****.**', password='******').status_code == 200 assert test_client.post('/edit_restaurant_informations/' + str(restaurant.id), data=fields, follow_redirects=True).status_code == 403 assert test_client.get('/logout', follow_redirects=True).status_code == 200 # try without logged user assert test_client.post('/edit_restaurant_informations/' + str(restaurant.id), data=fields, follow_redirects=True).status_code == 403
def test_unit_edit_restaurant(test_app): app, test_client = test_app # --- UNIT TESTS --- with app.app_context(): #---------------------------------------------------------- building the starting conditions # create a user assert create_user_EP(test_client, email='*****@*****.**', password='******', role='owner').status_code == 200 user_test = db.session.query(User).filter( User.email == '*****@*****.**').first() assert user_test is not None user_test_id = user_test.id #create a restaurant body_restaurant = dict(owner_id=user_test.id, name='Trial', lat=22, lon=22, phone='3346734121', cuisine_type=[Restaurant.CUISINE_TYPES(1)], capacity=10, prec_measures='leggeX', avg_time_of_stay=30, tot_reviews=5, avg_rating=5, likes=4) restaurant = Restaurant(**body_restaurant) db.session.add(restaurant) db.session.commit() #test if the restaurant was created restaurant_to_check = db.session.query(Restaurant).filter( Restaurant.id == restaurant.id).first() assert restaurant_to_check is not None check_restaurants(restaurant_to_check, restaurant) fields = { 'phone': '345', 'dishes-0-dish_name': 'pizza', 'dishes-0-price': 4, 'dishes-0-ingredients': 'pomodoro' } assert user_login_EP(test_client, email='*****@*****.**', password='******').status_code == 200 assert test_client.post('/edit_restaurant_informations/' + str(restaurant.id), data=fields, follow_redirects=True).status_code == 200 assert restaurant.phone == '345' dish = db.session.query(Dish).filter_by( restaurant_id=restaurant.id).first() assert dish.dish_name == 'pizza' assert dish.price == 4 assert dish.ingredients == 'pomodoro'
def test_insert_working_day(test_app): app, test_client = test_app # --- UNIT TESTS --- with app.app_context(): # create a user and a restaurant to testing working_day insertions assert create_user_EP( test_client, email='*****@*****.**').status_code == 200 user_test = db.session.query(User).filter( User.email == '*****@*****.**').first() assert user_test is not None restaurant_dict = dict(owner_id=user_test.id, name='Trial Restaurant', lat=22, lon=22, phone='3346734121', cuisine_type=[Restaurant.CUISINE_TYPES(1)], capacity=10, prec_measures='leggeX', avg_time_of_stay=30) restaurant = Restaurant(**restaurant_dict) db.session.add(restaurant) db.session.commit() restaurant = db.session.query(Restaurant).first() assert restaurant is not None # incorrect mandatory fields with validators incorrect_working_days = [ dict(restaurant_id=None, day=WorkingDay.WEEK_DAYS(1), work_shifts=[('12:00', '15:00'), ('19:00', '23:00')]), dict(restaurant_id=0, day=WorkingDay.WEEK_DAYS(1), work_shifts=[('12:00', '15:00'), ('19:00', '23:00')]), dict(restaurant_id=-1, day=WorkingDay.WEEK_DAYS(1), work_shifts=[('12:00', '15:00'), ('19:00', '23:00')]), dict(restaurant_id=restaurant.id, day=None, work_shifts=[('12:00', '15:00'), ('19:00', '23:00')]), dict(restaurant_id=restaurant.id, day=0, work_shifts=[('12:00', '15:00'), ('19:00', '23:00')]), dict(restaurant_id=restaurant.id, day='ciao', work_shifts=[('12:00', '15:00'), ('19:00', '23:00')]), dict(restaurant_id=restaurant.id, day=WorkingDay.WEEK_DAYS(1), work_shifts=None), dict(restaurant_id=restaurant.id, day=WorkingDay.WEEK_DAYS(1), work_shifts=[]), dict(restaurant_id=restaurant.id, day=WorkingDay.WEEK_DAYS(1), work_shifts=[1, 2]), dict(restaurant_id=restaurant.id, day=WorkingDay.WEEK_DAYS(1), work_shifts=['ei']), dict(restaurant_id=restaurant.id, day=WorkingDay.WEEK_DAYS(1), work_shifts=('12:00', '15:00')), dict(restaurant_id=restaurant.id, day=WorkingDay.WEEK_DAYS(1), work_shifts=[('12', '15:00'), ('19:00', '23:00')]), dict(restaurant_id=restaurant.id, day=WorkingDay.WEEK_DAYS(1), work_shifts=[('ciao', '15:00'), ('19:00', '23:00')]), dict(restaurant_id=restaurant.id, day=WorkingDay.WEEK_DAYS(1), work_shifts=[(1, '15:00'), ('19:00', '23:00')]), dict(restaurant_id=restaurant.id, day=WorkingDay.WEEK_DAYS(1), work_shifts=[('12:00', '15:00', '17:00'), ('19:00', '23:00')]), dict(restaurant_id=restaurant.id, day=WorkingDay.WEEK_DAYS(1), work_shifts=[('12:00'), ('19:00', '23:00')]), dict(restaurant_id=restaurant.id, day=WorkingDay.WEEK_DAYS(1), work_shifts=[('19:00', '18:00')]), dict(restaurant_id=restaurant.id, day=WorkingDay.WEEK_DAYS(2), work_shifts=[('8:00', '10:00'), ('12:00', '15:00'), ('19:00', '23:00')]) ] count_assert = 0 for w in incorrect_working_days: try: working_day = WorkingDay(**w) except ValueError: count_assert += 1 assert True assert len(incorrect_working_days) == count_assert # missing fields incorrect_working_days = [ dict(day=WorkingDay.WEEK_DAYS(1), work_shifts=[('12:00', '15:00'), ('19:00', '23:00')]), dict(restaurant_id=restaurant.id, work_shifts=[('12:00', '15:00'), ('19:00', '23:00')]), dict(restaurant_id=restaurant.id, day=WorkingDay.WEEK_DAYS(1)) ] count_assert = 0 for w in incorrect_working_days: working_day = WorkingDay(**w) try: db.session.add(working_day) db.session.commit() except (exc.IntegrityError, exc.InvalidRequestError): db.session.rollback() count_assert += 1 assert True assert len(incorrect_working_days) == count_assert # correct working_days correct_working_days = [ dict(restaurant_id=restaurant.id, day=WorkingDay.WEEK_DAYS(1), work_shifts=[('19:00', '23:00')]), dict(restaurant_id=restaurant.id, day=WorkingDay.WEEK_DAYS(2), work_shifts=[('12:00', '15:00'), ('19:00', '23:00')]), ] for w in correct_working_days: working_day = WorkingDay(**w) db.session.add(working_day) db.session.commit() working_day_to_check = db.session.query(WorkingDay).filter( WorkingDay.restaurant_id == working_day.restaurant_id).filter( WorkingDay.day == working_day.day).first() assert working_day_to_check is not None check_working_days(working_day_to_check, working_day) # the insertion of the same day for the same restaurant must fail w = dict(restaurant_id=restaurant.id, day=WorkingDay.WEEK_DAYS(1), work_shifts=[('19:00', '23:00')]) working_day = WorkingDay(**w) count_assert = 0 try: db.session.add(working_day) db.session.commit() except (exc.IntegrityError, exc.InvalidRequestError): db.session.rollback() count_assert += 1 assert True assert count_assert == 1 # check total working_days working_days = db.session.query(WorkingDay).all() assert len(working_days) == len(correct_working_days)
def create_app(tests=False): app = Flask(__name__) app.config["WTF_CSRF_SECRET_KEY"] = "A SECRET KEY" app.config["SECRET_KEY"] = "ANOTHER ONE" if tests is False: app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///gooutsafe.db" else: app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///tests/gooutsafe.db" app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False for bp in blueprints: app.register_blueprint(bp) bp.app = app db.init_app(app) login_manager.init_app(app) db.create_all(app=app) # create a first admin user with app.app_context(): # create the user roles q = db.session.query(Role).filter(Role.id == 1) role = q.first() if role is None: role = Role() role.value = "ADMIN" role.label = "Admin role" db.session.add(role) role = Role() role.value = "OPERATOR" role.label = "Operator role" db.session.add(role) role = Role() role.value = "CUSTOMER" role.label = "Customer role" db.session.add(role) role = Role() role.value = "HEALTH" role.label = "Health role" db.session.add(role) db.session.commit() # an Admin user q = db.session.query(User).filter(User.email == "*****@*****.**") user = q.first() if user is None: admin_user = User() admin_user.firstname = "Admin" admin_user.lastname = "Admin" admin_user.email = "*****@*****.**" admin_user.phone = "3334455678" admin_user.dateofbirth = datetime.datetime(2020, 10, 5) admin_user.is_admin = True admin_user.set_password("admin") admin_user.role_id = 1 db.session.add(admin_user) db.session.commit() # an operator q = db.session.query(User).filter(User.email == "*****@*****.**") user = q.first() if user is None: first_operator = User() first_operator.firstname = "Ham" first_operator.lastname = "Burger" first_operator.email = "*****@*****.**" first_operator.phone = "222333567" first_operator.is_admin = False first_operator.set_password("operator") first_operator.role_id = 2 db.session.add(first_operator) db.session.commit() # a customer q = db.session.query(User).filter(User.email == "*****@*****.**") user = q.first() if user is None: first_customer = User() first_customer.firstname = "John" first_customer.lastname = "Doe" first_customer.email = "*****@*****.**" first_customer.phone = "111234765" first_customer.is_admin = False first_customer.set_password("customer") first_customer.role_id = 3 db.session.add(first_customer) db.session.commit() # health autority q = db.session.query(User).filter( User.email == "*****@*****.**") user = q.first() if user is None: health_authority = User() health_authority.firstname = "Health" health_authority.lastname = "Authority" health_authority.email = "*****@*****.**" health_authority.phone = "321456783" health_authority.is_admin = False health_authority.set_password("nocovid") health_authority.role_id = 4 db.session.add(health_authority) db.session.commit() # a restaurant q = db.session.query(Restaurant).filter(Restaurant.id == 1) restaurant = q.first() if restaurant is None: # load the first operator q = db.session.query(User).filter( User.email == "*****@*****.**") user = q.first() first_restaurant = Restaurant() first_restaurant.name = "Trial Restaurant" first_restaurant.likes = 42 first_restaurant.phone = 555123456 first_restaurant.covid_measures = "Distance between tables 2mt; Menù touch; Alcohol Gel; Only Electronic Payment" first_restaurant.lat = 43.720586 first_restaurant.lon = 10.408347 first_restaurant.owner_id = user.id db.session.add(first_restaurant) db.session.commit() # a table q = db.session.query(RestaurantTable).filter(RestaurantTable.id == 1) table = q.first() if table is None: # insert the first table q = db.session.query(Restaurant).filter( Restaurant.name == "Trial Restaurant") restaurant = q.first() first_table = RestaurantTable() first_table.restaurant_id = restaurant.id first_table.name = "Table 1" first_table.max_seats = 6 first_table.available = True db.session.add(first_table) db.session.commit() # another table q = db.session.query(RestaurantTable).filter(RestaurantTable.id == 2) table = q.first() if table is None: # insert the first table q = db.session.query(Restaurant).filter( Restaurant.name == "Trial Restaurant") restaurant = q.first() second_table = RestaurantTable() second_table.restaurant_id = restaurant.id second_table.name = "Table 2" second_table.max_seats = 4 second_table.available = True db.session.add(second_table) db.session.commit() # insert some opening hours q = (db.session.query(OpeningHours).filter( OpeningHours.restaurant_id == 1).filter( OpeningHours.week_day == 0)) openinghour = q.first() if openinghour is None: q = db.session.query(Restaurant).filter( Restaurant.name == "Trial Restaurant") restaurant = q.first() first_opening_hours = OpeningHours() first_opening_hours.restaurant_id = restaurant.id first_opening_hours.week_day = 0 first_opening_hours.open_lunch = datetime.time(hour=12) first_opening_hours.close_lunch = datetime.time(hour=15) first_opening_hours.open_dinner = datetime.time(hour=20) first_opening_hours.close_dinner = datetime.time(hour=22) db.session.add(first_opening_hours) db.session.commit() # insert some opening hours q = (db.session.query(OpeningHours).filter( OpeningHours.restaurant_id == 1).filter( OpeningHours.week_day == 2)) openinghour = q.first() if openinghour is None: q = db.session.query(Restaurant).filter( Restaurant.name == "Trial Restaurant") restaurant = q.first() second_opening_hours = OpeningHours() second_opening_hours.restaurant_id = restaurant.id second_opening_hours.week_day = 2 second_opening_hours.open_lunch = datetime.time(hour=12) second_opening_hours.close_lunch = datetime.time(hour=15) second_opening_hours.open_dinner = datetime.time(hour=20) second_opening_hours.close_dinner = datetime.time(hour=22) db.session.add(second_opening_hours) db.session.commit() # insert some opening hours q = (db.session.query(OpeningHours).filter( OpeningHours.restaurant_id == 1).filter( OpeningHours.week_day == 4)) openinghour = q.first() if openinghour is None: q = db.session.query(Restaurant).filter( Restaurant.name == "Trial Restaurant") restaurant = q.first() third_opening_hours = OpeningHours() third_opening_hours.restaurant_id = restaurant.id third_opening_hours.week_day = 4 third_opening_hours.open_lunch = datetime.time(hour=12) third_opening_hours.close_lunch = datetime.time(hour=15) third_opening_hours.open_dinner = datetime.time(hour=20) third_opening_hours.close_dinner = datetime.time(hour=22) db.session.add(third_opening_hours) db.session.commit() # a reservation q = db.session.query(Reservation).filter(Reservation.id == 1) reservation = q.first() if reservation is None: # insert the first table q = db.session.query(User).filter( User.email == "*****@*****.**") customer = q.first() q = db.session.query(RestaurantTable).filter( RestaurantTable.id == 1) table = q.first() q = db.session.query(Restaurant).filter(Restaurant.id == 1) restaurant = q.first() first_reservation = Reservation() first_reservation.reservation_date = datetime.datetime(2020, 10, 28, hour=12) first_reservation.reservation_end = ( first_reservation.reservation_date + datetime.timedelta(minutes=restaurant.avg_time)) first_reservation.customer_id = customer.id first_reservation.table_id = table.id first_reservation.people_number = 2 db.session.add(first_reservation) db.session.commit() # insert a review q = db.session.query(Review).filter_by(id=1).first() if q is None: review = Review() q = db.session.query(Restaurant).filter( Restaurant.name == "Trial Restaurant") restaurant = q.first() q = db.session.query(User).filter( User.email == "*****@*****.**") user = q.first() review.restaurant_id = restaurant.id review.reviewer_id = user.id review.review = "ciao" review.stars = decimal.Decimal(4.5) db.session.add(review) db.session.commit() # CALCULATE_RATING_RESTAURANTS return app
def test_insert_dish(test_app): app, test_client = test_app # --- UNIT TESTS --- with app.app_context(): # create a user and a restaurant to testing dish insertions assert create_user_EP(test_client, email='*****@*****.**').status_code == 200 user_test = db.session.query(User).filter(User.email == '*****@*****.**').first() assert user_test is not None restaurant_dict = dict( owner_id = user_test.id, name = 'Trial Restaurant', lat = 22, lon = 22, phone = '3346734121', cuisine_type = [Restaurant.CUISINE_TYPES(1)], capacity = 10, prec_measures = 'leggeX', avg_time_of_stay = 30 ) restaurant = Restaurant(**restaurant_dict) db.session.add(restaurant) db.session.commit() restaurant = db.session.query(Restaurant).first() assert restaurant is not None # incorrect fields with validators incorrect_dishes = [ dict(restaurant_id = None, dish_name = 'pizza', price = 4.0, ingredients = 'pomodoro, mozzarella'), dict(restaurant_id = 0, dish_name = 'pizza', price = 4.0, ingredients = 'pomodoro, mozzarella'), dict(restaurant_id = -1, dish_name = 'pizza', price = 4.0, ingredients = 'pomodoro, mozzarella'), dict(restaurant_id = restaurant.id, dish_name = None, price = 4.0, ingredients = 'pomodoro, mozzarella'), dict(restaurant_id = restaurant.id, dish_name = '', price = 4.0, ingredients = 'pomodoro, mozzarella'), dict(restaurant_id = restaurant.id, dish_name = 'pizza', price = None, ingredients = 'pomodoro, mozzarella'), dict(restaurant_id = restaurant.id, dish_name = 'pizza', price = 0, ingredients = 'pomodoro, mozzarella'), dict(restaurant_id = restaurant.id, dish_name = 'pizza', price = -1, ingredients = 'pomodoro, mozzarella'), dict(restaurant_id = restaurant.id, dish_name = 'pizza', price = 4.0, ingredients = None), dict(restaurant_id = restaurant.id, dish_name = 'pizza', price = 4.0, ingredients = '') ] count_assert = 0 for d in incorrect_dishes: try: dish = Dish(**d) except ValueError: count_assert += 1 assert True assert len(incorrect_dishes) == count_assert # missing mandatory fields incorrect_dishes = [ dict(dish_name = 'pizza', price = 4.0, ingredients = 'pomodoro, mozzarella'), dict(restaurant_id = restaurant.id, price = 4.0, ingredients = 'pomodoro, mozzarella'), dict(restaurant_id = restaurant.id, dish_name = 'pizza', ingredients = 'pomodoro, mozzarella'), dict(restaurant_id = restaurant.id, dish_name = 'pizza', price = 4.0) ] count_assert = 0 for d in incorrect_dishes: dish = Dish(**d) try: db.session.add(dish) db.session.commit() except (exc.IntegrityError, exc.InvalidRequestError): db.session.rollback() count_assert += 1 assert True assert len(incorrect_dishes) == count_assert # correct dishes correct_dishes = [ dict(restaurant_id = restaurant.id, dish_name = 'pizza', price = 4.0, ingredients = 'pomodoro, mozzarella'), dict(restaurant_id = restaurant.id, dish_name = 'p', price = 0.1, ingredients = 'p') ] for d in correct_dishes: dish = Dish(**d) db.session.add(dish) db.session.commit() dish_to_check = db.session.query(Dish).filter(Dish.id == dish.id).first() assert dish_to_check is not None check_dishes(dish_to_check, dish) # check total dishes dishes = db.session.query(Dish).all() assert len(dishes) == len(correct_dishes)
def test_insert_table(test_app): app, test_client = test_app # --- UNIT TESTS --- with app.app_context(): # create a user and a restaurant to testing table insertions assert create_user_EP(test_client, email='*****@*****.**').status_code == 200 user_test = db.session.query(User).filter(User.email == '*****@*****.**').first() assert user_test is not None restaurant_dict = dict( owner_id = user_test.id, name = 'Trial Restaurant', lat = 22, lon = 22, phone = '3346734121', cuisine_type = [Restaurant.CUISINE_TYPES(1)], capacity = 10, prec_measures = 'leggeX', avg_time_of_stay = 30 ) restaurant = Restaurant(**restaurant_dict) db.session.add(restaurant) db.session.commit() restaurant = db.session.query(Restaurant).first() assert restaurant is not None # incorrect mandatory fields with validators incorrect_tables = [ dict(restaurant_id = None, capacity = 1, table_name = 'table'), dict(restaurant_id = 0, capacity = 1, table_name = 'table'), dict(restaurant_id = -1, capacity = 1, table_name = 'table'), dict(restaurant_id = restaurant.id, capacity = None, table_name = 'table'), dict(restaurant_id = restaurant.id, capacity = 0, table_name = 'table'), dict(restaurant_id = restaurant.id, capacity = -1, table_name = 'table'), dict(restaurant_id = restaurant.id, capacity = 1, table_name = None), dict(restaurant_id = restaurant.id, capacity = 1, table_name = '') ] count_assert = 0 for t in incorrect_tables: try: table = Table(**t) except ValueError: count_assert += 1 assert True assert len(incorrect_tables) == count_assert # missing fields incorrect_tables = [ dict(capacity = 1, table_name = 'table'), dict(restaurant_id = restaurant.id, table_name = 'table'), dict(restaurant_id = restaurant.id, capacity = 1) ] count_assert = 0 for t in incorrect_tables: table = Table(**t) try: db.session.add(table) db.session.commit() except (exc.IntegrityError, exc.InvalidRequestError): db.session.rollback() count_assert += 1 assert True assert len(incorrect_tables) == count_assert # correct tables correct_tables = [ dict(restaurant_id = restaurant.id, capacity = 1, table_name = 'c'), dict(restaurant_id = restaurant.id, capacity = 30, table_name = 'big table'), ] for t in correct_tables: table = Table(**t) db.session.add(table) db.session.commit() table_to_check = db.session.query(Table).filter(Table.id == table.id).first() assert table_to_check is not None check_tables(table_to_check, table) # check total tables tables = db.session.query(Table).all() assert len(tables) == len(correct_tables)
return test_client.post('/edit_user_informations', data=data, follow_redirects=True) # --- UTILITIES RESTAURANT --- restaurant_example = [{ 'name': 'Restaurant 1', 'lat': 43.7216621, 'lon': 10.4083723, 'phone': '111111', 'cuisine_type': [Restaurant.CUISINE_TYPES(1), Restaurant.CUISINE_TYPES(6)], 'prec_measures': 'leggeX', 'avg_time_of_stay': 15, 'tables-0-table_name': 'res1red', 'tables-0-capacity': 2, 'dishes-0-dish_name': 'pizza', 'dishes-0-price': 4, 'dishes-0-ingredients': 'pomodoro, mozzarella',
def create_restaurant(): if current_user is not None and hasattr(current_user, 'id'): if (current_user.role == 'customer' or current_user.role == 'ha'): return make_response( render_template( 'error.html', message= "You are not a restaurant owner! Redirecting to home page", redirect_url="/"), 403) form = RestaurantForm() if request.method == 'POST': if form.validate_on_submit(): # if one or more fields that must not be present are must_not_be_present = [ 'owner_id', 'capacity', 'tot_reviews', 'avg_rating', 'likes' ] if any(k in must_not_be_present for k in request.form): return make_response( render_template('create_restaurant.html', form=RestaurantForm()), 400) working_days_to_add = [] tables_to_add = [] dishes_to_add = [] new_restaurant = Restaurant() # check that all restaurant/working days/tables/dishes fields are correct try: working_days_to_add = _check_working_days( form.workingdays.data) del form.workingdays tables_to_add, tot_capacity = _check_tables( form.tables.data) del form.tables dishes_to_add = _check_dishes(form.dishes.data) del form.dishes form.populate_obj(new_restaurant) new_restaurant.owner_id = current_user.id new_restaurant.capacity = tot_capacity except: return make_response( render_template('create_restaurant.html', form=RestaurantForm()), 400) db.session.add(new_restaurant) db.session.commit() # database check when insert the tables and dishes for l in [working_days_to_add, tables_to_add, dishes_to_add]: for el in l: el.restaurant_id = new_restaurant.id db.session.add(el) db.session.commit() return redirect('/') else: # invalid form return make_response( render_template('create_restaurant.html', form=form), 400) return render_template('create_restaurant.html', form=form) else: return make_response( render_template( 'error.html', message="You are not logged! Redirecting to login page", redirect_url="/login"), 403)
def test_compute_like_count(test_app): app, test_client = test_app with app.app_context(): user_test = add_user("*****@*****.**", '3333333333', "firstname", "lastname", "passwo", datetime.now(), 'customer') user_test1 = add_user("*****@*****.**", '3333333333', "firstname", "lastname", "passwo", datetime.now(), 'customer') user_test2 = add_user("*****@*****.**", '3333333333', "firstname", "lastname", "passwo", datetime.now(), 'customer') owner_test = add_user("*****@*****.**", '3333333333', "firstname", "lastname", "passwo", datetime.now(), 'owner') owner_test1 = add_user("*****@*****.**", '3333333333', "firstname", "lastname", "passwo", datetime.now(), 'owner') restaurants = [ dict(owner_id=owner_test.id, name='ciccio', lat=22, lon=22, phone='3346734121', cuisine_type=[Restaurant.CUISINE_TYPES(1)], capacity=10, prec_measures='leggeX', avg_time_of_stay=30), dict(owner_id=owner_test1.id, name='pluto', lat=22, lon=22, phone='3346734121', cuisine_type=[ Restaurant.CUISINE_TYPES(1), Restaurant.CUISINE_TYPES(2) ], capacity=1, prec_measures='', avg_time_of_stay=15, tot_reviews=None, avg_rating=None, likes=None) ] for r in restaurants: restaurant = Restaurant(**r) db.session.add(restaurant) db.session.commit() ciccio_restaurant = db.session.query(Restaurant).filter_by( name="ciccio").first() pluto_restaurant = db.session.query(Restaurant).filter_by( name="pluto").first() def putLike(user_id, restaurant_id): new_like = Like() new_like.liker_id = user_id new_like.restaurant_id = restaurant_id db.session.add(new_like) db.session.commit() assert (ciccio_restaurant.likes is not None) assert (pluto_restaurant.likes is not None) assert (ciccio_restaurant.likes == 0) assert (pluto_restaurant.likes == 0) putLike(user_test.id, ciccio_restaurant.id) compute_like_count() assert (ciccio_restaurant.likes == 1) assert (pluto_restaurant.likes == 0) putLike(user_test.id, pluto_restaurant.id) putLike(user_test1.id, pluto_restaurant.id) putLike(user_test2.id, pluto_restaurant.id) compute_like_count() pluto_restaurant = db.session.query(Restaurant).filter_by( id=pluto_restaurant.id).first() ciccio_restaurant = db.session.query(Restaurant).filter_by( id=ciccio_restaurant.id).first() assert (ciccio_restaurant.likes == 1) assert (pluto_restaurant.likes == 3) compute_like_count() putLike(user_test1.id, ciccio_restaurant.id) putLike(user_test2.id, ciccio_restaurant.id) compute_like_count() pluto_restaurant = db.session.query(Restaurant).filter_by( id=pluto_restaurant.id).first() ciccio_restaurant = db.session.query(Restaurant).filter_by( id=ciccio_restaurant.id).first() assert (ciccio_restaurant.likes == 3) assert (pluto_restaurant.likes == 3)