Ejemplo n.º 1
0
    def test_write_room(self):
        # create an user
        user_name = "cris"
        user_email = "*****@*****.**"
        created_time = datetime.now()
        user_phone = "858-2867-3567"
        user_description = "cultured man"
        user_school_year = "Third"
        user_major = "Data Science"
        user_object = crud.add_user(user_name, user_email, created_time,
                                    user_phone, user_description,
                                    user_school_year, user_major, self.session)

        # the input json should be the same as output json from room_json except for id
        test_json = {
            'name': '75 Big Rock Cove St. Middletown',
            'address': '75 Big Rock Cove St. Middletown, NY 10940',
            'distance': '20 mins',
            'pricePerMonth': 500,
            'from_month': 'June/18',
            'to_month': 'July/18',
            'early_date': '06/01/18',
            'late_date': '06/12/18',
            'roomType': 'Single',
            'other': [],
            'facilities': [],
            'leaserName': 'cris',
            'leaserEmail': '*****@*****.**',
            'leaserPhone': '858-2867-3567',
            'leaserSchoolYear': 'Third',
            'leaserMajor': 'Data Science',
            'photos': ['photo1', 'photo2'],
            'profilePhoto': 'profile_photo',
            'negotiable': True,
            'numBaths': 2.5,
            'numBeds': 2,
            'roomDescription': 'dream house in a life time'
        }

        crud.write_room(test_json, self.session, True)

        room_object = crud.get_row_if_exists(Room, self.session, **{'id': 1})

        self.assertEqual(room_object is not None, True)

        result_json = crud.room_json(room_object, self.session, True)

        test_json['roomId'] = 1

        self.assertEqual(result_json == test_json, True)
Ejemplo n.º 2
0
def test_post_rooms(client, app, new_room_json, login_first, corrupt_cookie,
                    check_s3, correct_response, verify_database,
                    correct_status_code, post_content_type):
    if login_first:
        client.post("/login",
                    data=json.dumps({"email": "*****@*****.**"}),
                    content_type="application/json")
    if corrupt_cookie:
        # corrupt the client's cookie => to create you have to destroy
        cookie_map = {cookie.name: cookie for cookie in client.cookie_jar}
        cookie_map[
            "access_token"].value = "fake token to see whether the engineer is dumb"
    new_room_json['photos'] = []
    valid_photos_cnt = 0
    for p_file in new_room_json["photo_files"]:
        with open(p_file, 'rb') as f:
            file_object = io.BytesIO(f.read())
            new_room_json['photos'].append((file_object, f.name))
            valid_photos_cnt += 1 if verify_image(
                file_object) != "error" else 0
    # get room id inferred by # of rows, assuming no deletes happening for test sake
    room_id = get_insert_id(Room, app.config["DB_CONNECTION"])
    if check_s3:
        app.config["OFFLINE_TESTING"] = False
    rv = client.post("/postRoom",
                     data=new_room_json,
                     content_type=post_content_type)
    response_data = json.loads(rv.data)
    for key, value in correct_response.items():
        assert response_data[key] == value
    assert rv.status_code == correct_status_code
    room = get_row_if_exists(Room, app.config["DB_CONNECTION"],
                             **{"id": room_id})
    if verify_database:
        room_json_test = room_json(room, app.config["DB_CONNECTION"], True)
        for key, value in json.loads(new_room_json["json"]).items():
            assert room_json_test[key] == value
    else:
        assert room is None
    if check_s3:
        images_uploaded = get_images("test_user1",
                                     extra_path=str(room_id) + "/")
        assert len(images_uploaded) == valid_photos_cnt
        # delete test folder
        delete_folder("test_user1", "houseit")
