def test_save_and_get_data(self): content = 'this is the first content.' update = Update(content=content) update.save() data = Update.query().first() self.assertEqual(data.id, update.id) self.assertEqual(data.content, content)
def test_update_list_all(self): content = 'this is a test update' summary = 'this is a test update for unittest' update = Update(content=content, summary=summary) update.save() res = self.check_status_code_with_admin('/update/items/all') data = res.data.decode() self.assertIn(update.content, data)
def test_update_list_view(self): content = 'this is a test update' summary = 'this is a test update for unittest' update = Update(content=content, summary=summary) update.save() id_ = update.id path = f'/update/item/{id_}' res = self.check_status_code_with_admin(path) data = res.data.decode() self.assertIn(content, data) self.assertIn(summary, data)
def update_item_edit(item_id): update = Update.get(item_id) kwargs = { 'status': 'success', 'task': 'edit', 'type': 'update', } if update is None: kwargs['status'] = 'error' kwargs['message'] = 'item not found' response = jsonify(**kwargs) response.status_code = 400 return response try: content = request.json.get('content') summary = request.json.get('summary') if content is not None: update.content = content if summary is not None: update.summary = summary update.save() response = jsonify(**kwargs) response.status_code = 201 except Exception as e: kwargs['status'] = 'error' kwargs['error'] = str(e) response = jsonify(**kwargs) response.status_code = 400 return response
def note(): updates = [ update.to_dict() for update in Update.query().order_by(Update.id.desc()).all() ] references = [ref.to_dict() for ref in Citation.query().all()] return render_template('note.html', updates=updates, references=references)
def test_update_item_to_dict(self): content = 'this is the first content.' update = Update(content=content) update.save() data = update.to_dict(date=True) self.assertIn('content', data) self.assertEqual(content, data.get('content')) self.assertIn('update_at', data) self.assertTrue(isinstance(data.get('update_at'), str)) dates = data.get('update_at').split('/') self.assertEqual(3, len(dates)) for d in dates: self.assertTrue(d.isdigit()) self.assertIn('summary', data)
def update_register(): form = UpdateForm() if form.validate_on_submit(): content = form.content.data summary = form.summary.data Update(content=content, summary=summary).save() return redirect(url_for('base.note')) return render_template('update_form.html', form=form)
def test_update_edit_view(self): update = Update(content='test') update.save() id_ = update.id path = f'/update/edit/{id_}' self.check_status_code_with_admin(path) target_content = 'test content' self.check_status_code_with_admin( path, method='PUT', content=target_content, ) # check if data is updated update_after = Update.get(id_) self.assertEqual(update_after.content, target_content)
def update_edit(item_id): update = Update.get(item_id) form = UpdateForm() if form.validate_on_submit(): update.content = form.content.data update.save() return redirect(url_for('base.note')) # set initial values form.content.data = update.content return render_template('update_form.html', form=form)
def fetch_update_items_all(): items = Update.query().all() kwargs = { 'status': 'success', 'task': 'read-only', 'type': 'update', } response = jsonify(data=[item.to_dict() for item in items], **kwargs) response.status_code = 200 return response
def test_update_register_view(self): self.check_status_code_with_admin('/update/register') self.auth_login() res = self.client.post( '/update/register', data=dict( content='test content', summary='test summary', ) ) self.assertEqual(res.status_code, 302) update = Update.query().filter_by(content='test content').first() self.assertEqual(update.summary, 'test summary')
def fetch_update_item(item_id): item = Update.get(item_id) kwargs = { 'status': 'success', 'task': 'read-only', 'type': 'update', } if item is None: data = {} else: data = item.to_dict() response = jsonify(data=data, **kwargs) response.status_code = 200 return response
def note(): kwargs = { 'status': 'success', 'task': 'read-only', 'type': 'note', } updates = [ update.to_dict(date=True) for update in Update.query().order_by(Update.id.desc()).all() ] references = [ref.to_dict() for ref in Citation.query().all()] data = {'updates': updates, 'references': references} response = jsonify(data=data, **kwargs) response.status_code = 200 return response
def update_item_register(): kwargs = { 'status': 'success', 'task': 'register', 'type': 'update', } try: content = request.json.get('content') summary = request.json.get('summary') Update(content=content, summary=summary).save() response = jsonify(**kwargs) response.status_code = 201 except Exception as e: kwargs['status'] = 'error' kwargs['error'] = str(e) response = jsonify(**kwargs) response.status_code = 400 return response
def list_update_items_all(): items = Update.query().all() return render_template('update_items_all.html', items=items)
def list_update_item(item_id): item = Update.get(item_id) if item is None: return redirect(url_for('base.note')) return render_template('update_item.html', item=item)
def __call__(self): content = fake.text(max_nb_chars=64) update_at = fake.past_date(start_date='-30d') return Update(content=content, update_at=update_at)