Best Python code snippet using localstack_python
test_views.py
Source:test_views.py
1from django import forms2from django.contrib.auth import get_user_model3from django.test import Client, TestCase4from django.urls import reverse5from posts.models import Group, Post, User6User = get_user_model()7class PostsTests(TestCase):8 @classmethod9 def setUpClass(cls):10 super().setUpClass()11 cls.user = User.objects.create_user(username='LucyTestContext')12 cls.user_second = User.objects.create_user(username='MikeTestContex')13 cls.group = Group.objects.create(14 title='LucyTestGroupContext',15 slug='TestovayaGroupContext',16 description='ÐÑа гÑÑппа Ñоздана Ð´Ð»Ñ ÑеÑÑиÑÐ¾Ð²Ð°Ð½Ð¸Ñ context'17 )18 cls.group_other = Group.objects.create(19 title='MikeTestGroupContext',20 slug='MikeTestovayaGroupContext',21 description='ÐÑа гÑÑппа Mike Ñоздана Ð´Ð»Ñ ÑеÑÑиÑÐ¾Ð²Ð°Ð½Ð¸Ñ context'22 )23 cls.post = Post.objects.create(24 text='1 ТеÑÑовÑй ÑекÑÑ Ð¿Ð¾ÑÑ',25 group=cls.group,26 author=cls.user27 )28 cls.index = (29 'posts:index',30 'posts/index.html',31 None)32 cls.group_list = (33 'posts:grouppa',34 'posts/group_list.html',35 [cls.group.slug]36 )37 cls.group_list_other = (38 'posts:grouppa',39 'posts/group_list.html',40 [cls.group_other.slug]41 )42 cls.profile = (43 'posts:profile',44 'posts/profile.html',45 [cls.user.username]46 )47 cls.detail = (48 'posts:post_detail',49 'posts/post_detail.html',50 [cls.post.id]51 )52 cls.edit = (53 'posts:post_edit',54 'posts/create_post.html',55 [cls.post.id]56 )57 cls.create = (58 'posts:post_create',59 'posts/create_post.html',60 None61 )62 cls.page_obj = Post.objects.all()63 cls.ass_urls = (64 cls.index, cls.group_list, cls.profile,65 cls.detail, cls.edit, cls.create66 )67 @classmethod68 def setUp(cls):69 cls.guest_client = Client()70 cls.authorized_client = Client()71 cls.authorized_client.force_login(cls.user)72 cls.authorized_client_second = Client()73 cls.authorized_client_second.force_login(cls.user_second)74 cls.post_author = Client()75 cls.post_author.force_login(cls.user)76 def test_pages_uses_correct_template(self):77 """URL-адÑÐµÑ Ð¸ÑполÑзÑÐµÑ ÑооÑвеÑÑÑвÑÑÑий Ñаблон."""78 for route, template, args in self.ass_urls:79 with self.subTest(route=route):80 response = self.post_author.get(81 reverse(route, args=args)82 )83 self.assertTemplateUsed(response, template)84 def test_create_post_page_show_correct_context(self):85 """Шаблон create_post ÑÑоÑмиÑован Ñ Ð¿ÑавилÑнÑми Ñипами полей."""86 """ФоÑма ÑÐ¾Ð·Ð´Ð°Ð½Ð¸Ñ Ð¿Ð¾ÑÑа"""87 app_route, template, args_url = self.create88 response = self.authorized_client.get(89 reverse(app_route)90 )91 form_fields = {92 'text': forms.fields.CharField,93 'group': forms.fields.ChoiceField94 }95 for value, expected in form_fields.items():96 with self.subTest(value=value):97 form_field = response.context.get('form').fields.get(98 value99 )100 self.assertIsInstance(form_field, expected)101 def test_edit_post_page_show_correct_context(self):102 """Шаблон edit_post ÑÑоÑмиÑован Ñ Ð¿ÑавилÑнÑми Ñипами полей."""103 """ФоÑма ÑедакÑиÑÐ¾Ð²Ð°Ð½Ð¸Ñ Ð¿Ð¾ÑÑа"""104 app_route, template, args_url = self.edit105 response = self.post_author.get(106 reverse(app_route, args=args_url)107 )108 form_fields = {109 'text': forms.fields.CharField,110 'group': forms.fields.ChoiceField111 }112 for value, expected in form_fields.items():113 with self.subTest(value=value):114 form_field = response.context.get('form').fields.get(115 value116 )117 self.assertIsInstance(form_field, expected)118 self.assertEqual(119 response.context.get('post_selected').text,120 self.post.text121 )122 self.assertEqual(123 response.context.get('post_selected').group.title,124 self.group.title125 )126 def check_context(self, check_object, proof_object):127 self.assertEqual(check_object.id, proof_object.id,)128 self.assertEqual(check_object.text, proof_object.text,)129 self.assertEqual(check_object.author, proof_object.author)130 self.assertEqual(check_object.group.id, proof_object.group.id)131 self.assertEqual(check_object.group.slug, proof_object.group.slug)132 def check_fields(self, check_object, proof_object):133 self.assertEqual(check_object.title, proof_object.title)134 self.assertEqual(check_object.slug, proof_object.slug)135 self.assertEqual(check_object.description, proof_object.description)136 def index_page_show_correct_context(self):137 """Шаблон index ÑÑоÑмиÑован Ñ Ð¿ÑавилÑнÑм конÑекÑÑом."""138 """СпиÑок поÑÑов Ñ ÑоÑÑиÑовкой Ð¾Ñ Ð½Ð¾Ð²ÑÑ
к ÑÑаÑÑм"""139 app_route, template, args_url = self.index140 response = self.authorized_client.get(141 reverse(app_route)142 )143 first_post = len(self.page_obj) - 1144 first_object = response.context['page_obj'][first_post]145 base_object = self.post146 self.check_context(first_object, base_object)147 def test_group_posts_show_correct_context(self):148 """Шаблон group_posts ÑÑоÑмиÑован Ñ Ð¿ÑавилÑнÑм конÑекÑÑом."""149 """СпиÑок поÑÑов ÐÑÑппÑ"""150 app_route, template, args_url = self.group_list151 response = self.authorized_client.get(152 reverse(app_route, args=args_url)153 )154 group_fields = response.context.get('group')155 base_group_fields = self.group156 self.check_fields(group_fields, base_group_fields)157 def test_profile_show_correct_context(self):158 """Шаблон group_posts ÑÑоÑмиÑован Ñ Ð¿ÑавилÑнÑм конÑекÑÑом."""159 """СпиÑок поÑÑов полÑзоваÑелÑ"""160 app_route, template, args_url = self.profile161 response = self.authorized_client.get(162 reverse(app_route, args=args_url)163 )164 first_post = len(self.page_obj) - 1165 first_object = response.context['page_obj'][first_post]166 base_object = self.post167 self.assertEqual(168 response.context.get('author').username,169 self.post.author.username170 )171 self.assertTrue(172 response.context.get('is_profile')173 )174 self.check_context(first_object, base_object)175 def test_post_exist_index(self):176 """ÐÑовеÑÑем ÑÑо поÑÑ Ð¿Ð¾ÑвилÑÑ Ð½Ð° ÑÑÑаниÑе поÑÑов"""177 app_route, template, args_url = self.index178 response = self.authorized_client.get(179 reverse(app_route)180 )181 all_posts = response.context['page_obj']182 first_post = len(all_posts.object_list) - 1183 first_post_post = all_posts.object_list[first_post]184 base_object = self.post185 self.check_context(first_post_post, base_object)186 def test_post_exist_group_page(self):187 """ÐÑовеÑÑем, ÑÑо поÑÑ Ð¿Ð¾ÑвилÑÑ Ð½Ð° ÑÑÑаниÑе гÑÑппÑ"""188 app_route, template, args_url = self.group_list189 response = self.authorized_client.get(190 reverse(app_route, args=args_url)191 )192 all_posts = response.context['posts']193 first_post = len(all_posts) - 1194 self.assertEqual(195 all_posts[first_post].text,196 self.page_obj.latest('-pub_date').text197 )198 self.assertEqual(199 all_posts[first_post].group.slug,200 self.page_obj.latest('-pub_date').group.slug201 )202 self.assertEqual(203 all_posts[first_post].author.username,204 self.page_obj.latest('-pub_date').author.username205 )206 def test_post_exist_profile_page(self):207 """ÐÑовеÑÑем ÑÑо поÑÑ Ð¿Ð¾ÑвилÑÑ Ð² пÑоÑайле полÑзоваÑелÑ"""208 app_route, template, args_url = self.profile209 response = self.authorized_client.get(210 reverse(app_route, args=args_url)211 )212 all_posts = response.context['page_obj']213 self.assertIn(self.post, all_posts)214 self.assertEqual(215 response.context['author'].username,216 self.post.author.username217 )218 def test_post_not_exist_in_different_group_page(self):219 """ÐÑовеÑÑем, ÑÑо поÑÑ ÐРпоÑвилÑÑ Ð½Ð° ÑÑÑаниÑе не Ñвоей гÑÑппÑ"""220 app_route, template, args_url = self.group_list_other221 response = self.authorized_client.get(222 reverse(app_route, args=args_url)223 )...
tests_forms.py
Source:tests_forms.py
1from django.contrib.auth import get_user_model2from django.test import Client, TestCase3from django.urls import reverse4from posts.forms import PostForm5from posts.models import Group, Post, User6User = get_user_model()7class PostCreateFormTests(TestCase):8 @classmethod9 def setUpClass(cls):10 super().setUpClass()11 cls.user = User.objects.create_user(username='LucyTesterForm')12 cls.user_author = User.objects.create_user(username='LucyTestAuthor')13 cls.group = Group.objects.create(14 title='LucyFormTesterGroup_titles',15 slug='LucyFormTesterGroup_slug',16 description='ÐÑа гÑÑппа Ñоздана Ð´Ð»Ñ ÑеÑÑиÑÐ¾Ð²Ð°Ð½Ð¸Ñ Form'17 )18 cls.post = Post.objects.create(19 text='1 ТеÑÑовÑй ÑекÑÑ Ð¿Ð¾ÑÑ',20 group=cls.group,21 author=cls.user_author22 )23 cls.form = PostForm()24 cls.detail = (25 'posts:post_detail',26 'posts/post_detail.html',27 [cls.post.id]28 )29 cls.edit = (30 'posts:post_edit',31 'posts/create_post.html',32 [cls.post.id]33 )34 cls.create = (35 'posts:post_create',36 'posts/create_post.html',37 None38 )39 cls.ass_urls = (40 cls.detail, cls.edit41 )42 @classmethod43 def setUp(cls):44 cls.authorized_client = Client()45 cls.authorized_client.force_login(cls.user)46 cls.post_author = User.objects.get(username='LucyTestAuthor')47 cls.post_author = Client()48 cls.post_author.force_login(cls.user_author)49 def test_create_post(self):50 """ÐÐ°Ð»Ð¸Ð´Ð½Ð°Ñ ÑоÑма ÑÐ¾Ð·Ð´Ð°ÐµÑ Ð·Ð°Ð¿Ð¸ÑÑ Ð² Post."""51 tasks_count = Post.objects.count()52 form_data = {53 'text': 'ТеÑÑовÑй ÑекÑÑ Ð¾Ñ LucyTesterForm',54 'group': self.group.id55 }56 response = self.authorized_client.post(57 reverse('posts:post_create'),58 data=form_data,59 follow=True60 )61 self.assertRedirects(62 response,63 reverse(64 'posts:profile',65 kwargs={'username': f'{self.user.username}'}66 )67 )68 self.assertEqual(Post.objects.count(), tasks_count + 1)69 def test_edit_post(self):70 """ÐÑовеÑÑем, ÑÑо пÑоиÑÑ
Ð¾Ð´Ð¸Ñ Ð¸Ð·Ð¼ÐµÐ½ÐµÐ½Ð¸Ðµ поÑÑа"""71 app_route, template, args_url = self.edit72 response = self.post_author.get(73 reverse(app_route, args=args_url)74 )75 author_post = response.context['post_selected']76 upd_form_data = {77 'text': 'UPD 1 ТеÑÑовÑй ÑекÑÑ Ð¿Ð¾ÑÑ',78 'group': author_post.group.id,79 'post_author': self.user_author.username80 }81 response = self.post_author.post(82 reverse(app_route, args=args_url),83 data=upd_form_data,84 follow=True85 )86 self.assertRedirects(87 response,88 reverse(89 'posts:post_detail',90 kwargs={'post_id': f'{self.post.id}'}91 )92 )93 def test_fields_created_post(self):94 """ТеÑÑиÑÑем ÑÑо Ð¿Ð¾Ð»Ñ Ð½Ð¾Ð²Ð¾Ð³Ð¾ поÑÑа ÑоÑ
ÑанилиÑÑ"""95 app_route, template, args_url = self.create96 form_data = {97 'text': 'СамÑй новÑй ÑеÑÑовÑй ÑекÑÑ Ð¾Ñ LucyTesterForm',98 'group': self.group.id99 }100 self.authorized_client.post(101 reverse(app_route),102 data=form_data,103 follow=True104 )105 last_object = Post.objects.first()106 fields_dict = {107 last_object.author.username: self.user.username,108 last_object.text: form_data['text'],109 last_object.group.id: self.group.id110 }111 for field, expected in fields_dict.items():112 with self.subTest(field=field):113 self.assertEqual(field, expected)114 def test_fields_updates_post(self):115 """ТеÑÑиÑÑем ÑÑо Ð¿Ð¾Ð»Ñ Ð¿ÑоапдеÑенного поÑÑа ÑоÑ
ÑанилиÑÑ"""116 app_route, template, args_url = self.edit117 response = self.post_author.get(118 reverse(app_route, args=args_url)119 )120 author_post = response.context['post_selected']121 upd_form_data = {122 'text': 'UPD 1 ТеÑÑовÑй ÑекÑÑ Ð¿Ð¾ÑÑ',123 'group': author_post.group.id,124 'post_author': self.user_author.username125 }126 response = self.post_author.post(127 reverse(app_route, args=args_url),128 data=upd_form_data,129 follow=True130 )131 edited_post = Post.objects.last()132 fields_dict = {133 edited_post.author.username: self.user_author.username,134 edited_post.text: upd_form_data['text'],135 edited_post.group.id: self.group.id136 }137 for field, expected in fields_dict.items():138 with self.subTest(field=field):...
app_router.py
Source:app_router.py
1from flask import Blueprint, render_template2import db.mysql3app_route = Blueprint('app_route',__name__)4@app_route.route("/")5def index():6 # Flask ë ëë¬7 db.mysql.Connect()8 return render_template("index.html")9@app_route.route('/healthz')10def healthEcho():11 # K8S Health check 12 return 'Hello flask, i\'m alive!'13@app_route.route('/headers')14def headers():15 # HTTP ìì¸í ì°ì´ë³´ê¸° (ê°ë° í
ì¤í¸)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!