Files
our-claude-skills/custom-skills/97-ourdigital-okf/code/scripts/tests/test_okf_common.py
Andrew Yim 7b239dda8f feat(okf): add okf_validate; harden YAML parser for block lists + folded scalars
Verified conformant against Google reference bundles (crypto_bitcoin, ga4, stackoverflow).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-16 20:35:29 +09:00

63 lines
2.6 KiB
Python

import sys, unittest
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from okf_common import (parse_frontmatter, parse_yaml_subset,
extract_links, concept_id, FrontmatterError)
class TestFrontmatter(unittest.TestCase):
def test_parse_basic(self):
text = "---\ntype: BigQuery Table\ntitle: Orders\ntags: [sales, revenue]\n---\n\n# Body\n"
meta, body = parse_frontmatter(text)
self.assertEqual(meta["type"], "BigQuery Table")
self.assertEqual(meta["title"], "Orders")
self.assertEqual(meta["tags"], ["sales", "revenue"])
self.assertIn("# Body", body)
def test_url_value_with_colons(self):
meta, _ = parse_frontmatter("---\ntype: X\nresource: https://e.com/a?b=c\n---\nbody")
self.assertEqual(meta["resource"], "https://e.com/a?b=c")
def test_quoted_value(self):
self.assertEqual(parse_yaml_subset('okf_version: "0.1"')["okf_version"], "0.1")
def test_empty_list(self):
self.assertEqual(parse_yaml_subset("tags: []")["tags"], [])
def test_block_list(self):
raw = "type: BigQuery Dataset\ntags:\n- cryptocurrency\n- bitcoin\n- public data\ntimestamp: '2026-05-28T22:44:47+00:00'"
meta = parse_yaml_subset(raw)
self.assertEqual(meta["tags"], ["cryptocurrency", "bitcoin", "public data"])
self.assertEqual(meta["timestamp"], "2026-05-28T22:44:47+00:00")
def test_folded_multiline_scalar(self):
raw = "description: This dataset contains a complete history of the Bitcoin\n blockchain and updates every 10 minutes.\ntype: BigQuery Dataset"
meta = parse_yaml_subset(raw)
self.assertEqual(
meta["description"],
"This dataset contains a complete history of the Bitcoin blockchain and updates every 10 minutes.",
)
self.assertEqual(meta["type"], "BigQuery Dataset")
def test_no_frontmatter(self):
meta, body = parse_frontmatter("# Just markdown\n")
self.assertIsNone(meta)
self.assertEqual(body, "# Just markdown\n")
def test_unclosed_raises(self):
with self.assertRaises(FrontmatterError):
parse_frontmatter("---\ntype: X\n\nbody without close")
class TestLinksAndIds(unittest.TestCase):
def test_extract_links(self):
body = "See [a](/tables/a.md) and [b](./b.md) and [ext](https://x.com)."
self.assertEqual(extract_links(body), ["/tables/a.md", "./b.md", "https://x.com"])
def test_concept_id(self):
self.assertEqual(concept_id("/bundle", "/bundle/tables/users.md"), "tables/users")
if __name__ == "__main__":
unittest.main()