Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImportError: Failed to import test module: tests.test_data #125

Open
botsp opened this issue Oct 24, 2024 · 0 comments
Open

ImportError: Failed to import test module: tests.test_data #125

botsp opened this issue Oct 24, 2024 · 0 comments

Comments

@botsp
Copy link

botsp commented Oct 24, 2024

Hi there,
I recently cloned the example project from GitHub that uses Cookiecutter templates for generating the project structure. I encountered an issue with the test_data.py file when trying to validate my example YAML data.
In "Creating a new project-->Step 1: Generate the project files-->7. main_schema_class", I set the example as "Person", however, this would triger the test error.


C:\Users\kevin\OneDrive - The University Of Hong Kong\Desktop\LinkML\How_to\KS_LinkML>poetry run python -m unittest discover
E
======================================================================
ERROR: tests.test_data (unittest.loader._FailedTest.tests.test_data)
----------------------------------------------------------------------
ImportError: Failed to import test module: tests.test_data
Traceback (most recent call last):
  File "C:\Users\kevin\AppData\Local\Programs\Python\Python312\Lib\unittest\loader.py", line 396, in _find_test_path
    module = self._get_module_from_name(name)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\kevin\AppData\Local\Programs\Python\Python312\Lib\unittest\loader.py", line 339, in _get_module_from_name
    __import__(name)
  File "C:\Users\kevin\OneDrive - The University Of Hong Kong\Desktop\LinkML\How_to\KS_LinkML\tests\test_data.py", line 7, in <module>
    from ks_linkml.datamodel.ks_linkml import PersonCollection
ImportError: cannot import name 'PersonCollection' from 'ks_linkml.datamodel.ks_linkml' (C:\Users\kevin\OneDrive - The University Of Hong Kong\Desktop\LinkML\How_to\KS_LinkML\src\ks_linkml\datamodel\ks_linkml.py)

Then I checked below two files:

..\teststest_data.py

"""Data test."""
import os
import glob
import unittest

from linkml_runtime.loaders import yaml_loader
from ks_linkml.datamodel.ks_linkml import PersonCollection

ROOT = os.path.join(os.path.dirname(__file__), '..')
DATA_DIR = os.path.join(ROOT, "src", "data", "examples")

EXAMPLE_FILES = glob.glob(os.path.join(DATA_DIR, '*.yaml'))

class TestData(unittest.TestCase):
    """Test data and datamodel."""

    def test_data(self):
        """Data test."""
        for path in EXAMPLE_FILES:
            obj = yaml_loader.load(path, target_class=PersonCollection)
            assert obj

But the example data ..\src\data\examples\Person-001.yaml is

# Example data object
---
entries:
  - id: example:Person001
    name: foo bar
    primary_email: [email protected]
    age_in_years: 33

However, the test_data.py expects the data to be in a PersonCollection format, which doesn't match the structure of my YAML file. To make it work, I had to adjust the test_data.py to manually load and parse the entries list from the YAML file.

Is the original test_data.py designed to work with a specific YAML structure? If so, could you provide an example of how the YAML file should be structured to match the PersonCollection format?

Thanks for your time!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant