Skip to content

Commit

Permalink
Db 1080 improvements (#92)
Browse files Browse the repository at this point in the history
* adding rename method

* linting issues

* fixing unit tests

* updated name of method

* pr changes

* adding unit test
  • Loading branch information
macedomario authored Jan 10, 2022
1 parent 3ac8368 commit 54aa08f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
8 changes: 8 additions & 0 deletions syngenta_digital_dta/s3/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,16 @@ def list_dir_subfolders(self, **kwargs):
def list_dir_files(self, **kwargs):
paginator = self.client.get_paginator('list_objects_v2')
pages = paginator.paginate(Bucket=self.bucket, Prefix=kwargs.get('dir_name'))
if kwargs.get('date'):
return [obj['Key'] for page in pages for obj in page['Contents'] if obj['LastModified'].replace(tzinfo=None) > kwargs.get('date')]

return [obj['Key'] for page in pages for obj in page['Contents']]

def rename_object(self, **kwargs):
old_file_key = kwargs.get('old_file_name')
self.resource.Object(self.bucket, kwargs.get('new_file_name')).copy_from(CopySource={'Bucket': self.bucket, 'Key': old_file_key})
self.delete(s3_path=old_file_key)

def __upload_part(self, **kwargs):
response = self.client.upload_part(
Body=kwargs['chunk'],
Expand Down
10 changes: 10 additions & 0 deletions tests/syngenta_digital_dta/s3/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import uuid
import unittest
import warnings
from datetime import datetime

import boto3
import requests
Expand Down Expand Up @@ -151,3 +152,12 @@ def test_list_dir_files(self):
file = 'test/test-create.json'
result = self.adapter.list_dir_files(dir_name='test/')
self.assertIn(file, result)

def test_list_dir_files_with_date(self):
file = 'test/test-create.json'
result = self.adapter.list_dir_files(dir_name='test/', date=datetime(2000,1,1))
self.assertIn(file, result)

def test_rename_object(self):
old_file = 'test/test-create.json'
self.adapter.rename_object(old_file_name=old_file, new_file_name='test/test-new.json')

0 comments on commit 54aa08f

Please sign in to comment.