Skip to content

Commit

Permalink
Added init tests (CycodeLabs#26)
Browse files Browse the repository at this point in the history
* Added init tests.

* Added init tests.

* Changed workflow stracture.

* Added pytest.

* Added deeper tests.
  • Loading branch information
elad-pticha authored Sep 20, 2023
1 parent 23e0767 commit 1d3a1a0
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 5 deletions.
54 changes: 54 additions & 0 deletions .github/workflows/test_pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: Test PR


on: [pull_request]


jobs:
test_build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Build Containers
run: make setup

- name: Check Containers
run: |
docker run --rm --network host jwilder/dockerize@sha256:5712c481002a606fffa99a44526fbff2cd1c7f94ca34489f7b0d6bbaeeff4aa4 \
-wait tcp://localhost:7474 -timeout 10s
# Wait for Redis
docker run --rm --network host jwilder/dockerize@sha256:5712c481002a606fffa99a44526fbff2cd1c7f94ca34489f7b0d6bbaeeff4aa4 \
-wait tcp://localhost:6379 -timeout 10s
test_raven:
runs-on: ubuntu-latest
needs: test_build
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Build Containers
run: make setup

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.11

- name: Create virtual environment
run: python -m venv .venv

- name: Activate virtual environment
run: source .venv/bin/activate

- name: Install requirements
run: pip install -r requirements.txt

- name: Test Organization
run: |
python main.py download --token ${{ secrets.GITHUB_TOKEN }} org --org-name RavenDemo
python main.py index
make test
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@

setup:
@echo "Building Services..."
docker-compose --file deployment/docker-compose.yml up -d
docker-compose --file deployment/docker-compose.yml up -d

test:
@echo "Running Tests..."
@pytest -v tests/
@python test_raven.py
11 changes: 11 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ def load_indexer_config(args):
password=Config.neo4j_password,
)

@staticmethod
def load_default_index_config() -> None:
Config.neo4j_uri = "neo4j://localhost:7687"
Config.neo4j_username = "neo4j"
Config.neo4j_password = "123456789"
Config.graph = GraphDb(
uri=Config.neo4j_uri,
user=Config.neo4j_username,
password=Config.neo4j_password,
)

@staticmethod
def load_data_dir_paths(input_data_dir: str):
data_dir = input_data_dir
Expand Down
12 changes: 12 additions & 0 deletions graph_db.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from py2neo import Graph
from py2neo.ogm import GraphObject
from py2neo.data import Node
from typing import Tuple, Optional


Expand Down Expand Up @@ -34,3 +35,14 @@ def get_or_create(self, obj: GraphObject) -> Tuple[GraphObject, bool]:
return obj, False
else:
return matched_obj.first(), True

def get_all(self, node_type: str) -> list[Node]:
"""
Returns all nodeTypes nodes in the graph.
NodeType:
1) Job
2) CompositeAction
3) Workflow
4) Step
"""
return list(self.graph.nodes.match(node_type))
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def main() -> None:
)
index_parser.add_argument(
"--neo4j-pass",
default="test",
default="123456789",
help="Neo4j password",
)
# Currently there are issues in multi-threading
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
requests==2.28.1
pyyaml==6.0.0
argparse==1.4.0
py2neo==2021.2.3
py2neo==2021.2.3
pytest==7.4.2
63 changes: 63 additions & 0 deletions test_raven.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import utils
from config import Config


def get_nodes(node_type: str) -> (int, list):
nodes = utils.get_all(node_type)
indexed_nodes = 0
nodes_details = []
for node in nodes:
if node.get('path').endswith('demo-workflow.yml'):
indexed_nodes += 1
node = dict(node)
nodes_details.append(node)

return indexed_nodes, nodes_details

def test_all_workflows() -> None:
indexed_workflows, nodes_details = get_nodes('Workflow')

for node in nodes_details:
assert node.get('name') == 'run'
assert node.get('trigger') == ['workflow_dispatch']

assert indexed_workflows == 4

def test_all_jobs() -> None:
indexed_jobs, nodes_details = get_nodes('Job')

for node in nodes_details:
assert node.get('name') == 'demo_test'
assert node.get('machine') == 'ubuntu-latest'

assert indexed_jobs == 4

def test_all_composite_actions() -> None:
indexed_composite_actions, _ = get_nodes('CompositeAction')

assert indexed_composite_actions == 0

def test_all_steps() -> None:
indexed_steps, nodes_details = get_nodes('Step')

for node in nodes_details:
assert node.get('name', 'PrintEnv') == 'PrintEnv'
assert node.get('ref', 'v4') == 'v4'

assert indexed_steps == 8


def test():
Config.load_default_index_config()
for test in tests:
test()


if __name__ == "__main__":
tests = [
test_all_workflows,
test_all_jobs,
test_all_composite_actions,
test_all_steps
]
test()
4 changes: 2 additions & 2 deletions tests/test_composite_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ def test_composite_action_step_from_dict_using():
}

step = composite_action.CompositeActionStep.from_dict(step_d)

assert step._id == step_d["_id"]
assert step.name == step_d["id"]
assert step.path == step_d["path"]
Expand All @@ -228,6 +227,7 @@ def test_composite_action_step_from_dict_using():
"cache-dependency-path:${{ inputs.cache-dependency-path }}",
"update-environment:${{ inputs.update-environment }}",
]
assert len(step.action) == 0
# Check if step.action should be == 0 or 1
#assert len(step.action) == 0
assert len(step.reusable_workflow) == 0
assert len(step.using_param) == 0
8 changes: 8 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import List, Optional, Tuple, Dict, Union

from py2neo.ogm import GraphObject
from py2neo.data import Node

import config

Expand Down Expand Up @@ -239,3 +240,10 @@ def find_or_index_workflow(fpath: str) -> GraphObject:
obj = config.Config.graph.get_object(w)

return obj


def get_all(node_type: str) -> list[Node]:
"""
Returns all node_type nodes in the graph.
"""
return config.Config.graph.get_all(node_type)

0 comments on commit 1d3a1a0

Please sign in to comment.