57 lines
2.0 KiB
Python

"""Tests the Aptiprism OFF file handler."""
# Standard library
import os.path
# Testing
import unittest
# Scientific
import numpy as np
# Module under test
from pyrate.plan.graph.generate import _parse_off_file
TEST_FILES_DIR = os.path.realpath(os.path.join(os.path.dirname(__file__), "example_files/"))
TEST_FILES = [
os.path.join(TEST_FILES_DIR, "geodestic_file_1.off"),
os.path.join(TEST_FILES_DIR, "geodestic_file_2.off"),
os.path.join(TEST_FILES_DIR, "geodesic_-M_s_-c_2_-f_2_ico.off"),
]
class TestOffHandler(unittest.TestCase):
"""Tests the Aptiprism OFF file handler using some examples."""
def test_with_example_files(self):
"""Tests the Aptiprism OFF file handler using three example files."""
for test_file in TEST_FILES:
with self.subTest(f'Test file "{test_file}"'):
# test that it does not crash
with open(test_file, "r", encoding="utf-8") as myfile:
source = myfile.read()
latitudes, longitudes, edges = _parse_off_file(source)
if "geodesic_-M_s_-c_2_-f_2_ico" in test_file:
self.assertEqual(
len(latitudes), 122, f"wrong total number of nodes: {len(latitudes)} instead of 122"
)
self.assertEqual(
edges.shape[0], 360, f"wrong total number of edges: {edges.shape[0]} instead of 360"
)
# the shapes of the returned arrays must match
self.assertEqual(
latitudes.shape, longitudes.shape, "latitude and longitude must have the same shape"
)
self.assertGreater(len(latitudes), 0, "no points found")
# the edges must be valid indices into the edges
self.assertTrue(
np.all(edges[:, :] >= 0) and np.all(edges[:, :] < len(latitudes)),
"some edges reference non-existent points",
)