def test_can_convert_timer_to_dict(self): user = TestObjects().get_test_user() timer = Timer(notes="Just a test timer", user=user, tags=["Unit Tests"]) tf = TimerFormatter() timer_entity_as_dict = tf.model_to_dict(timer) assert(timer_entity_as_dict is not None) assert(timer_entity_as_dict["notes"] == timer.notes)
def get_many(self): args = self._get_args(request) # For now, just get everything for the current user: dao = TimerDAO() timers = dao.get_all_timers_for_user(current_user.id) formatter = TimerFormatter() timers_payload = [formatter.model_to_dict(timer) for timer in timers] return jsonify(dict(timers=timers_payload))
def get_many(self): args = self._get_args(request) # For now, just get everything for the current user: dao = TimerDAO() timers = dao.get_all_timers_for_user(current_user.id) formatter = TimerFormatter() timers_payload = [formatter.model_to_dict(timer) for timer in timers] return jsonify(dict(timers=timers_payload))
def test_can_convert_timer_to_dict(self): user = TestObjects().get_test_user() timer = TimerEntity(notes="Just a test timer", user=user, tags=["Unit Tests"]) tf = TimerFormatter() timer_entity_as_dict = tf.model_to_dict(timer) assert (timer_entity_as_dict is not None) assert (timer_entity_as_dict["notes"] == timer.notes)
def index(): user = current_user dao = TimerDAO() timers = dao.get_all_timers_for_user(current_user.id) formatter = TimerFormatter() timers_payload = [formatter.model_to_dict(timer) for timer in timers] #timers = render_template("timer/user_timer.html", userId=user.id, userEmail=user.email, authToken=user.get_auth_token()) # timers_json = dumps(timers) return render_template("timer/user_timer.html", userId=user.id, userEmail=user.email, authToken=user.get_auth_token(), timers=timers_payload)
def test_can_dump_and_load_timer(self): user = TestObjects().get_test_user() timer = Timer(notes="Just a test timer", user=user, tags=["Unit Tests"], seconds = 22, running = True) timer.save() tf = TimerFormatter() timer_entity_as_dict = tf.model_to_dict(timer) timer.delete() timer2 = tf.dict_to_model(Timer, timer_entity_as_dict) # This won't pass, there are subtle, trivial differences in datetimes once dates have been serialized #assert(timer.lastRestart == timer2.lastRestart) #assert(timer.dateEntered == timer2.dateEntered) assert(timer.tags == timer2.tags) assert(timer.running == timer2.running) assert(timer.seconds == timer2.seconds) assert(timer.notes == timer2.notes) assert(timer.user == timer2.user) assert(timer.to_json() == timer2.to_json())
def post(self): api_v1_root = '/api/v1' timer = TimerFormatter().dict_to_model(TimerEntity, request.json) dao = TimerDAO() dao.put(timer) id = str(timer.id) resp = jsonify(dict(id=id)) resp.headers["Location"] = self.make_location(request.url, id) resp.status_code = http.client.CREATED return resp
def get_one(self, id): parser = RequestParser() # Look only in the querystring parser.add_argument('user', location='args', default=None) args = parser.parse_args(request) dao = TimerDAO() timer = dao.get(id) as_dict = TimerFormatter().model_to_dict(timer) resp = jsonify(as_dict) return resp
def test_can_dump_and_load_timer(self): user = TestObjects().get_test_user() timer = TimerEntity(notes="Just a test timer", user=user, tags=["Unit Tests"], seconds=22, running=True) timer.save() tf = TimerFormatter() timer_entity_as_dict = tf.model_to_dict(timer) timer.delete() timer2 = tf.dict_to_model(TimerEntity, timer_entity_as_dict) # This won't pass, there are subtle, trivial differences in datetimes once dates have been serialized #assert(timer.lastRestart == timer2.lastRestart) #assert(timer.dateEntered == timer2.dateEntered) assert (timer.tags == timer2.tags) assert (timer.running == timer2.running) assert (timer.seconds == timer2.seconds) assert (timer.notes == timer2.notes) assert (timer.user == timer2.user) assert (timer.to_json() == timer2.to_json())
def put(self, id): #api_v1_root = '/api/v1' timer_new = TimerFormatter().dict_to_model(TimerEntity, request.json) dao = TimerDAO() timer = dao.get(id) if timer is not None: timer.update_attributes(timer_new) dao.put(timer) id = str(timer.id) resp = jsonify(dict(id=id)) #resp.headers["Location"] = api_v1_root + self.root + "/" + id resp.headers["Location"] = self.make_location(request.url, id) resp.status_code = http.client.OK return resp
def test_get_week_ending_date(self): formatter = TimerFormatter() assert(formatter.get_week_ending_date(2016,0) == "01/02/2016") assert(formatter.get_week_ending_date(2015,0) == "01/03/2015") assert(formatter.get_week_ending_date(2014,0) == "01/04/2014") assert(formatter.get_week_ending_date(2001,0) == "01/06/2001") assert(formatter.get_week_ending_date(2005,0) == "01/01/2005") assert(formatter.get_week_ending_date(2006,0) == "01/07/2006") assert(formatter.get_week_ending_date(2015,7) == "02/21/2015") assert(formatter.get_week_ending_date(2016,11) == "03/19/2016")
def test_timer_resource_all(self): token = test_credentials.get_auth_token() user = TestObjects().get_test_user() timer = TimerEntity(notes="Just a test timer", user=user, tags=["Unit Tests"], seconds=22, running=True) timer_dict = TimerFormatter().model_to_dict(timer) # Not authorized w/o token response = requests.post(v1_api + "/timer", headers={'content-type': 'application/json'}, data=timer.to_json()) assert (response.status_code == http.client.UNAUTHORIZED) # With token, should get "CREATED" response = requests.post(v1_api + "/timer", headers={'content-type': 'application/json'}, auth=token, data=timer.to_json()) assert (response.status_code == http.client.CREATED) # Object location url = response.headers["Location"] # Not authorized without the token (401) response = requests.get(url) assert (response.status_code == http.client.UNAUTHORIZED) # Re-send the request with the token, this time get OK (200) response = requests.get(url, auth=token) assert (response.status_code == http.client.OK) timer_dict2 = loads(response.text) assert (timer_dict2["seconds"] == 22) # Update the seconds and PUT the request timer_dict2["seconds"] = 99 response = requests.put(url, headers={'content-type': 'application/json'}, auth=token, data=dumps(timer_dict2)) assert (response.status_code == http.client.OK) response = requests.get(url, auth=token) assert (response.status_code == http.client.OK) timer_dict3 = loads(response.text) assert (timer_dict3["seconds"] == 99) # Currently our GET all means "Everything for the current user. This will change, and we'll need a new test get_many_url = v1_api + "/timer" # Not authorized without the token (401) response = requests.get(get_many_url) assert (response.status_code == http.client.UNAUTHORIZED) # Re-send the request with the token, this time get OK (200) response = requests.get(get_many_url, auth=token) assert (response.status_code == http.client.OK) print(response.text) # Delete the resource response = requests.delete(url, auth=token) assert (response.status_code == http.client.NO_CONTENT)