Skip to content

Commit

Permalink
Unit test for generated init file (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
slundqui authored Nov 28, 2023
1 parent 9bf7b4e commit 7d4d8e1
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 19 deletions.
13 changes: 1 addition & 12 deletions pypechain/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@

from web3.exceptions import NoABIFunctionsFound

from pypechain.render.init import render_init_file
from pypechain.render.main import render_files
from pypechain.utilities.file import write_string_to_file
from pypechain.utilities.format import apply_black_formatting
from pypechain.utilities.templates import get_jinja_env


def main(argv: Sequence[str] | None = None) -> None:
Expand Down Expand Up @@ -64,15 +62,6 @@ def main(argv: Sequence[str] | None = None) -> None:
render_init_file(output_dir, file_names, line_length)


def render_init_file(output_dir: str, file_names: list[str], line_length):
"""Creates an __init__.py file that imports all other files."""
env = get_jinja_env()
init_template = env.get_template("init.py.jinja2")
init_code = init_template.render(file_names=file_names)
formatted_init_code = apply_black_formatting(init_code, line_length)
write_string_to_file(f"{output_dir}/__init__.py", formatted_init_code)


def gather_json_files(directory: str) -> list:
"""Gathers all JSON files in the specified directory and its subdirectories."""
return list(Path(directory).rglob("*.json"))
Expand Down
10 changes: 3 additions & 7 deletions pypechain/render/contract_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@


class TestOverloading:
"""Tests pipeline from bots making trades to viewing the trades in the db"""
"""Tests overloading for code gen using snapshots"""

def test_overloading(self, snapshot):
"""Runs the entire pipeline and checks the database at the end.
All arguments are fixtures.
"""
"""Tests overloading for code gen using snapshots"""

env = get_jinja_env()
functions_template = env.get_template("contract.py/functions.py.jinja2")
Expand Down Expand Up @@ -69,9 +67,7 @@ def test_overloading(self, snapshot):
snapshot.assert_match(functions_block, "expected_overloading.py")

def test_notoverloading(self, snapshot):
"""Runs the entire pipeline and checks the database at the end.
All arguments are fixtures.
"""
"""Tests not overloading for code gen using snapshots"""

env = get_jinja_env()
functions_template = env.get_template("contract.py/functions.py.jinja2")
Expand Down
13 changes: 13 additions & 0 deletions pypechain/render/init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""Functions to render __init__.py from a list of filenames usng a jinja2 template."""
from pypechain.utilities.file import write_string_to_file
from pypechain.utilities.format import apply_black_formatting
from pypechain.utilities.templates import get_jinja_env


def render_init_file(output_dir: str, file_names: list[str], line_length):
"""Creates an __init__.py file that imports all other files."""
env = get_jinja_env()
init_template = env.get_template("init.py.jinja2")
init_code = init_template.render(file_names=file_names)
formatted_init_code = apply_black_formatting(init_code, line_length)
write_string_to_file(f"{output_dir}/__init__.py", formatted_init_code)
35 changes: 35 additions & 0 deletions pypechain/render/init_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""Tests for rendering the init file."""
import os

from pypechain.utilities.templates import get_jinja_env

# using pytest fixtures necessitates this.
# pylint: disable=redefined-outer-name

current_path = os.path.abspath(os.path.dirname(__file__))
project_root = os.path.dirname(os.path.dirname(current_path))


class TestRenderInit:
"""Tests rendering for the init file using snapshots"""

def test_render_init(self, snapshot):
"""
Tests the code gen init file using snapshots
"""

env = get_jinja_env()
init_template = env.get_template("init.py.jinja2")

# TODO: add return types to function calls

file_names = [
"ContractFile1",
"ContractFile2",
"ContractFile3",
]

init_code = init_template.render(file_names=file_names)

snapshot.snapshot_dir = "snapshots" # This line is optional.
snapshot.assert_match(init_code, "expected_init.py")
8 changes: 8 additions & 0 deletions snapshots/expected_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""Export all types from generated files.
DO NOT EDIT. This file was generated by pypechain. See documentation at
https://github.com/delvtech/pypechain"""

from .ContractFile1 import *
from .ContractFile2 import *
from .ContractFile3 import *

0 comments on commit 7d4d8e1

Please sign in to comment.