spotify-metadata-parquet / code /smoke_test_export.py
alpha31476's picture
Add dataset card, analysis code, and release documentation
9a1f607 verified
Raw History Blame Contribute Delete
3.42 kB
"""Test export, validation and queries on two real rows from each source table.
The four original databases are opened read-only. Use a new empty work folder.
"""
from __future__ import annotations
import argparse
import json
from pathlib import Path
import sqlite3
import subprocess
import sys
def quote(name):
return '"' + name.replace('"', '""') + '"'
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--source-dir', type=Path, required=True)
parser.add_argument('--work-dir', type=Path, required=True)
args = parser.parse_args()
root = Path(__file__).resolve().parents[1]
source_dir = args.source_dir.resolve(strict=True)
work = args.work_dir.resolve()
if work.exists() and any(work.iterdir()):
parser.error('Choose a new or empty work directory')
tiny = work / 'sqlite'
tiny.mkdir(parents=True)
manifest = json.loads((root / 'manifest.json').read_text(encoding='utf-8'))
counts = []
source_identity = {}
for filename, database in manifest['databases'].items():
path = source_dir / filename
source_identity[filename] = (path.stat().st_size, path.stat().st_mtime_ns)
with sqlite3.connect(path.as_uri() + '?mode=ro&immutable=1', uri=True) as source:
with sqlite3.connect(tiny / filename) as dest:
for name in database['tables']:
sql = next(item['sql'] for item in database['sql_objects'] if item['type'] == 'table' and item['name'] == name)
dest.execute(sql)
columns = [row[1] for row in source.execute('PRAGMA table_info(' + quote(name) + ')')]
# Preserve hidden rowids too, since relationships use them.
fields = ['_rowid_'] + columns
rows = source.execute('SELECT ' + ','.join(quote(c) for c in fields) + ' FROM ' + quote(name) + ' NOT INDEXED LIMIT 2').fetchall()
insert = 'INSERT INTO ' + quote(name) + '(' + ','.join(quote(c) for c in fields) + ') VALUES (' + ','.join('?' for _ in fields) + ')'
dest.executemany(insert, rows)
counts.append({'File': filename, 'Table': name, 'Rows': len(rows), 'Columns': len(columns), 'SizeBytes': 0})
count_path = work / 'counts.json'
count_path.write_text(json.dumps(counts, indent=2), encoding='utf-8')
output = work / 'parquet'
commands = [
[sys.executable, str(root / 'code/export_sqlite_to_parquet.py'), '--source-dir', str(tiny), '--output-dir', str(output), '--source-counts', str(count_path), '--work-dir', str(work / 'scratch'), '--workers', '2'],
[sys.executable, str(root / 'verify_export.py'), '--data-dir', str(output)],
[sys.executable, str(root / 'query_spotify.py'), '--data-dir', str(output), '--sql', 'SHOW TABLES'],
[sys.executable, str(root / 'query_spotify.py'), '--data-dir', str(output), '--sql', 'SELECT count(*) AS rows FROM track_audio_features'],
]
for command in commands:
subprocess.run(command, check=True)
for filename, identity in source_identity.items():
path = source_dir / filename
assert identity == (path.stat().st_size, path.stat().st_mtime_ns), 'Source changed during test'
print('PASS: all 13 tables exported, checked and queried. Original databases unchanged.')
if __name__ == '__main__':
main()