Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: run pre commit #51

Merged
merged 1 commit into from
Jan 16, 2024
Merged
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
13 changes: 13 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
default_install_hook_types: [pre-push]
default_stages: [pre-push]
repos:
- repo: local
hooks:
- id: pytest-check
name: pytest-check
entry: make
language: system
pass_filenames: false
always_run: true
args: ["pre-commit"]
stages: [pre-push]
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

Serverless Functions represent an innovative approach to implementing and executing code in cloud computing environments. This architecture enables developers to create and execute code fragments in a granular manner, without concerns about the underlying infrastructure.

## Pre-Commit Setup

Before pushing your changes, it's recommended to set up pre-commit to run automated tests locally. Run the following command (needs to be done once):

```bash
pre-commit install
```

## OpenFaaS: Simplifying Serverless Functions Management

OpenFaaS, or Open Functions as a Service, is an open-source platform simplifying the entire life cycle of Serverless Functions. It provides a highly flexible and scalable framework for developing and executing functions in both cloud environments and local infrastructures.
Expand Down
9 changes: 4 additions & 5 deletions das-query-engine/action_dispatcher.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from typing import Union

from action_mapper import ActionMapper
from actions import ActionType
from exceptions import UnknownActionDispatcher, PayloadMalformed
from exceptions import PayloadMalformed, UnknownActionDispatcher
from validators import validate
from action_mapper import ActionMapper


