Best Python code snippet using hypothesis
oncall_handler.py
Source:oncall_handler.py
...14 oncall = ''15 if oncall_type == 'desktop':16 if display_type == 'email':17 result = User.objects.select_related().filter(userprofile__current_desktop_oncall=1)[0]18 oncall = {'user':result.username, 'pager_type': result.get_profile().pager_type, 'epager_address': result.get_profile().epager_address, 'pager_number': result.get_profile().pager_number}19 elif display_type == 'irc_nick':20 result = User.objects.select_related().filter(userprofile__current_desktop_oncall=1)[0]21 oncall = {'user':result.get_profile().irc_nick, 'pager_type': result.get_profile().pager_type, 'epager_address': result.get_profile().epager_address, 'pager_number': result.get_profile().pager_number}22 elif display_type == 'all':23 oncall = []24 list = User.objects.select_related().filter(userprofile__is_desktop_oncall=1)25 for u in list:26 oncall.append({'user':u.get_profile().irc_nick, 'pager_type': u.get_profile().pager_type, 'pager_number': u.get_profile().pager_number})27 elif oncall_type == 'sysadmin':28 if display_type == 'email':29 result = User.objects.select_related().filter(userprofile__current_sysadmin_oncall=1)[0]30 oncall = {'user':result.username, 'pager_type': result.get_profile().pager_type, 'epager_address': result.get_profile().epager_address, 'pager_number': result.get_profile().pager_number}31 elif display_type == 'irc_nick':32 result = User.objects.select_related().filter(userprofile__current_sysadmin_oncall=1)[0]33 oncall = {'user':result.get_profile().irc_nick, 'pager_type': result.get_profile().pager_type, 'epager_address': result.get_profile().epager_address, 'pager_number': result.get_profile().pager_number}34 elif display_type == 'all':35 oncall = []36 list = User.objects.select_related().filter(userprofile__is_sysadmin_oncall=1)37 for u in list:38 oncall.append({'user':u.get_profile().irc_nick, 'pager_type': u.get_profile().pager_type, 'pager_number': u.get_profile().pager_number})39 elif oncall_type == 'services':40 if display_type == 'email':41 result = User.objects.select_related().filter(userprofile__current_services_oncall=1)[0]42 oncall = {'user':result.username, 'pager_type': result.get_profile().pager_type, 'epager_address': result.get_profile().epager_address, 'pager_number': result.get_profile().pager_number}43 elif display_type == 'irc_nick':44 result = User.objects.select_related().filter(userprofile__current_services_oncall=1)[0]45 oncall = {'user':result.get_profile().irc_nick, 'pager_type': result.get_profile().pager_type, 'epager_address': result.get_profile().epager_address, 'pager_number': result.get_profile().pager_number}46 elif display_type == 'all':47 oncall = []48 list = User.objects.select_related().filter(userprofile__is_services_oncall=1)49 for u in list:50 oncall.append({'user':u.get_profile().irc_nick, 'pager_type': u.get_profile().pager_type, 'pager_number': u.get_profile().pager_number})51 else:52 resp = rc.NOT_FOUND53 return resp54 return oncall55 def update(self, request, oncall_type = None, display_type=None):56 user = display_type57 from django.db import connection, transaction58 if request.method == 'PUT':59 if oncall_type == 'setdesktop':60 cursor = connection.cursor()61 cursor.execute("UPDATE `user_profiles` set `current_desktop_oncall` = 0")62 transaction.commit_unless_managed()63 if re.match("^[a-zA-Z0-9._%-]+@[a-zA-Z0-9._%-]+.[a-zA-Z]{2,6}$", user):64 new_oncall = User.objects.get(username=user)65 else:66 new_oncall = User.objects.get(userprofile__irc_nick=user)67 new_oncall.get_profile().current_desktop_oncall = 168 new_oncall.get_profile().save()69 new_oncall.save()70 resp = rc.ALL_OK71 return resp72 73 elif oncall_type == 'setsysadmin':74 cursor = connection.cursor()75 cursor.execute("UPDATE `user_profiles` set `current_sysadmin_oncall` = 0")76 transaction.commit_unless_managed()77 if re.match("^[a-zA-Z0-9._%-]+@[a-zA-Z0-9._%-]+.[a-zA-Z]{2,6}$", user):78 new_oncall = User.objects.get(username=user)79 else:80 new_oncall = User.objects.get(userprofile__irc_nick=user)81 new_oncall.get_profile().current_sysadmin_oncall = 182 new_oncall.get_profile().save()83 new_oncall.save()84 resp = rc.ALL_OK85 return resp86 elif oncall_type == 'setservices':87 cursor = connection.cursor()88 cursor.execute("UPDATE `user_profiles` set `current_services_oncall` = 0")89 transaction.commit_unless_managed()90 if re.match("^[a-zA-Z0-9._%-]+@[a-zA-Z0-9._%-]+.[a-zA-Z]{2,6}$", user):91 new_oncall = User.objects.get(username=user)92 else:93 new_oncall = User.objects.get(userprofile__irc_nick=user)94 new_oncall.get_profile().current_services_oncall = 195 new_oncall.get_profile().save()96 new_oncall.save()97 resp = rc.ALL_OK98 return resp99 else:100 resp = rc.NOT_FOUND101 return resp102 def delete(self, request, oncall_type=None):...
tests.py
Source:tests.py
1import pytest2from profile import get_profile3def test_get_profile_no_name():4 with pytest.raises(TypeError):5 assert get_profile()6def test_get_profile_no_age():7 with pytest.raises(TypeError):8 assert get_profile('tim')9def test_get_profile_valueerror():10 with pytest.raises(ValueError):11 assert get_profile('tim', 'nonint')12def test_get_profile_too_many_sports():13 with pytest.raises(ValueError):14 sports = ['tennis', 'basketball', 'badminton',15 'baseball', 'volleyball', 'boxing']16 assert get_profile('tim', 36, *sports)17def test_get_profile_dict():18 assert get_profile('tim', 36) == {'name': 'tim', 'age': 36}19def test_get_profile_one_sport():20 expected = {'name': 'tim', 'age': 36,21 'sports': ['tennis']}22 assert get_profile('tim', 36, 'tennis') == expected23def test_get_profile_two_sports():24 expected = {'name': 'tim', 'age': 36,25 'sports': ['basketball', 'tennis']}26 assert get_profile('tim', 36, 'tennis', 'basketball') == expected27def test_get_profile_award():28 expected = {'name': 'tim', 'age': 36,29 'awards': {'champ': 'helped out team in crisis'}}30 assert get_profile('tim', 36,31 champ='helped out team in crisis') == expected32def test_get_profile_two_sports_and_one_award():33 expected = {'name': 'tim', 'age': 36,34 'sports': ['basketball', 'tennis'],35 'awards': {'champ': 'helped out team in crisis'}}36 assert get_profile('tim', 36, 'tennis', 'basketball',37 champ='helped out team in crisis') == expected38def test_get_profile_two_sports_and_three_awards():39 expected = {'name': 'tim', 'age': 36,40 'sports': ['basketball', 'tennis'],41 'awards': {'champ': 'helped out the team in crisis',42 'service': 'going the extra mile for our customers',43 'attitude': 'unbeatable positive + uplifting'}}44 assert get_profile('tim', 36, 'tennis', 'basketball',45 service='going the extra mile for our customers',46 champ='helped out the team in crisis',...
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!!