import os

# Python Floe Package pip installed with -e
import classic_floes

from artemis.test import FloeTestCase
from artemis.decorators import package
from artemis.wrappers import DatasetWrapper, WorkFloeWrapper, OutputDatasetWrapper

from datarecord import Types, OEField

# Path to the top level directory
PACKAGE_DIR = os.path.dirname(os.path.dirname(classic_floes.__file__))

FILE_DIR = os.path.join(PACKAGE_DIR, "tests", "data")
FLOE_DIR = os.path.join(PACKAGE_DIR, "floes")


@package(PACKAGE_DIR)
class ExampleFloeTest(FloeTestCase):
    def test_example_floe(self):
        workfloe = WorkFloeWrapper.get_workfloe(
            os.path.join(FLOE_DIR, "classic_omega.py"),  # Path to the local workfloe
            run_timeout=1200,  # Max time to run
            queue_timeout=600,  # Max time to spend queued
        )
        input_file = DatasetWrapper.from_file(os.path.join(FILE_DIR, "eMol_ran50.ism"))
        output_dataset = OutputDatasetWrapper()
        failure_dataset = OutputDatasetWrapper()
        workfloe.start(
            {
                "promoted": {
                    "Input Molecules": input_file.identifier,
                    "out": output_dataset.identifier,
                    "failed": failure_dataset.identifier,
                    "Maximum Conformers": 10,
                }
            }
        )
        self.assertWorkFloeComplete(workfloe)

        # Convert dataset's iterator of records to a list of records
        records = list(output_dataset.records())

        self.assertEqual(len(records), 38)
        expected_field = OEField("Conformer Torsion Profile", Types.String)
        for record in records:
            self.assertTrue(record.has_value(expected_field))
