test_s3_service

python 1 week ago
import unittest from unittest.mock import MagicMock, patch from s3service import S3Service from minio import S3Error from io import BytesIO class TestS3Service(unittest.TestCase): def setUp(self): self.mock_config = { "s3": { "DEV": { "BUCKET_NAME": "dev-bucket", "ENDPOINT": "localhost:9000", "ACCESS_KEY_ID": "dev-access-key", "SECRET_ACCESS_KEY": "dev-secret-key", "NUM_POOLS": 10, "MAXSIZE": 50 }, "QA": { "BUCKET_NAME": "qa-bucket", "ENDPOINT": "localhost:9001", "ACCESS_KEY_ID": "qa-access-key", "SECRET_ACCESS_KEY": "qa-secret-key", "NUM_POOLS": 10, "MAXSIZE": 50 }, "PROD": { "BUCKET_NAME": "prod-bucket", "ENDPOINT": "localhost:9002", "ACCESS_KEY_ID": "prod-access-key", "SECRET_ACCESS_KEY": "prod-secret-key", "NUM_POOLS": 10, "MAXSIZE": 50 } } } self.env = "DEV" self.trace_id = "trace123" with patch("s3service.Minio") as mock_minio: self.mock_minio_client = MagicMock() mock_minio.return_value = self.mock_minio_client self.s3_service = S3Service(self.mock_config, env=self.env) def test_initialize_minio_instance(self): with patch("s3service.Minio") as mock_minio: s3_service = S3Service(self.mock_config, env="DEV") self.assertIsNotNone(s3_service.minio_client) mock_minio.assert_called_with( "localhost:9000", access_key="dev-access-key", secret_key="dev-secret-key", http_client=MagicMock() ) def test_get_bucket_list(self): self.mock_minio_client.list_buckets.return_value = ["bucket1", "bucket2"] buckets = self.s3_service.get_bucket_list() self.assertEqual(buckets, ["bucket1", "bucket2"]) def test_upload_files_to_bucket(self): self.mock_minio_client.bucket_exists.return_value = False response = self.s3_service.upload_files_to_bucket( "test-bucket", "file_path.txt", "file_name.txt", self.trace_id ) self.mock_minio_client.make_bucket.assert_called_once_with("test-bucket") self.mock_minio_client.fput_object.assert_called_once_with( "test-bucket", "file_name.txt", "file_path.txt" ) self.assertIn("Successfully uploaded", response["Status"]) def test_file_exists_in_bucket(self): self.mock_minio_client.stat_object.return_value = MagicMock() exists = self.s3_service.file_exists_in_bucket("path/to/file.txt") self.assertTrue(exists) self.mock_minio_client.stat_object.side_effect = S3Error("Error", "Mock error") exists = self.s3_service.file_exists_in_bucket("path/to/missing_file.txt") self.assertFalse(exists) def test_list_files_in_bucket(self): mock_objects = [ MagicMock(object_name="file1.txt"), MagicMock(object_name="file2.txt") ] self.mock_minio_client.list_objects.return_value = mock_objects files = self.s3_service.list_files_in_bucket("prefix/", self.trace_id) self.assertEqual(files, {"file1.txt", "file2.txt"}) def test_download_video_from_bucket(self): mock_data_stream = MagicMock() mock_data_stream.stream.return_value = [b"data1", b"data2"] self.mock_minio_client.get_object.return_value = mock_data_stream response = self.s3_service.download_video_from_bucket("path/to/video.mp4", self.trace_id) self.assertIn("Successfully downloaded", response["Status"]) self.assertIsInstance(response["Data"], BytesIO) def test_stream_object_to_bucket(self): mock_io_object = BytesIO(b"test data") response = self.s3_service.stream_object_to_bucket( mock_io_object, "test_object.txt", self.trace_id ) self.mock_minio_client.put_object.assert_called_once() self.assertIn("Successfully uploaded", response["Status"]) def test_delete_from_bucket(self): mock_objects = [ MagicMock(object_name="file1.txt"), MagicMock(object_name="file2.txt") ] self.mock_minio_client.list_objects.return_value = mock_objects deleted_count = self.s3_service.delete_from_bucket("folder/", self.trace_id) self.assertEqual(deleted_count, 2) self.mock_minio_client.remove_object.assert_any_call(self.mock_config["s3"][self.env]["BUCKET_NAME"], "file1.txt") self.mock_minio_client.remove_object.assert_any_call(self.mock_config["s3"][self.env]["BUCKET_NAME"], "file2.txt") def test_merge_strings(self): result = self.s3_service.merge_strings("prefix/", "/suffix") self.assertEqual(result, "prefix/suffix") result = self.s3_service.merge_strings("prefix123", "123suffix") self.assertEqual(result, "prefix123suffix") if __name__ == "__main__": unittest.main()
43
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