Skip to content

Commit

Permalink
Christopherngutierrez/test context parse results (#37)
Browse files Browse the repository at this point in the history
* Modified Context in ParseResults to verify that _commands does not contain multiple Context or no Context. Doing so will raise a LookupError with a description of the problem. Two test cases are also included in tests/test_kerngen.py to verify that the correct error is raised.

* Renamed tests for ParseResults. Modified check to occur when accessing ParseResults.context and removed checks in constructor
* Update kerngen/tests/test_kerngen.py
* Fixed copyright issue in test_kerngen.py.
* Removed modified README.md file from pull request

---------

Co-authored-by: Hamish Hunt <[email protected]>
  • Loading branch information
christopherngutierrez and hamishun committed Oct 21, 2024
1 parent e646786 commit d826851
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
16 changes: 13 additions & 3 deletions kerngen/high_parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,22 @@ def __init__(self, iterable, symbols_map):
self._commands = list(iterable)
self._symbols_map = symbols_map

@staticmethod
def _get_context_from_commands_list(commands):
"""Validates that the commands list contains a single context"""
context_list = [context for context in commands if isinstance(context, Context)]
if not context_list:
raise LookupError("No Context found for commands list for ParseResults")
if len(context_list) > 1:
raise LookupError(
"Multiple Context found in commands list for ParseResults"
)
return context_list[0]

@property
def context(self):
"""Return found context"""
return next(
context for context in self._commands if isinstance(context, Context)
)
return ParseResults._get_context_from_commands_list(self._commands)

@property
def commands(self):
Expand Down
25 changes: 25 additions & 0 deletions kerngen/tests/test_kerngen.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# Copyright (C) 2024 Intel Corporation
# SPDX-License-Identifier: Apache-2.0

"""Test the expected behaviour of the kerngen script"""

from enum import Enum
from pathlib import Path
from subprocess import run
from high_parser.parser import ParseResults
from high_parser.types import Context
import pytest


Expand Down Expand Up @@ -93,6 +96,28 @@ def test_invalid_scheme(kerngen_path):
assert result.returncode != 0


def test_parse_results_missing_context():
"""Test ParseResults constructor for missing context"""
with pytest.raises(LookupError) as e:
parse_results = ParseResults([], {})
print(parse_results.context) # will generate a lookup error
assert "No Context found for commands list for ParseResults" in str(e.value)


def test_parse_results_multiple_context():
"""Test ParseResults constructor for multiple context"""
with pytest.raises(LookupError) as e:
parse_results = ParseResults(
[
Context(scheme="BGV", poly_order=8192, max_rns=1),
Context(scheme="CKKS", poly_order=8192, max_rns=1),
],
{},
)
print(parse_results.context) # will raise a LookupError
assert "Multiple Context found in commands list for ParseResults" in str(e.value)


@pytest.fixture(name="gen_op_data")
def fixture_gen_op_data(request):
"""Given an op name, return both the input and expected output strings"""
Expand Down
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
pythonpath = kerngen

0 comments on commit d826851

Please sign in to comment.