blob: 063d76245ca103e07fff8101dedf5ef8e8e44cc2 [file] [log] [blame]
# coding=utf-8
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import sys
from os import path
# Add utilites module to the pythonpath.
sys.path.append(path.dirname(path.dirname(path.dirname(path.abspath(__file__)))))
import unittest
from mock import *
import sys
import plpy_mock as plpy
m4_changequote(`<!', `!>')
class ValidateArgsTestCase(unittest.TestCase):
def setUp(self):
patches = {
'plpy': plpy
}
self.plpy_mock_execute = MagicMock()
plpy.execute = self.plpy_mock_execute
self.module_patcher = patch.dict('sys.modules', patches)
self.module_patcher.start()
import validate_args
self.subject = validate_args
def tearDown(self):
self.module_patcher.stop()
def test_input_tbl_valid_null_tbl_raises_exception(self):
with self.assertRaises(plpy.PLPYException):
self.subject.input_tbl_valid(None, "unittest_module")
def test_input_tbl_valid_whitespaces_tbl_raises(self):
with self.assertRaises(plpy.PLPYException):
self.subject.input_tbl_valid(" ", "unittest_module")
def test_input_tbl_valid_table_not_exists_raises(self):
self.subject.table_exists = Mock(return_value=False)
with self.assertRaises(plpy.PLPYException):
self.subject.input_tbl_valid("foo", "unittest_module")
def test_input_tbl_valid_table_tbl_empty_raises(self):
self.subject.table_exists = Mock(return_value=True)
self.subject.table_is_empty = Mock(return_value=True)
with self.assertRaises(plpy.PLPYException):
self.subject.input_tbl_valid("foo", "unittest_module")
def test_input_tbl_valid_table_tbl_empty_passes(self):
self.subject.table_exists = Mock(return_value=True)
self.subject.table_is_empty = Mock(return_value=True)
self.subject.input_tbl_valid("foo", "unittest_module",
check_empty=False)
def test_input_tbl_valid_table_passes(self):
self.subject.table_exists = Mock(return_value=True)
self.subject.table_is_empty = Mock(return_value=False)
self.subject.input_tbl_valid("foo", "unittest_module")
def test_output_tbl_valid_null_tbl_raises_exception(self):
with self.assertRaises(plpy.PLPYException):
self.subject.output_tbl_valid(None, "unittest_module")
def test_output_tbl_valid_whitespaces_tbl_raises_exception(self):
with self.assertRaises(plpy.PLPYException):
self.subject.output_tbl_valid(" ", "unittest_module")
def test_output_tbl_valid_null_raises(self):
with self.assertRaises(plpy.PLPYException):
self.subject.output_tbl_valid(" null ", "unittest_module")
def test_output_tbl_valid_tbl_exists_raises(self):
self.subject.table_exists = Mock(return_value=True)
with self.assertRaises(plpy.PLPYException):
self.subject.output_tbl_valid(" foo ", "unittest_module")
def test_output_tbl_valid_table_passes(self):
self.subject.table_exists = Mock(return_value=False)
self.subject.output_tbl_valid("foo", "unittest_module")
def test_input_tbl_valid_table_not_exists_raises_custom_error(self):
self.subject.table_exists = Mock(return_value=False)
with self.assertRaises(plpy.PLPYException) as error:
self.subject.input_tbl_valid("foo", "unittest_module",
error_suffix_str="custom exception")
self.assertIn('custom exception', str(error.exception))
def test_input_tbl_valid_table_not_exists_raises_custom_error_none(self):
self.subject.table_exists = Mock(return_value=False)
with self.assertRaises(plpy.PLPYException) as error:
self.subject.input_tbl_valid("foo", "unittest_module")
self.assertNotIn('custom exception', str(error.exception))
def test_get_algorithm_name(self):
self.assertEqual('abc', self.subject.get_algorithm_name(
'abc', 'aaa', ['aaa','abc','bcd'], 'qwerty'))
self.assertEqual('aaa', self.subject.get_algorithm_name(
'aaa', 'aaa', ['aaa','abc','bcd'], 'qwerty'))
self.assertEqual('aaa', self.subject.get_algorithm_name(
'aa', 'aaa', ['aaa','abc','bcd'], 'qwerty'))
self.assertEqual('bcd', self.subject.get_algorithm_name(
'bc', 'aaa', ['aaa','abc','bcd'], 'qwerty'))
# If two options satisfy the given selection,
# pick the first one from the list
self.assertEqual('aaa', self.subject.get_algorithm_name(
'a', 'abc', ['aaa','abc','bcd'], 'qwerty'))
self.assertEqual('aqq', self.subject.get_algorithm_name(
'a', 'abc', ['aqq','abc','bcd'], 'qwerty'))
if __name__ == '__main__':
unittest.main()