File: test_document_load.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 (409 lines) | stat: -rw-r--r-- 14,340 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/* Glom
 *
 * Copyright (C) 2010 Openismus GmbH
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA.
 */

#include "tests/test_utils.h"
#include <libglom/document/document.h>
#include <libglom/init.h>
#include <libglom/db_utils.h>
#include <giomm/file.h>
#include <glibmm/convert.h>
#include <glibmm/miscutils.h>

#include <iostream>

template<typename T_Container, typename T_Value>
bool contains(const T_Container& container, const T_Value& name)
{
  typename T_Container::const_iterator iter =
    std::find(container.begin(), container.end(), name);
  return iter != container.end();
}

template<typename T_Container>
bool contains_named(const T_Container& container, const Glib::ustring& name)
{
  typename T_Container::const_iterator iter =
    Glom::find_if_same_name(container, name);
  return iter != container.end();
}

template<typename T_Container>
bool contains_value(const T_Container& container, const Glib::ustring& name)
{
  for(const auto& item : container)
  {
    if(item->get_value() == Gnome::Gda::Value(name))
      return true;
  }

  return false;
}


static bool get_group_named(const Glom::Document::type_list_groups& container, const Glib::ustring& name, Glom::GroupInfo& group_info)
{
  Glom::Document::type_list_groups::const_iterator iter =
    std::find_if(container.begin(), container.end(),
      [&name] (const Glom::GroupInfo& info)
      {
        return info.get_name() == name;
      }
    );
  if(iter != container.end())
  {
    group_info = *iter;
    return true;
  }
  
  group_info = Glom::GroupInfo();
  return false;
}

static bool needs_navigation(Glom::Document& document, const Glib::ustring& table_name, const Glib::ustring& field_name)
{
  std::shared_ptr<Glom::LayoutItem_Field> layout_item = std::make_shared<Glom::LayoutItem_Field>();
  layout_item->set_name(field_name);
  layout_item->set_full_field_details(
    document.get_field(table_name, field_name));

  std::shared_ptr<Glom::Relationship> field_used_in_relationship_to_one;
  return Glom::DbUtils::layout_field_should_have_navigation(table_name, 
    layout_item, &document, field_used_in_relationship_to_one);
}

