Best Python code snippet using tempest_python
tests.py
Source:tests.py
1from django.test import TestCase2from .models import ListingCategory, Listing, ListingComment3from user_accounts_app.models import UserAccount4from django.contrib.messages import get_messages5# Create your tests here.6# Test Cases needed:7# 1. A person can create a listing DONE8# 2. A person can update a listing's details9# 3. A person can delete a listing10class ListingCategoryTest(TestCase):11 def testCanCreateListingCategory(self):12 LC = ListingCategory(name="furniture",description="""13 Furniture refers to movable objects intended to support various human activities such as seating 14 (e.g., chairs, stools, and sofas), eating (tables), and sleeping (e.g., beds). """)15 LC.save()16 17 LC2 = ListingCategory.objects.all().get(pk=LC.id)18 self.assertEquals(LC.name,LC2.name)19 self.assertEquals(LC.description,LC2.description)20 21 def testCanUpdateListingCategory(self):22 LC = ListingCategory(name="furniture",description="""23 Furniture refers to movable objects intended to support various human activities such as seating 24 (e.g., chairs, stools, and sofas), eating (tables), and sleeping (e.g., beds). """)25 LC.save()26 27 LC.name="fashion"28 LC.description="Fashion is a popular style, especially in clothing, footwear, lifestyle, accessories, makeup, hairstyle and body. Fashion is a distinctive and often constant trend in the style in which people present themselves."29 LC.save()30 31 32 lc_from_db = ListingCategory.objects.all().get(pk=LC.id)33 self.assertEquals(lc_from_db.name,"fashion")34 self.assertEquals(lc_from_db.description,"Fashion is a popular style, especially in clothing, footwear, lifestyle, accessories, makeup, hairstyle and body. Fashion is a distinctive and often constant trend in the style in which people present themselves.")35 36 def testCanDeleteListingCategory(self):37 LC = ListingCategory(name="furniture",description="""38 Furniture refers to movable objects intended to support various human activities such as seating 39 (e.g., chairs, stools, and sofas), eating (tables), and sleeping (e.g., beds). """)40 LC.save()41 42 LC2 = ListingCategory.objects.all().get(pk=LC.id)43 self.assertEquals(LC.name,LC2.name)44 self.assertEquals(LC.description,LC2.description)45 46 ListingCategory.objects.filter(id=LC.id).delete()47 lc_from_db = list(ListingCategory.objects.all().filter(pk=LC.id))48 self.assertEquals(lc_from_db,[])49 50class ListingTest(TestCase):51 def testCanCreateListing(self):52 ta = UserAccount(username="penguinrider",password="password123",email="asd@asd.com",first_name="penguin",last_name="rider")53 ta.save()54 test_listing = Listing(name="Bench",description="Rustic Bench, very rustic.",price=53.99,location="Bedok Avenue 1",used=True,seller=ta)55 test_listing.save()56 57 test_lc_1 = ListingCategory(name="furniture",description="""58 Furniture refers to movable objects intended to support various human activities such as seating 59 (e.g., chairs, stools, and sofas), eating (tables), and sleeping (e.g., beds). """)60 61 test_lc_1.save()62 test_listing.categories.add(test_lc_1)63 64 tl_from_db = Listing.objects.all().get(pk=test_listing.id)65 66 self.assertEquals(test_listing.name,tl_from_db.name)67 self.assertEquals(test_listing.description,tl_from_db.description)68 self.assertEquals(test_listing.price,tl_from_db.price)69 self.assertEquals(test_listing.location,tl_from_db.location)70 self.assertEquals(test_listing.used,tl_from_db.used)71 self.assertEquals(tl_from_db.categories.count(),1)72 self.assertEquals(tl_from_db.categories.all()[0].name,"furniture")73 self.assertEquals(tl_from_db.seller.username,ta.username)74 self.assertEquals(tl_from_db.seller.password,ta.password)75 self.assertEquals(tl_from_db.seller.email,ta.email)76 77 def testListingCanHaveManyCategories(self):78 ta = UserAccount(username="penguinrider",password="password123",email="asd@asd.com",first_name="penguin",last_name="rider")79 ta.save()80 test_listing = Listing(name="Bench",description="Rustic Bench, very rustic.",price=53.99,location="Bedok Avenue 1",seller=ta)81 test_listing.save()82 83 test_lc_1 = ListingCategory(name="furniture",description="""84 Furniture refers to movable objects intended to support various human activities such as seating 85 (e.g., chairs, stools, and sofas), eating (tables), and sleeping (e.g., beds). """)86 87 test_lc_1.save()88 89 test_lc_2 = ListingCategory(name="rustic",description="""90 simple and often rough in appearance; typical of the countryside """)91 92 test_lc_2.save()93 94 test_listing.categories.add(test_lc_1,test_lc_2)95 96 tl_from_db = Listing.objects.all().get(pk=test_listing.id)97 98 self.assertEquals(tl_from_db.categories.count(),2)99 self.assertEquals(tl_from_db.categories.all()[0].name,"furniture")100 self.assertEquals(tl_from_db.categories.all()[1].name,"rustic")101 102 def testCanDeleteListing(self):103 ta = UserAccount(username="penguinrider",password="password123",email="asd@asd.com",first_name="penguin",last_name="rider")104 ta.save()105 test_listing = Listing(name="Bench",description="Rustic Bench, very rustic.",price=53.99,location="Bedok Avenue 1",seller=ta)106 test_listing.save()107 108 test_lc_1 = ListingCategory(name="furniture",description="""109 Furniture refers to movable objects intended to support various human activities such as seating 110 (e.g., chairs, stools, and sofas), eating (tables), and sleeping (e.g., beds). """)111 112 test_lc_1.save()113 test_listing.categories.add(test_lc_1)114 115 Listing.objects.filter(id=test_listing.id).delete()116 tl_from_db = list(Listing.objects.all().filter(pk=test_listing.id))117 self.assertEquals(tl_from_db,[])118 119 def testCanUpdateListingDetails(self):120 ta = UserAccount(username="penguinrider",password="password123",email="asd@asd.com",first_name="penguin",last_name="rider")121 ta.save()122 test_listing = Listing(name="Bench",description="Rustic Bench, very rustic.",price=53.99,location="Bedok Avenue 1",seller=ta)123 test_listing.save()124 125 test_listing.name="Stool"126 test_listing.description="Not so rustic stool"127 test_listing.price=14.99128 test_listing.location="Yishun Avenue 2"129 test_listing.save()130 131 tl_from_db = Listing.objects.all().get(pk=test_listing.id)132 self.assertEquals(tl_from_db.name,"Stool")133 self.assertEquals(tl_from_db.description,"Not so rustic stool")134 self.assertEquals(tl_from_db.price,14.99)135 self.assertEquals(tl_from_db.location,"Yishun Avenue 2")136 137 def testCanRemoveCategory(self):138 ta = UserAccount(username="penguinrider",password="password123",email="asd@asd.com",first_name="penguin",last_name="rider")139 ta.save()140 141 test_listing = Listing(name="Bench",description="Rustic Bench, very rustic.",price=53.99,location="Bedok Avenue 1",seller=ta)142 test_listing.save()143 144 test_lc_1 = ListingCategory(name="furniture",description="""145 Furniture refers to movable objects intended to support various human activities such as seating 146 (e.g., chairs, stools, and sofas), eating (tables), and sleeping (e.g., beds). """)147 148 test_lc_1.save()149 150 test_lc_2 = ListingCategory(name="rustic",description="""151 simple and often rough in appearance; typical of the countryside """)152 153 test_lc_2.save()154 155 test_listing.categories.add(test_lc_1,test_lc_2)156 157 tl_from_db = Listing.objects.all().get(pk=test_listing.id)158 159 self.assertEquals(tl_from_db.categories,test_listing.categories)160 161 test_listing.categories.remove(test_lc_1)162 new_tl_from_db = Listing.objects.all().get(pk=test_listing.id)163 164 self.assertEquals(new_tl_from_db.categories.count(),1)165 self.assertEquals(new_tl_from_db.categories.all()[0].name,"rustic")166 167class ListingCommentTest(TestCase):168 def testCanCreateComent(self):169 ta = UserAccount(username="penguinrider",password="password123",email="asd@asd.com",first_name="penguin",last_name="rider")170 ta.save()171 172 ta2 = UserAccount(username="rhinorider",password="asd123",email="qwe@qwe.com",first_name="rhino",last_name="rider")173 ta2.save()174 175 test_listing = Listing(name="Bench",description="Rustic Bench, very rustic.",price=53.99,location="Bedok Avenue 1",used=True,seller=ta)176 test_listing.save()177 178 test_lc_1 = ListingCategory(name="furniture",description="""179 Furniture refers to movable objects intended to support various human activities such as seating 180 (e.g., chairs, stools, and sofas), eating (tables), and sleeping (e.g., beds). """)181 182 test_lc_1.save()183 test_listing.categories.add(test_lc_1)184 test_listing.likes.add(ta)185 186 tc = ListingComment(comment="This product looks wonky",user=ta2,listing=test_listing)187 tc.save()188 189 tc_from_db = ListingComment.objects.all().get(pk=tc.id)190 self.assertEquals(tc_from_db.comment,tc.comment)191 self.assertEquals(tc_from_db.user,ta2)192 self.assertEquals(tc_from_db.listing,test_listing)193 194 def testCanUpdateComment(self):195 ta = UserAccount(username="penguinrider",password="password123",email="asd@asd.com",first_name="penguin",last_name="rider")196 ta.save()197 198 ta2 = UserAccount(username="rhinorider",password="asd123",email="qwe@qwe.com",first_name="rhino",last_name="rider")199 ta2.save()200 201 test_listing = Listing(name="Bench",description="Rustic Bench, very rustic.",price=53.99,location="Bedok Avenue 1",used=True,seller=ta)202 test_listing.save()203 204 test_lc_1 = ListingCategory(name="furniture",description="""205 Furniture refers to movable objects intended to support various human activities such as seating 206 (e.g., chairs, stools, and sofas), eating (tables), and sleeping (e.g., beds). """)207 208 test_lc_1.save()209 test_listing.categories.add(test_lc_1)210 test_listing.likes.add(ta)211 212 tc = ListingComment(comment="This product looks wonky",user=ta2,listing=test_listing)213 tc.save()214 215 tc.comment="I have received a new product and now it is not wonky"216 tc.save()217 218 tc_from_db = ListingComment.objects.all().get(pk=tc.id)219 220 self.assertEquals(tc_from_db.comment,"I have received a new product and now it is not wonky")221 222 def testCanDeleteComment(self):223 ta = UserAccount(username="penguinrider",password="password123",email="asd@asd.com",first_name="penguin",last_name="rider")224 ta.save()225 226 ta2 = UserAccount(username="rhinorider",password="asd123",email="qwe@qwe.com",first_name="rhino",last_name="rider")227 ta2.save()228 229 test_listing = Listing(name="Bench",description="Rustic Bench, very rustic.",price=53.99,location="Bedok Avenue 1",used=True,seller=ta)230 test_listing.save()231 232 test_lc_1 = ListingCategory(name="furniture",description="""233 Furniture refers to movable objects intended to support various human activities such as seating 234 (e.g., chairs, stools, and sofas), eating (tables), and sleeping (e.g., beds). """)235 236 test_lc_1.save()237 test_listing.categories.add(test_lc_1)238 test_listing.likes.add(ta)239 240 tc = ListingComment(comment="This product looks wonky",user=ta2,listing=test_listing)241 tc.save()242 243 ListingComment.objects.filter(id=tc.id).delete()244 245 tc_from_db = list(ListingComment.objects.all().filter(pk=tc.id))246 247 self.assertEquals(tc_from_db,[])248 249 def testPostCanHaveMultipleCommentsFromTheSamePerson(self):250 #Multiple Comments From the Same Person251 ta = UserAccount(username="penguinrider",password="password123",email="asd@asd.com",first_name="penguin",last_name="rider")252 ta.save()253 254 ta2 = UserAccount(username="rhinorider",password="asd123",email="qwe@qwe.com",first_name="rhino",last_name="rider")255 ta2.save()256 257 test_listing = Listing(name="Bench",description="Rustic Bench, very rustic.",price=53.99,location="Bedok Avenue 1",used=True,seller=ta)258 test_listing.save()259 260 tc = ListingComment(comment="This product looks wonky",user=ta2,listing=test_listing)261 tc.save()262 263 tc2 = ListingComment(comment="This product looks alright",user=ta2,listing=test_listing)264 tc2.save()265 266 self.assertEquals(ListingComment.objects.all().count(),2)267 self.assertEquals(ListingComment.objects.all().filter(user=ta2).count(),2)268 self.assertEquals(ListingComment.objects.all().filter(user=ta2)[0].listing,test_listing)269 self.assertEquals(ListingComment.objects.all().filter(user=ta2)[1].listing,test_listing)270 271 def testPostCanHaveMultipleCommentsFromDifferentPeople(self):272 #Multiple Comments From Different People273 ta = UserAccount(username="penguinrider",password="password123",email="asd@asd.com",first_name="penguin",last_name="rider")274 ta.save()275 276 ta2 = UserAccount(username="rhinorider",password="asd123",email="qwe@qwe.com",first_name="rhino",last_name="rider")277 ta2.save()278 279 test_listing = Listing(name="Bench",description="Rustic Bench, very rustic.",price=53.99,location="Bedok Avenue 1",used=True,seller=ta)280 test_listing.save()281 282 ta3 = UserAccount(username="girafferider",password="qwerty",email="qazwsx@qaz.com",first_name="giraffe",last_name="rider")283 ta3.save()284 285 tc = ListingComment(comment="This product looks wonky",user=ta2,listing=test_listing)286 tc.save()287 288 tc2 = ListingComment(comment="I love waffles",user=ta3,listing=test_listing)289 tc2.save()290 291 self.assertEquals(ListingComment.objects.all().filter(user=ta2).count(),1)292 self.assertEquals(ListingComment.objects.all().filter(user=ta3).count(),1)293 self.assertEquals(ListingComment.objects.all().filter(user=ta2)[0].listing,test_listing)294 self.assertEquals(ListingComment.objects.all().filter(user=ta3)[0].listing,test_listing)295 296 def testUsersCanHaveMultipleCommentsOnDifferentListingsFromTheSameSeller(self):297 ta = UserAccount(username="penguinrider",password="password123",email="asd@asd.com",first_name="penguin",last_name="rider")298 ta.save()299 300 ta2 = UserAccount(username="rhinorider",password="asd123",email="qwe@qwe.com",first_name="rhino",last_name="rider")301 ta2.save()302 303 test_listing = Listing(name="Bench",description="Rustic Bench, very rustic.",price=53.99,location="Bedok Avenue 1",used=True,seller=ta)304 test_listing.save()305 306 test_listing2 = Listing(name="Stool",description="Rustic stuhl, not so very rustic.",price=43.99,location="Yishun Avenue 1",used=True,seller=ta)307 test_listing2.save()308 309 tc = ListingComment(comment="This product looks wonky",user=ta2,listing=test_listing)310 tc.save()311 312 tc2 = ListingComment(comment="This product looks alright",user=ta2,listing=test_listing2)313 tc2.save()314 315 self.assertEquals(ListingComment.objects.all().count(),2)316 self.assertEquals(ListingComment.objects.all().filter(user=ta2).count(),2)317 self.assertEquals(ListingComment.objects.all().filter(user=ta2)[0].listing,test_listing)318 self.assertEquals(ListingComment.objects.all().filter(user=ta2)[1].listing,test_listing2)319 320 def testUsersCanHaveMultipleCommentsOnDifferentListingsFromDifferentSellers(self):321 ta = UserAccount(username="penguinrider",password="password123",email="asd@asd.com",first_name="penguin",last_name="rider")322 ta.save()323 324 ta2 = UserAccount(username="rhinorider",password="asd123",email="qwe@qwe.com",first_name="rhino",last_name="rider")325 ta2.save()326 327 ta3 = UserAccount(username="girafferider",password="qwerty",email="qazwsx@qaz.com",first_name="giraffe",last_name="rider")328 ta3.save()329 330 test_listing = Listing(name="Bench",description="Rustic Bench, very rustic.",price=53.99,location="Bedok Avenue 1",used=True,seller=ta)331 test_listing.save()332 333 test_listing2 = Listing(name="Stool",description="Rustic stuhl, not so very rustic.",price=43.99,location="Yishun Avenue 1",used=True,seller=ta2)334 test_listing2.save()335 336 tc = ListingComment(comment="This product looks wonky",user=ta3,listing=test_listing)337 tc.save()338 339 tc2 = ListingComment(comment="This product looks alright",user=ta3,listing=test_listing2)340 tc2.save()341 342 self.assertEquals(ListingComment.objects.all().count(),2)343 self.assertEquals(ListingComment.objects.all().filter(user=ta3).count(),2)344 self.assertEquals(ListingComment.objects.all().filter(user=ta3)[0].listing,test_listing)345 self.assertEquals(ListingComment.objects.all().filter(user=ta3)[1].listing,test_listing2)346 347class ListingLikesTest(TestCase):348 def testListingCanBeLiked(self):349 ta = UserAccount(username="penguinrider",password="password123",email="asd@asd.com",first_name="penguin",last_name="rider")350 ta.save()351 352 ta2 = UserAccount(username="rhinorider",password="asd123",email="qwe@qwe.com",first_name="rhino",last_name="rider")353 ta2.save()354 355 test_listing = Listing(name="Bench",description="Rustic Bench, very rustic.",price=53.99,location="Bedok Avenue 1",used=True,seller=ta)356 test_listing.save()357 358 test_listing.likes.add(ta2)359 360 tl_from_db = Listing.objects.all().get(pk=test_listing.id)361 362 self.assertEquals(tl_from_db.likes,test_listing.likes)363 self.assertEquals(tl_from_db.likes.all()[0].username,"rhinorider")364 self.assertEquals(ta2.liked_listings.count(),1)365 366 def testListingCanBeUnliked(self):367 ta = UserAccount(username="penguinrider",password="password123",email="asd@asd.com",first_name="penguin",last_name="rider")368 ta.save()369 370 ta2 = UserAccount(username="rhinorider",password="asd123",email="qwe@qwe.com",first_name="rhino",last_name="rider")371 ta2.save()372 373 test_listing = Listing(name="Bench",description="Rustic Bench, very rustic.",price=53.99,location="Bedok Avenue 1",used=True,seller=ta)374 test_listing.save()375 376 test_listing.likes.add(ta2)377 378 tl_from_db = Listing.objects.all().get(pk=test_listing.id)379 380 self.assertEquals(tl_from_db.likes,test_listing.likes)381 self.assertEquals(ta2.liked_listings.count(),1)382 383 test_listing.likes.remove(ta2)384 385 new_tl_from_db = Listing.objects.all().get(pk=test_listing.id)386 387 self.assertEquals(new_tl_from_db.likes.count(),0)388 self.assertEquals(ta2.liked_listings.count(),0)389 390 def testListingCanHaveManyLikes(self):391 ta = UserAccount(username="penguinrider",password="password123",email="asd@asd.com",first_name="penguin",last_name="rider")392 ta.save()393 394 ta2 = UserAccount(username="rhinorider",password="asd123",email="qwe@qwe.com",first_name="rhino",last_name="rider")395 ta2.save()396 397 ta3 = UserAccount(username="girafferider",password="qwerty",email="qazwsx@qaz.com",first_name="giraffe",last_name="rider")398 ta3.save()399 400 test_listing = Listing(name="Bench",description="Rustic Bench, very rustic.",price=53.99,location="Bedok Avenue 1",used=True,seller=ta)401 test_listing.save()402 403 test_listing.likes.add(ta2)404 test_listing.likes.add(ta3)405 406 tl_from_db = Listing.objects.all().get(pk=test_listing.id)407 self.assertEquals(tl_from_db.likes,test_listing.likes)408 self.assertEquals(tl_from_db.likes.all()[0].username,"rhinorider")409 self.assertEquals(tl_from_db.likes.all()[1].username,"girafferider")410 self.assertEquals(ta2.liked_listings.count(),1)411 self.assertEquals(ta3.liked_listings.count(),1)412 413 def testUser_Can_Have_Many_Likes_On_Different_Listings_From_The_Same_Seller(self):414 ta = UserAccount(username="penguinrider",password="password123",email="asd@asd.com",first_name="penguin",last_name="rider")415 ta.save()416 417 ta2 = UserAccount(username="rhinorider",password="asd123",email="qwe@qwe.com",first_name="rhino",last_name="rider")418 ta2.save()419 420 test_listing = Listing(name="Bench",description="Rustic Bench, very rustic.",price=53.99,location="Bedok Avenue 1",used=True,seller=ta)421 test_listing.save()422 423 test_listing2 = Listing(name="Stool",description="Rustic stuhl, not so very rustic.",price=43.99,location="Yishun Avenue 1",used=True,seller=ta)424 test_listing2.save()425 426 test_listing.likes.add(ta2)427 test_listing2.likes.add(ta2)428 429 tl_from_db = Listing.objects.all().get(pk=test_listing.id)430 tl2_from_db = Listing.objects.all().get(pk=test_listing2.id)431 self.assertEquals(tl_from_db.likes,test_listing.likes)432 self.assertEquals(tl2_from_db.likes,test_listing2.likes)433 self.assertEquals(tl_from_db.likes.all()[0].username,ta2.username)434 self.assertEquals(tl2_from_db.likes.all()[0].username,ta2.username)435 self.assertEquals(ta2.liked_listings.count(),2)436 437 def testUser_Can_Have_Many_Likes_On_Different_Listings_From_Different_Sellers(self):438 ta = UserAccount(username="penguinrider",password="password123",email="asd@asd.com",first_name="penguin",last_name="rider")439 ta.save()440 441 ta2 = UserAccount(username="rhinorider",password="asd123",email="qwe@qwe.com",first_name="rhino",last_name="rider")442 ta2.save()443 444 ta3 = UserAccount(username="girafferider",password="qwerty",email="qazwsx@qaz.com",first_name="giraffe",last_name="rider")445 ta3.save()446 447 test_listing = Listing(name="Bench",description="Rustic Bench, very rustic.",price=53.99,location="Bedok Avenue 1",used=True,seller=ta)448 test_listing.save()449 450 test_listing2 = Listing(name="Stool",description="Rustic stuhl, not so very rustic.",price=43.99,location="Yishun Avenue 1",used=True,seller=ta2)451 test_listing2.save()452 453 test_listing.likes.add(ta3)454 test_listing2.likes.add(ta3)455 456 tl_from_db = Listing.objects.all().get(pk=test_listing.id)457 tl2_from_db = Listing.objects.all().get(pk=test_listing2.id)458 459 self.assertEquals(tl_from_db.likes,test_listing.likes)460 self.assertEquals(tl2_from_db.likes,test_listing2.likes)461 self.assertEquals(tl_from_db.likes.all()[0].username,ta3.username)462 self.assertEquals(tl2_from_db.likes.all()[0].username,ta3.username)463 self.assertEquals(ta3.liked_listings.count(),2)464class MarketplaceViewsTest(TestCase):465 def setUp(self):466 ta = UserAccount(username="penguinrider",password="password123",email="asd@asd.com",first_name="penguin",last_name="rider")467 ta.set_password('password123')468 ta.save()469 470 ta2 = UserAccount(username="rhinorider",password="asd123",email="qwe@qwe.com",first_name="rhino",last_name="rider")471 ta2.set_password('asd123')472 ta2.save()473 474 test_listing = Listing.objects.create(name="Bench",description="Rustic Bench, very rustic.",price=53.99,location="Bedok Avenue 1",used=True,seller=ta)475 476 def testCanLoadMarketPlacePage(self):477 response = self.client.get('/marketplace/')478 479 self.assertEqual(response.status_code, 200)480 self.assertTemplateUsed(response, 'marketplace.html')481 482 def testCanLoadFavouritesPage(self):483 response = self.client.get('/marketplace/favourites/')484 self.assertEqual(response.status_code, 302)485 486 self.client.login(username='penguinrider',password='password123')487 488 response_postlogin = self.client.get('/marketplace/favourites/')489 490 self.assertEqual(response_postlogin.status_code, 200)491 self.assertTemplateUsed(response_postlogin, 'favourites.html')492 493 def testCanLoadListingCreatorPage(self):494 response = self.client.get('/marketplace/listing/creator/')495 self.assertEqual(response.status_code, 302)496 497 self.client.login(username='penguinrider',password='password123')498 499 response_postlogin = self.client.get('/marketplace/listing/creator/')500 501 self.assertEqual(response_postlogin.status_code, 200)502 self.assertTemplateUsed(response_postlogin, 'listing-creator.html')503 504 def testCanLoadListingEditorPage(self):505 response = self.client.get('/marketplace/listing/editor/1')506 self.assertEqual(response.status_code, 302)507 508 self.client.login(username='penguinrider',password='password123')509 510 response_postlogin = self.client.get('/marketplace/listing/editor/1')511 512 self.assertEqual(response_postlogin.status_code, 200)513 self.assertTemplateUsed(response_postlogin, 'listing-editor.html')514 515 def testCannotLoadListingEditorPage_UserIsNotTheSeller(self):516 response = self.client.get('/marketplace/listing/editor/1')517 self.assertEqual(response.status_code, 302)518 519 self.client.login(username='rhinorider',password='asd123')520 521 response_postlogin = self.client.get('/marketplace/listing/editor/1')522 523 self.assertEqual(response_postlogin.status_code, 302)524 messages = list(get_messages(response_postlogin.wsgi_request))525 self.assertEqual(len(messages), 1)...
test_models.py
Source:test_models.py
1from django.test import TestCase2from e_commerse_auction.models import User, Listing, Bid, Comment3from django.utils import timezone4class UserModelTest(TestCase):5 def setUp(self):6 self.user = User.objects.create(username='udav', first_name='Max', last_name='Chmurov')7 def test_user_model_entry(self):8 """9 Test User model data insertion/types/field attributes10 """11 test_user = self.user12 self.assertTrue(isinstance(test_user, User))13 def test_user_str(self):14 """15 Test User __str__ conversion16 """17 test_user = self.user18 self.assertEquals(test_user.__str__(), 'udav (Max Chmurov)' )19class ListingModelTest(TestCase):20 def setUp(self):21 self.user = User.objects.create(username='python', first_name='Vitaly', last_name='Butoma')22 self.listing = Listing.objects.create(user=self.user, title='Learning Python', price='20.0',23 description='Import this', date=timezone.now(), photo='https://files.realpython.com/media/django-pony.c61d43c33ab3.png',24 category='Education', status='True')25 def test_listing_model_entry(self):26 """27 Test Listing model data insertion/types/field attributes28 """29 test_listing = self.listing30 self.assertTrue(isinstance(test_listing, Listing))31 def test_listing_str(self):32 """33 Test Listing __str__ conversion34 """35 test_listing = self.listing36 self.assertEquals(test_listing.__str__(), f'User:{test_listing.user} Item:{test_listing.title} Price:{test_listing.price} Date Listed:{test_listing.date}')37 def test_title_max_length(self):38 """39 Test title max_length limit40 """41 test_listing = self.listing42 max_length = test_listing._meta.get_field('title').max_length43 self.assertEquals(max_length, 64)44 def test_photo_url_max_length(self):45 """46 Test photo url max_length limit47 """48 test_listing = self.listing49 max_length = test_listing._meta.get_field('photo').max_length50 self.assertEquals(max_length, 200)51 def test_description_max_length(self):52 """53 Test description max_length limit54 """55 test_listing = self.listing56 max_length = test_listing._meta.get_field('description').max_length57 self.assertEquals(max_length, 256)58class BidModelTest(TestCase):59 def setUp(self):60 self.user = User.objects.create(username='kavalski', first_name='Vladiskav', last_name='Kovalev')61 self.listing = Listing.objects.create(user=self.user, title='Learning Python', price='20.0',62 description='Import this', date=timezone.now(), photo='https://files.realpython.com/media/django-pony.c61d43c33ab3.png',63 category='Education', status='True')64 self.bid = Bid.objects.create(listing=self.listing, price='100.0', bidder=self.user)65 def test_bid_model_entry(self):66 """67 Test Bid model data insertion/types/field attributes68 """69 test_bid = self.bid70 self.assertTrue(isinstance(test_bid, Bid))71 def test_bid_model_str(self):72 """73 Test Bid __str__ conversion74 """75 test_bid = self.bid76 self.assertEquals(test_bid.__str__(), f'Listing:{test_bid.listing} price:{test_bid.price}')77class CommentModelTest(TestCase):78 def setUp(self):79 self.user = User.objects.create(username='chort', first_name='Alex', last_name='Letoho')80 self.listing = Listing.objects.create(user=self.user, title='Learning Python', price='20.0',81 description='Import this', date=timezone.now(), photo='https://files.realpython.com/media/django-pony.c61d43c33ab3.png',82 category='Education', status='True')83 self.comment = Comment.objects.create(user=self.user, date=timezone.now(), comment='Go strikeball', listing=self.listing)84 def test_comment_model_entry(self):85 """86 Test Comment model data insertion/types/field attributes87 """88 test_comment = self.comment89 self.assertTrue(isinstance(test_comment, Comment))90 def test_comment_str(self):91 """92 Test Comment __str__ conversion93 """94 test_comment = self.comment95 self.assertEquals(test_comment.__str__(), f'Date:{test_comment.date} Comment:{test_comment.comment} Listing:{test_comment.listing}')96 def test_comment_max_length(self):97 """98 Test Comment max_length99 """100 test_comment = self.comment101 max_length = test_comment._meta.get_field('comment').max_length...
test_parsers.py
Source:test_parsers.py
1from vebeg_scraper.parser import (2 CategoryParser,3 ListingsParser,4 clean_string,5 ListingParser,6 AuctionResultsParser,7)8from vebeg_scraper.models import Category9from tests.proxy_test import TestProxy10from datetime import datetime11import pytest # type: ignore12import pathlib13@pytest.fixture14def test_proxy():15 return TestProxy("")16def test_clean_string():17 assert (18 clean_string("Jahreswagen\n\t\t\t\t\t\t\t\t\t\t\t\t/ Kfz. bis 24 Monate")19 == "Jahreswagen / Kfz. bis 24 Monate"20 )21def test_get_all_listings(test_proxy):22 categories = [23 Category(id=1000, name="", is_top_level=True),24 Category(id=1757, name="", is_top_level=False),25 ]26 listing_parse = ListingsParser(test_proxy, categories)27 listings = listing_parse.get_listings()28 assert listings != []29def test_parse_listing_id(test_proxy):30 # given31 categories = [Category(id=1100, name="", is_top_level=False)]32 parser = ListingParser(test_proxy)33 listing = parser.get_listing("test_listing", categories[0])34 assert listing.id == 203052000135def test_prase_listing_title(test_proxy):36 categories = [Category(id=1100, name="", is_top_level=False)]37 parser = ListingParser(test_proxy)38 listing = parser.get_listing("test_listing", categories[0])39 assert listing.title == "Pkw BMW 740e iPerformance"40def test_parse_listing_gebotstermin(test_proxy):41 categories = [Category(id=1100, name="", is_top_level=False)]42 parser = ListingParser(test_proxy)43 listing = parser.get_listing("test_listing", categories[0])44 assert listing.gebotstermin == datetime(2020, 7, 24, 13, 0)45def test_parse_listing_kurzbeschreibung(test_proxy):46 categories = [Category(id=1100, name="", is_top_level=False)]47 parser = ListingParser(test_proxy)48 listing = parser.get_listing("test_listing", categories[0])49 assert (50 "oft-Close-Automatik" in listing.kurzbeschreibung51 and "Frontbereich/-schiebe" in listing.kurzbeschreibung52 )53def test_prase_listing_gebotsbasis(test_proxy):54 categories = [Category(id=1100, name="", is_top_level=False)]55 parser = ListingParser(test_proxy)56 listing = parser.get_listing("test_listing", categories[0])57 assert "stück" == listing.gebotsbasis.lower()58def test_parse_lagerort(test_proxy):59 categories = [Category(id=1100, name="", is_top_level=False)]60 parser = ListingParser(test_proxy)61 listing = parser.get_listing("test_listing", categories[0])62 assert "GrellstraÃe" in listing.lagerort63 assert "10409 Berlin" in listing.lagerort64 assert "Lagerort / Standort" not in listing.lagerort65def test_parse_daten(test_proxy):66 categories = [Category(id=1100, name="", is_top_level=False)]67 parser = ListingParser(test_proxy)68 listing = parser.get_listing("test_listing", categories[0])69 assert listing.daten.get("Erstzulassung") == "09/18"70def test_download_image(test_proxy, tmpdir):71 categories = [Category(id=1100, name="", is_top_level=False)]72 parser = ListingParser(test_proxy)73 parser.download_dir = pathlib.Path(tmpdir.dirname)74 parser.get_listing("test_listing", categories[0])75 new_line = parser.download_dir.joinpath("2030520001_0.jpg")76 assert new_line.is_file() is True77def test_auction_results(test_proxy):78 # given79 parser = AuctionResultsParser(test_proxy)80 results = parser.get_all_results()81 assert results[0].id == 203152000182 assert results[0].gebotstermin == datetime(2020, 7, 31)...
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!!