Exemplo n.º 1
0
    def test_search_restaurant_by_key_ok_partial_name(self):
        """
        This test unit test the service to perform the search by keyword of the restaurants
        on persistence
        """
        form = RestaurantForm()
        form.name.data = "rest_mock_{}".format(randrange(10000))
        form.phone.data = "096321343{}".format(randrange(10000))
        form.lat.data = 12
        form.lon.data = 12
        form.n_tables.data = 50
        form.covid_measures.data = "Random comment {}".format(randrange(10000))
        form.cuisine.data = ["Italian food"]
        form.open_days.data = ["0"]
        form.open_lunch.data = datetime.time(datetime(2020, 7, 1, 12, 00))
        form.close_lunch.data = datetime.time(datetime(2020, 7, 1, 12, 00))
        form.open_dinner.data = datetime.time(datetime(2020, 7, 1, 18, 00))
        form.close_dinner.data = datetime.time(datetime(2020, 6, 1, 22, 00))

        user = create_user_on_db(randrange(6000, 9000), role_id=2)
        assert user is not None
        assert user.role_id == 2

        restaurant = RestaurantServices.create_new_restaurant(
            form, user.id, 6, user.email)
        assert restaurant is not None
        rest_by_name = RestaurantServices.get_restaurants_by_keyword(
            name=restaurant.name)
        assert len(rest_by_name) is 1

        del_user_on_db(user.id)
        RestaurantServices.delete_restaurant(restaurant.id)
Exemplo n.º 2
0
    def test_create_restaurant(self):
        """
        test create user
        :return:
        """
        form = RestaurantForm()
        form.name.data = "rest_mock_{}".format(randrange(10000))
        form.phone.data = "096321343{}".format(randrange(10000))
        form.lat.data = 12
        form.lon.data = 12
        form.n_tables.data = 50
        form.covid_measures.data = "Random comment {}".format(randrange(10000))
        form.cuisine.data = ["Italian food"]
        form.open_days.data = ["0"]
        form.open_lunch.data = datetime.time(datetime(2020, 7, 1, 12, 00))
        form.close_lunch.data = datetime.time(datetime(2020, 7, 1, 12, 00))
        form.open_dinner.data = datetime.time(datetime(2020, 7, 1, 18, 00))
        form.close_dinner.data = datetime.time(datetime(2020, 6, 1, 22, 00))

        user = create_user_on_db(randrange(1, 60000), role_id=2)
        assert user is not None
        assert user.role_id == 2

        restaurant = RestaurantServices.create_new_restaurant(
            form, user.id, 6, user.email)
        assert restaurant is not None

        ## This call should be delete also the restaurants
        del_user_on_db(user.id)
        RestaurantServices.delete_restaurant(restaurant.id)
Exemplo n.º 3
0
    def test_restaurant_name(self):
        """
        check the function that return the restaurant name
        """
        form = RestaurantForm()
        form.name.data = "rest_mock_{}".format(randrange(10000))
        form.phone.data = "096321343{}".format(randrange(10000))
        form.lat.data = 12
        form.lon.data = 12
        form.n_tables.data = 50
        form.covid_measures.data = "Random comment {}".format(randrange(10000))
        form.cuisine.data = ["Italian food"]
        form.open_days.data = ["0"]
        form.open_lunch.data = datetime.time(datetime(2020, 7, 1, 12, 00))
        form.close_lunch.data = datetime.time(datetime(2020, 7, 1, 12, 00))
        form.open_dinner.data = datetime.time(datetime(2020, 7, 1, 18, 00))
        form.close_dinner.data = datetime.time(datetime(2020, 6, 1, 22, 00))

        user = create_user_on_db(randrange(10, 50000), role_id=2)
        assert user is not None
        assert user.role_id == 2

        restaurant = RestaurantServices.create_new_restaurant(
            form, user, 6, user.email)
        assert restaurant is not None

        name = RestaurantServices.get_restaurant_name(restaurant.id)
        assert restaurant.name == name

        ## This call should be delete also the restaurants
        # At this point also the review should be killed with the restaurants
        del_user_on_db(user.id)
        RestaurantServices.delete_restaurant(restaurant.id)
Exemplo n.º 4
0
 def test_all_restaurant(self):
     """
     test about the services restaurant to test the result of all restaurants
     :return:
     """
     all_restauirants = RestaurantServices.get_all_restaurants()
     assert len(all_restauirants) > 0
