def test_get_weights_up_to_and_including_date(self, api, client):
     now = dt.datetime.utcnow()
     token, _ = register_and_login_confirmed_user(api, client, "tyson",
                                                  "*****@*****.**",
                                                  "knockout", 178, 95.0,
                                                  dt.date(1966, 6, 30))
     url = api.url_for(ProfileWeight,
                       username="******",
                       endAt=(dt.date(now.year, now.month, now.day) -
                              dt.timedelta(days=4)))
     profile = User.find_by_username("tyson").profile
     profile.save()
     ProfileWeightHistory(weight=90.0,
                          profile_id=profile.id,
                          created_at=(now - dt.timedelta(days=5))).save()
     ProfileWeightHistory(weight=91.0,
                          profile_id=profile.id,
                          created_at=(now - dt.timedelta(days=4))).save()
     ProfileWeightHistory(weight=92.0,
                          profile_id=profile.id,
                          created_at=(now - dt.timedelta(days=3))).save()
     ProfileWeightHistory(weight=93.0,
                          profile_id=profile.id,
                          created_at=(now - dt.timedelta(days=2))).save()
     ProfileWeightHistory(weight=94.0,
                          profile_id=profile.id,
                          created_at=(now - dt.timedelta(days=1))).save()
     response = client.get(url, headers=get_authorization_header(token))
     response_json = get_response_json(response.data)
     assert (response.status_code == 200)
     assert (len(response_json) == 2)
     assert (response_json[0]["weight"] == 91.0)
     assert (response_json[1]["weight"] == 90.0)
	def test_no_update_if_no_data(self, api, client):
		token,_ = register_and_login_confirmed_user(api, client, "profiler", "*****@*****.**", "passwd", 179, 75.0, dt.date(1980, 2, 29))
		url = api.url_for(Profile, username="******")
		profile = User.find_by_username("profiler").profile
		old_updated_at = profile.updated_at
		response = client.put(url, headers=get_authorization_header(token))
		assert(response.status_code == 200)
		assert(profile.height == 179)
		assert(str(profile.birth_date) == "1980-02-29")
		assert(profile.updated_at == old_updated_at)
	def test_update_profile_actually_saved(self, api, client):
		token,_ = register_and_login_confirmed_user(api, client, "profiler", "*****@*****.**", "passwd", 179, 75.0, dt.date(1980, 2, 29))
		url = api.url_for(Profile, username="******")
		profile = User.find_by_username("profiler").profile
		old_updated_at = profile.updated_at
		client.put(url, data={ "birthDate":"2001-02-03", "height":180, "weight":70.1 }, headers=get_authorization_header(token))
		assert(profile.height == 180)
		assert(profile.weight == 70.1)
		assert(str(profile.birth_date) == "2001-02-03")
		assert(profile.updated_at > old_updated_at)
    def test_get_profileweight_only_single_user_data_is_returned(
            self, api, client):
        register_confirmed_user("another", "*****@*****.**", "different")
        other_profile = User.find_by_username("another").profile
        other_profile.set_weight(66.6)
        other_profile.save()

        token, _ = register_and_login_confirmed_user(api, client, "tyson",
                                                     "*****@*****.**",
                                                     "knockout", 178, 95.0,
                                                     dt.date(1966, 6, 30))
        profile = User.find_by_username("tyson").profile
        profile.set_weight(101.0)
        profile.save()

        url = api.url_for(ProfileWeight, username="******")
        response = client.get(url, headers=get_authorization_header(token))
        response_json = get_response_json(response.data)
        assert (response.status_code == 200)
        assert (len(response_json) == 1)
        assert (response_json[0]["weight"] == 101.0)
 def test_get_profileweight_logged_in(self, api, client):
     token, _ = register_and_login_confirmed_user(api, client, "tyson",
                                                  "*****@*****.**",
                                                  "knockout", 178, 95.0,
                                                  dt.date(1966, 6, 30))
     url = api.url_for(ProfileWeight, username="******")
     profile = User.find_by_username("tyson").profile
     profile.set_weight(101.0)
     profile.save()
     response = client.get(url, headers=get_authorization_header(token))
     response_json = get_response_json(response.data)
     assert (response.status_code == 200)
     assert (len(response_json) == 1)
     assert (response_json[0]["weight"] == 101.0)
 def test_return_weights_for_several_days(self, api, client):
     token, _ = register_and_login_confirmed_user(api, client, "tyson",
                                                  "*****@*****.**",
                                                  "knockout", 178, 95.0,
                                                  dt.date(1966, 6, 30))
     url = api.url_for(ProfileWeight, username="******")
     profile = User.find_by_username("tyson").profile
     profile.set_weight(101.0)  # today's date
     profile.save()
     ProfileWeightHistory(weight=102.1,
                          profile_id=profile.id,
                          created_at=(dt.datetime.utcnow() -
                                      dt.timedelta(days=5))).save()
     ProfileWeightHistory(weight=99.1,
                          profile_id=profile.id,
                          created_at=dt.datetime.utcnow() -
                          dt.timedelta(days=1)).save()
     response = client.get(url, headers=get_authorization_header(token))
     response_json = get_response_json(response.data)
     assert (response.status_code == 200)
     assert (len(response_json) == 3)
     assert (response_json[0]["weight"] == 101.0)  # most recent first
     assert (response_json[1]["weight"] == 99.1)
     assert (response_json[2]["weight"] == 102.1)