File: test_python_execute_func_date.cc

package info (click to toggle)
glom 1.30.4-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 41,260 kB
  • sloc: ansic: 160,257; cpp: 72,338; javascript: 9,331; sh: 4,971; xml: 476; makefile: 315; perl: 236
file content (128 lines) | stat: -rw-r--r-- 4,798 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//#include <Python.h> //Include it before anything else to avoid "_POSIX_C_SOURCE redefined".
//#if PY_VERSION_HEX >= 0x02040000
//# include <datetime.h> /* From Python */
//#endif


#include <glom/libglom/init.h>
#include <glom/python_embed/glom_python.h>
#include <libglom/data_structure/glomconversions.h>
#include <iostream>

void execute_func_with_date_return_value()
{
  const char* calculation = "import datetime;return datetime.date.today();";
  Glom::type_map_fields field_values;
  Glib::RefPtr<Gnome::Gda::Connection> connection;

  //Execute a python function:
  Glib::ustring error_message;
  const auto value = Glom::glom_evaluate_python_function_implementation(
    Glom::Field::glom_field_type::DATE, calculation, field_values,
    0 /* document */, "" /* table name */,
    std::shared_ptr<Glom::Field>(), Gnome::Gda::Value(), // primary key details. Not used in this test.
    connection,
    error_message);

  //std::cout << "type=" << g_type_name(value.get_value_type()) << std::endl;

  //Check that there was no python error:
  if(!error_message.empty()) {
    std::cerr << "Unexpected error message: " << error_message << "\n";
  }
  g_assert(error_message.empty());
  
  //Check that the return value is of the expected type:
  g_assert(value.get_value_type() == G_TYPE_DATE);

  //Check that the return value is of the expected value:
  Glib::Date date_current;
  date_current.set_time_current();
  const Glib::Date date_result= value.get_date();
  g_assert(date_current == date_result);

  //std::cout << "value=" << value.to_string() << std::endl;
}
                            
void execute_func_with_date_input_value()
{
  const char* calculation = "import datetime\n"
                            "return record[\"test_field\"].year";
  Glom::type_map_fields field_values;
  const auto input_date = Glib::Date(11, Glib::Date::MAY, 1973);
  field_values["test_field"] = Gnome::Gda::Value(input_date);
  Glib::RefPtr<Gnome::Gda::Connection> connection;

  //Execute a python function:
  Glib::ustring error_message;
  const auto value = Glom::glom_evaluate_python_function_implementation(
    Glom::Field::glom_field_type::NUMERIC, calculation, field_values,
    0 /* document */, "" /* table name */,
    std::shared_ptr<Glom::Field>(), Gnome::Gda::Value(), // primary key details. Not used in this test.
    connection,
    error_message);

  //std::cout << "type=" << g_type_name(value.get_value_type()) << std::endl;

  //Check that there was no python error:
  if(!error_message.empty()) {
    std::cerr << "Unexpected error message: " << error_message << "\n";
  }
  g_assert(error_message.empty());
  
  //Check that the return value is of the expected type:
  g_assert(value.get_value_type() == GDA_TYPE_NUMERIC);

  //Check that the return value is of the expected value:
  //std::cout << "GdaNumeric number=" << value.get_numeric()->number << std::endl;
  g_assert(value.get_numeric().get_double() == 1973);

  //std::cout << "value=" << value.to_string() << std::endl;
}

/* This would require the extra dateutil python module.
void execute_func_with_date_input_value_relativedelta()
{
  const char* calculation = "from dateutil.relativedelta import relativedelta\n"
                            "import datetime\n"
                            "today = datetime.date.today()\n"
                            "date_of_birth = record[\"test_field\"]\n"
                            "rd = relativedelta(today, date_of_birth)\n"
                            "return rd.year";
  Glom::type_map_fields field_values;
  const auto input_date = Glib::Date(11, Glib::Date::MAY, 1973);
  field_values["test_field"] = Gnome::Gda::Value(input_date);
  Glib::RefPtr<Gnome::Gda::Connection> connection;

  //Execute a python function:
  const auto value = Glom::glom_evaluate_python_function_implementation(
    Glom::Field::glom_field_type::NUMERIC, calculation, field_values,
    0, "",
    std::shared_ptr<Glom::Field>(), Gnome::Gda::Value(), // primary key details. Not used in this test.
    connection);

  //std::cout << "type=" << g_type_name(value.get_value_type()) << std::endl;

  //Check that the return value is of the expected type:
  g_assert(value.get_value_type() == GDA_TYPE_NUMERIC);

  //Check that the return value is of the expected value:
  g_assert(value.get_numeric());
  g_assert(value.get_numeric()->number);
  //std::cout << "GdaNumeric number=" << value.get_numeric()->number << std::endl;
  //g_assert(value.get_numeric()->number == std::string("1973"));

  std::cout << "value=" << value.to_string() << std::endl;
}
*/

int main()
{
  Glom::libglom_init(); //Also initializes python.

  execute_func_with_date_return_value();
  execute_func_with_date_input_value();
  //execute_func_with_date_input_value_relativedelta();

  return EXIT_SUCCESS;
}