71 lines
2.4 KiB
Python
71 lines
2.4 KiB
Python
# This file is part of the Open Parts Database software
|
|
# Copyright (C) 2022 Valentin Lorentz
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify it under the
|
|
# terms of the GNU Affero General Public License version 3, as published by the
|
|
# Free Software Foundation.
|
|
#
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
|
# PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General Public License along with
|
|
# this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
"""
|
|
Tests basic insertion and retrieval functions
|
|
"""
|
|
|
|
import datetime
|
|
import random
|
|
|
|
from opdb.db import Db, models
|
|
|
|
|
|
def test_missing_web_page_snapshot(opdb_db: Db):
|
|
"""Tests retrieving a missing web page returns None."""
|
|
assert opdb_db.get_last_web_page_snapshot("http://nonexistent.org") is None
|
|
|
|
|
|
def test_add_web_page_snapshot(opdb_db: Db):
|
|
"""Tests adding a web page and that it can be retrieved."""
|
|
date = datetime.datetime.now(tz=datetime.timezone.utc)
|
|
snapshots = [
|
|
models.WebPageSnapshot(
|
|
url=f"http://example.org/{i}",
|
|
snapshot_date=datetime.datetime.now(tz=datetime.timezone.utc),
|
|
snapshot_url=None,
|
|
retrieved_at=date,
|
|
retrieved_by="localhost",
|
|
response_headers={"Content-Length": "7"},
|
|
content=f"snapshot {i}".encode(),
|
|
)
|
|
for i in range(100)
|
|
]
|
|
opdb_db.add_web_page_snapshots(snapshots)
|
|
|
|
assert opdb_db.get_last_web_page_snapshot("http://example.org/10") == snapshots[10]
|
|
|
|
|
|
def test_get_last_web_page_snapshot(opdb_db: Db):
|
|
"""Tests adding a web page and that it can be retrieved."""
|
|
date = datetime.datetime.now(tz=datetime.timezone.utc)
|
|
|
|
snapshots = [
|
|
models.WebPageSnapshot(
|
|
url="http://example.org/",
|
|
snapshot_date=datetime.datetime.now(tz=datetime.timezone.utc),
|
|
snapshot_url=None,
|
|
retrieved_at=date,
|
|
retrieved_by="localhost",
|
|
response_headers={"Content-Length": "7"},
|
|
content=f"snapshot {i}".encode(),
|
|
)
|
|
for i in range(100)
|
|
]
|
|
last_snapshot = snapshots[-1]
|
|
random.shuffle(snapshots)
|
|
opdb_db.add_web_page_snapshots(snapshots)
|
|
|
|
assert opdb_db.get_last_web_page_snapshot("http://example.org/") == last_snapshot
|