예제 #1
0
static void
grl_bookmarks_source_init (GrlBookmarksSource *source)
{
  GError *error = NULL;
  gchar *path;
  gchar *db_path;
  GList *object_types;

  source->priv = GRL_BOOKMARKS_GET_PRIVATE (source);

  path = g_build_filename (g_get_user_data_dir (), "grilo-plugins", NULL);

  if (!g_file_test (path, G_FILE_TEST_IS_DIR)) {
    g_mkdir_with_parents (path, 0775);
  }

  GRL_DEBUG ("Opening database connection...");
  db_path = g_build_filename (path, GRL_SQL_DB, NULL);
  g_free (path);

  source->priv->adapter = gom_adapter_new ();
  if (!gom_adapter_open_sync (source->priv->adapter, db_path, &error)) {
    GRL_WARNING ("Could not open database '%s': %s", db_path, error->message);
    g_error_free (error);
    g_free (db_path);
    return;
  }
  g_free (db_path);

  source->priv->repository = gom_repository_new (source->priv->adapter);
  object_types = g_list_prepend(NULL, GINT_TO_POINTER(BOOKMARKS_TYPE_RESOURCE));
  gom_repository_automatic_migrate_async (source->priv->repository, 2, object_types, migrate_cb, source);
}
예제 #2
0
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);
}
static void
test_notnull (void)
{
  GomAdapter *adapter;
  GomRepository *repository;
  GList *object_types;
  ItemResource *item;

  GError *error = NULL;

  adapter = gom_adapter_new();
  gom_adapter_open_sync(adapter, ":memory:", &error);
  g_assert_no_error(error);

  repository = gom_repository_new(adapter);
  object_types = g_list_prepend(NULL, GINT_TO_POINTER(ITEM_TYPE_RESOURCE));
  gom_repository_automatic_migrate_sync(repository, 1, object_types, &error);
  g_assert_no_error(error);

  /* Insert an item without a name */
  item = g_object_new (ITEM_TYPE_RESOURCE, "repository", repository,
                       "email", "*****@*****.**",
                       NULL);
  gom_resource_save_sync(GOM_RESOURCE(item), &error);
  g_assert_error(error, GOM_ERROR, GOM_ERROR_COMMAND_SQLITE);
  g_assert (strstr (error->message, "NOT NULL") != NULL);
  g_assert (strstr (error->message, "items.name") != NULL);
  g_clear_error(&error);
  g_object_unref(item);

  /* Try inserting an item without an email */
  item = g_object_new (ITEM_TYPE_RESOURCE, "repository", repository,
                       "name", "gom",
                       NULL);
  gom_resource_save_sync(GOM_RESOURCE(item), &error);
  g_assert_error(error, GOM_ERROR, GOM_ERROR_COMMAND_SQLITE);
  g_assert (strstr (error->message, "NOT NULL") != NULL);
  g_assert (strstr (error->message, "items.email") != NULL);
  g_clear_error(&error);
  g_object_unref(item);

  gom_adapter_close_sync(adapter, &error);
  g_assert_no_error(error);

  g_object_unref(repository);
  g_object_unref(adapter);
}
static void
test_unique (void)
{
  GomAdapter *adapter;
  GomRepository *repository;
  GList *object_types;
  ItemResource *item;

  GomFilter *filter;
  GValue value = { 0, };
  guint id;
  gchar *name, *email;

  GError *error = NULL;

  adapter = gom_adapter_new();
  gom_adapter_open_sync(adapter, ":memory:", &error);
  g_assert_no_error(error);

  repository = gom_repository_new(adapter);
  object_types = g_list_prepend(NULL, GINT_TO_POINTER(ITEM_TYPE_RESOURCE));
  gom_repository_automatic_migrate_sync(repository, 1, object_types, &error);
  g_assert_no_error(error);

  /* Insert an item */
  item = g_object_new (ITEM_TYPE_RESOURCE, "repository", repository,
                       "name", "gom", "email", "*****@*****.**",
                       NULL);
  gom_resource_save_sync(GOM_RESOURCE(item), &error);
  g_assert_no_error(error);
  g_object_get(item, "id", &id, NULL);
  g_object_unref(item);

  /* Fetch it, to make sure it was saved correctly */
  g_value_init(&value, G_TYPE_UINT);
  g_value_set_uint(&value, id);
  filter = gom_filter_new_eq(ITEM_TYPE_RESOURCE, "id", &value);
  g_value_unset(&value);

  item = ITEM_RESOURCE (gom_repository_find_one_sync(repository,
                                                     ITEM_TYPE_RESOURCE,
                                                     filter,
                                                     &error));
  g_assert_no_error(error);
  g_assert(item);
  g_object_unref(filter);

  g_object_get(item, "name", &name, "email", &email, NULL);
  g_assert_cmpstr(name, ==, "gom");
  g_free(name);
  g_assert_cmpstr(email, ==, "*****@*****.**");
  g_free(email);
  g_object_unref(item);

  /* Now try inserting a new item with the same name (non UNIQUE column) */
  item = g_object_new (ITEM_TYPE_RESOURCE, "repository", repository,
                       "name", "gom", "email", "*****@*****.**",
                       NULL);
  gom_resource_save_sync(GOM_RESOURCE(item), &error);
  g_assert_no_error(error);
  g_object_get(item, "id", &id, NULL);
  g_object_unref(item);

  /* Fetch it, to make sure it was saved correctly */
  g_value_init(&value, G_TYPE_UINT);
  g_value_set_uint(&value, id);
  filter = gom_filter_new_eq(ITEM_TYPE_RESOURCE, "id", &value);
  g_value_unset(&value);

  item = ITEM_RESOURCE (gom_repository_find_one_sync(repository,
                                                     ITEM_TYPE_RESOURCE,
                                                     filter,
                                                     &error));
  g_assert_no_error(error);
  g_assert(item);
  g_object_unref(filter);

  g_object_get(item, "name", &name, "email", &email, NULL);
  g_assert_cmpstr(name, ==, "gom");
  g_free(name);
  g_assert_cmpstr(email, ==, "*****@*****.**");
  g_free(email);
  g_object_unref(item);

  /* And finally try inserting an item with the same email (UNIQUE column) */
  item = g_object_new (ITEM_TYPE_RESOURCE, "repository", repository,
                       "name", "gom2", "email", "*****@*****.**",
                       NULL);
  gom_resource_save_sync(GOM_RESOURCE(item), &error);
  g_assert_error(error, GOM_ERROR, GOM_ERROR_COMMAND_SQLITE);
  g_assert (strstr (error->message, "UNIQUE") != NULL);
  g_assert (strstr (error->message, "items.email") != NULL);
  g_object_unref(item);
  g_clear_error(&error);

  gom_adapter_close_sync(adapter, &error);
  g_assert_no_error(error);

  g_object_unref(repository);
  g_object_unref(adapter);
}