py_optional_ut.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include "ut3/py_test_engine.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. using namespace NPython;
  4. Y_UNIT_TEST_SUITE(FromPyNone) {
  5. Y_UNIT_TEST(FromPyNone) {
  6. TPythonTestEngine engine;
  7. engine.ToMiniKQL<NUdf::TOptional<ui32>>(
  8. "def Test(): return None",
  9. [](const NUdf::TUnboxedValuePod& value) {
  10. UNIT_ASSERT(!value);
  11. });
  12. }
  13. Y_UNIT_TEST(FromPyObject) {
  14. TPythonTestEngine engine;
  15. engine.ToMiniKQL<NUdf::TOptional<ui32>>(
  16. "def Test(): return 42",
  17. [](const NUdf::TUnboxedValuePod& value) {
  18. UNIT_ASSERT(value);
  19. UNIT_ASSERT_EQUAL(value.Get<ui32>(), 42);
  20. });
  21. }
  22. Y_UNIT_TEST(ToPyNone) {
  23. TPythonTestEngine engine;
  24. engine.ToPython<NUdf::TOptional<char*>>(
  25. [](const TType* type, const NUdf::IValueBuilder& vb) {
  26. Y_UNUSED(type); Y_UNUSED(vb);
  27. return NUdf::TUnboxedValuePod();
  28. },
  29. "def Test(value):\n"
  30. " assert value == None\n");
  31. }
  32. Y_UNIT_TEST(ToPyFilledOptional) {
  33. TPythonTestEngine engine;
  34. engine.ToPython<NUdf::TOptional<NUdf::TTuple<NUdf::TUtf8, bool>>>(
  35. [](const TType* type, const NUdf::IValueBuilder& vb) {
  36. const TOptionalType* optType =
  37. static_cast<const TOptionalType*>(type);
  38. NUdf::TUnboxedValue* items = nullptr;
  39. auto tuple = vb.NewArray(static_cast<const TTupleType*>(optType->GetItemType())->GetElementsCount(), items);
  40. items[0] = vb.NewString("test string");
  41. items[1] = NUdf::TUnboxedValuePod(false);
  42. return NUdf::TUnboxedValue(tuple);
  43. },
  44. "def Test(value):\n"
  45. " assert isinstance(value, tuple)\n"
  46. " assert len(value) == 2\n"
  47. " assert value == ('test string', False)\n");
  48. }
  49. }