test_profile_service_3

python 1 day, 5 hours ago
import unittest from unittest.mock import MagicMock, patch from collections import defaultdict from fastapi.responses import JSONResponse from fastapi import BackgroundTasks import pandas as pd import pymongo from modelclasses.profiler_request import ProfilerRequest from modelclasses.get_profiler_request import GetProfilerRequest class TestProfilerService(unittest.TestCase): def setUp(self): self.config = {"db": "test_db"} self.mongo_service = MagicMock() self.oracle_service = MagicMock() self.profiler_service = ProfilerService(self.config, mongo_service=self.mongo_service) @patch("module_name.convert_df_obj_columns") # Replace module_name with the actual module @patch("module_name.create_mongo_dict") @patch("module_name.create_base_custom_dict") @patch("module_name.fill_analysis") @patch("module_name.replace_period_with_hyphen") @patch("module_name.reduce_word_counts_size") @patch("module_name.fill_use_for_anomaly") @patch("module_name.convert_epoch_to_dt") @patch("module_name.convert_percent_to_p") def test_run_profiler( self, mock_convert_percent_to_p, mock_convert_epoch_to_dt, mock_fill_use_for_anomaly, mock_reduce_word_counts_size, mock_replace_period_with_hyphen, mock_fill_analysis, mock_create_base_custom_dict, mock_create_mongo_dict, mock_convert_df_obj_columns, ): profiler_request = ProfilerRequest( datasetId="test_dataset", requestId="test_request", UUID="test_uuid", ) # Mocking dependencies self.oracle_service.execute_query.side_effect = [ [{"DATASET_NAME": "test_collection"}], [{"DATASET_TIMESTAMP_COLUMN": "test_timestamp"}], ] self.mongo_service.execute_aggregate_pipeline.side_effect = [ [{"numFields": 10}], [{"METRIC_TIME": 1629936000}, {"METRIC_TIME": 1629939600}], ] mock_convert_df_obj_columns.return_value = ( pd.DataFrame({"col1": [1, 2, 3]}), "mock_resolution", [{"col_name": "col1", "mean_len": 10, "dtype": "str"}], ) mock_create_mongo_dict.return_value = {} mock_create_base_custom_dict.return_value = defaultdict(dict) result = self.profiler_service.run_profiler(profiler_request) # Assertions self.assertEqual(result, "Success") self.mongo_service.upsert_query.assert_called_once() mock_convert_df_obj_columns.assert_called_once() def test_profiler_request_success(self): profiler_request = ProfilerRequest( datasetId="test_dataset", requestId="test_request", UUID="test_uuid", ) background_task = MagicMock() response = self.profiler_service.profiler_request(profiler_request, background_task) self.assertIsInstance(response, JSONResponse) self.assertEqual(response.status_code, 200) background_task.add_task.assert_called_once() def test_profiler_request_failure(self): profiler_request = ProfilerRequest( datasetId="test_dataset", requestId="test_request", UUID="test_uuid", ) background_task = MagicMock() self.profiler_service.run_profiler = MagicMock(side_effect=Exception("Test exception")) response = self.profiler_service.profiler_request(profiler_request, background_task) self.assertIsInstance(response, JSONResponse) self.assertEqual(response.status_code, 500) def test_get_profiler_no_results(self): self.mongo_service.execute_query.return_value = [] profiler_request = GetProfilerRequest( datasetId="test_dataset", requestId=None, UUID=None, ) response = self.profiler_service.get_profiler(profiler_request) self.assertIsInstance(response, JSONResponse) self.assertEqual(response.status_code, 404) self.assertIn("Error", response.body.decode()) def test_get_profiler_with_results(self): query_results = [ { "requestId": "test_request", "datasetId": "test_dataset", "uuid": "test_uuid", "data": {}, } ] self.mongo_service.execute_query.return_value = query_results profiler_request = GetProfilerRequest( datasetId="test_dataset", requestId="test_request", UUID=None, ) response = self.profiler_service.get_profiler(profiler_request) self.assertIsInstance(response, JSONResponse) self.assertEqual(response.status_code, 200) self.assertIn("test_request", response.body.decode()) if __name__ == "__main__": unittest.main()
17
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