def post(self): """ Method: POST Create an Entry URL path: mydiary/api/v1/entries/ """ entries = Entry() data = self.parser.parse_args() if len(entries) == 0: id_ = 1 else: id_ = entries[-1]['id'] + 1 if data['title'] == '' or data['content'] == '': return {'Message': 'title or content cannot be empty'}, 400 else: try: new_entry = { 'id': id_, 'date': datetime.now().strftime("%d-%m-%Y"), 'title': data['title'], 'content': data['content'] } Entry().save(new_entry) except: return { 'Message': 'An error occured while processing your request' }, 500 return make_response(jsonify(new_entry), 201)
def put(self, entryId): """ Method: PUT Modifies an entry by it's Id URL path: mydiary/api/v1/entries/<int:entryId> """ entry = Entry().get_by_id(entryId) if entry is None: return make_response(jsonify({'message': "Entry does not exist"}), 404) else: data = self.parser.parse_args() Entry().update_entry(data, entryId) return make_response(jsonify(entry), 200)
def delete(self, entryId): """ Method: DELETE Deletes an entry by it's Id URL path: mydiary/api/v1/entries/<int:entryId> """ entry = Entry().get_by_id(entryId) if entry is None: return make_response(jsonify({'message': "Entry does not exist"}), 404) else: Entry().delete_entry(entry) return make_response( jsonify({'message': 'Your entry was successfully deleted'}), 200)
def setUp(self): app = create_app('testing') self.tester = app.test_client(self) self.test_data = { "id": 1, "date": "18-7-2018", "title": "coding", "content": "Andela challenge two" } self.test_data2 = { "id": 2, "date": "23-7-2018", "title": "Bootcamp", "content": "Bootcamp day one" } self.db = Entry() self.db.save(self.test_data) self.db.save(self.test_data2)
def get(self): """ Method: GET Get all entries URL path: mydiary/api/v1/entries/ """ entries = Entry().all_items() response = {'All Entries': entries} return response
def get(self, entryId): """ Method: GET Fetches a single entry by it's Id URL path: mydiary/api/v1/entries/<int:entryId> """ entry = Entry().get_by_id(entryId) if entry: return make_response(jsonify(entry), 200) return {'Message': "Entry requested does not exist"}, 404
def setUp(self): app = create_app('testing') self.tester = app.test_client(self) self.payload = { 'title': 'off day', 'content': 'Going to watch football' } self.data = {'content': 'Going to watch football'} self.update_data = {'title': 'work day', 'content': 'Going to work'} self.url_route1 = '/mydiary/api/v1/entries' self.url_route2 = '/mydiary/api/v1/entries/1' self.entry = { "id": 1, "date": '12-2-2018', 'title': "Hobbie", 'content': 'I love writing flask apps' } # initializing database with data since its empty Entry().save(self.entry)
class TestsMagicMethods(unittest.TestCase): def setUp(self): app = create_app('testing') self.tester = app.test_client(self) self.test_data = { "id": 1, "date": "18-7-2018", "title": "coding", "content": "Andela challenge two" } self.test_data2 = { "id": 2, "date": "23-7-2018", "title": "Bootcamp", "content": "Bootcamp day one" } self.db = Entry() self.db.save(self.test_data) self.db.save(self.test_data2) def tearDown(self): self.db.clear_all() # Tests whether dunder __str__ method returns an object as a string def test__str__returns_str(self): self.assertTrue('[]', self.db) # Tests whether dunder __len__ method returns length of an object def test__len__returns_length(self): self.assertEquals(2, len(self.db)) #Tests whether dunder __getitem__ method indexes objects def test__getitem__permits_indexing(self): self.assertEquals(2, (self.db[1]["id"])) self.assertEquals(1, (self.db[0]["id"])) self.assertNotEqual(2, (self.db[0]["id"]))
class TestModels(unittest.TestCase): """ This class contains different tests to test models of the app. """ def setUp(self): app = create_app('testing') self.tester = app.test_client(self) self.test_data = { "id": 1, "date": "18-7-2018", "title": "coding", "content": "Andela challenge two" } self.test_data2 = { "id": 2, "date": "23-7-2018", "title": "Bootcamp", "content": "Bootcamp day one" } self.updated_data = { 'title': 'python', 'content': 'Am learning python' } self.db = Entry() self.db.save(self.test_data) self.db.save(self.test_data2) def tearDown(self): self.db.clear_all() # tests whether this model saves an entry to db def test_save_entry(self): self.assertIn("Andela challenge two", str(self.db)) self.assertIn('Bootcamp', str(self.db)) # Tests whether get_by_id() filters entries by id correctly def test_get_by_id(self): self.db.save(self.test_data2) self.assertIn('Bootcamp day one', str(self.db.get_by_id(2))) # tests whether this model deletes an entry to db def test_delete_entry(self): self.db.delete_entry(self.test_data2) self.assertEquals(1, len(self.db)) # tests whether this model update an entry given its id. def test_update_entry(self): self.db.update_entry(self.updated_data, 1) self.assertTrue('python', self.db) self.assertIn('Am learning python', str(self.db)) # Tests whether this model gets entries from db def test_get_entries(self): self.assertEquals(2, len(self.db)) # Tests whether this model gets entries from db def test_clear_all(self): self.db.clear_all() self.assertEquals(0, len(self.db))