class ActionDispatcher:
Expand All @@ -17,9 +18,7 @@ def dispatch(
action_map = self.action_mapper.get_action_dispatcher(action_type)

if action_map is None:
raise UnknownActionDispatcher(
f"Exception at dispatch: action {action_type} unknown"
)
raise UnknownActionDispatcher(f"Exception at dispatch: action {action_type} unknown")

action = action_map["action"]
validator = action_map.get("validator", None)
Expand Down
18 changes: 8 additions & 10 deletions das-query-engine/action_mapper.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from actions import Actions
from typing import Any, Dict

from actions import Actions, ActionType
from exceptions import UnknownActionDispatcher
from validators.actions import (
GetAtomValidator,
GetNodeValidator,
GetLinkValidator,
GetIncomingLinksValidator,
GetLinksValidator,
GetLinkValidator,
GetNodeValidator,
QueryValidator,
GetIncomingLinksValidator,
)
from actions import ActionType
from exceptions import UnknownActionDispatcher
from typing import Any, Dict


class ActionMapper:
Expand Down Expand Up @@ -62,7 +62,5 @@ def get_action_dispatcher(self, action_type: ActionType) -> Dict[str, Any]:
action_map = self._build_dispatcher().get(action_type)

if action_map is None:
raise UnknownActionDispatcher(
f"Exception at dispatch: action {action_type} unknown"
)
raise UnknownActionDispatcher(f"Exception at dispatch: action {action_type} unknown")
return action_map
8 changes: 4 additions & 4 deletions das-query-engine/actions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import os
from enum import Enum
from hyperon_das import DistributedAtomSpace
from hyperon_das.utils import QueryOutputFormat
from typing import List, Dict, Any, Tuple
from utils.decorators import remove_none_args, execution_time_tracker
from typing import Any, Dict, List, Tuple

from exceptions import UnreachableConnection
from hyperon_das import DistributedAtomSpace
from utils.decorators import execution_time_tracker, remove_none_args


class ActionType(str, Enum):
Expand Down
21 changes: 6 additions & 15 deletions das-query-engine/handler.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,31 @@
import json
import base64
import json
import traceback

from action_dispatcher import ActionDispatcher
from action_mapper import ActionMapper
from exceptions import PayloadMalformed, UnknownActionDispatcher, UnreachableConnection
from hyperon_das.logger import logger
from utils.dotenv import load_env
from validators import validate
from validators.event import EventValidator
from action_dispatcher import ActionDispatcher
from exceptions import UnknownActionDispatcher, UnreachableConnection, PayloadMalformed
from action_mapper import ActionMapper

load_env()


def _response(
http_code_response: int,
result: str,
context: any = None,
headers: dict = {},
):
logger().info(f"Function status code response - {http_code_response}")

if context is None:
try:
return json.loads(result)
except:
return result

return {
"statusCode": http_code_response,
"body": json.dumps(result),
"headers": {
"Content-Type": "application/json",
"X-Handler-Method-Timestamp": headers.get(
"X-Handler-Method-Timestamp", None
),
"X-Handler-Method-Timestamp": headers.get("X-Handler-Method-Timestamp", None),
},
}

Expand Down Expand Up @@ -87,7 +79,6 @@ def handle(event: any, context=None):
return _response(
http_code_response,
result,
context=context,
headers={
"X-Handler-Method-Timestamp": elapsed_time,
},
Expand Down
7 changes: 4 additions & 3 deletions das-query-engine/tests/integration/handle/base_test_action.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import pytest
from abc import ABC, abstractmethod
from handler import handle, UnreachableConnection
import json
from abc import ABC, abstractmethod

import pytest
from handler import UnreachableConnection, handle


class BaseTestHandlerAction(ABC):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest
from actions import ActionType
from tests.integration.handle.base_test_action import BaseTestHandlerAction
import pytest


class TestCountAtomsAction(BaseTestHandlerAction):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from actions import ActionType
from tests.integration.handle.base_test_action import BaseTestHandlerAction
from hyperon_das_atomdb.utils.expression_hasher import ExpressionHasher
import pytest
from tests.integration.handle.base_test_action import BaseTestHandlerAction


class TestGetAtomAction(BaseTestHandlerAction):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from actions import ActionType
from tests.integration.handle.base_test_action import BaseTestHandlerAction
from hyperon_das_atomdb.utils.expression_hasher import ExpressionHasher
import pytest
from tests.integration.handle.base_test_action import BaseTestHandlerAction


class TestGetLinkAction(BaseTestHandlerAction):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import pytest
from actions import ActionType
from tests.integration.handle.base_test_action import BaseTestHandlerAction
from hyperon_das_atomdb.utils.expression_hasher import ExpressionHasher
import pytest


class TestGetLinksAction(BaseTestHandlerAction):
Expand All @@ -11,9 +10,6 @@ def action_type(self):

@pytest.fixture
def valid_event(self, action_type):
human_handle = ExpressionHasher.terminal_hash("Concept", "human")
monkey_handle = ExpressionHasher.terminal_hash("Concept", "monkey")

return {
"body": {
"action": action_type,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from actions import ActionType
from tests.integration.handle.base_test_action import BaseTestHandlerAction
from hyperon_das_atomdb.utils.expression_hasher import ExpressionHasher
import pytest
from tests.integration.handle.base_test_action import BaseTestHandlerAction


class TestGetNodeAction(BaseTestHandlerAction):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest
from actions import ActionType
from tests.integration.handle.base_test_action import BaseTestHandlerAction
import pytest


class TestPingAction(BaseTestHandlerAction):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest
from actions import ActionType
from tests.integration.handle.base_test_action import BaseTestHandlerAction
import pytest


class TestQueryAction(BaseTestHandlerAction):
Expand Down
5 changes: 3 additions & 2 deletions das-query-engine/tests/unit/test_action_dispatcher.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import pytest
from unittest.mock import MagicMock

import pytest
from action_dispatcher import (
ActionDispatcher,
ActionType,
UnknownActionDispatcher,
PayloadMalformed,
UnknownActionDispatcher,
)
from validators.actions import GetAtomValidator

Expand Down
10 changes: 4 additions & 6 deletions das-query-engine/tests/unit/test_action_mapper.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import re
from unittest.mock import Mock

import pytest
from actions import ActionType
from action_mapper import ActionMapper
from unittest.mock import Mock
from actions import ActionType
from exceptions import UnknownActionDispatcher


Expand Down Expand Up @@ -93,10 +94,7 @@ def test_build_dispatcher_commit_changes_action():

dispatchers = action_mapper._build_dispatcher()

assert (
dispatchers[ActionType.COMMIT_CHANGES]["action"]
== expected_actions.commit_changes
)
assert dispatchers[ActionType.COMMIT_CHANGES]["action"] == expected_actions.commit_changes
assert dispatchers[ActionType.COMMIT_CHANGES]["validator"] is None


Expand Down
7 changes: 3 additions & 4 deletions das-query-engine/tests/unit/test_decorators.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# test_execution_helpers.py
import time
from utils.decorators import remove_none_args, execution_time_tracker

from utils.decorators import execution_time_tracker, remove_none_args


def test_remove_none_args():
Expand All @@ -11,9 +12,7 @@ def example_function(arg1, arg2, kwarg1="kwarg1", kwarg2="kwarg2"):
result = example_function(1, 2, kwarg1="value", kwarg2=None)
assert result == (1, 2, "value", "kwarg2")

result_all_not_none = example_function(
1, "2", kwarg1="value", kwarg2="another_value"
)
result_all_not_none = example_function(1, "2", kwarg1="value", kwarg2="another_value")
assert result_all_not_none == (1, "2", "value", "another_value")


Expand Down
2 changes: 1 addition & 1 deletion das-query-engine/tests/unit/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ def test_handle_invalid_action():
payload = {"action": "INVALID_ACTION", "input": {}}
result = handle(payload)

assert "error" in result
assert "body" in result
12 changes: 6 additions & 6 deletions das-query-engine/tests/unit/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@


def test_validate_dict_empty():
assert validate_dict({}) == True
assert validate_dict({}) is True


def test_validate_dict_non_empty():
assert validate_dict({"key": "value"}) == True
assert validate_dict({"key": "value"}) is True


def test_validate_dict_invalid_input():
assert validate_dict("not a dict") == False
assert validate_dict("not a dict") is False


def test_validate_dict_none_input():
assert validate_dict(None) == False
assert validate_dict(None) is False


def test_validate_dict_with_args():
assert validate_dict({}, 1, 2, 3) == True
assert validate_dict({}, 1, 2, 3) is True


def test_validate_dict_with_kwargs():
assert validate_dict({}, key="value") == True
assert validate_dict({}, key="value") is True
5 changes: 2 additions & 3 deletions das-query-engine/utils/decorators.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import time
from functools import wraps

from hyperon_das.logger import logger


Expand All @@ -20,9 +21,7 @@ def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
end_time = time.time()
elapsed_time = end_time - start_time
logger().info(
f"The function '{func.__name__}' took {elapsed_time} seconds to execute"
)
logger().info(f"The function '{func.__name__}' took {elapsed_time} seconds to execute")
return result, elapsed_time

return wrapper
7 changes: 2 additions & 5 deletions das-query-engine/utils/dotenv.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os

from config import env_schema
from hyperon_das.logger import logger

Expand All @@ -15,11 +16,7 @@ def load_env():
raise Exception(error_message)

var_value_display = (
"*****"
if constraints["hidden"]
else var_value
if var_value != ""
else "(empty)"
"*****" if constraints["hidden"] else var_value if var_value != "" else "(empty)"
)
env_log_entries.append(f"{var} - {var_value_display}")

Expand Down
5 changes: 3 additions & 2 deletions das-query-engine/validators/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
from incoming import PayloadValidator

from exceptions import PayloadMalformed
from incoming import PayloadValidator


def validate(validator: PayloadValidator, payload: any) -> any:
Expand All @@ -14,6 +15,6 @@ def validate(validator: PayloadValidator, payload: any) -> any:
return payload
except Exception as e:
raise PayloadMalformed(
message=f"Exception at validate: payload malformed",
message="Exception at validate: payload malformed",
details=str(e),
)
Loading