File: /var/www/vhosts/dial-copper-dev.2amigos.us/dialpad-copper-crm/api/tests/testbase.py
"""Module with basic funcionality common for all tests
"""
import json
from os import path
from typing import List
def read_mock_file(file_name: str) -> str:
"""Reads and returns the content of a mock file
Args:
file_name (str): Mock folder file name with extension
Returns:
str: the file contents
"""
file_path = path.join(path.dirname(__file__), 'mocks', file_name)
if not path.exists(file_path):
raise ValueError(f'The path {file_path} is not valid')
with open(file_path, 'r') as f:
return f.read()
def call_event_mocks(unrelated_events: bool = False) -> List[dict]:
"""Mock of a sequence of call events
Args:
unrelated_events (bool): Get a mock with all call events unrelated to each other
Returns:
List[dict]: JSON Parsed events
"""
if unrelated_events:
return json.loads(read_mock_file('dialpad-full-call-events.json'))
return json.loads(read_mock_file('dialpad-call-events.json'))
def call_event_mock_by_state(state: str) -> dict:
"""Call event mock by a desired state
Args:
state (str): desired mock state
Returns:
dict: JSON Parsed event
"""
return next(iter(filter(lambda e: e['state'] == state, call_event_mocks())))