-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from edsu/init-schema
Add init command and migrations
- Loading branch information
Showing
7 changed files
with
607 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,57 @@ | ||
from click.testing import CliRunner | ||
from warcdb import warcdb_cli | ||
import os | ||
import re | ||
import pathlib | ||
import pytest | ||
import sqlite_utils | ||
from unittest import TestCase | ||
|
||
db_file = "test_warc.db" | ||
tests_dir = pathlib.Path(__file__).parent | ||
|
||
# all these WARC files were created with wget except for apod.warc.gz which was | ||
# created with browsertrix-crawler | ||
|
||
@pytest.mark.parametrize("warc_path", [str(tests_dir / "google.warc"), | ||
str(tests_dir / "google.warc.gz"), | ||
str(tests_dir / "no-warc-info.warc"), | ||
"https://tselai.com/data/google.warc", | ||
"https://tselai.com/data/google.warc.gz" | ||
]) | ||
|
||
|
||
def test_import(warc_path): | ||
runner = CliRunner() | ||
|
||
with runner.isolated_filesystem() as fs: | ||
DB_FILE = "test_warc.db" | ||
# initialize db | ||
result = runner.invoke(warcdb_cli, ['init', db_file]) | ||
assert result.exit_code == 0 | ||
|
||
args = ["import", db_file, warc_path] | ||
result = runner.invoke(warcdb_cli, args) | ||
assert result.exit_code == 0 | ||
db = sqlite_utils.Database(db_file) | ||
assert set(db.table_names()) == { | ||
'metadata', 'request', 'resource', 'response', 'warcinfo', '_sqlite_migrations' | ||
} | ||
|
||
if warc_path == str(tests_dir / "google.warc"): | ||
assert db.table('warcinfo').get('<urn:uuid:7ABED2CA-7CBD-48A0-92E5-0059EBFC111A>') | ||
assert db.table('request').get('<urn:uuid:524F62DD-D788-4085-B14D-22B0CDC0AC53>') | ||
|
||
os.remove(db_file) | ||
|
||
|
||
def test_column_names(): | ||
runner = CliRunner() | ||
runner.invoke(warcdb_cli, ['init', db_file]) | ||
runner.invoke(warcdb_cli, ["import", db_file, str(pathlib.Path('tests/google.warc'))]) | ||
|
||
args = ["import", DB_FILE, warc_path] | ||
result = runner.invoke(warcdb_cli, args) | ||
assert result.exit_code == 0 | ||
db = sqlite_utils.Database(DB_FILE) | ||
assert set(db.table_names()) == { | ||
'metadata', 'request', 'resource', 'response', 'warcinfo' | ||
} | ||
# make sure that the columns are named correctly (lowercase with underscores) | ||
db = sqlite_utils.Database(db_file) | ||
for table in db.tables: | ||
for col in table.columns: | ||
assert re.match(r'^[a-z_]+', col.name), f'column {col.name} named correctly' | ||
|
||
if warc_path == str(tests_dir / "google.warc"): | ||
assert db.table('warcinfo').get('<urn:uuid:7ABED2CA-7CBD-48A0-92E5-0059EBFC111A>') | ||
assert db.table('request').get('<urn:uuid:524F62DD-D788-4085-B14D-22B0CDC0AC53>') | ||
os.remove(db_file) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
from sqlite_migrate import Migrations | ||
|
||
migration = Migrations("warcdb") | ||
|
||
|
||
@migration() | ||
def m001_initial(db): | ||
db["warcinfo"].create( | ||
{ | ||
"warc_type": str, | ||
"content_type": str, | ||
"warc_date": str, | ||
"warc_record_id": str, | ||
"warc_filename": str, | ||
"warc_block_digest": str, | ||
"content_length": int, | ||
"payload": str, | ||
}, | ||
pk="warc_record_id", | ||
) | ||
|
||
db["request"].create( | ||
{ | ||
"warc_type": str, | ||
"warc_target_uri": str, | ||
"content_type": str, | ||
"warc_date": str, | ||
"warc_record_id": str, | ||
"warc_ip_address": str, | ||
"warc_warcinfo_id": str, | ||
"warc_block_digest": str, | ||
"content_length": int, | ||
"payload": str, | ||
"http_headers": str, | ||
}, | ||
pk="warc_record_id", | ||
foreign_keys=[("warc_warcinfo_id", "warcinfo", "warc_record_id")], | ||
) | ||
|
||
db["response"].create( | ||
{ | ||
"warc_type": str, | ||
"warc_record_id": str, | ||
"warc_warcinfo_id": str, | ||
"warc_concurrent_to": str, | ||
"warc_target_uri": str, | ||
"warc_date": str, | ||
"warc_ip_address": str, | ||
"warc_block_digest": str, | ||
"warc_payload_digest": str, | ||
"content_type": str, | ||
"content_length": int, | ||
"payload": str, | ||
"http_headers": str, | ||
}, | ||
pk="warc_record_id", | ||
foreign_keys=[ | ||
("warc_warcinfo_id", "warcinfo", "warc_record_id"), | ||
("warc_concurrent_to", "request", "warc_record_id"), | ||
], | ||
) | ||
|
||
db["metadata"].create( | ||
{ | ||
"warc_type": str, | ||
"warc_record_id": str, | ||
"warc_warcinfo_id": str, | ||
"warc_target_uri": str, | ||
"warc_date": str, | ||
"warc_block_digest": str, | ||
"content_type": str, | ||
"content_length": int, | ||
"payload": str, | ||
}, | ||
pk="warc_record_id", | ||
foreign_keys=[("warc_warcinfo_id", "warcinfo", "warc_record_id")], | ||
) | ||
|
||
db["resource"].create( | ||
{ | ||
"warc_type": str, | ||
"warc_record_id": str, | ||
"warc_warcinfo_id": str, | ||
"warc_concurrent_to": str, | ||
"warc_target_uri": str, | ||
"warc_date": str, | ||
"warc_block_digest": str, | ||
"content_type": str, | ||
"content_length": int, | ||
"payload": str, | ||
}, | ||
pk="warc_record_id", | ||
foreign_keys=[ | ||
("warc_warcinfo_id", "warcinfo", "warc_record_id"), | ||
("warc_concurrent_to", "metadata", "warc_record_id"), | ||
], | ||
) |