added tests for json and archive_url, updated broken tests (#34)

* added tests for json and archive_url, updated broken tests

* drop 2.7 support
This commit is contained in:
Akash Mahanty 2020-10-16 19:25:45 +05:30 committed by GitHub
parent 9de6393cd5
commit 7f927ec7be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 72 additions and 58 deletions

View File

@ -3,9 +3,8 @@ os: linux
dist: xenial dist: xenial
cache: pip cache: pip
python: python:
- 2.7
- 3.6 - 3.6
- 3.8 - 3.9
before_install: before_install:
- python --version - python --version
- pip install -U pip - pip install -U pip

View File

@ -19,75 +19,87 @@ if sys.version_info > (3, 7):
if codecov_python: if codecov_python:
def test_save(): def test_save():
args = argparse.Namespace(user_agent=None, url="https://pypi.org/user/akamhy/", total=False, version=False, args = argparse.Namespace(user_agent=None, url="https://pypi.org/user/akamhy/", total=False, version=False,
oldest=False, save=True, newest=False, near=False, alive=False, subdomain=False, known_urls=False, get=None) oldest=False, save=True, json=False, archive_url=False, newest=False, near=False, alive=False, subdomain=False, known_urls=False, get=None)
reply = cli.args_handler(args) reply = cli.args_handler(args)
assert "pypi.org/user/akamhy" in reply assert "pypi.org/user/akamhy" in str(reply)
def test_json():
args = argparse.Namespace(user_agent=None, url="https://pypi.org/user/akamhy/", total=False, version=False,
oldest=False, save=False, json=True, archive_url=False, newest=False, near=False, alive=False, subdomain=False, known_urls=False, get=None)
reply = cli.args_handler(args)
assert "archived_snapshots" in str(reply)
def test_archive_url():
args = argparse.Namespace(user_agent=None, url="https://pypi.org/user/akamhy/", total=False, version=False,
oldest=False, save=False, json=False, archive_url=True, newest=False, near=False, alive=False, subdomain=False, known_urls=False, get=None)
reply = cli.args_handler(args)
assert "https://web.archive.org/web/" in str(reply)
def test_oldest(): def test_oldest():
args = argparse.Namespace(user_agent=None, url="https://pypi.org/user/akamhy/", total=False, version=False, args = argparse.Namespace(user_agent=None, url="https://pypi.org/user/akamhy/", total=False, version=False,
oldest=True, save=False, newest=False, near=False, alive=False, subdomain=False, known_urls=False, get=None) oldest=True, save=False, json=False, archive_url=False, newest=False, near=False, alive=False, subdomain=False, known_urls=False, get=None)
reply = cli.args_handler(args) reply = cli.args_handler(args)
assert "pypi.org/user/akamhy" in reply assert "pypi.org/user/akamhy" in str(reply)
def test_newest(): def test_newest():
args = argparse.Namespace(user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 \ args = argparse.Namespace(user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 \
(KHTML, like Gecko) Version/8.0.8 Safari/600.8.9", url="https://pypi.org/user/akamhy/", total=False, version=False, (KHTML, like Gecko) Version/8.0.8 Safari/600.8.9", url="https://pypi.org/user/akamhy/", total=False, version=False,
oldest=False, save=False, newest=True, near=False, alive=False, subdomain=False, known_urls=False, get=None) oldest=False, save=False, json=False, archive_url=False, newest=True, near=False, alive=False, subdomain=False, known_urls=False, get=None)
reply = cli.args_handler(args) reply = cli.args_handler(args)
assert "pypi.org/user/akamhy" in reply assert "pypi.org/user/akamhy" in str(reply)
def test_total_archives(): def test_total_archives():
args = argparse.Namespace(user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 \ args = argparse.Namespace(user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 \
(KHTML, like Gecko) Version/8.0.8 Safari/600.8.9", url="https://pypi.org/user/akamhy/", total=True, version=False, (KHTML, like Gecko) Version/8.0.8 Safari/600.8.9", url="https://pypi.org/user/akamhy/", total=True, version=False,
oldest=False, save=False, newest=False, near=False, alive=False, subdomain=False, known_urls=False, get=None) oldest=False, save=False, json=False, archive_url=False, newest=False, near=False, alive=False, subdomain=False, known_urls=False, get=None)
reply = cli.args_handler(args) reply = cli.args_handler(args)
assert isinstance(reply, int) assert isinstance(reply, int)
def test_known_urls(): def test_known_urls():
args = argparse.Namespace(user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 \ args = argparse.Namespace(user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 \
(KHTML, like Gecko) Version/8.0.8 Safari/600.8.9", url="https://akamhy.github.io", total=False, version=False, (KHTML, like Gecko) Version/8.0.8 Safari/600.8.9", url="https://akamhy.github.io", total=False, version=False,
oldest=False, save=False, newest=False, near=False, alive=True, subdomain=True, known_urls=True, get=None) oldest=False, save=False, json=False, archive_url=False, newest=False, near=False, alive=True, subdomain=True, known_urls=True, get=None)
reply = cli.args_handler(args) reply = cli.args_handler(args)
assert "github" in reply assert "github" in str(reply)
def test_near(): def test_near():
args = argparse.Namespace(user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 \ args = argparse.Namespace(user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 \
(KHTML, like Gecko) Version/8.0.8 Safari/600.8.9", url="https://pypi.org/user/akamhy/", total=False, version=False, (KHTML, like Gecko) Version/8.0.8 Safari/600.8.9", url="https://pypi.org/user/akamhy/", total=False, version=False,
oldest=False, save=False, newest=False, near=True, alive=False, subdomain=False, known_urls=False, get=None, year=2020, month=7, day=15, hour=1, minute=1) oldest=False, save=False, json=False, archive_url=False, newest=False, near=True, alive=False, subdomain=False, known_urls=False, get=None, year=2020, month=7, day=15, hour=1, minute=1)
reply = cli.args_handler(args) reply = cli.args_handler(args)
assert "202007" in reply assert "202007" in str(reply)
def test_get(): def test_get():
args = argparse.Namespace(user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 \ args = argparse.Namespace(user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 \
(KHTML, like Gecko) Version/8.0.8 Safari/600.8.9", url="https://pypi.org/user/akamhy/", total=False, version=False, (KHTML, like Gecko) Version/8.0.8 Safari/600.8.9", url="https://pypi.org/user/akamhy/", total=False, version=False,
oldest=False, save=False, newest=False, near=False, alive=False, subdomain=False, known_urls=False, get="url") oldest=False, save=False, json=False, archive_url=False, newest=False, near=False, alive=False, subdomain=False, known_urls=False, get="url")
reply = cli.args_handler(args) reply = cli.args_handler(args)
assert "waybackpy" in reply assert "waybackpy" in str(reply)
args = argparse.Namespace(user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 \ args = argparse.Namespace(user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 \
(KHTML, like Gecko) Version/8.0.8 Safari/600.8.9", url="https://pypi.org/user/akamhy/", total=False, version=False, (KHTML, like Gecko) Version/8.0.8 Safari/600.8.9", url="https://pypi.org/user/akamhy/", total=False, version=False,
oldest=False, save=False, newest=False, near=False, alive=False, subdomain=False, known_urls=False, get="oldest") oldest=False, save=False, json=False, archive_url=False, newest=False, near=False, alive=False, subdomain=False, known_urls=False, get="oldest")
reply = cli.args_handler(args) reply = cli.args_handler(args)
assert "waybackpy" in reply assert "waybackpy" in str(reply)
args = argparse.Namespace(user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 \ args = argparse.Namespace(user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 \
(KHTML, like Gecko) Version/8.0.8 Safari/600.8.9", url="https://pypi.org/user/akamhy/", total=False, version=False, (KHTML, like Gecko) Version/8.0.8 Safari/600.8.9", url="https://pypi.org/user/akamhy/", total=False, version=False,
oldest=False, save=False, newest=False, near=False, alive=False, subdomain=False, known_urls=False, get="newest") oldest=False, save=False, json=False, archive_url=False, newest=False, near=False, alive=False, subdomain=False, known_urls=False, get="newest")
reply = cli.args_handler(args) reply = cli.args_handler(args)
assert "waybackpy" in reply assert "waybackpy" in str(reply)
if codecov_python: if codecov_python:
args = argparse.Namespace(user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 \ args = argparse.Namespace(user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 \
(KHTML, like Gecko) Version/8.0.8 Safari/600.8.9", url="https://pypi.org/user/akamhy/", total=False, version=False, (KHTML, like Gecko) Version/8.0.8 Safari/600.8.9", url="https://pypi.org/user/akamhy/", total=False, version=False,
oldest=False, save=False, newest=False, near=False, alive=False, subdomain=False, known_urls=False, get="save") oldest=False, save=False, json=False, archive_url=False, newest=False, near=False, alive=False, subdomain=False, known_urls=False, get="save")
reply = cli.args_handler(args) reply = cli.args_handler(args)
assert "waybackpy" in reply assert "waybackpy" in str(reply)
args = argparse.Namespace(user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 \ args = argparse.Namespace(user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 \
(KHTML, like Gecko) Version/8.0.8 Safari/600.8.9", url="https://pypi.org/user/akamhy/", total=False, version=False, (KHTML, like Gecko) Version/8.0.8 Safari/600.8.9", url="https://pypi.org/user/akamhy/", total=False, version=False,
oldest=False, save=False, newest=False, near=False, alive=False, subdomain=False, known_urls=False, get="BullShit") oldest=False, save=False, json=False, archive_url=False, newest=False, near=False, alive=False, subdomain=False, known_urls=False, get="BullShit")
reply = cli.args_handler(args) reply = cli.args_handler(args)
assert "get the source code of the" in reply assert "get the source code of the" in str(reply)
def test_args_handler(): def test_args_handler():
args = argparse.Namespace(version=True) args = argparse.Namespace(version=True)
@ -96,7 +108,7 @@ def test_args_handler():
args = argparse.Namespace(url=None, version=False) args = argparse.Namespace(url=None, version=False)
reply = cli.args_handler(args) reply = cli.args_handler(args)
assert ("waybackpy %s" % (__version__)) in reply assert ("waybackpy %s" % (__version__)) in str(reply)
def test_main(): def test_main():
# This also tests the parse_args method in cli.py # This also tests the parse_args method in cli.py

View File

@ -2,7 +2,7 @@
import sys import sys
import pytest import pytest
import random import random
import time
sys.path.append("..") sys.path.append("..")
import waybackpy.wrapper as waybackpy # noqa: E402 import waybackpy.wrapper as waybackpy # noqa: E402
@ -28,8 +28,7 @@ def test_dunders():
user_agent = "UA" user_agent = "UA"
target = waybackpy.Url(url, user_agent) target = waybackpy.Url(url, user_agent)
assert "waybackpy.Url(url=%s, user_agent=%s)" % (url, user_agent) == repr(target) assert "waybackpy.Url(url=%s, user_agent=%s)" % (url, user_agent) == repr(target)
assert len(target) == len(url) assert "en.wikipedia.org" in str(target)
assert str(target) == url
def test_archive_url_parser(): def test_archive_url_parser():
request_url = "https://amazon.com" request_url = "https://amazon.com"
@ -47,7 +46,6 @@ def test_url_check():
def test_save(): def test_save():
# Test for urls that exist and can be archived. # Test for urls that exist and can be archived.
time.sleep(10)
url_list = [ url_list = [
"en.wikipedia.org", "en.wikipedia.org",
@ -64,7 +62,7 @@ def test_save():
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 " "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/36.0.1944.0 Safari/537.36", "(KHTML, like Gecko) Chrome/36.0.1944.0 Safari/537.36",
) )
archived_url1 = target.save() archived_url1 = str(target.save())
assert url1 in archived_url1 assert url1 in archived_url1
if sys.version_info > (3, 6): if sys.version_info > (3, 6):
@ -73,7 +71,6 @@ def test_save():
with pytest.raises(Exception): with pytest.raises(Exception):
url2 = "ha ha ha ha" url2 = "ha ha ha ha"
waybackpy.Url(url2, user_agent) waybackpy.Url(url2, user_agent)
time.sleep(5)
url3 = "http://www.archive.is/faq.html" url3 = "http://www.archive.is/faq.html"
# Test for urls not allowed to archive by robot.txt. Doesn't works anymore. Find alternatives. # Test for urls not allowed to archive by robot.txt. Doesn't works anymore. Find alternatives.
# with pytest.raises(Exception): # with pytest.raises(Exception):
@ -84,7 +81,6 @@ def test_save():
# "Gecko/20100101 Firefox/25.0", # "Gecko/20100101 Firefox/25.0",
# ) # )
# target.save() # target.save()
# time.sleep(5)
# Non existent urls, test # Non existent urls, test
with pytest.raises(Exception): with pytest.raises(Exception):
target = waybackpy.Url( target = waybackpy.Url(
@ -100,7 +96,6 @@ def test_save():
def test_near(): def test_near():
time.sleep(10)
url = "google.com" url = "google.com"
target = waybackpy.Url( target = waybackpy.Url(
url, url,
@ -108,11 +103,10 @@ def test_near():
"(KHTML, like Gecko) Version/5.0.3 Safari/533.19.4", "(KHTML, like Gecko) Version/5.0.3 Safari/533.19.4",
) )
archive_near_year = target.near(year=2010) archive_near_year = target.near(year=2010)
assert "2010" in archive_near_year assert "2010" in str(archive_near_year)
if sys.version_info > (3, 6): if sys.version_info > (3, 6):
time.sleep(5) archive_near_month_year = str(target.near(year=2015, month=2))
archive_near_month_year = target.near(year=2015, month=2)
assert ( assert (
("201502" in archive_near_month_year) ("201502" in archive_near_month_year)
or ("201501" in archive_near_month_year) or ("201501" in archive_near_month_year)
@ -124,9 +118,9 @@ def test_near():
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246", "(KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246",
) )
archive_near_hour_day_month_year = target.near( archive_near_hour_day_month_year = str(target.near(
year=2008, month=5, day=9, hour=15 year=2008, month=5, day=9, hour=15
) ))
assert ( assert (
("2008050915" in archive_near_hour_day_month_year) ("2008050915" in archive_near_hour_day_month_year)
or ("2008050914" in archive_near_hour_day_month_year) or ("2008050914" in archive_near_hour_day_month_year)
@ -146,13 +140,22 @@ def test_near():
def test_oldest(): def test_oldest():
url = "github.com/akamhy/waybackpy" url = "github.com/akamhy/waybackpy"
target = waybackpy.Url(url, user_agent) target = waybackpy.Url(url, user_agent)
assert "20200504141153" in target.oldest() assert "20200504141153" in str(target.oldest())
def test_json():
url = "github.com/akamhy/waybackpy"
target = waybackpy.Url(url, user_agent)
assert "archived_snapshots" in str(target.JSON)
def test_archive_url():
url = "github.com/akamhy/waybackpy"
target = waybackpy.Url(url, user_agent)
assert "github.com/akamhy" in str(target.archive_url)
def test_newest(): def test_newest():
url = "github.com/akamhy/waybackpy" url = "github.com/akamhy/waybackpy"
target = waybackpy.Url(url, user_agent) target = waybackpy.Url(url, user_agent)
assert url in target.newest() assert url in str(target.newest())
def test_get(): def test_get():