Skip to content

Commit

Permalink
Merge pull request #172 from Crinibus/add-tests-of-functions
Browse files Browse the repository at this point in the history
Add more tests
  • Loading branch information
Crinibus authored Sep 23, 2022
2 parents 21ac02c + 862aa3d commit b67d55a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
41 changes: 22 additions & 19 deletions tests/test_add_product.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
import pytest
from contextlib import nullcontext as does_not_raise

from scraper.add_product import add_product
from scraper.exceptions import WebsiteNotSupported

test_domains = [
"https://www.amazon.com/",
"https://www.ebay.com/",
"https://www.komplett.dk/",
"https://www.proshop.dk/",
"https://www.computersalg.dk/",
"https://www.elgiganten.dk/",
"https://www.avxperten.dk/",
"https://www.av-cables.dk/",
"https://www.power.dk/",
"https://www.expert.dk/",
"https://www.mm-vision.dk/",
"https://www.coolshop.dk/",
"https://www.sharkgaming.dk/",
"https://www.newegg.com/",
"https://www.hifiklubben.dk/",
("https://www.amazon.com/", does_not_raise()),
("https://www.ebay.com/", does_not_raise()),
("https://www.komplett.dk/", does_not_raise()),
("https://www.proshop.dk/", does_not_raise()),
("https://www.computersalg.dk/", does_not_raise()),
("https://www.elgiganten.dk/", does_not_raise()),
("https://www.avxperten.dk/", does_not_raise()),
("https://www.av-cables.dk/", does_not_raise()),
("https://www.power.dk/", does_not_raise()),
("https://www.expert.dk/", does_not_raise()),
("https://www.mm-vision.dk/", does_not_raise()),
("https://www.coolshop.dk/", does_not_raise()),
("https://www.sharkgaming.dk/", does_not_raise()),
("https://www.newegg.com/", does_not_raise()),
("https://www.hifiklubben.dk/", does_not_raise()),
("https://www.notsupported.com/", pytest.raises(WebsiteNotSupported)),
]


# Tests to make sure the websites that are supported can be added to be scraped
@pytest.mark.parametrize("url", test_domains)
def test_add_product(url, mocker):
@pytest.mark.parametrize("url,expectation", test_domains)
def test_add_product(url, expectation, mocker):
mocker.patch("scraper.Scraper.scrape_info", return_value=None)
mocker.patch("scraper.Scraper.save_info", return_value=None)
mocker.patch("scraper.filemanager.Filemanager.add_product_to_csv", return_value=None)
mocker.patch("scraper.add_product.check_if_product_exists", return_value=False)
mocker.patch("scraper.add_product.check_if_product_exists_csv", return_value=False)
mocker.patch("scraper.add_product.add_product_to_records", return_value=None)

# expect no exceptions to raise
add_product("test", url)
with expectation:
add_product("test", url)
19 changes: 19 additions & 0 deletions tests/test_domains.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pytest

from scraper.domains import get_website_name

test_websites = [
("https://www.amazon.com/", "amazon"),
("https://www.komplett.dk/", "komplett"),
("https://www.av-cables.dk/", "av-cables"),
("https://nowww.com/", "nowww"),
("https://no-ending-slash.com", "no-ending-slash"),
("https://www.test.testing.com/", "test.testing"),
("https://www.test.hello.com/hello/world", "test.hello"),
]


@pytest.mark.parametrize("url,expected", test_websites)
def test_get_website_name(url, expected):
result = get_website_name(url)
assert result == expected

0 comments on commit b67d55a

Please sign in to comment.