Best Python code snippet using pytest-django_python
test_link.py
Source:test_link.py
1# Copyright 2022 UW-IT, University of Washington2# SPDX-License-Identifier: Apache-2.03from django.utils import timezone4from datetime import datetime, timedelta5from myuw.dao.user import get_user_model6from myuw.models import VisitedLinkNew7from myuw.test import get_request_with_user8from myuw.test.api import MyuwApiTest9TEST_URLS = ('http://google.com', 'http://uw.edu', 'http://cs.uw.edu')10class TestLink(MyuwApiTest):11 def test_user_recent_basic(self):12 req = get_request_with_user('none')13 user = get_user_model(req)14 for url in TEST_URLS:15 VisitedLinkNew.objects.create(user=user,16 url=url)17 recent = VisitedLinkNew.recent_for_user(user)18 self.assertEquals(len(recent), 3)19 self.assertEquals(recent[0].url, TEST_URLS[2])20 self.assertEquals(recent[1].url, TEST_URLS[1])21 self.assertEquals(recent[2].url, TEST_URLS[0])22 def test_user_recent_multi_visit(self):23 req = get_request_with_user('none')24 user = get_user_model(req)25 for url in TEST_URLS:26 for i in range(3):27 VisitedLinkNew.objects.create(user=user,28 url=url)29 recent = VisitedLinkNew.recent_for_user(user)30 self.assertEquals(len(recent), 3)31 self.assertEquals(recent[0].url, TEST_URLS[2])32 self.assertEquals(recent[1].url, TEST_URLS[1])33 self.assertEquals(recent[2].url, TEST_URLS[0])34 def test_max_old_links(self):35 req = get_request_with_user('none')36 user = get_user_model(req)37 VisitedLinkNew.objects.create(user=user, url=TEST_URLS[0])38 for i in range(VisitedLinkNew.MAX_RECENT_HISTORY):39 VisitedLinkNew.objects.create(user=user, url=TEST_URLS[1])40 VisitedLinkNew.objects.create(user=user, url=TEST_URLS[2])41 recent = VisitedLinkNew.recent_for_user(user)42 self.assertEquals(len(recent), 2)43 self.assertEquals(recent[0].url, TEST_URLS[2])44 self.assertEquals(recent[1].url, TEST_URLS[1])45 def test_max_by_date(self):46 req = get_request_with_user('none')47 user = get_user_model(req)48 visit_date = (timezone.now() +49 VisitedLinkNew.OLDEST_RECENT_TIME_DELTA +50 timedelta(seconds=-1))51 o = VisitedLinkNew.objects.create(user=user, url=TEST_URLS[0])52 o.visit_date = visit_date53 o.save()54 VisitedLinkNew.objects.create(user=user, url=TEST_URLS[1])55 recent = VisitedLinkNew.recent_for_user(user)56 self.assertEquals(len(recent), 1)57 self.assertEquals(recent[0].url, TEST_URLS[1])58 def test_none(self):59 req = get_request_with_user('none')60 user = get_user_model(req)61 recent = VisitedLinkNew.recent_for_user(user)62 self.assertEquals(recent, [])63 def test_popular(self):64 req1 = get_request_with_user('none')65 user1 = get_user_model(req1)66 req2 = get_request_with_user('none1')67 user2 = get_user_model(req2)68 VisitedLinkNew.objects.create(user=user1, url=TEST_URLS[0],69 is_student=True, is_seattle=True)70 VisitedLinkNew.objects.create(user=user2, url=TEST_URLS[0],71 is_seattle=True)72 VisitedLinkNew.objects.create(user=user2, url=TEST_URLS[1])73 VisitedLinkNew.objects.create(user=user2, url=TEST_URLS[2],74 is_seattle=True)75 popular_links = VisitedLinkNew.get_popular()76 self.assertEquals(len(popular_links), 3)77 self.assertEquals(popular_links[0]['url'], TEST_URLS[0])78 self.assertEquals(popular_links[0]['popularity'], 4)79 self.assertEquals(popular_links[1]['url'], TEST_URLS[2])80 self.assertEquals(popular_links[1]['popularity'], 1)81 self.assertEquals(popular_links[2]['url'], TEST_URLS[1])82 self.assertEquals(popular_links[2]['popularity'], 1)83 popular_links = VisitedLinkNew.get_popular(is_student=True)84 self.assertEquals(len(popular_links), 1)85 self.assertEquals(popular_links[0]['url'], TEST_URLS[0])86 self.assertEquals(popular_links[0]['popularity'], 1)87 popular_links = VisitedLinkNew.get_popular(is_seattle=True)88 self.assertEquals(len(popular_links), 2)89 self.assertEquals(popular_links[0]['url'], TEST_URLS[0])90 self.assertEquals(popular_links[0]['popularity'], 4)91 self.assertEquals(popular_links[1]['url'], TEST_URLS[2])92 self.assertEquals(popular_links[1]['popularity'], 1)93 def test_popular_multi_labels(self):94 req1 = get_request_with_user('none')95 user1 = get_user_model(req1)96 req2 = get_request_with_user('none1')97 user2 = get_user_model(req2)98 req3 = get_request_with_user('none2')99 user3 = get_user_model(req3)100 VisitedLinkNew.objects.create(user=user1, url=TEST_URLS[0], label="x",101 is_student=True, is_seattle=True)102 VisitedLinkNew.objects.create(user=user2, url=TEST_URLS[0], label="x",103 is_seattle=True)104 VisitedLinkNew.objects.create(user=user2, url=TEST_URLS[0], label=None,105 is_seattle=True)106 VisitedLinkNew.objects.create(user=user3, url=TEST_URLS[0], label=None,107 is_seattle=True)108 VisitedLinkNew.objects.create(user=user1, url=TEST_URLS[1], label=None,109 is_student=True, is_seattle=True)110 VisitedLinkNew.objects.create(user=user2, url=TEST_URLS[1], label="x",111 is_seattle=True)112 VisitedLinkNew.objects.create(user=user3, url=TEST_URLS[1], label="y",113 is_seattle=True)114 popular_links = VisitedLinkNew.get_popular()115 self.assertEquals(len(popular_links), 2)116 self.assertEquals(popular_links[0]['url'], TEST_URLS[0])117 self.assertEquals(popular_links[0]['popularity'], 16)118 self.assertEquals(popular_links[0]['labels'], ['', 'x'])119 self.assertEquals(popular_links[1]['url'], TEST_URLS[1])120 self.assertEquals(popular_links[1]['popularity'], 9)...
tests_deploy.py
Source:tests_deploy.py
1# -*- coding: utf-8 -*-2from __future__ import absolute_import, unicode_literals3import random4from urllib.parse import urlparse5from bs4 import BeautifulSoup6import requests7import asyncio8import aiohttp9from module.site.site import Site10def tests_main():11 _tests_develop()12 _tests_production()13 14def _tests_develop():15 """16 éçºç°å¢ã®è©¦é¨17 """18 host = "127.0.0.1:5000"19 url_base = "http://{}/{}/"20 site = random.choice(Site.get_all())21 test_url = url_base.format(host, site.title)22 parse_and_request(test_url)23def _tests_production():24 """25 æ¬çªç°å¢ã®è©¦é¨26 """27 host = "www.niku.tokyo"28 url_base = "http://{}/{}/"29 site = random.choice(Site.get_all())30 test_url = url_base.format(host, site.title)31 parse_and_request(test_url)32def parse_and_request(url):33 """34 urlããã¦ã³ãã¼ããã¦ãbs4ã解æãã¦35 å
¨ãªã³ã¯ã®ã¹ãã¼ã¿ã¹ãã§ãã¯ãã36 """37 # urlããã¼ã¹38 o = urlparse(url)39 host = o.netloc40 # æå®ãããURLãGETãã¦è§£æ41 response = requests.get(url, timeout=4)42 assert response.status_code == 20043 soup = BeautifulSoup(response.text, "lxml")44 test_urls = []45 for a in soup.find_all("a"):46 href = a.get("href")47 if href[0] == '#':48 pass49 elif href[0] == '/':50 # ç¸å¯¾ãªã³ã¯51 test_url = 'http://{}{}'.format(host, href)52 test_urls.append(test_url)53 elif host in href:54 # 絶対ãªã³ã¯ãã¤ãåä¸ãã¡ã¤ã³55 test_urls.append(href)56 else:57 # å¤é¨ãµã¤ããªã³ã¯ã¯ãã¹ãããªã58 print('IGNORE:{}'.format(href))59 # éè¤æé¤60 test_urls = list(set(test_urls))61 for test_url in test_urls:62 print(test_url)63 # ãªã³ã¯ãçãã¦ãããéåæå®è¡ãã¦ãã§ãã¯64 loop = asyncio.get_event_loop()65 loop.run_until_complete(asyncio.wait([check_url(url) for url in test_urls]))66async def check_url(url):67 """68 éåæã§URLããã§ãã¯ãã¦ãHTTP STATUSã200ãå¿çãããã¨ããã§ãã¯69 :param url: str70 """71 response = await aiohttp.request('GET', url)72 status_code = response.status73 assert status_code == 200, status_code...
test_url_queue.py
Source:test_url_queue.py
1import pytest2from infrastructure.fetch.url_queue import URLQueue3class TestUrlQueue:4 test_urls = [5 'https://www.python.org/',6 'https://xkcd.com/',7 ]8 @staticmethod9 @pytest.fixture10 def url_queue() -> URLQueue:11 return URLQueue()12 def test_create_empty_url_queue(self, url_queue: URLQueue):13 assert url_queue.urls == list()14 def test_create_filled_url_queue(self):15 url_queue = URLQueue(self.test_urls)16 assert url_queue.urls == self.test_urls17 def test_fill_url_queue(self, url_queue: URLQueue):18 url_queue.add(self.test_urls)19 assert url_queue.urls == self.test_urls20 def test_add_url_to_queue(self, url_queue: URLQueue):21 new_url = 'https://code-specialist.com'22 previous_length = len(url_queue.urls)23 url_queue.add(new_url)24 assert len(url_queue.urls) == previous_length + 125 assert 'https://code-specialist.com' in url_queue.urls26 def test_url_queue_builtin_add(self, url_queue: URLQueue):27 new_url = 'https://code-specialist.com'28 previous_length = len(url_queue.urls)29 url_queue += new_url30 assert len(url_queue.urls) == previous_length + 131 assert 'https://code-specialist.com' in url_queue.urls32 def test_url_queue_add_fail(self, url_queue: URLQueue):33 with pytest.raises(ValueError):34 url_queue.add(42)35 def test_url_queue_clear(self, url_queue: URLQueue):36 url_queue.add(self.test_urls)37 url_queue.clear()38 assert url_queue.urls == list()39 def test_url_queue_len(self, url_queue: URLQueue):40 assert len(url_queue) == 041 url_queue.add(self.test_urls)42 assert len(url_queue) == len(self.test_urls)43 def test_url_queue_is_empty_true(self, url_queue: URLQueue):44 assert url_queue.is_empty()45 def test_url_queue_is_empty_false(self, url_queue: URLQueue):46 url_queue.add(self.test_urls)47 assert not url_queue.is_empty()48 def test_url_queue_bool_true(self, url_queue: URLQueue):49 url_queue.add(self.test_urls)50 assert url_queue51 def test_url_queue_bool_false(self, url_queue: URLQueue):52 assert not url_queue53 def test_process_url_queue(self, url_queue: URLQueue):54 url_queue.add(self.test_urls)55 for url in url_queue:56 assert url in self.test_urls...
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!!