class TestPost(unittest.TestCase): def setUp(self): tag = TagCategory() self.post = Post(title='Primeiro post', text='<b>Nose test e fd</b>', tags=tag.string_to_category('python, nose, tdd')) self.post.put() def test_verifica_se_post_foi_salvo_corretamente(self): assert_equals(self.post.url, 'primeiro-post') def test_verifica_se_texto_foi_salvo_corretamente(self): assert_equals(self.post.text, u'<b>Nose test e fd</b>') def test_verifica_se_tags_property_criado_corretamente(self): tag = TagCategory() categories = tag.string_to_category('python, nose, tdd') assert_equals(self.post.tags, categories) def test_verifica_se_title_foi_salvo_corretamente(self): assert_equals(self.post.title, 'Primeiro post') def test_apos_salvar_post_put_deve_retornar_a_chave_da_entidade(self): tag = TagCategory() post = Post(title='Primeiro post', text='<b>Nose test e fd</b>', tags=tag.string_to_category('python, nose, tdd')) assert_true(type(post.put().id()).__name__ == 'int') post.delete() def tearDown(self): self.post.delete()
def setUp(self): tag = TagCategory() self.post = Post(title='Primeiro post', text='<b>Nose test e fd</b>', tags=tag.string_to_category('python, nose, tdd')) self.post.put()
class TestPost(unittest.TestCase): def setUp(self): tag = TagCategory() self.post = Post(title='Primeiro post', text='<b>Nose test e fd</b>', tags=tag.string_to_category('python, nose, tdd')) self.post.put() def test_verifica_se_post_foi_salvo_corretamente(self): assert_equals(self.post.url, 'primeiro-post') def test_verifica_se_texto_foi_salvo_corretamente(self): assert_equals(self.post.text, u'<b>Nose test e fd</b>') def test_verifica_se_tags_property_criado_corretamente(self): tag = TagCategory() categories = tag.string_to_category('python, nose, tdd') assert_equals(self.post.tags, categories) def test_verifica_se_title_foi_salvo_corretamente(self): assert_equals(self.post.title, 'Primeiro post') def test_apos_salvar_post_put_deve_retornar_a_chave_da_entidade(self): tag = TagCategory() post = Post(title='Primeiro post', text='<b>Nose test e fd</b>', tags=tag.string_to_category('python, nose, tdd')) assert_true(type(post.put().id()).__name__ == 'int') post.delete() def tearDown(self): self.post.delete()
def test_apos_salvar_post_put_deve_retornar_a_chave_da_entidade(self): tag = TagCategory() post = Post(title='Primeiro post', text='<b>Nose test e fd</b>', tags=tag.string_to_category('python, nose, tdd')) assert_true(type(post.put().id()).__name__ == 'int') post.delete()
def test_apos_salvar_post_put_deve_retornar_a_chave_da_entidade(self): tag = TagCategory() post = Post(title='Primeiro post', text='<b>Nose test e fd</b>', tags=tag.string_to_category('python, nose, tdd')) assert_true(type(post.put().id()).__name__ == 'int') post.delete()
def setUp(self): self.app = app.test_client() tag = TagCategory() self.post = Post(title='Primeiro post', text='<b>Nose test e fd</b>', tags=tag.string_to_category('python, nose, tdd')) self.post.put() self.mocker = mocker.Mocker()
def seed(): content = """Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.""" for i in range(25): post = Post(title="Test Post #{}".format(i), content=content) session.add(post) session.commit()
def setUp(self): tag = TagCategory() self.post = Post(title='Primeiro post', text='<b>Nose test e fd</b>', tags=tag.string_to_category('python, nose, tdd')) self.post.put()
def test_post_mapper_can_retrieve_post(session): session.execute( "INSERT INTO posts (title, body, created_at, updated_at)" "VALUES ('test title', 'test body', '2020-10-26', '2020-10-25')") expected = [ Post("test title", "test body", date(2020, 10, 26), date(2020, 10, 25)) ] assert session.query(Post).all() == expected
def add(): form = AddPost() if form.validate_on_submit(): post = Post(current_user.id, form.title.data, form.post.data, form.short_description.data) db.session.add(post) db.session.commit() return redirect(url_for('home')) return render_template('posts/add_post.html', form=form, title='Add Post')
def new_post(): title = request.get_json()['title'] description = request.get_json()['description'] error = False data = {} new_post = Post(title=title, content=description, author=current_user) db.session.add(new_post) db.session.commit() data['description'] = description data['title'] = title return jsonify(data)
def test_update_post(self): self.admin_loggend(10) data={ 'title':"My uppdates.", 'tags':"python, nose, tdd, dojo", 'text':"<b>Nose test e fd</b>" } url = "%i/update" % self.post.key().id() response = self.app.post(url, data=data, follow_redirects=True) post = Post.get_by_id( self.post.key().id() ) assert_equals(post.title, data['title'] )
def post(): form=new_post() if form.validate_on_submit(): id=current_user.id print (id) post=Post(user_id=id,title=form.title.data,content=form.content.data) db.session.add(post) db.session.commit() flash("{} Article has created".format(form.title.data),'success') return redirect(url_for('home')) return render_template("post.html",form=form,title="New Post",legend="New Post")
def test_create_post(self): self.admin_loggend(10) data = { 'title':"My first post. bla bla bla", 'tags':"python,tdd", 'text':"bla bla bla bla bla bla bla bla" } response = self.app.post("/create", data=data, follow_redirects=True) post = Post.all().filter('url', slug(data['title'])).get() assert_equals(post.title, data['title']) post.delete()
def test_delete_post(self): self.admin_loggend(5) tag = TagCategory() post = Post(title='Primeiro post', text='<b>Nose test e fd</b>', tags=tag.string_to_category('python, nose, tdd')) post.put() url="%i/delete" % post.key().id() self.app.get(url) post = Post.get_by_id( post.key().id() ) assert_true(post is None)
def new_post(): form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data, subtitle=form.subtitle.data, content=form.content.data, author=current_user) db.session.add(post) db.session.commit() flash('貼文已成功發出', 'success') return redirect(url_for('main.home')) return render_template('create_post.html', form=form, legend='New Post', title='New Post')
class TestView(unittest.TestCase): def setUp(self): self.app = app.test_client() tag = TagCategory() self.post = Post(title='Primeiro post', text='<b>Nose test e fd</b>', tags=tag.string_to_category('python, nose, tdd')) self.post.put() self.mocker = mocker.Mocker() def tearDown(self): self.post.delete() self.mocker.restore() def test_index(self): response = self.app.get('/') assert_true('<title> rlopes | Henrique Lopes</title>' in str(response.data) ) def test_read_post(self): response = self.app.get("/%s" % self.post.url) title = '<h3><span><a href="/%s">%s</a></span></h3>' % (self.post.url, self.post.title) assert_true(title in str(response.data)) def test_read_post_that_not_exist(self): response = self.app.get("/post/meu-primeiro-post") assert_true('<title> Ops! Error 404 | Henrique Lopes</title>' in str(response.data)) def admin_loggend(self, times=1): loggend = self.mocker.replace('google.appengine.api.users.is_current_user_admin') loggend() self.mocker.result(True) self.mocker.count(times) self.mocker.replay() def test_form_insert(self): self.admin_loggend(10) response = self.app.get("/post/form") assert_true('<title> Criar post | Henrique Lopes</title>' in str(response.data)) def test_form_update(self): self.admin_loggend(5) url = "/%i/form" % self.post.key().id() response = self.app.get(url) title = '<title> Update - %s | Henrique Lopes</title>' % self.post.title assert_true(title in str(response.data)) def test_form_update_error(self): self.admin_loggend(5) url = "/%i/form" % 1 response = self.app.get(url) title = '<title> Ops! Error 404 | Henrique Lopes</title>' assert_true(title in str(response.data)) def test_create_post(self): self.admin_loggend(10) data = { 'title':"My first post. bla bla bla", 'tags':"python,tdd", 'text':"bla bla bla bla bla bla bla bla" } response = self.app.post("/create", data=data, follow_redirects=True) post = Post.all().filter('url', slug(data['title'])).get() assert_equals(post.title, data['title']) post.delete() def test_create_post_validate(self): self.admin_loggend(10) data={} response = self.app.post("/create", data=data, follow_redirects=True) assert_true("O Titulo deve ter entre 5 a 60 caracteres." in str(response.data)) def test_update_post(self): self.admin_loggend(10) data={ 'title':"My uppdates.", 'tags':"python, nose, tdd, dojo", 'text':"<b>Nose test e fd</b>" } url = "%i/update" % self.post.key().id() response = self.app.post(url, data=data, follow_redirects=True) post = Post.get_by_id( self.post.key().id() ) assert_equals(post.title, data['title'] ) def test_update_post_validate(self): self.admin_loggend(10) data={ 'title':"My uppdates.", 'tags':"python, nose, tdd, dojo", 'text':"" } url = "%i/update" % self.post.key().id() response = self.app.post(url, data=data, follow_redirects=True) assert_true("O Texto deve ter no minimo 5 caracteres." in str(response.data)) def tags_to_string(self, tags): tstr = "" for tag in tags: tstr += "%s," % tag return tstr[:-1] def test_form_populate(self): self.admin_loggend(10) url = "%i/form" % self.post.key().id() response = self.app.get(url) dom = html.fromstring(response.data) input_title = dom.xpath('//input[@type="text" and @name="title"]')[0] input_tags = dom.xpath('//input[@type="text" and @name="tags"]')[0] textare_text = dom.xpath('//textarea[@name="text"]')[0] assert_equals(input_title.value, self.post.title) assert_equals(input_tags.value, self.tags_to_string(self.post.tags)) assert_equals(textare_text.value, self.post.text) def test_delete_post(self): self.admin_loggend(5) tag = TagCategory() post = Post(title='Primeiro post', text='<b>Nose test e fd</b>', tags=tag.string_to_category('python, nose, tdd')) post.put() url="%i/delete" % post.key().id() self.app.get(url) post = Post.get_by_id( post.key().id() ) assert_true(post is None) def test_send_email(self): data = { 'nome':"Henrique", 'email':"*****@*****.**", 'mensagem':"O blog esta ficando bom." } response = self.app.post('/contact', data=data, follow_redirects=True) assert_true("True", str(response.data)) def test_send_email_error(self): data={ 'nome':"", 'email':"*****@*****.**", 'text':"" } response= self.app.post('/contact', data=data, follow_redirects=True) assert_true("[['#mensagem', 'false', 'Digite sua mensagem'],\ ['#nome', 'false', 'Digite seu nome']]", str(response.data))