Esempio n. 1
0
    def test_response_is_ok___file_is_created_with_correct_content(self):
        client = OasisAPIClient('http://localhost:8001')
        client.DOWNLOAD_CHUCK_SIZE_IN_BYTES = 10

        expected_content = ''.join(
            choice(string.ascii_letters)
            for i in range(2 * client.DOWNLOAD_CHUCK_SIZE_IN_BYTES)).encode()

        with NamedTemporaryFile('w') as f:
            local_filename = f.name

        with responses.RequestsMock() as rsps:
            rsps.add(responses.GET,
                     'http://localhost:8001/foo',
                     status=200,
                     body=expected_content)

            client.download_resource('foo', local_filename)

            self.assertTrue(os.path.exists(local_filename))

            with io.open(local_filename, 'rb') as f:
                dld_content = f.read()

            os.remove(local_filename)
            self.assertEqual(expected_content, dld_content)
Esempio n. 2
0
    def test_local_file_already_exists___exception_is_raised_and_file_is_unchanged(self):
        client = OasisAPIClient('http://localhost:8001')

        with NamedTemporaryFile('w+') as f:
            f.write('foobarboo')
            f.flush()

            with self.assertRaises(OasisException):
                client.download_resource('foo', f.name)

            f.seek(0)
            self.assertEqual('foobarboo', f.read())
Esempio n. 3
0
    def test_response_is_not_ok___exception_is_raised_and_file_is_not_created(self):
        client = OasisAPIClient('http://localhost:8001')

        with NamedTemporaryFile('w') as f:
            local_filename = f.name

        with responses.RequestsMock() as rsps:
            rsps.add(responses.GET, 'http://localhost:8001/foo', status=400)

            with self.assertRaises(OasisException):
                client.download_resource('foo', local_filename)

            self.assertFalse(os.path.exists(local_filename))
Esempio n. 4
0
    def test_download_resource_is_called_with_the_correct_parameters(self):
        client = OasisAPIClient('http://localhost:8001')
        client.download_resource = Mock()

        client.download_exposure('foo', 'local_filename')

        client.download_resource.assert_called_once_with('/exposure/foo', 'local_filename')