Skip to content
This repository
tree: 3e4fcd0a15
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 133 lines (113 sloc) 4.032 kb
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
;;; elnode-db.el --- a database interface -*- lexical-binding: t -*-

;; Copyright (C) 2012 Nic Ferrier

;; Author: Nic Ferrier <nferrier@ferrier.me.uk>
;; Maintainer: Nic Ferrier <nferrier@ferrier.me.uk>
;; Created: 30 June 2012
;; Keywords: lisp, http, hypermedia

;; This file is NOT part of GNU Emacs.

;; 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 3 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, see <http://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; These are the tests for elnode-db.
;;
;; elnode-db needs it's own tests because elnode-db is a depend of
;; elnode itself and thus it gets loaded before elnode. If it did not
;; have it's own tests it would cause a dependancy on ERT for all of
;; elnode.

;; This code originated from this gmane thread:
;; http://permalink.gmane.org/gmane.emacs.devel/115821 by Thierry Volpiatto

;;; Source code
;;
;; elnode's code can be found here:
;; http://github.com/nicferrier/elnode

;;; Style note
;;
;; This codes uses the Emacs style of:
;;
;; elnode--private-function
;;
;; for private functions.


;;; Code:

(eval-when-compile (require 'cl))
(require 'ert)
(require 'elnode-db)

(ert-deftest elnode-db-get ()
  "Test the database interface and the hash implementation."
  ;; Make a hash-db with no filename
  (let ((db (elnode-db-make '(elnode-db-hash))))
    (should-not (elnode-db-get "test-key" db))
    (elnode-db-put "test-key" 321 db)
    (should
     (equal
      321
      (elnode-db-get "test-key" db)))))

(ert-deftest elnode-db-put ()
  "Test the put interface."
  (let ((db (elnode-db-make '(elnode-db-hash))))
    (should-not (elnode-db-get "test-key" db))
    (should
     (equal
      '("1" "2" "3")
      (elnode-db-put "test-key" '("1" "2" "3") db)))))

(ert-deftest elnode-db-hash--save ()
  "Test the saving of a hash db."
  (unwind-protect
       (progn
         (let ((db (elnode-db-make
                    ;; You shouldn't use an extension but let elnode deal
                    ;; with it.
                    '(elnode-db-hash :filename "/tmp/elnode-test-db"))))
           ;; Override the save so it does nothing from put
           (flet ((elnode-db-hash--save (db)
                    t))
             (elnode-db-put 'test1 "value1" db)
             (elnode-db-put 'test2 "value2" db))
           ;; And now save
           (elnode-db-hash--save db))
         ;; And now load in a different scope
         (let ((db (elnode-db-make
                    '(elnode-db-hash :filename "/tmp/elnode-test-db"))))
           (should
            (equal "value1"
                   (elnode-db-get 'test1 db)))))
    (delete-file "/tmp/elnode-test-db.elc")))

(ert-deftest elnode-db-filter ()
  "Test the filtering."
  (let ((db (elnode-db-make
             '(elnode-db-hash :filename "/tmp/elnode-test-db"))))
    (elnode-db-put
     "test001"
     '(("uid" . "test001")
       ("fullname" . "test user 1"))
     db)
    (elnode-db-put
     "test002"
     '(("uid" . "test002")
       ("fullname" . "test user 2"))
     db)
    (elnode-db-put
     "test003"
     '(("uid" . "test001")
       ("fullname" . "test user 1"))
     db)
    (flet ((filt (key value)
             (cdr (assoc "fullname" value))))
      (let ((filtered
             (elnode-db-make
              `(elnode-db-filter
                :source ,db
                :filter filt))))
        (plist-get filtered :source)
        (should
         (equal (elnode-db-get "test002" filtered) "test user 2"))))))

(provide 'elnode-db-tests)

;;; elnode-db-tests.el ends here
Something went wrong with that request. Please try again.