Exemplo n.º 5
0
    def test_get_restaurant_people_none(self):
        """
        The method test the function inside the RestaurantServices to search all the people
        inside the restaurants, the function return an array that looks like
        [people_to_lunch, people_to_dinner, people_checkin]

        Test flow
        - new restaurants
        - get all people
        - del restaurant
        """
        owner_one = create_user_on_db(randrange(10, 50000), role_id=2)
        assert owner_one is not None

        restaurant_one = create_restaurants_on_db(name="First",
                                                  user_id=owner_one.id,
                                                  user_email=owner_one.email)
        assert restaurant_one is not None

        all_people = RestaurantServices.get_restaurant_people(
            restaurant_one.id)
        assert all_people is not None
        assert len(all_people) == 3
        assert all_people[0] == 0
        assert all_people[1] == 0
        assert all_people[2] == 0
        del_restaurant_on_db(restaurant_one.id)
        del_user_on_db(owner_one.id)
Exemplo n.º 6
0
    def test_checkin_reservation(self):
        """
        test mark checked in a reservation by operator
        """
        owner = create_user_on_db(randrange(1, 500000), role_id=2)
        assert owner is not None

        rest = create_restaurants_on_db(user_id=owner.id,
                                        user_email=owner.email)
        assert rest is not None

        user = create_user_on_db(randrange(1, 500000))
        assert user is not None

        date_time = datetime(2022, 10, 28, 21, 30)

        reservation = create_random_booking(1, rest.id, user, date_time,
                                            "*****@*****.**")

        assert reservation is not None

        response = RestaurantServices.checkin_reservations(reservation["id"])
        assert response is not None

        del_booking(reservation["id"], user.id)
        del_user_on_db(user.id)
        del_user_on_db(owner.id)
        del_restaurant_on_db(rest.id)
Exemplo n.º 7
0
    def test_delete_dish_menu(self, client):
        """
        check if dish get deletedS
        """

        user = create_user_on_db(randrange(10, 50000), role_id=2)
        assert user is not None

        dish = DishModel()
        dish.name = "Pearà"
        dish.price = 5.50
        dish.restaurant_id = 1
        dish = RestaurantServices.insert_dish(dish)
        assert dish is not None

        response = RestaurantServices.delete_dish(dish["id"])
        print(response)
        assert response is not None
Exemplo n.º 8
0
    def test_three_reviews(self):
        """
        check the three reviews fetcher
        """
        form = RestaurantForm()
        form.name.data = "rest_mock_{}".format(randrange(10000))
        form.phone.data = "096321343{}".format(randrange(10000))
        form.lat.data = 12
        form.lon.data = 12
        form.n_tables.data = 50
        form.covid_measures.data = "Random comment {}".format(randrange(10000))
        form.cuisine.data = ["Italian food"]
        form.open_days.data = ["0"]
        form.open_lunch.data = datetime.time(datetime(2020, 7, 1, 12, 00))
        form.close_lunch.data = datetime.time(datetime(2020, 7, 1, 12, 00))
        form.open_dinner.data = datetime.time(datetime(2020, 7, 1, 18, 00))
        form.close_dinner.data = datetime.time(datetime(2020, 6, 1, 22, 00))

        user = create_user_on_db(randrange(10, 50000), role_id=2)
        assert user is not None
        assert user.role_id == 2

        restaurant = RestaurantServices.create_new_restaurant(
            form, user.id, 6, user.email)
        assert restaurant is not None

        reviewer = create_user_on_db(randrange(10, 50000), role_id=3)

        review1 = RestaurantServices.review_restaurant(restaurant.id,
                                                       reviewer.email, 5,
                                                       "test1")
        assert review1 is not None
        review2 = RestaurantServices.review_restaurant(restaurant.id,
                                                       reviewer.email, 4,
                                                       "test2")
        assert review2 is not None
        review3 = RestaurantServices.review_restaurant(restaurant.id,
                                                       reviewer.email, 3,
                                                       "test3")
        assert review3 is not None

        three_reviews = RestaurantServices.get_three_reviews(restaurant.id)
        assert three_reviews is not None
        assert len(three_reviews) == 3

        ## This call should be delete also the restaurants
        # At this point also the review should be killed with the restaurants
        del_user_on_db(user.id)
        del_user_on_db(reviewer.id)
        RestaurantServices.delete_restaurant(restaurant.id)
Exemplo n.º 9
0
 def delete_user(user_id: int = None):
     user = UserService.get_user_by_id(user_id=user_id)
     if user is None:
         return False
     with current_app.test_request_context():
         if user.role_id == 2 and "RESTAURANT_ID" in session:
             restaurant_id = session["RESTAURANT_ID"]
             response = RestaurantServices.delete_restaurant(
                 restaurant_id=restaurant_id)
             if response is False:
                 current_app.logger.debug("Impossible delete restaurant")
                 return False
     url = "{}/delete/{}".format(USER_MICROSERVICE_URL, str(user_id))
     response = HttpUtils.make_delete_request(url)
     if response is not None:
         return True
     else:
         return False
