# -*- coding: utf-8 -*- """Which plant folder a job resolves to when nothing names one: the producer on the model's Project Information, matched against each plant folder's aliases. Host-testable half of `knowledge.detect_plant`.""" import io import os import pytest from vibe import knowledge, plantpath ALIASES = {'alpha': ['alpha', 'alpha precast', 'ap'], 'bravo': ['bravo', 'bravo concrete']} def test_exact_alias_resolves_case_insensitively(): assert knowledge.match_plant('ALPHA', ALIASES)[0] == 'alpha' assert knowledge.match_plant('Bravo Concrete', ALIASES)[0] == 'bravo' def test_alias_words_inside_a_longer_name_resolve(): plant, why = knowledge.match_plant('Alpha Precast Co., Inc.', ALIASES) assert plant == 'alpha' assert 'alpha' in why def test_whole_words_only(): # 'ap' must not hide inside 'apex'. assert knowledge.match_plant('Apex Structures', ALIASES) == ( None, knowledge.match_plant('Apex Structures', ALIASES)[1]) assert knowledge.match_plant('Apex Structures', ALIASES)[0] is None def test_no_producer_named_declines_with_a_reason(): plant, why = knowledge.match_plant('', ALIASES) assert plant is None assert 'names no precast producer' in why assert knowledge.match_plant(None, ALIASES)[0] is None def test_several_matches_decline_and_name_them(): aliases = dict(ALIASES, charlie=['alpha']) plant, why = knowledge.match_plant('alpha', aliases) assert plant is None assert 'several' in why and 'alpha' in why and 'charlie' in why def test_no_plant_folders_declines(): plant, why = knowledge.match_plant('alpha', {}) assert plant is None assert 'none of the plant folders' in why def test_plant_ids_lists_folders_with_a_knowledge_file(tmp_path, monkeypatch): monkeypatch.setenv(plantpath.PLANT_ENV, str(tmp_path)) for name in ('alpha', 'bravo'): (tmp_path / name).mkdir() (tmp_path / name / 'knowledge.yaml').write_text('plant:\n id: %s\n' % name) (tmp_path / 'empty').mkdir() # no knowledge.yaml (tmp_path / 'knowledge.yaml').write_text('x: 1\n') # the machine-wide FILE assert plantpath.plant_ids() == ['alpha', 'bravo'] def test_plant_ids_without_a_root_is_empty(tmp_path, monkeypatch): monkeypatch.setenv(plantpath.PLANT_ENV, str(tmp_path / 'nowhere')) assert plantpath.plant_ids() == []