def test_dl_1(): # bad protocol bad_link, num = "httpnotrealprotocol://example.com", '01' with sqlite3.connect(DB) as conn: ariaq.add_job(bad_link, num, conn) fails = ariaq.start_working(DB) assert len(fails) == 1 assert (bad_link, num) == fails[0]
def test_add_2(): # file already exists with sqlite3.connect(DB) as conn: link, num = "http://example.com", "05" filename = f'{FILE_PREFIX}{num}.{FILE_SUFFIX}' full_filename = f'{OUT_PATH}{filename}' with open(full_filename, 'w') as f: f.write('blah blah') with pytest.raises(ValueError): ariaq.add_job(link, num, conn) os.remove(full_filename)
def test_status_0(): actual_jobs = 0 link = "https://example.com" with sqlite3.connect(DB) as conn: status = ariaq.get_status_message(conn) assert actual_jobs == extract_num_jobs(status) for _ in range(15): actual_jobs += 1 ariaq.add_job(link, str(actual_jobs), conn) status = ariaq.get_status_message(conn) assert actual_jobs == extract_num_jobs(status)
def test_add_0(): # expected input with sqlite3.connect(DB) as conn: link, num = "http://example.com", "05" assert 0 == conn.execute( '''SELECT COUNT(num) FROM Tasks WHERE link=? AND num=?''', (link, num)).fetchone()[0] ariaq.add_job(link, num, conn) assert 1 == conn.execute( '''SELECT COUNT(num) FROM Tasks WHERE link=? AND num=?''', (link, num)).fetchone()[0]
def test_poll_0(): link = "https://example.com" with sqlite3.connect(DB) as conn: for job_num in range(15): ariaq.add_job(link, str(job_num).zfill(2), conn) order = sorted(map(lambda x: str(x).zfill(2), range(15))) for job_num in order: curr = ariaq.poll(conn) assert curr assert len(curr) == 2 assert curr[0] == link assert curr[1] == str(job_num)
def test_dl_0(): link = "http://example.com" subprocess.run(['wget', '-q', link, '-O', 'foo'], stdout=subprocess.DEVNULL) correct_hash = md5('foo') os.remove('foo') with sqlite3.connect(DB) as conn: for job_num in range(3): ariaq.add_job(link, str(job_num), conn) fails = ariaq.start_working(DB) assert len(fails) == 0 for job_num in range(3): filename = f'{FILE_PREFIX}{str(job_num)}.{FILE_SUFFIX}' full_filename = f'{OUT_PATH}{filename}' assert correct_hash == md5(full_filename) os.remove(full_filename)
def test_add_3(): # entry in db with matching num with sqlite3.connect(DB) as conn: link, num = "http://example.com", "05" ariaq.add_job(link, num, conn) with pytest.raises(ValueError): ariaq.add_job(link, num, conn)
def test_add_1(): # not a number with sqlite3.connect(DB) as conn: with pytest.raises(ValueError): link, num = "http://example.com", "not_a_number" ariaq.add_job(link, num, conn)