from ..downloader import Downloader
import os

import pytest


@pytest.fixture
def cwd_to_tmpdir(tmpdir):
    os.chdir(str(tmpdir))


def test_audiobook_download(cwd_to_tmpdir, monkeypatch):
    audiobook_url = "https://www.scribd.com/audiobook/237606860/100-Ways-to-Motivate-Yourself-Change-Your-Life-Forever"
    audiobook_downloader = Downloader(audiobook_url)
    audio = audiobook_downloader.download()
    assert audio[0] == "100_Ways_to_Motivate_Yourself__Change_Your_Life_Forever_preview.mp3"
    assert os.path.getsize(audio[0]) == 2127830


def test_text_document_download(cwd_to_tmpdir):
    text_doc_url = "https://www.scribd.com/document/96882378/Trademark-License-Agreement"
    text_downloader = Downloader(text_doc_url)
    md_doc = text_downloader.download(is_image_document=False)
    assert os.path.getsize(md_doc.input_content) in range(1000, 2000)
    md_doc.to_pdf()
    assert os.path.getsize(md_doc.pdf_path) in range(20000, 31000)


def test_img_document_download(cwd_to_tmpdir):
    img_doc_url = "https://www.scribd.com/doc/136711944/Signature-Scanning-and-Verification-in-Finacle"
    img_downloader = Downloader(img_doc_url)
    imgs = img_downloader.download(is_image_document=True)
    assert len(imgs.input_content) == 2
    imgs.to_pdf()
    assert os.path.getsize(imgs.pdf_path) in range(140000, 150000)


def test_book_download(cwd_to_tmpdir, monkeypatch):
    book_url = "https://www.scribd.com/read/262694921/Acting-The-First-Six-Lessons"
    book_downloader = Downloader(book_url)
    # We don't want to clutter stdout with book contents if this test fails
    monkeypatch.setattr("builtins.print", lambda x: None)
    md_book = book_downloader.download()
    assert os.path.getsize(md_book.input_content) in range(10000, 20000)
    md_book.to_pdf()
    assert os.path.getsize(md_book.pdf_path) in range(200000, 2500000)


[((17, 11, 17, 36), 'os.path.getsize', 'os.path.getsize', ({(17, 27, 17, 35): 'audio[0]'}, {}), '(audio[0])', False, 'import os\n'), ((24, 11, 24, 48), 'os.path.getsize', 'os.path.getsize', ({(24, 27, 24, 47): 'md_doc.input_content'}, {}), '(md_doc.input_content)', False, 'import os\n'), ((26, 11, 26, 43), 'os.path.getsize', 'os.path.getsize', ({(26, 27, 26, 42): 'md_doc.pdf_path'}, {}), '(md_doc.pdf_path)', False, 'import os\n'), ((35, 11, 35, 41), 'os.path.getsize', 'os.path.getsize', ({(35, 27, 35, 40): 'imgs.pdf_path'}, {}), '(imgs.pdf_path)', False, 'import os\n'), ((44, 11, 44, 49), 'os.path.getsize', 'os.path.getsize', ({(44, 27, 44, 48): 'md_book.input_content'}, {}), '(md_book.input_content)', False, 'import os\n'), ((46, 11, 46, 44), 'os.path.getsize', 'os.path.getsize', ({(46, 27, 46, 43): 'md_book.pdf_path'}, {}), '(md_book.pdf_path)', False, 'import os\n')]