Exemplo n.º 10
0
    def test_reservation_local_ko_by_email(self):
        """
        This test cases, tru to test the logic inside the services to avoid
        stupid result

        http://localhost:5000/my_reservations?fromDate=2013-10-07&toDate=2014-10-07&[email protected]
        :return:
        """
        user = create_user_on_db(randrange(1, 500000), role_id=2)
        assert user is not None

        from_date = datetime(2013, 10, 7)
        to_date = datetime(2014, 10, 7)

        def_rest = RestaurantServices.get_all_restaurants()[0]
        assert def_rest is not None
        all_reservation = BookingServices.get_reservation_by_constraint(
            user.id,
            from_date,
            to_date,
            def_rest["id"],
        )
        assert len(all_reservation) == 0
Exemplo n.º 11
0
    def search_contacts(user_email: str, user_phone: str):
        if user_email == "" and user_phone == "":
            return "Insert an email or a phone number"
        response = UserService.search_possible_contacts(user_email, user_phone)

        if response is None:
            return "The customer not registered or not positive"

        contact_users_GUI = []
        # now we have the information about the positivity of the user in response
        date_marking = response["from_date"]
        user_id = response["user_id"]

        # start to check contacts
        # API: get all reservation of the customer between date_marking and date_marking -14
        date_marking = datetime.strptime(date_marking, "%Y-%m-%d")
        to_date = date_marking - timedelta(days=14)
        reservations_customer = BookingServices.get_reservation_by_constraint(
            user_id, from_data=to_date, to_data=date_marking)

        i = 1
        if reservations_customer is not None:
            all_reservations = BookingServices.get_reservation_by_constraint(
                from_data=to_date, to_data=date_marking)
            if all_reservations is None:
                return None

            for reservation in reservations_customer:
                restaurant_id = reservation["table"]["restaurant"]["id"]

                start = datetime.strptime(reservation["reservation_date"],
                                          "%Y-%m-%dT%H:%M:%SZ")
                end = datetime.strptime(reservation["reservation_end"],
                                        "%Y-%m-%dT%H:%M:%SZ")
                current_app.logger.debug(
                    "I'm working with reserv from {} to {}".format(start, end))
                for one_reservation in all_reservations:
                    restaurant_id_contact = one_reservation["table"][
                        "restaurant"]["id"]
                    if restaurant_id_contact != restaurant_id:
                        continue
                    start_contact = datetime.strptime(
                        one_reservation["reservation_date"],
                        "%Y-%m-%dT%H:%M:%SZ")
                    end_contact = datetime.strptime(
                        one_reservation["reservation_end"],
                        "%Y-%m-%dT%H:%M:%SZ")

                    # if people are in the same restaurant in the same day
                    if start.date() == start_contact.date():
                        openings = RestaurantServices.get_opening_hours_restaurant(
                            restaurant_id)

                        dayNumber = start.weekday()
                        restaurant_hours = []
                        current_app.logger.debug(
                            "I got openings. Start is {}".format(dayNumber))
                        for opening in openings:
                            if opening["week_day"] == dayNumber:
                                restaurant_hours.append(
                                    datetime.strptime(opening["open_lunch"],
                                                      "%H:%M"))
                                restaurant_hours.append(
                                    datetime.strptime(opening["close_lunch"],
                                                      "%H:%M"))
                                restaurant_hours.append(
                                    datetime.strptime(opening["open_dinner"],
                                                      "%H:%M"))
                                restaurant_hours.append(
                                    datetime.strptime(opening["close_dinner"],
                                                      "%H:%M"))

                        # if people are in the restaurant at lunch or dinner
                        if (restaurant_hours[0].time() <= start.time()
                                and restaurant_hours[1].time() >= end.time()
                            ) or (restaurant_hours[2].time() <= start.time()
                                  and
                                  restaurant_hours[3].time() >= end.time()):
                            # people are in the same restaurant at lunch
                            # if they are in the same time
                            if (end_contact < start
                                    or start_contact > end) is False:
                                # they are contacts!
                                # API: get user email and name of the contact
                                user = UserService.get_user_by_id(
                                    one_reservation["customer_id"])
                                if user is not None:
                                    contact_users_GUI.append([
                                        i,
                                        user.firstname + " " + user.lastname,
                                        user.dateofbirth,
                                        user.email,
                                        user.phone,
                                    ])
                                    i += 1
        return contact_users_GUI
