# test_efe_mongo_to_oracle.py

python 14 hours, 5 minutes ago
# test_efe_mongo_to_oracle.py import unittest from unittest.mock import Mock, patch from efe_mongo_to_oracle import EfeMongoToOracle from datetime import datetime, timezone import pandas as pd class TestEfeMongoToOracle(unittest.TestCase): def setUp(self): # Set up mock services and configuration self.config = Mock() self.mongo_service = Mock() self.oracle_service = Mock() self.moog_service = Mock() # Initialize EfeMongoToOracle with mocked dependencies self.efe_service = EfeMongoToOracle( self.config, self.mongo_service, self.oracle_service, self.moog_service ) @patch("efe_mongo_to_oracle.logging") def test_get_mongo_data_by_runtimes_valid_data(self, mock_logging): # Test successful fetching of MongoDB data by runtimes runtimes = "2024-03-19 12:12:00" namespace = "EFE" process_name = "PROCESS_NAME" column_mapping_df = pd.DataFrame({"SRC_TABLE": ["test_table"]}) # Mock MongoDB data mock_data = [{"_id": 1, "value": "data1"}, {"_id": 2, "value": "data2"}] self.mongo_service.execute_query.return_value = mock_data # Execute the method result = self.efe_service.get_mongo_data_by_runtimes(runtimes, namespace, process_name, column_mapping_df) # Verify MongoDB query call and correct result returned self.mongo_service.execute_query.assert_called_once_with("test_table", {"RUN_TIME": {"$in": [datetime(2024, 3, 19, 12, 12)]}}) self.assertEqual(result, mock_data) @patch("efe_mongo_to_oracle.logging") def test_get_mongo_data_by_runtimes_invalid_collection(self, mock_logging): # Test handling of invalid MongoDB collection in column mapping runtimes = "2024-03-19 12:12:00" namespace = "EFE" process_name = "PROCESS_NAME" column_mapping_df = pd.DataFrame({"SRC_TABLE": ["table1", "table2"]}) # Execute the method result = self.efe_service.get_mongo_data_by_runtimes(runtimes, namespace, process_name, column_mapping_df) # Verify no MongoDB call and appropriate log message self.mongo_service.execute_query.assert_not_called() mock_logging.info.assert_called_with( "Invalid data in AI_SRC_COLUMN_MAPPING. Did not find exactly one src table for namespace EFE and process name PROCESS_NAME" ) self.assertIsNone(result) def test_clean_mongo_data_efe(self): # Test transformation and cleaning of MongoDB data mongo_data = [{"SRC_COLUMN1": "SomeLongText", "SRC_COLUMN2": "data2"}] column_mapping_df = pd.DataFrame({ "SRC_COLUMN": ["SRC_COLUMN1", "SRC_COLUMN2"], "DEST_DATA_TYPE": ["STRING", "STRING"] }) # Execute the method result = self.efe_service.clean_mongo_data_efe(mongo_data, column_mapping_df) # Verify data cleaning result expected_result = [{"SRC_COLUMN1": "SomeLongText", "SRC_COLUMN2": "data2"}] self.assertEqual(result, expected_result) @patch("efe_mongo_to_oracle.logging") def test_get_column_mapping_df(self, mock_logging): # Test fetching and formatting of column mapping from Oracle namespace = "EFE" process_name = "PROCESS_NAME" oracle_result = str([{ 'SRC_TABLE': 'source_table', 'DEST_TABLE': 'dest_table', 'SRC_COLUMN': 'src_column', 'DEST_COLUMN': 'dest_column', 'SRC_DATA_TYPE': 'STRING', 'DEST_DATA_TYPE': 'STRING', 'SRC_FORMAT': None, 'DEST_FORMAT': None }]) # Mock Oracle response self.oracle_service.execute_select_query.return_value = oracle_result # Execute the method result = self.efe_service.get_column_mapping_df(namespace, process_name) # Verify Oracle query call and expected DataFrame result self.oracle_service.execute_select_query.assert_called_once() self.assertIsInstance(result, pd.DataFrame) self.assertIn("SRC_COLUMN", result.columns) @patch("efe_mongo_to_oracle.logging") def test_write_mongo_to_oracle_efe(self, mock_logging): # Test complete process of writing Mongo data to Oracle runtimes = "2024-03-19 12:12:00" namespace = "EFE" process_name = "PROCESS_NAME" column_mapping_df = pd.DataFrame({ "SRC_TABLE": ["source_table"], "DEST_TABLE": ["dest_table"], "SRC_COLUMN": ["src_column"], "DEST_COLUMN": ["dest_column"], "DEST_DATA_TYPE": ["STRING"] }) mongo_data = [{"src_column": "data1"}] # Mock all dependencies in the method self.efe_service.get_column_mapping_df = Mock(return_value=column_mapping_df) self.efe_service.get_mongo_data_by_runtimes = Mock(return_value=mongo_data) self.efe_service.clean_mongo_data_efe = Mock(return_value=mongo_data) # Execute the method self.efe_service.write_mongo_to_oracle_efe(runtimes, namespace, process_name) # Verify logs and Oracle insertion call self.oracle_service.execute_query.assert_called_once() mock_logging.info.assert_any_call( f"Finished inserting {len(mongo_data)} data into oracle dest_table" ) @patch("efe_mongo_to_oracle.logging") def test_write_mongo_to_oracle_efe_exception_handling(self, mock_logging): # Test exception handling within write_mongo_to_oracle_efe runtimes = "2024-03-19 12:12:00" namespace = "EFE" process_name = "PROCESS_NAME" # Cause an exception in one of the method calls self.efe_service.get_column_mapping_df.side_effect = Exception("Test exception") # Execute the method and verify exception handling self.efe_service.write_mongo_to_oracle_efe(runtimes, namespace, process_name) mock_logging.info.assert_called() self.moog_service.create_event.assert_called_once_with( "Exception occurred in write_mongo_to_oracle_efe", "EFE Mongo To Oracle", 'write_mongo_to_oracle_efe' )
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