"""Tests for parser.py.""" import pytest from features.utils import parser def test_parse_feature_empty(): """Test parsing feature from a empty string fails.""" with pytest.raises(Exception) as excinfo: parser._parse_features_from_yaml('') assert 'Yaml content is not valid.' in str(excinfo.value) def test_parse_feature_from_string(): """Test parsing feature from a yaml string.""" features = parser._parse_features_from_yaml(""" - name: better_ads variants: minimalist: 0 control: 1 - name: background_color variants: minimalist: 0 catchy: 0 control: 0 force: control: user_ids: [id1] vendor_ids: [7123] """) assert len(features) == 2 better_ads = features['better_ads'] assert better_ads.name == 'better_ads' assert len(better_ads.variants) == 2 assert len(better_ads.force_rules) == 0 background_color = features['background_color'] assert len(background_color.variants) == 3 assert len(background_color.force_rules) == 2 def test_parse_feature_with_duplicate_name(): """Parse features with duplicate names.""" with pytest.raises(Exception) as excinfo: parser._parse_features_from_yaml(""" - name: better_ads variants: minimalist: 0 - name: better_ads variants: maximalist: 0 """) assert 'This feature better_ads is already used' in str(excinfo.value)