-
Couldn't load subscription status.
- Fork 16
DPNLPF-1571: smart_geo action, handler #218
Open
SyrexMinus
wants to merge
2
commits into
main
Choose a base branch
from
feature/smart_geo
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| from typing import Dict, Any, Optional, Union, List | ||
|
|
||
| from core.basic_models.actions.command import Command | ||
| from core.basic_models.actions.string_actions import StringAction | ||
| from core.model.base_user import BaseUser | ||
| from core.text_preprocessing.base import BaseTextPreprocessingResult | ||
| from smart_kit.names.message_names import GET_PROFILE_DATA | ||
|
|
||
|
|
||
| class SmartGeoAction(StringAction): | ||
| def run(self, user: BaseUser, text_preprocessing_result: BaseTextPreprocessingResult, | ||
| params: Optional[Dict[str, Union[str, float, int]]] = None) -> List[Command]: | ||
| commands = [Command(GET_PROFILE_DATA)] | ||
| return commands |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| from core.logging.logger_utils import log | ||
| from core.names.field import MESSAGE | ||
| from core.text_preprocessing.preprocessing_result import TextPreprocessingResult | ||
| from scenarios.user.user_model import User | ||
|
|
||
| from smart_kit.handlers.handler_base import HandlerBase | ||
| from smart_kit.names.field import PROFILE_DATA, STATUS_CODE, CODE, GEO | ||
|
|
||
|
|
||
| class HandlerTakeProfileData(HandlerBase): | ||
| SUCCESS_CODE = 1 | ||
|
|
||
| def run(self, payload, user: User): | ||
| super().run(payload, user) | ||
| log(f"{self.__class__.__name__} started", user) | ||
|
|
||
| text_preprocessing_result = TextPreprocessingResult(payload.get(MESSAGE, {})) | ||
| action = user.descriptions["external_actions"]["smart_geo_fail"] | ||
| if payload.get(STATUS_CODE, {}).get(CODE) == self.SUCCESS_CODE: | ||
| action = user.descriptions["external_actions"]["smart_geo_success"] | ||
| user.variables.set("smart_geo", payload.get(PROFILE_DATA, {}).get(GEO)) | ||
| return action.run(user, text_preprocessing_result) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| PROFILE_DATA = "profile_data" | ||
| STATUS_CODE = "status_code" | ||
| CODE = "code" | ||
| GEO = "geo" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
tests/smart_kit_tests/handlers/test_handle_take_profile_data.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # coding: utf-8 | ||
| import unittest | ||
| from unittest.mock import Mock, MagicMock | ||
|
|
||
| from smart_kit.handlers import handler_take_profile_data | ||
|
|
||
|
|
||
| class MockVariables(Mock): | ||
| _storage = {} | ||
|
|
||
| def set(self, x, y): | ||
| self._storage[x] = y | ||
|
|
||
| def __getitem__(self, item): | ||
| return self._storage[item] | ||
|
|
||
| def get(self, item, default=None): | ||
| if item in self._storage: | ||
| return self._storage[item] | ||
| return default | ||
|
|
||
|
|
||
| class HandleTakeProfileDataTest(unittest.TestCase): | ||
| def setUp(self): | ||
| self.app_name = "TestAppName" | ||
| self.test_payload_1 = {"server_action": {}} | ||
| self.test_payload_2 = {"server_action": {"action_id": 1, "parameters": 1}} | ||
| self.test_user = MagicMock('user', message=MagicMock(message_name="some_name"), variables=MockVariables()) | ||
| self.test_user.descriptions = {"external_actions": {"smart_geo_fail": MagicMock(run=lambda x, y: "fail"), | ||
| "smart_geo_success": MagicMock(run=lambda x, y: "success")}} | ||
|
|
||
| def test_handle_take_profile_data_init(self): | ||
| obj = handler_take_profile_data.HandlerTakeProfileData(self.app_name) | ||
| self.assertIsNotNone(obj.SUCCESS_CODE) | ||
| self.assertIsNotNone(handler_take_profile_data.GEO) | ||
|
|
||
| def test_handle_take_profile_data_run_fail(self): | ||
| obj = handler_take_profile_data.HandlerTakeProfileData(self.app_name) | ||
| payload = {"status_code": {"code": 102}} | ||
| self.assertEqual(obj.run(payload, self.test_user), "fail") | ||
|
|
||
| def test_handle_take_profile_data_run_success(self): | ||
| obj = handler_take_profile_data.HandlerTakeProfileData(self.app_name) | ||
| payload = {"profile_data": {"geo": {"reverseGeocoding": {"country": "Российская Федерация"}, | ||
| "location": {"lat": 10.125, "lon": 10.0124}}}, | ||
| "status_code": {"code": 1}} | ||
| self.assertEqual(obj.run(payload, self.test_user), "success") | ||
| self.assertEqual(self.test_user.variables.get("smart_geo"), payload["profile_data"]["geo"]) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.