def value(rhs: RightHandSideValueType) -> RightHandSideValue: if isinstance(rhs, RightHandSideValue): return rhs if Null.accepts_rhs(rhs): return Null() if String.accepts_rhs(rhs): return String(rhs) if Number.accepts_rhs(rhs): return Number(rhs) if DateTime.accepts_rhs(rhs): return DateTime(rhs) if Boolean.accepts_rhs(rhs): return Boolean(rhs) if Collection.accepts_rhs(rhs): return Collection(rhs) if Mapping.accepts_rhs(rhs): return Mapping(rhs) raise TypeError('Right-hand side value type is not supported: {}'.format( type(rhs).__name__))
def test_datetime_comparison_does_not_accept_null_lhs(): value = DateTime('2020-03-05T15:45:29+03:00') for function in [ DateTime.less_than, DateTime.less_than_or_equal, DateTime.greater_than, DateTime.greater_than_or_equal ]: with pytest.raises(LeftHandSideTypeError) as e: function(value, None) assert ( '[DateTime.{}] Could not apply left-hand side <NoneType> (None)'.format(function.__name__) in str(e.value) )
def test_datetime_does_not_accept_invalid_lhs(lhs, rhs): value = DateTime(rhs) for function in [ DateTime.equal, DateTime.match, DateTime.less_than, DateTime.less_than_or_equal, DateTime.greater_than, DateTime.greater_than_or_equal ]: with pytest.raises(LeftHandSideTypeError) as e: function(value, lhs) assert ( '[DateTime.{}] Could not apply left-hand side <{}>'.format(function.__name__, type(lhs).__name__) in str(e.value) )
def test_datetime_lhs_should_not_be_greater_than_or_equal_to_datetime_rhs(lhs, rhs): assert DateTime(rhs).greater_than_or_equal(lhs) is False
def test_datetime_does_not_accept_invalid_rhs(rhs): with pytest.raises(RightHandSideTypeError) as e: DateTime(rhs) assert '[DateTime] Invalid right-hand side <{}>'.format(type(rhs).__name__) in str(e.value)
def test_datetime_lhs_should_be_greater_than_datetime_rhs(lhs, rhs): assert DateTime(rhs).greater_than(lhs) is True
def test_datetime_lhs_should_be_less_than_or_equal_to_datetime_rhs(lhs, rhs): assert DateTime(rhs).less_than_or_equal(lhs) is True
def test_datetime_lhs_should_not_be_less_than_datetime_rhs(lhs, rhs): assert DateTime(rhs).less_than(lhs) is False
def test_datetime_rhs_should_not_equal_nor_match_date_lhs_if_date_component_is_different(lhs, rhs): assert DateTime(rhs).match(lhs) is False assert DateTime(rhs).equal(lhs) is False
def test_date_rhs_should_equal_and_match_date_lhs_if_date_component_is_the_same(lhs, rhs): assert DateTime(rhs).equal(lhs) is True assert DateTime(rhs).match(lhs) is True
def test_date_rhs_should_not_equal_nor_match_different_date_lhs(lhs, rhs): assert DateTime(rhs).equal(lhs) is False assert DateTime(rhs).match(lhs) is False
def test_datetime_rhs_is_not_equal_nor_match_none_lhs(rhs): assert DateTime(rhs).equal(None) is False assert DateTime(rhs).match(None) is False
def test_date_rhs_should_equal_and_match_exact_date_lhs(lhs, rhs): assert DateTime(rhs).equal(lhs) is True assert DateTime(rhs).match(lhs) is True
def test_datetime_rhs_should_equal_and_match_datetime_lhs_ignoring_microseconds(lhs, rhs): assert DateTime(rhs).equal(lhs) is True assert DateTime(rhs).match(lhs) is True
def test_datetime_rhs_should_equal_and_match_exact_datetime_lhs_computing_timezone_offsets(lhs, rhs): assert DateTime(rhs).equal(lhs) is True assert DateTime(rhs).match(lhs) is True