branch: externals/ebdb commit 3a652fe5dd315a64abaa8877f80d44d34a06572d Author: Eric Abrahamsen <e...@ericabrahamsen.net> Commit: Eric Abrahamsen <e...@ericabrahamsen.net>
Adjust initialize-instance for records * ebdb.el (initialize-instance): Fix the insurance regarding timestamp and creation-date fields. Should be addressing the "slots" argument, not the record's slots. * ebdb-test.el (ebdb-auto-insert-timestamp-creation): Test all this. --- ebdb-test.el | 33 +++++++++++++++++++++++++++++++++ ebdb.el | 21 +++++++++++++-------- 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/ebdb-test.el b/ebdb-test.el index e6158f6..b2285e2 100644 --- a/ebdb-test.el +++ b/ebdb-test.el @@ -135,6 +135,39 @@ rec (ebdb-parse 'ebdb-field-mail "n...@such.com")) :type 'ebdb-readonly-db)))))) +(ert-deftest ebdb-auto-insert-timestamp-creation () + "Test the creation of timestamp and creation-date fields. +Actually exercises the `initialize-instance' methods of records." + (ebdb-test-with-records + (ebdb-test-with-database (db ebdb-test-database-1) + (let* ((r1 (make-instance ebdb-default-record-class)) + (r2 (make-instance ebdb-default-record-class + :timestamp nil + :creation-date nil)) + (r2-date (slot-value (slot-value r2 'creation-date) 'timestamp))) + ;; `make-instance' with no :timestamp or :creation-date values + ;; should get the fields correctly. + (should (and (stringp (ebdb-string (slot-value r1 'timestamp))) + (stringp (ebdb-string (slot-value r1 'creation-date))))) + (delete-instance r1) + ;; `make-instance' with tags set to nil should still get + ;; correct fields (can happen in migration). + (should (and (stringp (ebdb-string (slot-value r2 'timestamp))) + (stringp (ebdb-string (slot-value r2 'creation-date))))) + ;; Creating a record, saving it to the database, then + ;; re-loading it shouldn't change the creation date. + (ebdb-db-add-record db r2) + (ebdb-db-save db) + (sleep-for 2) + (ebdb-db-unload db) + (setq db (eieio-persistent-read (slot-value db 'file) 'ebdb-db t)) + (should (equal r2-date + (slot-value + (slot-value + (car ebdb-record-tracker) + 'creation-date) + 'timestamp))))))) + (ert-deftest ebdb-cant-find-related-role () "Find org record from a role field. If it doesn't exist, raise `ebdb-related-unfound'." diff --git a/ebdb.el b/ebdb.el index 26c3b67..e783ec4 100644 --- a/ebdb.el +++ b/ebdb.el @@ -2572,14 +2572,19 @@ RECORD is responsible for parsing it correctly.") ;; created (whether manually or loaded). (let ((cache (make-instance 'ebdb-cache))) (setq slots (plist-put slots :cache cache)) - (with-slots (timestamp creation-date) record - (unless creation-date - (setf creation-date (make-instance 'ebdb-field-creation-date)) - (ebdb-stamp-time creation-date)) - (unless timestamp - (setf timestamp (make-instance 'ebdb-field-timestamp)) - (ebdb-stamp-time timestamp)) - (cl-call-next-method record slots)))) + (unless (plist-get slots :timestamp) + (setq slots + (plist-put slots :timestamp + (make-instance 'ebdb-field-timestamp))) + (ebdb-stamp-time (plist-get slots :timestamp)) + (setf (slot-value record 'dirty) t)) + (unless (plist-get slots :creation-date) + (setq slots + (plist-put slots :creation-date + (make-instance 'ebdb-field-creation-date))) + (ebdb-stamp-time (plist-get slots :creation-date)) + (setf (slot-value record 'dirty) t)) + (cl-call-next-method record slots))) (cl-defmethod ebdb-init-record ((record ebdb-record)) "Initiate a record after loading a database or creating a new