def test_finished_state(self):
        worker = Worker()
        worker.configure(destination=self.image_net_home,
                         number_of_images=5,
                         images_per_category=10)

        change_spy = QSignalSpy(worker.stateChanged)
        worker.start_download()

        while not worker.complete:
            received = change_spy.wait(500)

        worker = Worker()

        self.assertEqual(worker.download_state, 'finished')
        self.assertTrue(worker.complete)

        worker = Worker()
        expected_path = os.path.abspath(self.image_net_home)

        state_data = json.loads(worker.state_data_json)
        self.assertEqual(state_data['downloadPath'], expected_path)
        self.assertEqual(state_data['numberOfImages'], 5)
        self.assertEqual(state_data['imagesPerCategory'], 10)
        self.assertEqual(state_data['timeLeft'], '0 seconds')
        self.assertEqual(state_data['imagesLoaded'], 5)
        self.assertEqual(state_data['failures'], 0)
        self.assertEqual(state_data['failedUrls'], [])
        self.assertEqual(state_data['progress'], 1.0)
    def test_destination_path_validation(self):
        worker = Worker()
        dest = os.path.join('hello', 'world', 'folder')
        worker.configure(destination=dest,
                         number_of_images=10,
                         images_per_category=5)

        state_data = json.loads(worker.state_data_json)
        self.assertEqual(state_data['downloadPath'], '')
        self.assertEqual(worker.download_state, 'initial')
    def test_configure_changes_parameters(self):
        worker = Worker()
        worker.configure(destination=self.image_net_home,
                         number_of_images=10,
                         images_per_category=5)
        state_data = json.loads(worker.state_data_json)

        expected_path = os.path.abspath(self.image_net_home)
        self.assertEqual(state_data['downloadPath'], expected_path)
        self.assertEqual(state_data['numberOfImages'], 10)
        self.assertEqual(state_data['imagesPerCategory'], 5)
        self.assertEqual(state_data['timeLeft'], 'Eternity')
        self.assertEqual(state_data['imagesLoaded'], 0)
        self.assertEqual(state_data['failures'], 0)
        self.assertEqual(state_data['failedUrls'], [])
        self.assertEqual(state_data['progress'], 0)

        self.assertEqual(worker.download_state, 'ready')
    def test_initial_state(self):
        worker = Worker()
        state_data = json.loads(worker.state_data_json)

        self.assertEqual(worker.download_state, 'initial')

        self.assertEqual(state_data['downloadPath'], '')
        self.assertEqual(state_data['numberOfImages'], 100)
        self.assertEqual(state_data['imagesPerCategory'], 90)
        self.assertEqual(state_data['timeLeft'], 'Eternity')
        self.assertEqual(state_data['imagesLoaded'], 0)
        self.assertEqual(state_data['failures'], 0)
        self.assertEqual(state_data['failedUrls'], [])
        self.assertEqual(state_data['progress'], 0)
    def test_wait_until_download_complete(self):
        worker = Worker()
        worker.configure(destination=self.image_net_home,
                         number_of_images=10,
                         images_per_category=5)

        change_spy = QSignalSpy(worker.stateChanged)
        worker.start_download()

        while not worker.complete:
            received = change_spy.wait(500)

        self.assertEqual(worker.download_state, 'finished')
        self.assertTrue(worker.complete)
    def test_number_of_images_validation(self):
        worker = Worker()
        worker.configure(destination=self.image_net_home,
                         number_of_images=-1,
                         images_per_category=5)

        state_data = json.loads(worker.state_data_json)
        self.assertEqual(state_data['numberOfImages'], 100)
        self.assertEqual(worker.download_state, 'initial')

        worker.configure(destination=self.image_net_home,
                         number_of_images=0,
                         images_per_category=5)

        self.assertEqual(state_data['numberOfImages'], 100)
        self.assertEqual(worker.download_state, 'initial')
    def test_images_per_category_validation(self):
        worker = Worker()
        worker.configure(destination=self.image_net_home,
                         number_of_images=10,
                         images_per_category=-1)

        state_data = json.loads(worker.state_data_json)
        self.assertEqual(state_data['imagesPerCategory'], 90)
        self.assertEqual(worker.download_state, 'initial')

        worker.configure(destination=self.image_net_home,
                         number_of_images=10,
                         images_per_category=0)

        self.assertEqual(state_data['imagesPerCategory'], 90)
        self.assertEqual(worker.download_state, 'initial')
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
import logging
import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtQml import QQmlApplicationEngine
from PyQt5.QtGui import QGuiApplication
from util.py_qml_glue import Worker

logging.basicConfig(filename='MLpedia.log', level=logging.INFO)

if __name__ == '__main__':
    sys_argv = sys.argv
    sys_argv += ['--style', 'Imagine']
    app = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine()

    downloader = Worker()
    engine.rootContext().setContextProperty("downloader", downloader)

    engine.load(QUrl("qml/main.qml"))

    if not engine.rootObjects():
        sys.exit(-1)

    sys.exit(app.exec_())