void midgard_test_object_get_by_guid_created(MgdObjectTest *mot, gconstpointer data)
{
	g_assert(mot != NULL);

	MgdObject *_object = mot->object;
	MidgardConnection *mgd = mot->mgd;

	guint oid = 0;
	gchar *oguid = NULL;
	gchar *created = NULL;
	gchar *creator = NULL;
	g_object_get(_object,
			"id", &oid,
			"guid", &oguid,
			NULL);
	
	g_object_get(_object->metadata,
			"created", &created,
			"creator", &creator, NULL);

	MgdObject *object = midgard_test_object_basic_new(mgd, G_OBJECT_TYPE_NAME(_object), NULL);
	g_assert(object != NULL);
	gboolean fetched_by_guid =
		midgard_test_object_fetch_by_guid(object, oguid);
	g_assert(fetched_by_guid == TRUE);
	MIDGARD_TEST_ERROR_OK(mgd);
	midgard_test_metadata_check_create(object);
	g_object_unref(object);

	g_free(created);
	g_free(creator);
	g_free(oguid);
}
void midgard_test_object_get_by_id_updated(MgdObjectTest *mot, gconstpointer data)
{
	g_assert(mot != NULL);

	MgdObject *_object = mot->object;
	MidgardConnection *mgd = mot->mgd;

	guint oid = 0;
	gchar *oguid = NULL;
	gchar *revised = NULL;
	gchar *revisor = NULL;
	gchar *creator = NULL;
	gchar *created = NULL;
	g_object_get(_object,
			"id", &oid,
			"guid", &oguid,
			NULL);
	
	g_object_get(_object->metadata,
			"revised", &revised,
			"revisor", &revisor, 
			"created", &created, 
			"creator", &creator, 
			NULL);
	
	g_assert_cmpuint(oid, >, 0);

	/* midgard_object_get_by_id */
	MgdObject *object = midgard_test_object_basic_new(mgd, G_OBJECT_TYPE_NAME(_object), NULL);
	g_assert(object != NULL);
	gboolean fetched_by_id =
		midgard_test_object_fetch_by_id(object, oid);
	g_assert(fetched_by_id == TRUE);
	MIDGARD_TEST_ERROR_OK(mgd);
	midgard_test_metadata_check_update(object);
	midgard_test_metadata_check_datetime_properties(object, revised, "revised", NULL);
	midgard_test_metadata_check_datetime_properties(object, created, "created", NULL);
	midgard_test_metadata_check_person_references(object, revisor, "revisor", NULL);
	midgard_test_metadata_check_person_references(object, creator, "creator", NULL);
	g_object_unref(object);
	
	g_free(revised);
	g_free(revisor);
	g_free(created);
	g_free(creator);
	g_free(oguid);

}
/* DO NOT fail inside this function.
 * Main purpose is to get (or not) object intentionaly.
 * If it fails, return FALSE and fail with TRUE OR FALSE assertion 
 * depending on actual need */