Ejemplo n.º 3
0
    def test_room_json(self):
        # create an user
        user_name = "cris"
        user_email = "*****@*****.**"
        created_time = datetime.now()
        user_phone = "858-2867-3567"
        user_description = "cultured man"
        user_school_year = "Third"
        user_major = "Data Science"
        user_object = crud.add_user(
            user_name, 
            user_email,
            created_time, 
            user_phone,
            user_description, 
            user_school_year,
            user_major, 
            self.session)

        # create a move in
        early_date = datetime(2018, 6, 1)
        late_date = datetime(2018, 6, 12)
        move_in_object = crud.add_move_in(
            early_date, 
            late_date, 
            self.session)

        # create a stay period
        from_month = datetime(2018, 6, 1)
        to_month = datetime(2018, 7, 1)
        stay_period_object = crud.add_stay_period(
            from_month, 
            to_month, 
            self.session)

        # create an address
        address = "75 Big Rock Cove St.Middletown, NY 10940"
        distance = "20 mins"
        address_object = crud.add_address(
            distance, 
            address, 
            self.session)

        # create a room
        date_created = datetime.now()
        room_type = room_types[0]
        price = 500
        negotiable = True
        description = "dream house in a life time"
        no_rooms = 2
        no_bathrooms = 2
        room_object = crud.add_room(
            date_created, 
            room_type, 
            price,
            negotiable, 
            description,
            stay_period_object, 
            address_object,
            user_object, 
            move_in_object,
            no_rooms, 
            no_bathrooms,
            self.session)

        result_json = crud.room_json(
            room_object, 
            self.session, 
            True)

        self.assertEqual(result_json["name"] ==
                         "75 Big Rock Cove St.Middletown", True)
        self.assertEqual(result_json["location"] == address, True)
        self.assertEqual(result_json["pricePerMonth"] == price, True)
        self.assertEqual(result_json["from_month"] == "June/18", True)
        self.assertEqual(result_json["to_month"] == "July/18", True)
        self.assertEqual(result_json["early"] == "06/01/18", True)
        self.assertEqual(result_json["late"] == "06/12/18", True)
        self.assertEqual(result_json["roomType"] == room_type, True)
        self.assertEqual(result_json["other"] == [], True)
        self.assertEqual(result_json["facilities"] == [], True)
        self.assertEqual(result_json["leaserName"] == user_name, True)
        self.assertEqual(result_json["leaserEmail"] == user_email, True)
        self.assertEqual(result_json["leaserPhone"] == user_phone, True)
        self.assertEqual(
            result_json["leaserSchoolYear"] == user_school_year, True)
        self.assertEqual(result_json["leaserMajor"] == user_major, True)
        self.assertEqual(result_json["photos"] == ["photo1", "photo2"], True)
        self.assertEqual(result_json["profilePhoto"] == "profile_photo", True)
        self.assertEqual(result_json["roomId"] == room_object.id, True)
        self.assertEqual(result_json["negotiable"] == negotiable, True)
        self.assertEqual(result_json["numBaths"] == no_rooms, True)
        self.assertEqual(result_json["numBeds"] == no_bathrooms, True)
        self.assertEqual(result_json["roomDescription"] == description, True)

        # add three attributes
        name, category = others[0], "other"

        # create an attribute
        attribute_object = crud.add_attribute(
            name, 
            category, 
            self.session)

        # connect room with the attribute
        house_attribute_object = crud.add_house_attribute(
            room_object, 
            attribute_object, 
            self.session)

        name, category = others[1], "other"

        # create an attribute
        attribute_object = crud.add_attribute(
            name, 
            category, 
            self.session)

        # connect room with the attribute
        house_attribute_object = crud.add_house_attribute(
            room_object, 
            attribute_object, 
            self.session)

        name, category = facilities[0], "facilities"

        # create an attribute
        attribute_object = crud.add_attribute(
            name, 
            category, 
            self.session)

        # connect room with the attribute
        house_attribute_object = crud.add_house_attribute(
            room_object, 
            attribute_object, 
            self.session)

        result_json = crud.room_json(
            room_object, 
            self.session, 
            True)

        self.assertEqual(result_json["other"] == [others[0], others[1]], True)
        self.assertEqual(result_json["facilities"] == [facilities[0]], True)
Ejemplo n.º 4
0
    def test_write_room(self):
        # create an user
        user_name = "cris"
        user_email = "*****@*****.**"
        created_time = datetime.now()
        user_phone = "858-2867-3567"
        user_description = "cultured man"
        user_school_year = "Third"
        user_major = "Data Science"
        user_object = crud.add_user(
            user_name,
            user_email,
            created_time,
            user_phone,
            user_description,
            user_school_year,
            user_major,
            self.session)

        # the input json should be the same as output json from room_json except for id
        test_json = {"name": "75 Big Rock Cove St. Middletown",
                     "address": "75 Big Rock Cove St. Middletown, NY 10940",
                     "distance": "20 mins", "pricePerMonth": 500,
                     "fromMonth": "June/18", "toMonth": "July/18",
                     "earlyDate": "06/01/18", "lateDate": "06/12/18",
                     "roomType": "Single", "other": [], "facilities": [],
                     "photos": ["photo1", "photo2"],
                     "profilePhoto": "profile_photo",
                     "negotiable": True,
                     "numBaths": 2.5,
                     "numBeds": 2,
                     "roomDescription": "dream house in a life time"}

        test_res_json = {"name": "75 Big Rock Cove St. Middletown",
                         "address": "75 Big Rock Cove St. Middletown, NY 10940",
                         "distance": "20 mins", "pricePerMonth": 500,
                         "fromMonth": "June/18", "toMonth": "July/18",
                         "earlyDate": "06/01/18", "lateDate": "06/12/18",
                         "roomType": "Single", "other": [], "facilities": [],
                         "photos": ["photo1", "photo2"],
                         "profilePhoto": "profile_photo",
                         "negotiable": True,
                         "numBaths": 2.5,
                         "numBeds": 2,
                         "roomDescription": "dream house in a life time",
                         "leaserName": user_name, "leaserEmail": user_email,
                         "leaserPhone": user_phone,
                         "leaserSchoolYear": user_school_year,
                         "leaserMajor": user_major, }

        crud.write_room(transform_json_underscore(test_json),
                        user_object.id, self.session, True)

        room_object = crud.get_row_if_exists(
            Room,
            self.session,
            **{"id": 1})

        self.assertEqual(room_object is not None, True)

        result_json = crud.room_json(
            room_object,
            self.session,
            True)

        test_res_json["roomId"] = 1

        self.assertEqual(result_json == test_res_json, True)