static std::shared_ptr<const Glom::LayoutItem_Portal> get_portal_from_details_layout(const Glom::Document& document, const Glib::ustring& table_name, const Glib::ustring& relationship_name)
{
  const auto groups = 
    document.get_data_layout_groups("details", table_name);
  if(groups.empty())
  {
    std::cerr << G_STRFUNC << ": groups is empty." << std::endl;
  }
  
  for(const auto& group : groups)
  {
    for(const auto& layout_item : group->get_items_recursive_with_groups())
    { 
      const std::shared_ptr<const Glom::LayoutGroup> child_group =
        std::dynamic_pointer_cast<const Glom::LayoutGroup>(layout_item);
      if(!child_group)
        continue;

      const std::shared_ptr<const Glom::LayoutItem_Portal> portal =
        std::dynamic_pointer_cast<const Glom::LayoutItem_Portal>(layout_item);
      if(!portal)
        continue;

      if(portal->get_relationship_name() == relationship_name)
        return portal;
    }
  }
      
  return std::shared_ptr<Glom::LayoutItem_Portal>();
}
  
 
int main()
{
  Glom::libglom_init();

  // Get a URI for a test file:
  Glib::ustring uri;

  try
  {
    const std::string path =
       Glib::build_filename(GLOM_DOCDIR_EXAMPLES_NOTINSTALLED,
         "example_film_manager.glom");
    uri = Glib::filename_to_uri(path);
  }
  catch(const Glib::ConvertError& ex)
  {
    std::cerr << G_STRFUNC << ": " << ex.what();
    return EXIT_FAILURE;
  }

  //std::cout << "URI=" << uri << std::endl;


  // Load the document:
  Glom::Document document;
  document.set_file_uri(uri);
  int failure_code = 0;
  const auto test = document.load(failure_code);
  //std::cout << "Document load result=" << test << std::endl;

  if(!test)
  {
    std::cerr << G_STRFUNC << ": Document::load() failed with failure_code=" << failure_code << std::endl;
    return EXIT_FAILURE;
  }

  //Test some known details:
  g_assert(document.get_is_example_file());
  g_assert(document.get_database_title_original() == "Openismus Film Manager");

  const auto table_names = document.get_table_names();
  g_assert(contains(table_names, "accommodation"));
  g_assert(contains(table_names, "cars"));
  g_assert(contains(table_names, "characters"));
  g_assert(contains(table_names, "companies"));
  g_assert(contains(table_names, "contacts"));
  g_assert(contains(table_names, "locations"));
  g_assert(contains(table_names, "scenes"));
  g_assert(!contains(table_names, "Scenes")); //The title, not the name.

  std::shared_ptr<Glom::TableInfo> table = document.get_table("scenes");
  g_assert(table);
  g_assert( table->get_title_original() == "Scenes" );
  g_assert( table->get_title_singular_original() == "Scene" );

  //Test known fields of one table:
  const auto fields = document.get_table_fields("scenes");
  g_assert(contains_named(fields, "scene_id"));
  g_assert(contains_named(fields, "comments"));
  g_assert(contains_named(fields, "description"));
  g_assert(contains_named(fields, "date"));
  g_assert(!contains_named(fields, "nosuchfield"));

  const auto relationships = document.get_relationships("scenes");
  g_assert(contains_named(relationships, "location"));
  g_assert(contains_named(relationships, "scene_crew"));
  g_assert(contains_named(relationships, "scene_cast"));

  //Check some fields:
  std::shared_ptr<const Glom::Field> field = document.get_field("contacts", "contact_id");
  g_assert(field);
  g_assert( field->get_title_original() == "Contact ID" );
  g_assert(field->get_glom_type() == Glom::Field::glom_field_type::NUMERIC);
  g_assert(field->get_auto_increment());
  field = document.get_field("locations", "rent");
  g_assert(field);
  g_assert( field->get_title_original() == "Rent" );
  g_assert(field->get_glom_type() == Glom::Field::glom_field_type::NUMERIC);
  g_assert(!field->get_auto_increment());
  g_assert(!field->get_unique_key());

  //Check a relationship:
  const std::shared_ptr<const Glom::Relationship> relationship = document.get_relationship("characters", "contacts_actor");
  g_assert(relationship);
  g_assert(relationship->get_from_field() == "contact_id");
  g_assert(relationship->get_to_table() == "contacts");
  g_assert(relationship->get_to_field() == "contact_id");


  //Check a layout:
  const Glom::Document::type_list_layout_groups groups = 
    document.get_data_layout_groups("details", "scenes");
  g_assert(groups.size() == 3);
  const std::shared_ptr<const Glom::LayoutGroup> group =
    groups[1];
  const Glom::LayoutGroup::type_list_const_items items = 
    group->get_items_recursive();
  //std::cout << "size: " << items.size() << std::endl;
  g_assert(items.size() == 13);
  const Glom::LayoutGroup::type_list_const_items items_with_groups = 
    group->get_items_recursive_with_groups();
  //std::cout << "size: " << items_with_groups.size() << std::endl;
  g_assert(items_with_groups.size() == 15);

  //Check that expected fields can be found on a layout.
  std::shared_ptr<const Glom::LayoutItem_Field> field_on_layout = 
    get_field_on_layout(document, "scenes", "locations", "address_town");
  g_assert(field_on_layout);
  g_assert(field_on_layout->get_table_used("scenes") == "locations");
  g_assert(field_on_layout->get_name() == "address_town");

  
  //Check Field Formatting:
  field = document.get_field("contacts", "name_title");  
  g_assert(field);
  g_assert(field->get_glom_type() == Glom::Field::glom_field_type::TEXT);
  const Glom::Formatting& formatting = field->m_default_formatting;
  g_assert(formatting.get_horizontal_alignment() == Glom::Formatting::HorizontalAlignment::AUTO);
  
  g_assert(formatting.get_has_choices());
  g_assert(formatting.get_has_custom_choices());
  g_assert(!formatting.get_has_related_choices());
  Glom::Formatting::type_list_values choices = formatting.get_choices_custom();
  g_assert(!choices.empty());
  g_assert(contains_value(choices, "Mr"));
  g_assert(contains_value(choices, "Mrs"));
  
  //Check that the default formatting is used on the layout:
  field_on_layout = 
    get_field_on_layout(document, "contacts", "contacts", "name_title");
  g_assert(field_on_layout);
  g_assert(field_on_layout->get_table_used("contacts") == "contacts");
  g_assert(field_on_layout->get_name() == "name_title");
  g_assert(field_on_layout->get_formatting_use_default());
  g_assert(field_on_layout->get_formatting_used() == formatting);

  //Test this utility method:
  g_assert( document.get_data_layout_groups_have_any_fields("list", "cars") );


  //Test library modules:
  const auto module_names = document.get_library_module_names();
  if(!module_names.empty()) //TODO: Test a document that actually has some?
  {
    std::cerr << G_STRFUNC << ": Failure: Unexpected library module names." << std::endl;
    return false;
  }


  //Test print layouts:  
  const std::vector<Glib::ustring> print_layout_names = 
    document.get_print_layout_names("contacts");
  if(print_layout_names.size() != 1)
  {
    std::cerr << G_STRFUNC << ": Failure: Unexpected number of print layouts." << std::endl;
    return false;
  }

  if(!contains(print_layout_names, "contact_details"))
  {
    std::cerr << G_STRFUNC << ": Failure: Could not find the expected print layout name." << std::endl;
    return false;
  }
  
  const std::shared_ptr<const Glom::PrintLayout> print_layout = document.get_print_layout("contacts", "contact_details");
  if(!print_layout)
  {
    std::cerr << G_STRFUNC << ": Failure: Could not get an expected print layout." << std::endl;
    return false;
  }
  
  if(print_layout->get_title_original() != "Contact Details")
  {
    std::cerr << G_STRFUNC << ": Failure: Unexpected print layout title." << std::endl;
    return false;
  }
  
  if(!print_layout->get_layout_group())
  {
    std::cerr << G_STRFUNC << ": Failure: The print layout has no layout group." << std::endl;
    return false;
  }


  const std::vector<Glib::ustring> report_names = 
    document.get_report_names("contacts");
  if(report_names.size() != 2)
  {
    std::cerr << G_STRFUNC << ": Failure: Unexpected number of reports." << std::endl;
    return false;
  }

  if(!contains(report_names, "by_country"))
  {
    std::cerr << G_STRFUNC << ": Failure: Could not find the expected report name." << std::endl;
    return false;
  }

  const std::shared_ptr<const Glom::Report> report = document.get_report("contacts", "by_country_by_town");
  if(!report)
  {
    std::cerr << G_STRFUNC << ": Failure: Could not get an expected report." << std::endl;
    return false;
  }
  
  if(report->get_title_original() != "By Country, By Town")
  {
    std::cerr << G_STRFUNC << ": Failure: Unexpected report title." << std::endl;
    return false;
  }
  
  if(!report->get_layout_group())
  {
    std::cerr << G_STRFUNC << ": Failure: The report has no layout group." << std::endl;
    return false;
  }

  
  //Test user groups:
  Glom::Document::type_list_groups user_groups = document.get_groups();
  Glom::GroupInfo group_info_ignored;
  g_assert(get_group_named(user_groups, "glom_developer", group_info_ignored));

  Glom::GroupInfo group_info_accounts;
  g_assert(get_group_named(user_groups, "props_department", group_info_accounts));
  Glom::GroupInfo::type_map_table_privileges::const_iterator iterFind =
    group_info_accounts.m_map_privileges.find("scenes");
  const bool privileges_found = (iterFind != group_info_accounts.m_map_privileges.end());
  g_assert(privileges_found);
  const Glom::Privileges privs = iterFind->second;
  g_assert(privs.m_view == true);
  g_assert(privs.m_edit == true);
  g_assert(privs.m_create == false);
  g_assert(privs.m_delete == false);

  //Test navigation:
  if(!needs_navigation(document, "scenes", "location_id"))
  {
    std::cerr << G_STRFUNC << ": Failure: DbUtils::layout_field_should_have_navigation() did not return the expected result." << std::endl;
    return false;
  }

  if(needs_navigation(document, "scenes", "description"))
  {
    std::cerr << G_STRFUNC << ": Failure: DbUtils::layout_field_should_have_navigation() did not return the expected result." << std::endl;
    return false;
  }


  //Test portal navigation.
  //Note that related records portals don't have names.
  //This example portal shows the scenes_cast table, but should navigate though that to the cast table.
  const Glib::ustring portal_relationship_name = "scene_cast";
  std::shared_ptr<const Glom::LayoutItem_Portal> portal =
    get_portal_from_details_layout(document, "scenes", portal_relationship_name);
  if(!portal)
  {
    std::cerr << G_STRFUNC << ": Failure: Could not get the portal from the layout." << std::endl;
    return false;
  }

  Glib::ustring navigation_table_name;
  std::shared_ptr<const Glom::UsesRelationship> navigation_relationship;
  portal->get_suitable_table_to_view_details(navigation_table_name, navigation_relationship, &document);

  if(navigation_table_name != "characters")
  {
    std::cerr << G_STRFUNC << ": Failure: get_suitable_table_to_view_details() returned an unexpected table name: " << navigation_table_name << std::endl;
    return false;
  }

  if(!navigation_relationship)
  {
    std::cerr << G_STRFUNC << ": Failure: get_suitable_table_to_view_details() returned an empty navigation_relationship." << std::endl;
    return false;
  }

  if(navigation_relationship->get_relationship_name() != "cast")
  {
    std::cerr << G_STRFUNC << ": Failure: get_suitable_table_to_view_details() returned an unexpected navigation_relationship name: " << navigation_relationship->get_relationship_name() << std::endl;
    return false;
  }

  Glom::libglom_deinit();

  return EXIT_SUCCESS;
}