Exemple #1
0
def test_paging(client, db):
    # add 8 days worth of data from Sun Apr 19, 2020 - Sun Apr 26, 2020
    # get current date and set to prev Sun (week starts on Monday)
    # this way, with 8 days of data, we will get a full week on one page,
    #    and one overflow date on page 2
    sunday = moment.date(2020, 4, 26)
    for i in range(8):
        prev = sunday.clone().subtract(days=i)
        point = Point(happy=5)
        point.timestamp = prev.date
        point.save()

    assert len(Point.query.all()) == 8, "Data loaded incorrectly"

    # get first page of data
    resp = client.get("/points")
    print(resp)
    assert resp.status_code == 200
    assert resp.json.get("success")
    assert resp.json.get("points") != None
    assert resp.json.get("meta").get("next_page")

    points = resp.json.get("points")
    assert len(points) == 7

    # get the next page of data
    resp = client.get("/points?page=2")
    assert len(resp.json.get("points")) == 1
    assert "04-19-2020" in resp.json.get("points")[0].get("date")
Exemple #2
0
def test_update_point(client, db):
    # first add data points to the db so there is something to retrieve;
    # point is added with a default current timestamp
    point = Point(happy=5)
    point.save()

    # use the api to update the entry with a timestamp from today
    HAPPY = 95
    params = {"happy": HAPPY}
    resp = client.post_json("/points", params=params)

    # check that our point now has 100 happy faces
    assert point.happy == 100
Exemple #3
0
def test_create_point_with_db_defaults(db):
    """Ensure that a new Point entry in the database has the correct default values"""
    point = Point()
    point.save()
    assert point.id, "the object should have an id if successfully saved in the db"

    retrieved = Point.query.get(point.id)
    assert retrieved == point, "the database object returned should be the same as the one just created"

    assert bool(point.timestamp)
    assert isinstance(point.timestamp, dt.datetime)
    assert point.happy == 0, "happy column should be initialized to 0"
    assert point.sad == 0, "sad column should be initialized to 0"
    assert point.total == 0, "total column should be initialized to 0"
Exemple #4
0
def add_points():

    happy_val = request.get_json().get("happy", 0)
    sad_val = request.get_json().get("sad", 0)
    notes_val = request.get_json().get("notes", "")

    # get the entry for the current date and update it
    start = moment.now().zero.date
    end = moment.now().replace(hours=23, minutes=59, seconds=59).date
    todays_point = Point.query.filter(Point.timestamp.between(start,
                                                              end)).first()

    if todays_point:
        todays_point.happy += happy_val
        todays_point.sad += sad_val
        todays_point.notes += ";" + notes_val
    else:
        todays_point = Point(happy_val, sad_val, notes_val)

    todays_point.save()

    return jsonify({"success": True, "point": todays_point.to_dict()})
Exemple #5
0
def test_update_point(db):
    """Ensure that we can update a database entry"""
    point = Point()
    point.save()

    point.happy = 5
    point.sad = 3
    point.save()

    assert point.id, "the object should have an id if successfully saved in the db"

    retrieved = Point.query.get(point.id)
    assert retrieved.happy == 5
    assert retrieved.sad == 3
    assert retrieved.total == 2, "the total column should automatically be set to happy-sad"
Exemple #6
0
def test_total_is_not_created_negative(db):
    """Ensure that the total column is never a negative number, even if there are more sad faces than happy"""
    point = Point(happy=1, sad=5)
    point.save()
    assert point.total == 0, "total column should never be negative"
Exemple #7
0
def test_create_point_with_automatic_total(db):
    """Ensure that the total column is correctly set to the difference between happy and sad"""
    point = Point(happy=5, sad=3)
    point.save()
    assert point.total == 2, "total column should be set to the value of happy - sad"