gboolean midgard_test_object_fetch_by_id(MgdObject *object, guint id)
{
	g_assert(object != NULL);

	MidgardConnection *mgd = object->mgd->_mgd;
	gboolean fetched = midgard_object_get_by_id(object, id);

	if(fetched == FALSE) {

		MIDGARD_TEST_ERROR_ASSERT(mgd, MGD_ERR_NOT_EXISTS);
		g_assert(fetched == FALSE);

		return FALSE;
	}

	MIDGARD_TEST_ERROR_OK(mgd);
	g_assert(fetched == TRUE);

	return TRUE;
}
MidgardObject *midgard_test_object_basic_new(MidgardConnection *mgd, const gchar *name, GValue *value)
{
	MidgardObject *object = midgard_object_new(mgd, name, value);
	g_assert(object != NULL);
	MIDGARD_TEST_ERROR_OK(mgd);

	guint id = 0;
	gchar *guid = NULL;

	g_object_get(object, 
			"id", &id,
			"guid", &guid, NULL);

	if(value == NULL) {
	
		g_assert_cmpint(id, ==, 0);
		g_assert_cmpstr(guid, ==, NULL);
		g_assert_cmpint(id, !=, 1);
		g_assert_cmpstr(guid, !=, "");
	
	} else {
void midgard_test_object_constructor_id_created(MgdObjectTest *mot, gconstpointer data)
{
	g_assert(mot != NULL);

	MgdObject *_object = mot->object;
	MidgardConnection *mgd = mot->mgd;
	guint oid = 0;
	gchar *oguid = NULL;
	gchar *created = NULL;
	gchar *creator = NULL;
	g_object_get(_object,
			"id", &oid,
			"guid", &oguid,
			NULL);
	
	g_object_get(_object->metadata,
			"created", &created,
			"creator", &creator, NULL);
	
	g_assert_cmpuint(oid, >, 0);

	/* midgard_object_new, construct with id */
	GValue gval = {0, };
	g_value_init(&gval, G_TYPE_UINT);
	g_value_set_uint(&gval, oid);
	MgdObject *object = midgard_test_object_basic_new(mgd, G_OBJECT_TYPE_NAME(_object), &gval);
	g_assert(object != NULL);
	MIDGARD_TEST_ERROR_OK(mgd);
	midgard_test_metadata_check_create(object);
	midgard_test_metadata_check_datetime_properties(object, created, "created", "revised", NULL);
	midgard_test_metadata_check_person_references(object, creator, "creator", "revisor", NULL);
	g_object_unref(object);
	g_value_unset(&gval);

	g_free(created);
	g_free(creator);
	g_free(oguid);
}
MidgardConnection *midgard_test_connection_open_user_config(const gchar *name, MidgardConfig **config)
{
	g_assert(*config == NULL);
	MidgardConnection *mgd = midgard_connection_new();

	gboolean opened = FALSE;
	GError *err = NULL;

	*config = midgard_test_config_new_user_config(name);
	g_assert(*config != NULL);

	opened  = midgard_connection_open_config(mgd, *config, err);
	
	/* Fail, we didn't make connection */
	g_assert(opened == TRUE);

	/* Fail if we connected and error is set */
	g_assert(err == NULL);

	/* Fail if midgard error is not OK */
	MIDGARD_TEST_ERROR_OK(mgd);
	
	return mgd;
}
void midgard_test_object_tree_create(MidgardObjectTest *mot, gconstpointer data)
{
	g_assert(mot != NULL);
	MidgardObject *_object = MIDGARD_OBJECT(mot->object);
	MidgardConnection *mgd = MIDGARD_CONNECTION(midgard_object_get_connection(_object));
	MidgardObjectClass *klass = MIDGARD_OBJECT_GET_CLASS(_object);

	/* Get parent (in tree) class and check if parent has the same child declared */
	const gchar *pname = midgard_schema_object_tree_get_parent_name(_object);
	
	if(!pname)
		return;

	MidgardObjectClass *pklass = MIDGARD_OBJECT_GET_CLASS_BY_NAME(pname);
	g_assert(pklass != NULL);

	/* Check if class has unique property */
	const gchar *unique_name = midgard_reflector_object_get_property_unique (G_OBJECT_CLASS_NAME (klass));

	if (!unique_name)
		return;

	/* Check if class has 'name' property registered */
	GParamSpec *unique_name_pspec = g_object_class_find_property(G_OBJECT_CLASS(klass), unique_name);
	g_assert (unique_name_pspec != NULL);

	/* Empty name */
	MidgardObject *object = midgard_object_new(mgd, G_OBJECT_TYPE_NAME(_object), NULL);
	g_object_set(object, unique_name, "", NULL);

	/* Workaround */
	const gchar *parent_property = midgard_reflector_object_get_property_parent (G_OBJECT_CLASS_NAME (klass));
	
	if (parent_property)
		g_object_set(object, parent_property, 1, NULL);

	gboolean created = midgard_object_create(object);
	MIDGARD_TEST_ERROR_OK(mgd);
	g_assert(created != FALSE);

	gboolean purged = midgard_object_purge(object, TRUE);
	g_assert(purged != FALSE);
	g_object_unref(object);

	object = midgard_object_new(mgd, G_OBJECT_TYPE_NAME(_object), NULL);
	g_object_set(object, "name", "", NULL);

	/* Workaround */
	parent_property = midgard_reflector_object_get_property_parent (G_OBJECT_CLASS_NAME (klass));
	
	if (parent_property)
		g_object_set(object, parent_property, 1, NULL);

	created = midgard_object_create(object);
	MIDGARD_TEST_ERROR_OK(mgd);
	g_assert(created != FALSE);

	purged = midgard_object_purge(object, TRUE);
	g_assert(purged != FALSE);
	g_object_unref(object);

	/* Unique name */
	object = midgard_object_new(mgd, G_OBJECT_TYPE_NAME(_object), NULL);
	g_object_set(object, unique_name, "Unique", NULL);

	/* Workaround */
	parent_property = midgard_reflector_object_get_property_parent (G_OBJECT_CLASS_NAME (klass));
	
	if (parent_property)
		g_object_set(object, parent_property, 1, NULL);

	created = midgard_object_create(object);
	MIDGARD_TEST_ERROR_OK(mgd);
	g_assert(created != FALSE);

	MidgardObject *dupobject = midgard_object_new(mgd, G_OBJECT_TYPE_NAME(_object), NULL);
	g_object_set(dupobject, unique_name, "Unique", NULL);
	
	/* Workaround */
	parent_property = midgard_reflector_object_get_property_parent (G_OBJECT_CLASS_NAME (klass));
	
	if (parent_property)
		g_object_set(dupobject, parent_property, 1, NULL);

	gboolean dupcreated = midgard_object_create(dupobject);	
	MIDGARD_TEST_ERROR_ASSERT(mgd, MGD_ERR_DUPLICATE);
	g_assert(dupcreated != TRUE);	
	g_object_unref(dupobject);

	purged = midgard_object_purge(object, TRUE);
	g_assert(purged != FALSE);
	g_object_unref(object);
}