Exemplo n.º 12
0
    def search_contacts_for_email(user_email: str, user_phone: str):

        if user_email == "" and user_phone == "":
            return "Insert an email or a phone number"

        friends = []
        contacts = []
        past_restaurants = []
        future_restaurants = []

        if user_email != "":
            URL = USER_MICROSERVICE_URL + "/positiveinfo/email/" + str(
                user_email)
        else:
            URL = USER_MICROSERVICE_URL + "/positiveinfo/phone/" + str(
                user_phone)

        # check if the user exists
        # check if the user is positive (get also date of marking) (API)
        response = HttpUtils.make_get_request(URL)
        if response is None:
            return "Error, please try again"

        # now we have the information about the positivity of the user in response
        date_marking = response["from_date"]
        user_id = response["user_id"]

        # start to check contacts
        # API: get all reservation of the customer between date_marking and date_marking -14
        date_marking = datetime.strptime(date_marking, "%Y-%m-%d")
        to_date = date_marking - timedelta(days=14)
        reservations_customer = BookingServices.get_reservation_by_constraint(
            user_id, from_data=to_date, to_data=date_marking)

        if reservations_customer is not None:

            # API: get all reservations between date_marking and date_m -14
            all_reservations = BookingServices.get_reservation_by_constraint(
                from_data=to_date, to_data=date_marking)
            for reservation in reservations_customer:
                restaurant_id = reservation["table"]["restaurant"]["id"]
                restaurant = RestaurantServices.get_rest_by_id(restaurant_id)
                if restaurant is None:
                    continue
                friends = friends + reservation["people"]
                start = datetime.strptime(reservation["reservation_date"],
                                          "%Y-%m-%dT%H:%M:%SZ")
                end = datetime.strptime(reservation["reservation_end"],
                                        "%Y-%m-%dT%H:%M:%SZ")
                past_restaurants.append({
                    "email":
                    restaurant.owner_email,
                    "name":
                    restaurant.name,
                    "date":
                    start.strftime("%Y-%m-%dT%H:%M:%SZ"),
                })
                for one_reservation in all_reservations:
                    restaurant_id_contact = one_reservation["table"][
                        "restaurant"]["id"]
                    if restaurant_id_contact != restaurant_id:
                        continue

                    start_contact = datetime.strptime(
                        one_reservation["reservation_date"],
                        "%Y-%m-%dT%H:%M:%SZ")
                    end_contact = datetime.strptime(
                        one_reservation["reservation_end"],
                        "%Y-%m-%dT%H:%M:%SZ")
                    # if people are in the same restaurant in the same day
                    if start.date() != start_contact.date():
                        continue
                    openings = RestaurantServices.get_opening_hours_restaurant(
                        restaurant_id)

                    dayNumber = start.weekday()
                    restaurant_hours = []
                    for opening in openings:
                        if opening["week_day"] == dayNumber:
                            restaurant_hours.append(
                                datetime.strptime(opening["open_lunch"],
                                                  "%H:%M"))
                            restaurant_hours.append(
                                datetime.strptime(opening["close_lunch"],
                                                  "%H:%M"))
                            restaurant_hours.append(
                                datetime.strptime(opening["open_dinner"],
                                                  "%H:%M"))
                            restaurant_hours.append(
                                datetime.strptime(opening["close_dinner"],
                                                  "%H:%M"))

                    # if people are in the restaurant at lunch or dinner
                    if (restaurant_hours[0].time() <= start.time()
                            and restaurant_hours[1].time() >= end.time()) or (
                                restaurant_hours[2].time() <= start.time()
                                and restaurant_hours[3].time() >= end.time()):
                        # people are in the same restaurant at lunch
                        # if they are in the same time
                        if not ((end_contact < start) or
                                (start_contact > end)):
                            # they are contacts!
                            # API: get user email and name of the contact
                            user = UserService.get_user_by_id(
                                one_reservation["customer_id"])
                            if user is not None:
                                contacts.append({
                                    "email":
                                    user.email,
                                    "name":
                                    user.firstname,
                                    "restaurant_name":
                                    restaurant.name,
                                    "date":
                                    start.strftime("%Y-%m-%dT%H:%M:%SZ"),
                                })
                                friends = friends + one_reservation["people"]
        if user_email != "":
            customer_email = user_email
        else:
            URL = USER_MICROSERVICE_URL + "/" + str(user_id)
            user = HttpUtils.make_get_request(URL)
            customer_email = user["email"]

        # API booking: get all future booking of the customer
        future_reservations = BookingServices.get_reservation_by_constraint(
            user_id, from_data=to_date, to_data=date_marking)
        for future_reservation in future_reservations:
            date = datetime.strptime(reservation["reservation_date"],
                                     "%Y-%m-%dT%H:%M:%SZ")
            restaurant_id = future_reservation["table"]["restaurant"]["id"]
            restaurant = RestaurantServices.get_rest_by_id(restaurant_id)
            if restaurant is not None:
                future_restaurants.append({
                    "email":
                    restaurant.owner_email,
                    "name":
                    restaurant.name,
                    "date":
                    date.strftime("%Y-%m-%dT%H:%M:%SZ"),
                    "customer_email":
                    customer_email,
                })
        return {
            "friends": friends,
            "contacts": contacts,
            "past_restaurants": past_restaurants,
            "reservation_restaurants": future_restaurants,
        }