OpenMetadata/ingestion/tests/unit/test_mf4_reader.py
IceS2 5009059441
chore(ingestion): migrate to ruff for format + isort + unused-import (#27739)
* chore(ingestion): replace black/isort/pycln with ruff

- Swap formatter + import-sorter + unused-import tooling for ruff
  (line-length 120, target py3.10) in ingestion + openmetadata-airflow-apis
- Drop dead [tool.mypy] config; basedpyright is the active type checker
- Bump requires-python to >=3.10 to match noxfile and CLAUDE.md (3.9 is
  documented as broken on Mac in noxfile.py)
- Bump pre-commit-hooks v2.3 -> v5.0; the new check-json catches four
  pre-existing JSON issues now excluded with an inline TODO
- Update Makefile py_format / py_format_check targets to call ruff

* chore(ingestion): grandfather ruff lint violations and apply ruff format

- 253 noqa markers added via 'ruff check --add-noqa' across 128 files,
  freezing existing violations so this PR is a tooling-only swap. Per-rule
  cleanup tracked in the TODO comment in ingestion/pyproject.toml.
- Bulk reformat from black 22.3 -> ruff format @ line-length 120.
  Cosmetic only: imports balanced (-32/+32), structural keywords balanced
  (-2221/+2221), no logic changes.
- Star-import rules (F403/F405) globally ignored; refactoring wildcard
  imports across connectors is a separate effort.

* chore(ingestion): fix pylint findings surfaced after ruff format

- filters.py: drop redundant parens around re.match(...) in `if`
  (C0325 superfluous-parens) — exposed when ruff format unwrapped them
- nosql_adaptor.py: move `# pylint: disable=unused-argument` from the
  `column:` line to the `def` line so it covers `table` too (W0613) —
  scope was line-based, lost when ruff split params onto multiple lines
- action1xx.py: replace `arguments-differ` with `signature-differs` in
  the disable directive (was always wrong code) and drop the now-useless
  `unused-argument` suppression (I0021)

* fix(ingestion): make ruff extend-exclude robust to multi-root invocations

CI's `make py_format_check` runs from the repo root and passes both
`ingestion/` and `./openmetadata-airflow-apis/` to ruff in a single
invocation. With multiple root paths, ruff's parallel file discovery
races on extend-exclude matching against the project root, so files
under `ingestion/src/metadata/generated/` were intermittently scanned
and produced ~830 I001 violations.

20-run repro: 10/20 fail without the fix, 20/20 pass with the fix.

Each excluded directory now appears twice in extend-exclude:
- the project-root-relative pattern (cwd = ingestion/)
- the prefixed pattern (cwd = repo root, multi-root invocation)

* chore(ingestion): address gitar-bot findings + cross-version pylint disable

- openmetadata-airflow-apis/pyproject.toml: switch coverage to module-name
  source + [tool.coverage.paths] glob remap (matches the ingestion pattern).
  Drops the hardcoded `env/lib/python3.9/site-packages/...` source path,
  which broke after the requires-python bump to 3.10. (Finding 1)
- ingestion/setup.py: remove dead python_version<'3.9' / >='3.9' guards on
  mysql-connector-python and testcontainers; promote locust to a regular
  test dep (was conditionally added under sys.version_info >= (3, 9)). Also
  remove the now-unused `import sys`. (Finding 3)
- ingestion/src/metadata/great_expectations/action1xx.py: cover both
  arguments-differ (great_expectations 0.18.x parent) and signature-differs
  (great_expectations 1.x parent) in the pylint disable comment, since
  CI installs 0.18.x and local often has 1.x. unused-argument covers the
  unused action_context. The opposite rule fires as I0021 useless-suppression
  on each environment, which is informational and does not affect pylint's
  exit code.
2026-04-27 10:05:28 +02:00

194 lines
7.1 KiB
Python

# Copyright 2025 Collate
# Licensed under the Collate Community License, Version 1.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# https://github.com/open-metadata/OpenMetadata/blob/main/ingestion/LICENSE
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
MF4 reader tests
"""
from unittest import TestCase
from unittest.mock import MagicMock, patch
from metadata.readers.dataframe.mf4 import MF4DataFrameReader
from metadata.readers.dataframe.models import DatalakeColumnWrapper
class TestMF4DataFrameReader(TestCase):
"""
Test MF4DataFrameReader functionality
"""
@patch("metadata.readers.dataframe.base.get_reader")
def setUp(self, mock_get_reader):
"""Set up test fixtures"""
# MF4DataFrameReader requires config_source and client
mock_config_source = MagicMock()
mock_client = MagicMock()
# Mock the reader that get_reader returns
mock_reader = MagicMock()
mock_get_reader.return_value = mock_reader
self.reader = MF4DataFrameReader(config_source=mock_config_source, client=mock_client)
self.mock_reader = mock_reader
def test_extract_schema_from_header_with_common_properties(self):
mock_mdf = MagicMock()
mock_header = MagicMock()
expected_common_props = {
"measurement_id": "TEST_001",
"vehicle_id": "VEH_123",
}
mock_header._common_properties = expected_common_props
mock_mdf.header = mock_header
result = MF4DataFrameReader._extract_header_from_mdf(mock_mdf)
self.assertIsInstance(result, DatalakeColumnWrapper)
self.assertIsNotNone(result.dataframes)
self.assertIsNotNone(result.dataframes())
self.assertEqual(result.raw_data, expected_common_props)
def test_extract_schema_from_header_without_common_properties(self):
mock_mdf = MagicMock()
mock_header = MagicMock()
del mock_header._common_properties
mock_mdf.header = mock_header
result = MF4DataFrameReader._extract_header_from_mdf(mock_mdf)
self.assertIsNotNone(result)
self.assertIsNotNone(result.dataframes)
chunks = list(result.dataframes())
self.assertEqual(len(chunks), 0)
@patch("asammdf.MDF")
def test_local_mf4_reading(self, mock_mdf_class):
mock_mdf = MagicMock()
mock_header = MagicMock()
mock_header._common_properties = {"test_key": "test_value"}
mock_mdf.header = mock_header
mock_mdf_class.return_value = mock_mdf
from metadata.generated.schema.entity.services.connections.database.datalakeConnection import (
LocalConfig,
)
config = LocalConfig()
reader = MF4DataFrameReader(config, None)
result = reader._read(key="test.mf4", bucket_name="")
self.assertIsNotNone(result)
self.assertIsNotNone(result.dataframes)
self.assertIsNotNone(result.dataframes())
@patch("asammdf.MDF")
@patch("tempfile.NamedTemporaryFile")
def test_s3_mf4_reading(self, mock_temp, mock_mdf_class):
from metadata.generated.schema.entity.services.connections.database.datalake.s3Config import (
S3Config,
)
from metadata.generated.schema.security.credentials.awsCredentials import (
AWSCredentials,
)
mock_tmp = MagicMock()
mock_tmp.name = "/tmp/test.mf4"
mock_temp.return_value.__enter__.return_value = mock_tmp
mock_mdf = MagicMock()
mock_header = MagicMock()
mock_header._common_properties = {"measurement_id": "TEST"}
mock_mdf.header = mock_header
mock_mdf_class.return_value = mock_mdf
mock_client = MagicMock()
mock_body = MagicMock()
mock_body.iter_chunks.return_value = [b"chunk1", b"chunk2"]
mock_client.get_object.return_value = {"Body": mock_body}
config = S3Config(
securityConfig=AWSCredentials(awsAccessKeyId="test", awsSecretAccessKey="test", awsRegion="us-east-1")
)
reader = MF4DataFrameReader(config, mock_client)
result = reader._read(key="test.mf4", bucket_name="test-bucket")
self.assertIsNotNone(result)
self.assertIsNotNone(result.dataframes)
dataframes = result.dataframes()
self.assertIsNotNone(dataframes)
@patch("asammdf.MDF")
@patch("gcsfs.GCSFileSystem")
@patch("tempfile.NamedTemporaryFile")
def test_gcs_mf4_reading(self, mock_temp, mock_gcsfs, mock_mdf_class):
from metadata.generated.schema.entity.services.connections.database.datalake.gcsConfig import (
GCSConfig,
)
mock_tmp = MagicMock()
mock_tmp.name = "/tmp/test.mf4"
mock_temp.return_value.__enter__.return_value = mock_tmp
mock_gcs = MagicMock()
mock_gcsfs.return_value = mock_gcs
mock_mdf = MagicMock()
mock_header = MagicMock()
mock_header._common_properties = {"vehicle_id": "VEH_123"}
mock_mdf.header = mock_header
mock_mdf_class.return_value = mock_mdf
config = GCSConfig()
reader = MF4DataFrameReader(config, None)
result = reader._read(key="test.mf4", bucket_name="test-bucket")
self.assertIsNotNone(result)
self.assertIsNotNone(result.dataframes)
dataframes = result.dataframes()
self.assertIsNotNone(dataframes)
@patch("asammdf.MDF")
@patch("adlfs.AzureBlobFileSystem")
@patch("metadata.readers.dataframe.mf4.return_azure_storage_options")
@patch("tempfile.NamedTemporaryFile")
def test_azure_mf4_reading(self, mock_temp, mock_storage_opts, mock_adlfs, mock_mdf_class):
from metadata.generated.schema.entity.services.connections.database.datalake.azureConfig import (
AzureConfig,
)
from metadata.generated.schema.security.credentials.azureCredentials import (
AzureCredentials,
)
mock_tmp = MagicMock()
mock_tmp.name = "/tmp/test.mf4"
mock_temp.return_value.__enter__.return_value = mock_tmp
mock_storage_opts.return_value = {"connection_string": "test"}
mock_fs = MagicMock()
mock_adlfs.return_value = mock_fs
mock_mdf = MagicMock()
mock_header = MagicMock()
mock_header._common_properties = {"test_date": "2025-01-01"}
mock_mdf.header = mock_header
mock_mdf_class.return_value = mock_mdf
config = AzureConfig(securityConfig=AzureCredentials(accountName="test", clientId="test", tenantId="test"))
reader = MF4DataFrameReader(config, None)
result = reader._read(key="test.mf4", bucket_name="test-container")
self.assertIsNotNone(result)
self.assertIsNotNone(result.dataframes)
dataframes = result.dataframes()
self.assertIsNotNone(dataframes)