Skip to content

Commit

Permalink
Add very basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Beitner committed Oct 15, 2020
1 parent faaee64 commit 25b014f
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[pytest]
addopts =
-rsxX
-vv

--cov-config=.coveragerc
--cov=pyan
--cov-report=html
--cov-report=term-missing:skip-covered
--no-cov-on-fail
testpaths = tests/
log_cli_level = ERROR
log_format = %(asctime)s %(levelname)s %(message)s
log_date_format = %Y-%m-%d %H:%M:%S
cache_dir = .cache
55 changes: 55 additions & 0 deletions tests/test_analyzer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import logging
from glob import glob
import os
import pytest

from pyan.analyzer import CallGraphVisitor

@pytest.fixture
def callgraph():
filenames = glob(os.path.join(os.path.dirname(__file__), "test_code/**/*.py"), recursive=True)
v = CallGraphVisitor(filenames, logger=logging.getLogger())
return v


def get_node(nodes, name):
filtered_nodes = [node for node in nodes if node.get_name() == name]
assert len(filtered_nodes) == 1, f"Node with name {name} should exist"
return filtered_nodes[0]

def get_in_dict(node_dict, name):
return node_dict[get_node(node_dict.keys(), name)]



def test_resolve_import_as(callgraph):
imports = get_in_dict(callgraph.uses_edges, "test_code.submodule2")
get_node(imports, "test_code.submodule1")
assert len(imports) == 1, "only one effective import"


imports = get_in_dict(callgraph.uses_edges, "test_code.submodule1")
get_node(imports, "test_code.subpackage1.submodule1.A")
get_node(imports, "test_code.subpackage1")


def test_import_relative(callgraph):
imports = get_in_dict(callgraph.uses_edges, "test_code.subpackage1.submodule1")
get_node(imports, "test_code.submodule2.test_2")


def test_resolve_use_in_class(callgraph):
uses = get_in_dict(callgraph.uses_edges, "test_code.subpackage1.submodule1.A.__init__")
get_node(uses, "test_code.submodule2.test_2")


def test_resolve_use_in_function(callgraph):
uses = get_in_dict(callgraph.uses_edges, "test_code.submodule2.test_2")
get_node(uses, "test_code.submodule1.test_func1")
get_node(uses, "test_code.submodule1.test_func2")






Empty file added tests/test_code/__init__.py
Empty file.
23 changes: 23 additions & 0 deletions tests/test_code/submodule1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from test_code.subpackage1 import A
from test_code import subpackage1 as subpackage


def test_func1(a):
return a

def test_func2(a):
return a


class B:

def __init__(self, k):
self.a = 1


def to_A(self):
return A(self)

def get_a_via_A(self):
return test_func1(self.to_A().b.a)

7 changes: 7 additions & 0 deletions tests/test_code/submodule2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from . import submodule1
import test_code.submodule1 as b

A = 32

def test_2(a):
return submodule1.test_func2(a) + A + b.test_func1(a)
3 changes: 3 additions & 0 deletions tests/test_code/subpackage1/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from test_code.subpackage1.submodule1 import A

__all__ = ["A"]
7 changes: 7 additions & 0 deletions tests/test_code/subpackage1/submodule1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

from ..submodule2 import test_2

class A:

def __init__(self, b):
self.b = test_2(b)

0 comments on commit 25b014f

Please sign in to comment.