File: test-gom-repository.c

package info (click to toggle)
libgom 0.5.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 784 kB
  • sloc: ansic: 9,785; python: 75; javascript: 50; sh: 33; makefile: 9
file content (162 lines) | stat: -rw-r--r-- 3,927 bytes parent folder | download | duplicates (5)
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
#include <gom/gom.h>

static GMainLoop *gMainLoop;

static gboolean
do_migrate (GomRepository  *repository,
            GomAdapter     *adapter,
            guint           version,
            gpointer        user_data,
            GError        **error)
{
#define EXEC_OR_FAIL(sql) \
   G_STMT_START { \
      GomCommand *c = g_object_new(GOM_TYPE_COMMAND, \
                                   "adapter", adapter, \
                                   "sql", (sql), \
                                   NULL); \
      if (!gom_command_execute(c, NULL, error)) { \
         g_object_unref(c); \
         goto failure; \
      } \
      g_object_unref(c); \
   } G_STMT_END

   if (version == 1) {
      EXEC_OR_FAIL("CREATE TABLE messages ("
                   " id INTEGER PRIMARY KEY,"
                   " sender TEXT,"
                   " recipient TEXT,"
                   " body TEXT"
                   ");");
      return TRUE;
   }

   if (version == 2) {
     EXEC_OR_FAIL("ALTER TABLE messages ADD COLUMN headers TEXT");
     return TRUE;
   }

failure:
   return FALSE;
}

static void
close_cb (GObject      *object,
	  GAsyncResult *result,
	  gpointer      user_data)
{
   GomAdapter *adapter = (GomAdapter *)object;
   gboolean *success = user_data;
   gboolean ret;
   GError *error = NULL;

   ret = gom_adapter_close_finish(adapter, result, &error);
   g_assert_no_error(error);
   g_assert(ret);

   *success = TRUE;
   g_main_loop_quit(gMainLoop);
}

static void
migrate_cb (GObject      *object,
            GAsyncResult *result,
            gpointer      user_data)
{
   GomRepository *repository = (GomRepository *)object;
   GomAdapter *adapter;
   gboolean ret;
   GError *error = NULL;

   ret = gom_repository_migrate_finish(repository, result, &error);
   g_assert_no_error(error);
   g_assert(ret);

   adapter = gom_repository_get_adapter(repository);
   gom_adapter_close_async(adapter, close_cb, user_data);
}

static void
open_cb (GObject      *object,
         GAsyncResult *result,
         gpointer      user_data)
{
   GomRepository *repository;
   GomAdapter *adapter = (GomAdapter *)object;
   gboolean ret;
   GError *error = NULL;

   ret = gom_adapter_open_finish(adapter, result, &error);
   g_assert_no_error(error);
   g_assert(ret);

   repository = gom_repository_new(adapter);
   gom_repository_migrate_async(repository, 2, do_migrate, NULL, migrate_cb, user_data);
   g_object_unref(repository);
}

static void
migrate (void)
{
   GomAdapter *adapter;
   gboolean success = FALSE;

   adapter = gom_adapter_new();
   gom_adapter_open_async(adapter, ":memory:", open_cb, &success);
   g_object_unref(adapter);

   g_main_loop_run(gMainLoop);

   g_assert(success);
}

static void
test_repo_finalize (void)
{
  GomAdapter *adapter;
  GomRepository *repository;
  gboolean ret;
  GError *error = NULL;

  /* Unref repo, then close and unref adapter */
  adapter = gom_adapter_new();
  ret = gom_adapter_open_sync (adapter, ":memory:", &error);
  g_assert_no_error (error);
  g_assert (ret);

  repository = gom_repository_new (adapter);

  g_clear_object (&repository);
  ret = gom_adapter_close_sync (adapter, &error);
  g_assert_no_error (error);
  g_assert (ret);

  g_clear_object (&adapter);

  /* Close and unref adapter, then unref repo */
  adapter = gom_adapter_new();
  ret = gom_adapter_open_sync (adapter, ":memory:", &error);
  g_assert_no_error (error);
  g_assert (ret);

  repository = gom_repository_new (adapter);

  ret = gom_adapter_close_sync (adapter, &error);
  g_assert_no_error (error);
  g_assert (ret);
  g_clear_object (&adapter);

  g_clear_object (&repository);
}

gint
main (gint   argc,
	  gchar *argv[])
{
   g_test_init(&argc, &argv, NULL);
   g_test_add_func("/GomRepository/migrate", migrate);
   g_test_add_func ("/GomRepository/test-repo-finalize", test_repo_finalize);
   gMainLoop = g_main_loop_new(NULL, FALSE);
   return g_test_run();
}