def test_each_question_has_unique_id(self): QuestionRepository.populate(2) items = self.retrieve_all_items() self.assertEqual(len(items), 2) self.assertNotEqual(items[0][u'id'], items[1][u'id'])
def get(self): ids_of_items_to_update = [int(items_id) for items_id in self.request.get_all('id')] if ids_of_items_to_update: QuestionRepository.add_answers(ids_of_items_to_update) self.response.headers['Content-Type'] = 'text/plain' self.response.out.write('OK')
def test_each_question_has_updated_field_that_is_equal_to_timestamp_field(self): QuestionRepository.populate(1) items = self.retrieve_all_items() self.assertEqual(len(items), 1) self.assertEqual(len(items), 1) self.assertEqual(items[0][u'created'], items[0][u'updated'])
def get(self): number_of_items_to_add = self.request.get('n') if not number_of_items_to_add: number_of_items_to_add = 10 QuestionRepository.populate(int(number_of_items_to_add)) self.response.headers['Content-Type'] = 'text/plain' self.response.out.write('OK')
def test_updating_selected_items_using_get_request(self): QuestionRepository.populate(5) updated = ItemsJSON.timestamp_to_float(QuestionRepository.all().get().updated) self.update_items([5, 3, 1]) new_items = self.retrieve_first_n_items_updated_after_given_timestamp(5, updated) self.assertEqual(3, len(new_items))
def test_each_question_has_timestamp_encoded_as_float(self): QuestionRepository.populate(2) items = self.retrieve_all_items() self.assertEqual(len(items), 2) self.assertEqual(len(items), 2) self.assertGreater(items[0][u'created'], items[1][u'created'])
def get(self): ids_of_items_to_update = [ int(items_id) for items_id in self.request.get_all('id') ] if ids_of_items_to_update: QuestionRepository.add_answers(ids_of_items_to_update) self.response.headers['Content-Type'] = 'text/plain' self.response.out.write('OK')
def test_retrieving_items_using_json(self): QuestionRepository.populate(5) items = self.retrieve_all_items() self.assertEqual(len(items), 5) self.assertEqual(len(items), 5) for record_index, record in enumerate(items): self.assertEqual(record[u'content'], "%s%d" % (self.TEST_ITEM, 5 - record_index - 1), "record_index:%d, record:%s" % (record_index, record)) self.assertEqual(record[u'header'], "%s%d" % (self.HEADER_FOR_ITEM, 5 - record_index - 1), "record_index:%d, record:%s" % (record_index, record))
def test_retrieving_first_n_items_posted_before_item_with_given_id(self): QuestionRepository.populate(15) items = self.retrieve_first_n_items(5) self.assertEqual(len(items), 5) oldest_item_id = items[4][u'id'] self.assertEqual('11', oldest_item_id) items = self.retrieve_first_n_items_before_item_with_id(int(oldest_item_id), 5) self.assertEqual(len(items), 5) self.assertEqual('10', items[0][u'id']) self.assertEqual('6', items[4][u'id'])
def test_adding_an_answer_to_a_question(self): QuestionRepository.populate(2) items = self.retrieve_all_items() self.assertEqual(len(items), 2) item = items[0] self.assertIsNone(item[u'answer']) QuestionRepository.update_item(item[u'id'], item[u'header'], item[u'content'], "New Answer") items = self.retrieve_all_items() item = items[0] self.assertIsNotNone(item[u'answer']) self.assertEqual("New Answer", item[u'answer']) self.assertGreater(item[u'updated'], item[u'created'])
def test_adding_more_items_to_the_database_using_add_more_get_request_with_explicit_number_of_items(self): QuestionRepository.populate(10) items = self.retrieve_all_items() self.assertEqual(len(items), 10) item = items[0] self.assertEqual('10', item[u'id']) self.add_more_items(50) items = self.retrieve_all_items() self.assertEqual(len(items), 60) item = items[0] self.assertEqual('60', item[u'id'])
def test_retrieving_first_n_items_posted_after_item_with_given_id(self): QuestionRepository.populate(10) items = self.retrieve_first_n_items(5) most_recent_item_id = items[0][u'id'] most_recent_item_updated = items[0][u'updated'] self.assertEqual('10', most_recent_item_id) oldest_item_id = items[4][u'id'] self.assertEqual('6', oldest_item_id) number_of_new_items = 5 QuestionRepository.populate(number_of_new_items) new_items = self.retrieve_first_n_items_updated_after_given_timestamp(number_of_new_items, most_recent_item_updated) self.assertEqual(len(new_items), number_of_new_items) self.assertEqual('11', new_items[0][u'id']) self.assertEqual('15', new_items[4][u'id'])
def main(): parser = argparse.ArgumentParser() parser.add_argument( '-n', action="store", default=0, dest="number_of_items", type=int, help="Number of test items in data store after initialization.") parser.add_argument( '-d', action="store", default=0, dest="delay", type=int, help= "Delay in finishing request in seconds. Used to simulate slow server connection." ) QuestionRepository.create_table() QuestionRepository.truncate() QuestionRepository.populate(parser.parse_args().number_of_items) ItemsJSON.set_delay(parser.parse_args().delay) httpserver.serve(app, host='0.0.0.0', port='9001')
def test_retrieving_updated_items(self): QuestionRepository.populate(10) items = self.retrieve_first_n_items(5) most_recent_item_id = items[0][u'id'] most_recent_item_updated = items[0][u'updated'] self.assertEqual('10', most_recent_item_id) oldest_item_id = items[4][u'id'] self.assertEqual('6', oldest_item_id) number_of_new_items = 5 QuestionRepository.populate(number_of_new_items) list_of_items_to_be_updated = [6, 8, 10] QuestionRepository.update_items(list_of_items_to_be_updated) new_items = self.retrieve_first_n_items_updated_after_given_timestamp(number_of_new_items, most_recent_item_updated) self.assertEqual(len(new_items), number_of_new_items) self.assertEqual('11', new_items[0][u'id']) self.assertEqual('15', new_items[4][u'id']) new_items = self.retrieve_first_n_items_updated_after_given_timestamp(len(list_of_items_to_be_updated), new_items[4][u'updated']) self.assertEqual(len(new_items), len(list_of_items_to_be_updated)) self.assertIn(int(new_items[0][u'id']), list_of_items_to_be_updated) self.assertIn(int(new_items[1][u'id']), list_of_items_to_be_updated) self.assertIn(int(new_items[2][u'id']), list_of_items_to_be_updated)
def main(): parser = argparse.ArgumentParser() parser.add_argument('-n', action="store", default=0, dest="number_of_items", type=int, help="Number of test items in data store after initialization.") parser.add_argument('-d', action="store", default=0, dest="delay", type=int, help="Delay in finishing request in seconds. Used to simulate slow server connection.") QuestionRepository.create_table() QuestionRepository.truncate() QuestionRepository.populate(parser.parse_args().number_of_items) ItemsJSON.set_delay(parser.parse_args().delay) httpserver.serve(app, host='0.0.0.0', port='9001')
def post(self): new_json_item = json.loads(self.request.body) QuestionRepository.add_item_with_header_and_content(new_json_item[0]["header"], new_json_item[0]["content"]) self.response.headers["Content-Type"] = "text/plain" self.response.out.write("ADDED")
def test_retrieving_first_n_items(self): QuestionRepository.populate(20) items = self.retrieve_first_n_items(10) self.assertEqual(len(items), 10)
def post(self): new_json_item = json.loads(self.request.body) QuestionRepository.add_item_with_header_and_content( new_json_item[0]['header'], new_json_item[0]['content']) self.response.headers['Content-Type'] = 'text/plain' self.response.out.write('ADDED')
def get_questions_with_timestamp_before_id(self, question_id, number_of_items_to_fetch): if number_of_items_to_fetch: items = QuestionRepository.fetch_n_before(int(question_id), int(number_of_items_to_fetch)) else: items = QuestionRepository.fetch_all_before(int(question_id)) return self.convert_to_json(items)
def get_n_questions_updated_after_timestamp(self, timestamp, n): questions = QuestionRepository.fetch_n_updated_after(float(timestamp), int(n)) return self.convert_to_json(questions)
def get_all_questions(self): return self.convert_to_json(QuestionRepository.all())
def test_retrieving_first_n_items_when_number_of_available_items_less_than_n(self): QuestionRepository.populate(5) items = self.retrieve_first_n_items(10) self.assertEqual(len(items), 5)
def setUp(self): QuestionRepository.create_table() QuestionRepository.truncate() self.testapp = webtest.TestApp(main.app)