From 8d9d51a7f776dc4c42d40c0b4368c6382203fd95 Mon Sep 17 00:00:00 2001 From: John Ralls Date: Tue, 15 Oct 2013 17:04:37 -0700 Subject: [PATCH] Add Account property "lot-next-id" Replaces direct kvp access. --- src/engine/Account.c | 26 +++++ src/engine/gnc-lot.c | 12 +-- src/engine/test/Makefile.am | 5 +- src/engine/test/test-engine-kvp-properties.c | 107 +++++++++++++++++++ src/engine/test/test-engine.c | 2 + 5 files changed, 144 insertions(+), 8 deletions(-) create mode 100644 src/engine/test/test-engine-kvp-properties.c diff --git a/src/engine/Account.c b/src/engine/Account.c index b1b0a7461a..21333eba4d 100644 --- a/src/engine/Account.c +++ b/src/engine/Account.c @@ -85,6 +85,8 @@ enum PROP_PLACEHOLDER, PROP_FILTER, PROP_SORT_ORDER, + + PROP_LOT_NEXT_ID, }; #define GET_PRIVATE(o) \ @@ -283,6 +285,8 @@ gnc_account_get_property (GObject *object, { Account *account; AccountPrivate *priv; + const gchar *key; + GValue *temp; g_return_if_fail(GNC_IS_ACCOUNT(object)); @@ -378,6 +382,12 @@ gnc_account_get_property (GObject *object, case PROP_SORT_ORDER: g_value_set_string(value, xaccAccountGetSortOrder(account)); break; + case PROP_LOT_NEXT_ID: + key = "lot-mgmt/next-id"; + /* Pre-set the value in case the frame is empty */ + g_value_set_int64 (value, 0); + qof_instance_get_kvp (QOF_INSTANCE (account), key, value); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); break; @@ -392,6 +402,7 @@ gnc_account_set_property (GObject *object, { Account *account; gnc_numeric *number; + gchar *key = NULL; g_return_if_fail(GNC_IS_ACCOUNT(object)); @@ -477,6 +488,10 @@ gnc_account_set_property (GObject *object, case PROP_SORT_ORDER: xaccAccountSetSortOrder(account, g_value_get_string(value)); break; + case PROP_LOT_NEXT_ID: + key = "lot-mgmt/next-id"; + qof_instance_set_kvp (QOF_INSTANCE (account), key, value); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); break; @@ -834,6 +849,17 @@ gnc_account_class_init (AccountClass *klass) "the sort order to be recalled.", NULL, G_PARAM_READWRITE)); + + g_object_class_install_property + (gobject_class, + PROP_LOT_NEXT_ID, + g_param_spec_int64 ("lot-next-id", + "Lot Next ID", + "Tracks the next id to use in gnc_lot_make_default.", + (gint64)1, + G_MAXINT64, + (gint64)1, + G_PARAM_READWRITE)); } static void diff --git a/src/engine/gnc-lot.c b/src/engine/gnc-lot.c index 26100b0825..d630b1d8c8 100644 --- a/src/engine/gnc-lot.c +++ b/src/engine/gnc-lot.c @@ -675,19 +675,19 @@ GNCLot * gnc_lot_make_default (Account *acc) { GNCLot * lot; gint64 id; - char buff[200]; + gchar *buff; lot = gnc_lot_new (qof_instance_get_book(acc)); /* Provide a reasonable title for the new lot */ xaccAccountBeginEdit (acc); - id = kvp_frame_get_gint64 (xaccAccountGetSlots (acc), "/lot-mgmt/next-id"); - snprintf (buff, 200, ("%s %" G_GINT64_FORMAT), _("Lot"), id); - kvp_frame_set_str (gnc_lot_get_slots (lot), "/title", buff); + qof_instance_get (QOF_INSTANCE (acc), "lot-next-id", &id, NULL); + buff = g_strdup_printf ("%s %" G_GINT64_FORMAT, _("Lot"), id); + gnc_lot_set_title (lot, buff); id ++; - kvp_frame_set_gint64 (xaccAccountGetSlots (acc), "/lot-mgmt/next-id", id); - qof_instance_set_dirty (QOF_INSTANCE(acc)); + qof_instance_set (QOF_INSTANCE (acc), "lot-next-id", id, NULL); xaccAccountCommitEdit (acc); + g_free (buff); return lot; } diff --git a/src/engine/test/Makefile.am b/src/engine/test/Makefile.am index 8c6b61330c..5e216d1cb3 100644 --- a/src/engine/test/Makefile.am +++ b/src/engine/test/Makefile.am @@ -113,8 +113,9 @@ noinst_PROGRAMS = ${TEST_PROGS} ${CHECK_PROGS} test_engine_SOURCES = \ test-engine.c \ utest-Account.c \ - utest-Budget.c \ - utest-Invoice.c + utest-Budget.c \ + utest-Invoice.c \ + test-engine-kvp-properties.c test_engine_LDADD = \ libutest-Split.la \ diff --git a/src/engine/test/test-engine-kvp-properties.c b/src/engine/test/test-engine-kvp-properties.c new file mode 100644 index 0000000000..9dda2f84a7 --- /dev/null +++ b/src/engine/test/test-engine-kvp-properties.c @@ -0,0 +1,107 @@ +/******************************************************************** + * test-engine-kvp-properties.c: GLib g_test test suite for * + * KVP-based properties in several engine classes. * + * Copyright 2013 John Ralls * + * * + * 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, contact: * + * * + * Free Software Foundation Voice: +1-617-542-5942 * + * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 * + * Boston, MA 02110-1301, USA gnu@gnu.org * +\********************************************************************/ + +/** + * Test Engine KVP Properties Acceptance testing for KVP Properties + * added to various engine classes to make private the internals of + * KVP storage used for a variety of parameters on several engine + * classes. + */ + +#include +#include +#include +#include +#include "../Transaction.h" +#include "../Split.h" +#include "../Account.h" +#include "../SchedXAction.h" +#include "../gncCustomer.h" +#include "../gncEmployee.h" +#include "../gncJob.h" +#include "../gncVendor.h" + +typedef struct +{ + union + { + Account *acct; + Transaction *trans; + GncCustomer *cust; + GncEmployee *emp; + GncJob *job; + GncVendor *vend; + }; + GSList *split; +} Fixture; + +/* Prototype to shut clang up */ +void test_suite_engine_kvp_properties (void); + +/* Private QofInstance function needed for testing */ +extern void qof_instance_mark_clean (QofInstance*); + +const gchar *suitename = "/engine/kvp-properties"; + +static void +setup_account (Fixture *fixture, gconstpointer pData) +{ + QofBook *book = qof_book_new (); + fixture->acct = xaccMallocAccount (book); +} + +static void +teardown (Fixture *fixture, gconstpointer pData) +{ +/* It doesn't actually matter which union member we use here, they're + * all QofInstances, so this will work for any of them. + */ + QofBook *book = qof_instance_get_book (QOF_INSTANCE (fixture->acct)); + test_destroy (fixture->acct); + test_destroy (book); +} + +static void +test_account_kvp_properties (Fixture *fixture, gconstpointer pData) +{ + gint64 next_id = 12345678909876; + gint64 next_id_r; + + qof_instance_set (QOF_INSTANCE (fixture->acct), + "lot-next-id", next_id, + NULL); + + g_assert (qof_instance_is_dirty (QOF_INSTANCE (fixture->acct))); + qof_instance_mark_clean (QOF_INSTANCE (fixture->acct)); + + qof_instance_get (QOF_INSTANCE (fixture->acct), + "lot-next-id", &next_id_r, + NULL); + g_assert_cmpint (next_id, ==, next_id_r); + g_assert (!qof_instance_is_dirty (QOF_INSTANCE (fixture->acct))); +} + +void test_suite_engine_kvp_properties (void) +{ + GNC_TEST_ADD (suitename, "Account", Fixture, NULL, setup_account, test_account_kvp_properties, teardown); +} diff --git a/src/engine/test/test-engine.c b/src/engine/test/test-engine.c index 508856e8a0..19221b8090 100644 --- a/src/engine/test/test-engine.c +++ b/src/engine/test/test-engine.c @@ -31,6 +31,7 @@ extern void test_suite_budget(); extern void test_suite_gncInvoice(); extern void test_suite_transaction(); extern void test_suite_split(); +extern void test_suite_engine_kvp_properties (void); int main (int argc, @@ -49,6 +50,7 @@ main (int argc, test_suite_gncInvoice(); test_suite_transaction(); test_suite_split(); + test_suite_engine_kvp_properties (); return g_test_run( ); }