Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you fix the last two function(test_get_info and test_get_name_with_valid_input) of the test and integration test(test_swimmer) ======the main file========================================================== import requests from bs4 import BeautifulSoup class

Can you fix the last two function(test_get_info and test_get_name_with_valid_input) of the test and integration test(test_swimmer)

======the main file==========================================================

import requests

from bs4 import BeautifulSoup

class Swimmer:

def __init__(self, url):

self.url = url

def get_http_status(self):

response = requests.get(self.url)

if not response:

return "Connection fail"

else:

return response.status_code

def get_soup(self):

response = requests.get(self.url)

soup = BeautifulSoup(response.content, 'html.parser')

if not soup:

return "Not possible to parse"

else:

return soup

def save_soup_to_file(soup, filename):

with open(filename, "w") as f:

f.write(str(soup))

def get_name(soup):

name = soup.find('title')

titleString = name.string

s1 = titleString.replace("| Swimcloud", "", 1)

if not s1:

return None

else:

return s1

def get_info(html):

newsoup = BeautifulSoup(html, 'html.parser')

hometown = newsoup.find('li').get_text()

print("Hometown:", hometown)

university = newsoup.find('a').get_text()

print("University:", university)

social_links = newsoup.find_all('a', class_='btn-icon-plain')

for link in social_links:

title = link.get('title')

if 'Twitter' in title:

twitter = link['href']

print("Twitter:", twitter)

elif 'Instagram' in title:

instagram = link['href']

print("Instagram:", instagram)

==================================================test files for unittest==============

import unittest

from unittest.mock import MagicMock, patch

from scranking.Swimmer import Swimmer

from bs4 import BeautifulSoup

class TestSwimmer(unittest.TestCase):

@patch('requests.get')

def test_get_http_status_if_response(self, mock_get):

swimmer = Swimmer("http://www.example.com")

mock_response = MagicMock()

mock_response.status_code = 200

mock_get.return_value = mock_response

self.assertEqual(swimmer.get_http_status(), 200)

@patch('requests.get')

def test_get_http_status_if_not_response(self, mock_get):

swimmer = Swimmer("http://www.example.com")

mock_response = MagicMock()

mock_response.status_code = 200

mock_get.return_value = None

self.assertEqual(swimmer.get_http_status(), "Connection fail")

@patch('requests.get')

def test_get_soup_if_soup(self, mock_get):

swimmer = Swimmer("http://www.example.com")

mock_response = MagicMock()

mock_response.content = b''

mock_get.return_value = mock_response

soup = swimmer.get_soup()

self.assertIsInstance(soup, BeautifulSoup)

def test_get_name_with_valid_input(self, mock_get):

html_doc = 'Sample Title | Swimcloud'

soup = BeautifulSoup(html_doc, 'html.parser')

# Call the function and check the output

self.assertEqual(swimmer.get_name(soup), "Sample Title")

def test_get_info(self):

swimmer = Swimmer("http://www.example.com")

html = "

" \

"

Twitter
" \

"

Instagram
" \

"

Facebook
"

swimmer.get_info(html)

self.assertEqual(swimmer.get_info(soup), "University Twitter Instagram Facebook")

=====intergartion test=====

from scranking.Swimmer import Swimmer

from bs4 import BeautifulSoup

def test_swimmer(self):

url = 'https://www.swimcloud.com/swimmer/549377/'

swimmer = Swimmer(url)

infohtml = '

  • city, state
  • University
    • name on Twitter
    • name on Instagram
'

assert swimmer.get_http_status() == 200

assert swimmer.get_name(soup) == "*name not shown"

#assert swimmer.get_info(infohtml) == returns info

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Students also viewed these Databases questions

Question

EXPLAIN the steps in job analysis.

Answered: 1 week ago