Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions auto_dev/commands/scaffold.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from web3 import Web3
from jinja2 import Environment, FileSystemLoader
from aea.configurations.constants import DEFAULT_AEA_CONFIG_FILE, PROTOCOL_LANGUAGE_PYTHON, SUPPORTED_PROTOCOL_LANGUAGES
from aea.configurations.data_types import PublicId
from aea.configurations.data_types import PublicId, SKILL

from auto_dev.base import build_cli
from auto_dev.enums import FileType, BehaviourTypes
Expand Down Expand Up @@ -718,20 +718,34 @@ def dialogues(


@scaffold.command()
@click.option("--component-type",
type=click.Choice([
SKILL,
]),
required=True,
help="The type of component to scaffold.",
default=SKILL,
)
@click.pass_context
def tests(
ctx,
component_type,
) -> None:
"""Generate tests for an aea component in the current directory
AEA handler from an OpenAPI 3 specification.
"""
Scaffold tests for generated components.
Expects the component to be generated first.
Expects to be in the working directory of the component.
"""

# we assume we are templating for an fsm skill.
logger = ctx.obj["LOGGER"]
verbose = ctx.obj["VERBOSE"]
env = Environment(loader=FileSystemLoader(Path(JINJA_TEMPLATE_FOLDER, "tests", "customs")), autoescape=True)
template = env.get_template("test_custom.jinja")
output = template.render(
name="test",
)
env = Environment(
loader=FileSystemLoader(Path(JINJA_TEMPLATE_FOLDER,
"tests", component_type + "s")),
autoescape=True)
template = env.get_template("tests.jinja")
output = template.render()
if verbose:
logger.info(f"Generated tests: {output}")

Expand Down
1 change: 1 addition & 0 deletions auto_dev/contracts/param_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ class ParamType(Enum):
BYTES = "bytes"
BYTES_ARRAY = "bytes[]"
BYTES32_ARRAY_ARRAY = "bytes32[][]"
BOOL_ARRAY = "bool[]"
1 change: 1 addition & 0 deletions auto_dev/contracts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
ParamType.INT256_ARRAY: "List[int]",
ParamType.UINT256_2_ARRAY: "List[int]",
ParamType.UINT256_3_ARRAY: "List[int]",
ParamType.BOOL_ARRAY: "List[bool]",
}


Expand Down
24 changes: 13 additions & 11 deletions auto_dev/data/templates/behaviours/simple_fsm.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@ class BaseState(State, ABC):
self._event = None
self._is_done = False # Initially, the state is not done

def act(self) -> None:
"""Perform the act."""
print(f"Performing action for state {self._state}")
self._is_done = True
self._event = {{class_name}}Events.DONE

def is_done(self) -> bool:
"""Is done."""
return self._is_done
Expand All @@ -66,17 +60,25 @@ class BaseState(State, ABC):
{% for state in states %}
class {{ state }}(BaseState):
"""This class implements the behaviour of the state {{ state }}."""
def __init__(self, **kwargs: Any) -> None:
super().__init__(**kwargs)
self._state = {{class_name}}States.{{state.upper()}}
{% endfor %}

_state = {{class_name}}States.{{state.upper()}}

def act(self) -> None:
"""Perform the act."""
print(f"Performing action for state {self._state}")
self._is_done = True
self._event = {{class_name}}Events.DONE
{% endfor %}


class {{class_name}}FsmBehaviour(FSMBehaviour):
"""This class implements a simple Finite State Machine behaviour."""

rounds = [
{% for state in states %}
{{state}},
{% endfor %}
]

def __init__(self, **kwargs: Any) -> None:
super().__init__(**kwargs)
self.register_state({{class_name}}States.{{default_start_state.upper()}}.value, {{default_start_state}}(**kwargs), True)
Expand Down
56 changes: 56 additions & 0 deletions auto_dev/data/templates/tests/skills/tests.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2023 {{author}}
# Copyright 2023 valory-xyz
Comment on lines +4 to +5
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong year, make it dynamic. Remove valory-xyz as well

#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ------------------------------------------------------------------------------

"""This package contains a behaviour test that is autogenerated from the protocol `{{protocol_name}}`."""

import json
import logging
from typing import cast
from pathlib import Path
from unittest.mock import patch
import pytest

from aea.test_tools.test_skill import BaseSkillTestCase
from aea.protocols.dialogue.base import DialogueMessage

from packages.{{public_id.author}}.skills.{{public_id.name}} import PUBLIC_ID
from packages.{{public_id.author}}.skills.{{public_id.name}}.behaviours import (
{{class_name}}FsmBehaviour,
)

ROOT_DIR = Path(__file__).parent.parent.parent.parent.parent.parent


class BaseTestCase(BaseSkillTestCase):
"""Base test case for the fsm."""

path_to_skill = Path(ROOT_DIR, "packages", PUBLIC_ID.author, "skills", PUBLIC_ID.name)

{% for state in states %}
class Test{{state}}Act(BaseSkillTestCase):
"""Test {{state}}."""
round_class = {{class_name}}FsmBehaviour.{{state}}

def test_act(self):
"""Test the act method of the round."""
round = self.round_class(name="test", skill_context=self.skill.skill_context)
round.act()
assert round.is_done()
{% endfor %}
19 changes: 16 additions & 3 deletions auto_dev/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import os
from pathlib import Path
from multiprocessing import cpu_count
import shutil
import sys

import pytest


COVERAGE_COMMAND = f"""coverage report \
Expand Down Expand Up @@ -62,5 +63,17 @@ def test_path(
args = [path, *extra_args]
os.environ["PYTHONPATH"] = "."
os.environ["PYTHONWARNINGS"] = "ignore"
result = pytest.main(args)
return result == 0
os.environ["PYTHONPYCACHEPREFIX"] = "/dev/null"
os.environ["PYTHONDONTWRITEBYTECODE"] = "1"
import pytest

sys.dont_write_bytecode = True
result = pytest.main(args, )


# We remove the __pycache__ directories to avoid conflicts
result = (result == 0 or result == pytest.ExitCode.NO_TESTS_COLLECTED)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means we dont need tests in a component for it to pass the tests.

I am in a 2 minds about this;

A) it definitely does make it easier,

But the thing about tasking the path of least resistance is that we SHOULD have tests for everything we generate.

if not result:
breakpoint()
return result

Loading
Loading