Best Python code snippet using django-test-plus_python
views.py
Source: views.py
1from rest_framework import generics, status2from .serializers import *3from core.models import *4from core.api.filter import *5from rest_framework.response import Response6from core.api.authentication import *7from rest_framework.permissions import IsAuthenticated8from django_filters.rest_framework import DjangoFilterBackend9response_204 = Response({'status': 204, 'description': 'OK'}, status=status.HTTP_204_NO_CONTENT)10response_400 = Response({'status': 400, 'description': 'Bad request.'}, status=status.HTTP_400_BAD_REQUEST)11response_500 = Response({'status': 500, 'description': 'There is an internal issue.'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)12response_200 = Response({'status': 200, 'description': 'OK.'}, status=status.HTTP_200_OK)13class InsertMovie(generics.CreateAPIView):14 permission_classes = (AdminPermission,)15 serializer_class = MovieSerializer16 def post(self, request, *args, **kwargs):17 if set(request.data.keys()) == set(['name', 'description']):18 try:19 Movie.objects.create(name=request.data['name'], description=request.data['description'])20 return response_20421 except: return response_50022 else: return response_40023class ManageMovie(generics.RetrieveUpdateDestroyAPIView):24 serializer_class = MovieSerializer25 permission_classes = (AdminPermission,)26 def put(self, request, *args, **kwargs):27 if set(request.data.keys()) == set(['name', 'description']):28 try:29 Movie.objects.get(id=kwargs['pk'])30 Movie.objects.filter(id=kwargs['pk']).update(name=request.data['name'], description=request.data['description'])31 return response_20432 except: return response_40033 else: return response_40034 def delete(self, request, *args, **kwargs):35 try:36 movie = Movie.objects.get(id=kwargs['pk'])37 movie.delete()38 return response_20439 except: return response_40040class ManageComment(generics.RetrieveUpdateDestroyAPIView):41 serializer_class = CommentSerializerAdmin 42 def put(self, request, *args, **kwargs):43 if set(request.data.keys()) == set(['approved']):44 try:45 Comment.objects.get(id=kwargs['pk'])46 Comment.objects.filter(id=kwargs['pk']).update(approved=request.data['approved'])47 return response_20448 except: return response_40049 else: return response_40050 def delete(self, request, *args, **kwargs):51 try:52 comment = Comment.objects.get(id=kwargs['pk'])53 comment.delete()54 return response_20455 except: return response_40056class UserVote(generics.CreateAPIView):57 serializer_class = VoteSerializer58 permission_classes = (UserPermission,)59 def post(self, request, *args, **kwargs):60 if set(request.data.keys()) == set(['movie_id', 'vote']):61 try:62 Vote.objects.create(rating=request.data['vote'], movie_id=request.data['movie_id'], user=request.user)63 return response_20464 except: return response_50065 else: return response_40066class UserComment(generics.CreateAPIView):67 serializer_class = CommentSerializer68 permission_classes = (UserPermission,)69 def post(self, request, *args, **kwargs):70 if set(request.data.keys()) == set(['movie_id', 'comment_body']):71 try:72 Movie.objects.get(id=request.data['movie_id'])73 Comment.objects.create(comment=request.data['comment_body'], movie_id=request.data['movie_id'], user=request.user)74 return response_20075 except: return response_40076 else: return response_40077class CommentAPI(generics.ListAPIView):78 permission_classes = (AnonymousPermission,)79 filter_backends = [DjangoFilterBackend]80 filter_class = CommentFilter81 serializer_class = GetCommentSerializer82 def get(self, request, *args, **kwargs):83 try:84 mv = Movie.objects.filter(id=request.query_params['movie_id'])85 mv[0]86 except:87 return response_40088 return super().get(request, *args, **kwargs)89 def get_queryset(self):90 try:91 return Comment.objects.filter(movie_id=self.request.query_params['movie_id'])92 except:93 return Comment.objects.none()94class MovieAPI(generics.ListAPIView):95 permission_classes = (AnonymousPermission,)96 serializer_class = GetMovieSerializer97 def get_queryset(self):98 return Movie.objects.all().order_by('id')99class MovieWithIDAPI(generics.ListAPIView):100 permission_classes = (AnonymousPermission,)101 serializer_class = MovieIDSerializer102 def get(self, request, *args, **kwargs):103 try:104 Movie.objects.get(id=kwargs['pk'])105 except:106 return response_400107 return super().get(request, *args, **kwargs)108 def get_queryset(self):...
osidb_api_v1_affects_destroy.py
Source: osidb_api_v1_affects_destroy.py
1from typing import Any, Dict, Optional2import requests3from ...client import AuthenticatedClient4from ...models.osidb_api_v1_affects_destroy_response_204 import OsidbApiV1AffectsDestroyResponse2045from ...types import UNSET, Response, Unset6def _get_kwargs(7 uuid: str,8 *,9 client: AuthenticatedClient,10) -> Dict[str, Any]:11 url = "{}/osidb/api/v1/affects/{uuid}".format(12 client.base_url,13 uuid=uuid,14 )15 headers: Dict[str, Any] = client.get_headers()16 return {17 "url": url,18 "headers": headers,19 }20def _parse_response(*, response: requests.Response) -> Optional[OsidbApiV1AffectsDestroyResponse204]:21 if response.status_code == 204:22 _response_204 = response.json()23 response_204: OsidbApiV1AffectsDestroyResponse20424 if isinstance(_response_204, Unset):25 response_204 = UNSET26 else:27 response_204 = OsidbApiV1AffectsDestroyResponse204.from_dict(_response_204)28 return response_20429 return None30def _build_response(*, response: requests.Response) -> Response[OsidbApiV1AffectsDestroyResponse204]:31 return Response(32 status_code=response.status_code,33 content=response.content,34 headers=response.headers,35 parsed=_parse_response(response=response),36 )37def sync_detailed(38 uuid: str,39 *,40 client: AuthenticatedClient,41) -> Response[OsidbApiV1AffectsDestroyResponse204]:42 kwargs = _get_kwargs(43 uuid=uuid,44 client=client,45 )46 response = requests.delete(47 verify=client.verify_ssl,48 auth=client.auth,49 timeout=client.timeout,50 **kwargs,51 )52 response.raise_for_status()53 return _build_response(response=response)54def sync(55 uuid: str,56 *,57 client: AuthenticatedClient,58) -> Optional[OsidbApiV1AffectsDestroyResponse204]:59 """ """60 return sync_detailed(61 uuid=uuid,62 client=client,...
osidb_api_v1_flaws_destroy.py
Source: osidb_api_v1_flaws_destroy.py
1from typing import Any, Dict, Optional2import requests3from ...client import AuthenticatedClient4from ...models.osidb_api_v1_flaws_destroy_response_204 import OsidbApiV1FlawsDestroyResponse2045from ...types import UNSET, Response, Unset6def _get_kwargs(7 id: str,8 *,9 client: AuthenticatedClient,10) -> Dict[str, Any]:11 url = "{}/osidb/api/v1/flaws/{id}".format(12 client.base_url,13 id=id,14 )15 headers: Dict[str, Any] = client.get_headers()16 return {17 "url": url,18 "headers": headers,19 }20def _parse_response(*, response: requests.Response) -> Optional[OsidbApiV1FlawsDestroyResponse204]:21 if response.status_code == 204:22 _response_204 = response.json()23 response_204: OsidbApiV1FlawsDestroyResponse20424 if isinstance(_response_204, Unset):25 response_204 = UNSET26 else:27 response_204 = OsidbApiV1FlawsDestroyResponse204.from_dict(_response_204)28 return response_20429 return None30def _build_response(*, response: requests.Response) -> Response[OsidbApiV1FlawsDestroyResponse204]:31 return Response(32 status_code=response.status_code,33 content=response.content,34 headers=response.headers,35 parsed=_parse_response(response=response),36 )37def sync_detailed(38 id: str,39 *,40 client: AuthenticatedClient,41) -> Response[OsidbApiV1FlawsDestroyResponse204]:42 kwargs = _get_kwargs(43 id=id,44 client=client,45 )46 response = requests.delete(47 verify=client.verify_ssl,48 auth=client.auth,49 timeout=client.timeout,50 **kwargs,51 )52 response.raise_for_status()53 return _build_response(response=response)54def sync(55 id: str,56 *,57 client: AuthenticatedClient,58) -> Optional[OsidbApiV1FlawsDestroyResponse204]:59 """ """60 return sync_detailed(61 id=id,62 client=client,...
Check out the latest blogs from LambdaTest on this topic:
“Test frequently and early.” If you’ve been following my testing agenda, you’re probably sick of hearing me repeat that. However, it is making sense that if your tests detect an issue soon after it occurs, it will be easier to resolve. This is one of the guiding concepts that makes continuous integration such an effective method. I’ve encountered several teams who have a lot of automated tests but don’t use them as part of a continuous integration approach. There are frequently various reasons why the team believes these tests cannot be used with continuous integration. Perhaps the tests take too long to run, or they are not dependable enough to provide correct results on their own, necessitating human interpretation.
How do we acquire knowledge? This is one of the seemingly basic but critical questions you and your team members must ask and consider. We are experts; therefore, we understand why we study and what we should learn. However, many of us do not give enough thought to how we learn.
While there is a huge demand and need to run Selenium Test Automation, the experts always suggest not to automate every possible test. Exhaustive Testing is not possible, and Automating everything is not sustainable.
It’s strange to hear someone declare, “This can’t be tested.” In reply, I contend that everything can be tested. However, one must be pleased with the outcome of testing, which might include failure, financial loss, or personal injury. Could anything be tested when a claim is made with this understanding?
Agile project management is a great alternative to traditional methods, to address the customer’s needs and the delivery of business value from the beginning of the project. This blog describes the main benefits of Agile for both the customer and the business.
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!!