Best Python code snippet using autotest_python
unit_tests.py
Source:unit_tests.py  
1from datetime import datetime, date, time2import pytest3from fastapi import HTTPException4from app.models.graphql.winner_model import Winner5from app.objects.all_users import AllUsers6from app.objects.all_words import AllWords7from app.objects.category import Category8from app.objects.user import User9from app.objects.word import Word10from app.utils.constants import DATE_FORMAT11def test_guessed_word_adding_with_cat():12    test_user = User("Billie")13    word = Word("tree", 100, "nature")14    test_user.add_guessed_word(word)15    assert (test_user.guessed_words_by_cat["nature"][0].word == "tree")16    assert (len(test_user.guessed_words) == 1)17    assert (test_user.guessed_words[word.word].word == "tree")18def test_guessed_word_adding_without_cat():19    test_user = User("Sam")20    test_string = "nothing"21    word = Word("tree", 100, "")22    test_user.add_guessed_word(word)23    if not test_user.guessed_words_by_cat:24        test_string = "empty"25    assert (test_user.guessed_words[word.word].word == "tree")26    assert (len(test_user.guessed_words) == 1)27    assert (test_string == "empty")28def test_guessed_word_adding_with_cat_twice():29    test_user = User("Bob")30    word = Word("tree", 100, "nature")31    word2 = Word("tree", 100, "nature")32    test_user.add_guessed_word(word)33    test_user.add_guessed_word(word2)34    assert (test_user.guessed_words_by_cat["nature"][0].word == "tree")35    assert (len(test_user.guessed_words) == 1)36    assert (test_user.guessed_words[word.word].word == "tree")37    word3 = Word("bear", 150, "nature")38    word4 = Word("fox", 150, "nature")39    test_user.add_guessed_word(word3)40    assert (test_user.guessed_words.get(word4) is None)41def test_rating_after_init():42    test_user = User("Alice")43    assert (test_user.get_rating() == 0)44def test_rating_after_changing():45    test_user = User("Clover")46    test_user.rating += 10047    assert (test_user.get_rating() == 100)48def test_words_after_init():49    test_user = User("Alex")50    assert (test_user.get_words(Category("")) == [])51def test_words_without_category():52    test_user = User("Will")53    word1 = Word("school", 100, "")54    word2 = Word("bird", 100, "")55    word3 = Word("summary", 100, "")56    test_user.guessed_words[word1.word] = word157    test_user.guessed_words[word2.word] = word258    test_user.guessed_words[word3.word] = word359    expected = [word1.word, word2.word, word3.word]60    actual = list(test_user.get_words("").keys())61    assert (actual == expected)62def test_words_with_category():63    test_user = User("Jacob")64    word1 = Word("school", 100, "study")65    word2 = Word("bird", 100, "animals")66    word3 = Word("pencil", 100, "study")67    test_user.add_guessed_word(word1)68    test_user.add_guessed_word(word2)69    test_user.add_guessed_word(word3)70    expected1 = [word1.word, word2.word, word3.word]71    actual1 = list(test_user.get_words("").keys())72    expected2 = [word1.word, word3.word]73    actual2 = [w.word for w in list(test_user.get_words("study"))]74    assert (actual2 == expected2)75    assert (actual1 == expected1)76def test_adding_user_zero_users():77    test_users = AllUsers()78    assert (len(test_users.users) == 0)79def test_adding_user_three_users():80    test_users = AllUsers()81    user1 = User("Ashley")82    user2 = User("Oliver")83    user3 = User("Laura")84    test_users.add_user(user1)85    test_users.add_user(user2)86    test_users.add_user(user3)87    assert (len(test_users.users) == 3)88def test_num_of_users_zero_users():89    test_users = AllUsers()90    assert (test_users.num_of_users() == 0)91def test_adding_user_two_users():92    test_users = AllUsers()93    user1 = User("Jackie")94    user2 = User("Ronald")95    test_users.add_user(user1)96    test_users.add_user(user2)97    assert (test_users.num_of_users() == 2)98def test_get_user_no_such_id_too_big():99    with pytest.raises(HTTPException):100        test_users = AllUsers()101        user1 = User("Harry")102        test_users.add_user(user1)103        test_users.get_user(10)104def test_get_user_no_such_id_neg():105    with pytest.raises(HTTPException):106        test_users = AllUsers()107        user1 = User("Lily")108        test_users.add_user(user1)109        test_users.get_user(-2)110def test_get_user_such_id_exists():111    test_users = AllUsers()112    user1 = User("James")113    user2 = User("Angelina")114    user3 = User("Fred")115    test_users.add_user(user1)116    test_users.add_user(user2)117    test_users.add_user(user3)118    assert (test_users.get_user(2).id == 2)119def test_get_global_rating():120    test_users = AllUsers()121    user1 = User("George")122    user1.rating = 200123    user2 = User("Luna")124    user2.rating = 100125    user3 = User("William")126    user3.rating = 200127    user4 = User("Beth")128    user4.rating = 300129    test_users.add_user(user1)130    test_users.add_user(user2)131    test_users.add_user(user3)132    test_users.add_user(user4)133    tmp = test_users.get_rating()134    assert ([u.id for u in tmp] == [3, 0, 2, 1])135def test_word_adding_with_cat():136    test_hat = AllWords()137    word = Word("tree", 100, "nature")138    test_hat.add_word(word)139    assert (len(test_hat.words) == 1)140    assert (test_hat.words[0].word == "tree")141def test_word_adding_without_cat():142    test_hat = AllWords()143    word = Word("tree", 100, "")144    test_hat.add_word(word)145    assert (len(test_hat.words) == 1)146    assert (test_hat.words[0].word == "tree")147def test_show_next_word():148    test_hat = AllWords()149    word1 = Word("school", 100, "study")150    word2 = Word("bird", 100, "animals")151    word3 = Word("pencil", 100, "study")152    test_hat.add_word(word1)153    test_hat.add_word(word2)154    test_hat.add_word(word3)155    next_word = test_hat.get_next_word()156    assert (next_word.word == "school")157    assert (len(test_hat.words) == 2)158    next_word = test_hat.get_next_word()159    assert (next_word.word == "bird")160    assert (len(test_hat.words) == 1)161    next_word = test_hat.get_next_word()162    assert (next_word.word == "pencil")163    assert (len(test_hat.words) == 0)164    next_word = test_hat.get_next_word()165    assert (next_word.word == "")166    assert (len(test_hat.words) == 0)167def test_add_to_storage():168    test_users = AllUsers()169    win_date = "2001-04-28"170    winner1 = User("Kate")171    test_users.storage.add(win_date, winner1)172    assert (len(test_users.storage.storage) == 1)173    assert (len(test_users.storage.win_dates) == 1)174    assert (test_users.storage.storage[date(2001, 4, 28)].name == "Kate")175def test_add_to_storage_dif_dates():176    test_users = AllUsers()177    win_date1 = "2001-04-28"178    win_date2 = "2001-04-29"179    winner1 = User("Kate")180    winner2 = User("Julia")181    test_users.storage.add(win_date1, winner1)182    assert (len(test_users.storage.storage) == 1)183    assert (len(test_users.storage.win_dates) == 1)184    assert (test_users.storage.storage[date(2001, 4, 28)].name == "Kate")185    test_users.storage.add(win_date2, winner2)186    assert (len(test_users.storage.storage) == 2)187    assert (len(test_users.storage.win_dates) == 2)188    assert (test_users.storage.storage[date(2001, 4, 29)].name == "Julia")189def test_add_to_storage_same_dates():190    test_users = AllUsers()191    win_date = "2001-04-28"192    winner1, winner2 = User("Kate"), User("Julia")193    test_users.storage.add(win_date, winner1)194    assert (len(test_users.storage.storage) == 1)195    assert (len(test_users.storage.win_dates) == 1)196    assert (test_users.storage.storage[date(2001, 4, 28)].name == "Kate")197    test_users.storage.add(win_date, winner2)198    assert (len(test_users.storage.storage) == 1)199    assert (len(test_users.storage.win_dates) == 1)200    assert (test_users.storage.storage[date(2001, 4, 28)].name == "Julia")201def test_get_no_such_key():202    test_users = AllUsers()203    win_date = "2001-04-28"204    assert (len(test_users.storage.storage) == 0)205    assert (len(test_users.storage.win_dates) == 0)206    assert (test_users.storage.storage.get(win_date) is None)207def test_get_such_key_exists():208    test_users = AllUsers()209    test_users.storage.storage["1996-03-15"] = Winner(1, 20, "Max")210    assert (len(test_users.storage.storage) == 1)211    assert (test_users.storage.storage.get("1996-03-15").name == "Max")212if __name__ == '__main__':...test_view_documents.py
Source:test_view_documents.py  
1from conftest import test_users, make_header2test_input = {"name" : "test", "content": "This is a test."}3def test_post_empty(client):4	response = client.post('/api/documents', 5		headers=make_header(test_users[0]["username"], 6			test_users[0]["password"]),7		json=None)8	assert response.status_code == 4009	assert "message" in response.json10def test_post_no_content(client):11	response = client.post('/api/documents', 12		headers=make_header(test_users[0]["username"], 13			test_users[0]["password"]),14		json={"name": "test"})15	assert response.status_code == 40016	assert "message" in response.json17def test_post_no_name(client):18	response = client.post('/api/documents', 19		headers=make_header(test_users[0]["username"], 20			test_users[0]["password"]),21		json={"content": "This is a test."})22	assert response.status_code == 40023	assert "message" in response.json24def test_post_normal(client):25	response = client.post('/api/documents', 26		headers=make_header(test_users[0]["username"], 27			test_users[0]["password"]),28		json=test_input)29	assert response.status_code == 20130	assert "Location" in response.headers31	assert "message" in response.json32	assert "id" in response.json33def test_post_repeat(client):34	response = client.post('/api/documents', 35		headers=make_header(test_users[0]["username"], 36			test_users[0]["password"]),37		json=test_input)38	assert response.status_code == 20139	assert "message" in response.json40	response = client.post('/api/documents', 41		headers=make_header(test_users[0]["username"], 42			test_users[0]["password"]),43		json=test_input)44	assert response.status_code == 30245	assert "message" in response.json46def test_post_normal_no_auth(client):47	response = client.post('/api/documents',48		json=test_input)49	assert response.status_code == 40150def test_delete_normal(client):51	response = client.post('/api/documents', 52		headers=make_header(test_users[0]["username"], 53			test_users[0]["password"]),54		json=test_input)55	assert response.status_code == 20156	assert "Location" in response.headers57	assert "message" in response.json58	assert "id" in response.json59	location = response.headers["Location"].split("/")60	id = None61	62	for w in location:63		try:64			id = int(w)65		except ValueError:66			pass67	assert id is not None68	response = client.delete('/api/documents/' + str(id),69		headers=make_header(test_users[0]["username"], 70			test_users[0]["password"]))71	assert response.status_code == 20072	assert "message" in response.json73	response = client.delete('/api/documents/' + str(id),74		headers=make_header(test_users[0]["username"], 75			test_users[0]["password"]))76	assert response.status_code == 40077	assert "message" in response.json78def test_delete_wrong_user(client):79	response = client.post('/api/documents', 80		headers=make_header(test_users[0]["username"], 81			test_users[0]["password"]),82		json=test_input)83	assert response.status_code == 20184	assert "Location" in response.headers85	assert "message" in response.json86	assert "id" in response.json87	location = response.headers["Location"].split("/")88	id = None89	90	for w in location:91		try:92			id = int(w)93		except ValueError:94			pass95	assert id is not None96	response = client.delete('/api/documents/' + str(id),97		headers=make_header(test_users[1]["username"], 98			test_users[1]["password"]))99	assert response.status_code == 403100	assert "message" in response.json101	response = client.delete('/api/documents/' + str(id),102		headers=make_header(test_users[0]["username"], 103			test_users[0]["password"]))104	assert response.status_code == 200...wallTest.py
Source:wallTest.py  
1import utils2import urls3import requests4import json5def do_wall_permission_test():6    #create test user7    test_users = []8    for i in range(0, 2):9        username = 'test' + str(i)10        test_user = ( username, username + '@test.com', '1234qwer' )11        utils.delete_user( test_user[0] )12        utils.sign_up_success( test_user[0], test_user[1], test_user[2] )13        test_users.append( test_user )14    15    # post wall fail because they are not friend16    utils.post_wall_fail( (test_users[0][0], test_users[0][2]), test_users[1][0], 'test0 posts this posting' )17    utils.post_wall_fail( (test_users[1][0], test_users[1][2]), test_users[0][0], 'test1 posts this posting' )18    # get wall fail because they are not friend19    utils.get_wall_posts_fail( ( test_users[0][0], test_users[0][2] ), test_users[1][0] )20    utils.get_wall_posts_fail( ( test_users[1][0], test_users[1][2] ), test_users[0][0] )21    # make friend..22    utils.post_friendrequest( ( test_users[0][0], test_users[0][2] ), test_users[1][0] )23    request_id=[]24    request_id.append(0)25    request_id.append( utils.get_friendrequest( (test_users[1][0], test_users[1][2]), test_users[0][0] ) )26    utils.accept_friendrequest( ( test_users[1][0], test_users[1][2]), request_id[1] )27    # post wall success because they are friend28    utils.post_wall_success( (test_users[0][0], test_users[0][2]), test_users[1][0], 'test0 posts this posting' )29    utils.post_wall_success( (test_users[1][0], test_users[1][2]), test_users[0][0], 'test1 posts this posting' )30    # get wall success because they are friend31    utils.get_wall_posts_success( ( test_users[0][0], test_users[0][2] ), test_users[1][0] )32    utils.get_wall_posts_success( ( test_users[1][0], test_users[1][2] ), test_users[0][0] )33print( 'wall permission test, i.e. show only posts of friends and user & only user and friend can post\n' )34do_wall_permission_test()...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!!
