def test_insert_delete_flower(client: FlaskClient): # Send a request and make sure the server reports success res = client.post("/flowerdex", data={ "flowercommonname": "Test Flower", "flowershapeid": "fs2", "flowercolorid": "fc6", "flowerspecies": "Testificus", "flowergenus": "Unit" }) data = check_ok_response(res, "Log a new flower success!") id = data["data"][0]["flower_id"] assert id > 0 # Try updating the flower # TODO Find a way to automate checking database contents, since the API doesn't expose res = client.put("/flowerdex/{}".format(id), data={"fcommon": "Test flower update"}) data = check_ok_response(res, "Update the Folwer information success!") assert data["data"][0]["flower_id"] == id # Clean up -- delete the flower we just created res = client.delete("/flowerdex/{}".format(id)) check_ok_response(res, "Delete flower success!") # Try to delete the flower we just deleted, we should get an error res = client.delete("/flowerdex/{}".format(id)) check_err_response(res, "flower id not found!", 404) # Try to update the flower we just deleted, we should get an error res = client.put("/flowerdex/{}".format(id), data={"fcommon": "This flower doesn't exist"}) check_err_response(res, "Flower not found!", 404)
def test_beerecord_crud(client: FlaskClient): """Full test of beerecord CRUD abilities""" # Dummy bee record bee_record = { "elevation": 30, "chead": "test head", "cabdomen": "test abdomen", "cthorax": "test thorax", "gender": "female", "loc": "40 40", "cityname": "Worcester MA", "fname": "im a flower watch me go", "fshape": "flower shape", "fcolor": "red", "beename": "im a bee! bzzz", "time": "2019-12-19T20:43:52.243Z", "beedictid": "1", "beebehavior": "1", "recordpicpath": "picture.bmp", "appversion": "1.2.2", } # Post a new bee record res = client.post("/record", data=bee_record) data = check_ok_response(res, "Log a new bee success!") assert len(data["data"]) == 1 id = data["data"][0]["beerecord_id"]
def test_beerecords(client: FlaskClient): res = client.get("/beerecords/1") data = check_ok_response(res, "Retrieve the Bee records success!") assert 0 < len(data["data"]) <= 50 # Verify all needed fields are present in every object in the data list version_matcher = re.compile("^\d+\.\d+\.\d+$") for datum in data["data"]: # Strings: for field in [ "bee_name", "coloration_abdomen", "coloration_thorax", "coloration_head", "flower_shape", "flower_color", "time", "loc_info", "user_id", "record_pic_path", "record_video_path", "flower_name", "city_name", "gender", "bee_behavior", "common_name", "app_version", "elevation" ]: assert field in datum assert type(datum[field]) == str or datum[field] is None # Numbers for field in ["beerecord_id", "bee_dict_id"]: assert field in datum assert type(datum[field]) == int or datum[field] is None # Date must follow specific format try: datetime.strptime(datum["time"], "%Y-%m-%dT%H:%M:%S.%fZ") except ValueError: assert False # App version must follow specific format assert len(version_matcher.findall(datum["app_version"])) == 1
def test_flowerdex_one(client: FlaskClient): # Base tests -- message must conform to expected response format res = client.get("/flowerdex/5") data = check_ok_response(res, "Retrieve the Flower information success!") assert len(data["data"]) > 0 # Data entry must have needed data entry = data["data"][0] assert type(entry["flower_id"]) == int assert type(entry["flower_latin_name"]) == str assert type(entry["flower_common_name"]) == str
def test_unmatched_flowers(client: FlaskClient): res = client.get("/unmatched_flowers") data = check_ok_response(res, "Retrieve the Bee records success!" ) # TODO This... is not the right response. assert len(data["data"]) > 0 for datum in data["data"]: for field in ["flower_name", "count"]: assert field in datum assert type(field) == str int( datum["count"] ) # Will throw an exception if the field can't be parsed into an int
def test_flower_shapes(client: FlaskClient, url=None): res = client.get("/flowershapes" if url is None else url) data = check_ok_response(res, "Retrieve the flower shapes success!") assert len(data["data"]) > 0 # All fields must be of the right type, must only return flower features for datum in data["data"]: for field in [ "feature_id", "feature_name", "feature_description", "feature_pic_path" ]: assert field in datum assert type(datum[field]) == str assert datum["feature_id"].startswith("fc")
def test_flower_list(client: FlaskClient): res = client.get("/flowerlist") data = check_ok_response(res, "Retrieve the Flower List success!") data = data["data"] for datum in data: for field in [ "latin_name", "main_common_name", "common_name", "main_color", "colors", "bloom_time", "shape", "image_src" ]: assert field in datum assert type(datum[field]) == str or datum[field] is None assert "flower_id" in datum assert type(datum["flower_id"]) == int
def test_beedex_one(client: FlaskClient): # Base tests -- message must conform to expected response format res = client.get("/beedex/5") data = check_ok_response(res, "Retrieve the Bee information success!") assert len(data["data"]) > 0 # Data entry must have needed data for datum in data["data"]: assert type(datum["bee_id"]) == int assert type(datum["bee_name"]) == str assert type(datum["common_name"]) == str assert type(datum["description"]) == str assert type(datum["confused"]) == str assert type(datum["confused"]) == str assert type(datum["bee_pic_path"]) == str assert "abdomen_list" in datum assert "thorax_list" in datum assert "head_list" in datum
def test_beevisrecords(client: FlaskClient): res = client.get("/beevisrecords") data = check_ok_response(res, "Retrieve the Bee records success!") assert len(data["data"]) > 0 # Verify all needed fields are present in every object in the data list for datum in data["data"]: for field in [ "bee_name", "loc_info", "flower_shape", "flower_name", "flower_color", "bee_behavior", "spgender", "elevation", "gender", "date" ]: assert field in datum assert type(datum[field]) == str or datum[field] is None # Date must follow specific format try: datetime.strptime(datum["date"], "%Y-%m-%dT%H:%M:%S.%fZ") except ValueError: assert False
def test_flowerdex_all(client: FlaskClient): res = client.get("/flowerdex") data = check_ok_response(res, "Retrieve the Flower information success!") assert len(data["data"]) > 0