test_write_to_mongo

python 10 hours, 28 minutes ago
import unittest from unittest.mock import MagicMock, patch import pandas as pd import datetime import pytz from your_module import WriteDataFromMongoToOracle # Replace with the actual module name class TestWriteDataFromMongoToOracle(unittest.TestCase): def setUp(self): # Mock the services and configuration dictionary self.config = { 'cloud-ai': { 'EMBEDDING_URL': 'http://embedding.url', 'LABELLING_URL': 'http://labelling.url', 'GET_STRING_ID_URL': 'http://string_id.url', 'CLASSIFICATION_URL': 'http://classification.url' }, 'email': { 'cc-intent-recipients': ['recipient@example.com'] } } self.mongo_service = MagicMock() self.oracle_service = MagicMock() self.moog_service = MagicMock() self.email_service = MagicMock() self.config_service = MagicMock() # Instantiate the main class with mocked dependencies self.instance = WriteDataFromMongoToOracle( self.config, self.mongo_service, self.oracle_service, self.moog_service, self.email_service, self.config_service ) @patch('your_module.datetime') # Mock datetime for consistency def test_get_model_results_from_mongo_with_valid_data(self, mock_datetime): # Set up mock data and time mock_datetime.datetime.fromtimestamp.return_value = datetime.datetime(2023, 1, 1, tzinfo=pytz.UTC) data = [{'run_time': '2023-01-01T00:00:00Z', 'call_id': '123', 'model_results': {'callSummary': 'summary'}}] self.mongo_service.execute_query.return_value = data # Call the method and verify results result = self.instance.get_model_results_from_mongo( 'client_code', None, 1672444800, 1672531200, 'trace_id', 'cc_action_collection' ) self.assertEqual(len(result), 1) self.assertIn('callID', result.columns) self.mongo_service.execute_query.assert_called_once() def test_create_vdn_caller_type_mapping(self): # Mock the oracle service response query_response = "[(1234, 'Type1'), (5678, 'Type2')]" self.oracle_service.execute_select_query.return_value = query_response # Call method vdn_mapping = self.instance.create_vdn_caller_type_mapping('client_code', 'trace_id') # Assert response matches expected dictionary expected_dict = {1234: 'Type1', 5678: 'Type2'} self.assertEqual(vdn_mapping, expected_dict) def test_split_data_on_vdn_type(self): # Mock input DataFrame data = { 'CALLER_TYPE': ['Shareholder', 'Rep'], 'callID': [1, 2] } model_tagged_data = pd.DataFrame(data) # Call method customer_data, rep_data = self.instance.split_data_on_vdn_type(model_tagged_data) # Verify customer and rep data separation self.assertEqual(len(customer_data), 1) self.assertEqual(len(rep_data), 1) self.assertEqual(customer_data.iloc[0]['CALLER_TYPE'], 'Shareholder') self.assertEqual(rep_data.iloc[0]['CALLER_TYPE'], 'Rep') def test_create_model_results_df(self): # Mock input data data = { 'callID': [1], 'model_results': [{'callSummary': 'summary'}], 'caller_type_vdn': [1234], 'ani': ['None'] } model_data = pd.DataFrame(data) vdn_caller_type_dict = {1234: 'Shareholder'} # Call method result_df = self.instance.create_model_results_df(model_data, vdn_caller_type_dict, 'client_code', '2023-01-01', 'trace_id') # Check output DataFrame structure and values self.assertIn('callID', result_df.columns) self.assertIn('CALLER_TYPE', result_df.columns) self.assertEqual(result_df.iloc[0]['CALLER_TYPE'], 'Shareholder') @patch('your_module.logger') def test_executor_updates_status_to_completed(self, mock_logger): # Mock the necessary data and methods self.mongo_service.execute_query.return_value = [] self.oracle_service.execute_query = MagicMock() # Call the method self.instance.executor('client_code', 'run_times', 'trace_id', scan_table='scan_table', scan_id='scan_id') # Verify that it updates the status to 'Completed' update_query_call_args = self.oracle_service.execute_query.call_args_list self.assertIn('Completed', update_query_call_args[1][0][0]) @patch('your_module.time') def test_timer_decorator(self, mock_time): mock_time.time.side_effect = [1, 3] # Mocking start and end times @self.instance.timer def sample_func(): return "Execution Complete" result = sample_func() self.assertEqual(result, "Execution Complete") # Assuming `logging.info` is configured, validate the logging behavior if needed if __name__ == '__main__': unittest.main()
10
Posted By
Python Script to create AWS beanstalk
#!/usr/bin/python
  
import boto
python aws beanstalk
sandeep sandeep
List all files and folders using python os mo
import os

def list_files_folders(path):
python python-os
kishore_kumar
Get current environment variables in python
import os
env = os.environ

python python-os
kishore_kumar
Get os details using python os
import os
print os.uname()
# Don't use os.system('uname -a'), its j
python python-os
kishore_kumar
Get stats ( lines, words, char count ) of fil
def file_stats(path):
    f = open(path, 'r')
    lines = f.readlines()
python
kishore_kumar
Use map function in python
def get_double(num):
    return num * 2

python
kishore_kumar
Python sample codes for beginners
print "Welcome to python"
python
gaya38 gaya38
Python program for even number checking
a=input("Enter a value:")
if (a%2==0):
    print "The given number is even numb
python
gaya38 gaya38
Python program for prime number check
a=input("Enter a value:")
k=0
b=(a/2)+1
python
gaya38 gaya38
Pass command line arguments in python
import sys
x=len(sys.argv)
a=[]
python
gaya38 gaya38
Python program for the largest number in an a
a = [1,43,98,5]#Dummy data
for l in range(len(a)-1):
        if (a[l]>a[l+1]):
python
gaya38 gaya38
print list of even numbers within a range
n=100
a=[10,20,30,40,50]
b=[60,70,80,90]
python
gaya38 gaya38
generate fibonacci series in python
n=input("Enter the constraint to print n
m=input("Enter the maximum value to prin
a=0
python
gaya38 gaya38
Generate Random number within the range in py
import random
print random.uniform(10,500)
python
gaya38 gaya38
Shuffle list elements in python
import random;
z = [1,90,4,2]
z = random.shuffle(z)
python
gaya38 gaya38
use python requests to get contents of url (
import requests

req = requests.get("https://httpbin.org/
python python-requests
kishore_kumar
how to iterate or get values in python dictio
sample_dict = { "number": 1, "fruits": [

for key in sample_dict:
python
kishore_kumar
create matrix and multiply using numpy in pyt
import numpy as np

matrix = [[1,2,3], [4,5,6], [7,8,9]]
python numpy
kishore_kumar
generate random numbers matrix with numpy pyt
import numpy as np

random_arr = np.random.randint(1,50,9)
python numpy
kishore_kumar
Find min , max and mean for numpy arrays
import numpy as np

random_arr = np.random.randint(1,50,9)
python numpy
kishore_kumar