import os

import lxml.html

from dark_keeper.log import get_log
from dark_keeper.storage import Storage


def test_storage(export_dir, html_for_storage):
    storage = Storage(
        [
            ('title', '.show-episode-page h1'),
            ('desc', '.large-content-text'),
            ('mp3', '.episode-buttons a[href$=".mp3"]'),
        ],
        export_dir,
    )

    assert storage == [['title', 'desc', 'mp3']]

    soup = lxml.html.fromstring(html_for_storage)
    storage.append_row(soup)

    assert storage == [
        ['title', 'desc', 'mp3'],
        ['title one', 'desc one', '/mp3/podcast_0.mp3'],
    ]


def test_exports(export_dir, html_for_storage):
    storage = Storage(
        [
            ('title', '.show-episode-page h1'),
            ('desc', '.large-content-text'),
            ('mp3', '.episode-buttons a[href$=".mp3"]'),
        ],
        export_dir,
    )

    soup = lxml.html.fromstring(html_for_storage)
    storage.append_row(soup)

    print('')
    exported_files = storage.export_files(get_log())
    for exported_file in exported_files:
        assert os.path.isfile(exported_file)

        if exported_file.endswith('.csv'):
            with open(exported_file, 'r') as f:
                assert f.read() == '''title,desc,mp3
title one,desc one,/mp3/podcast_0.mp3
'''


def test_create_dirs(export_dir):
    dirs = [
        '',
        os.path.join(export_dir, 'export'),
        os.path.join(export_dir, 'export_two'),
        os.path.join(export_dir, 'export_level_one', 'export_level_two'),
    ]
    for dir in dirs:
        is_created = Storage.create_dirs(dir)
        if is_created:
            assert os.path.isdir(dir)
        else:
            assert is_created is None

@@ -20,16 +20,48 @@
 xml.html
+%0Afrom pymongo import MongoClient
 %0A%0Afrom d
@@ -172,32 +172,83 @@
 l_for_storage):%0A
+    mongo_client = MongoClient('localhost', 27017)%0A
     storage = St
@@ -437,32 +437,54 @@
     export_dir,%0A
+        mongo_client,%0A
     )%0A%0A    asser
@@ -771,24 +771,75 @@
 r_storage):%0A
+    mongo_client = MongoClient('localhost', 27017)%0A
     storage 
@@ -1012,32 +1012,32 @@
 %5D'),%0A        %5D,%0A
-
         export_d
@@ -1036,24 +1036,46 @@
 export_dir,%0A
+        mongo_client,%0A
     )%0A%0A    s

Fix Storage tests