"""Tests for s3 module.""" from unittest.mock import Mock from unittest.mock import patch from flows import s3 def test_split_url(): """Test split_url function.""" expected = {'bucket': 'foobar', 'key': 'this/is/the.path'} assert s3.split_url('s3://foobar/this/is/the.path') == expected assert s3.split_url('https://foo.com/foobar/this/is/the.path') == expected @patch('flows.s3.boto3') def test_get_object(boto3): """Test get_object function.""" mock_object = Mock() mock_s3 = Mock() mock_s3.Object.return_value = mock_object boto3.resource.return_value = mock_s3 result = s3.get_object('s3://some-bucket/object/path.json') assert result == mock_object mock_s3.Object.assert_called_with('some-bucket', 'object/path.json') @patch('flows.s3.get_object') def test_download_csv(get_object): """Test download_csv function.""" s3_path = 's3://bucket/path/filename.csv' get_object(s3_path).get()['Body'].read = Mock( return_value=b'Hello World,b\r1,11\n2,22\r\n3,33\n') rows = s3.download_csv(s3_path) assert rows == [['1', '11'], ['2', '22'], ['3', '33']]