aboutsummaryrefslogtreecommitdiff
path: root/src/clojure/contrib/datalog/tests/test_database.clj
blob: 527c8faeb62ab631ef5084e80b0f7633339687a8 (plain)
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
;;  Copyright (c) Jeffrey Straszheim. All rights reserved.  The use and
;;  distribution terms for this software are covered by the Eclipse Public
;;  License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can
;;  be found in the file epl-v10.html at the root of this distribution.  By
;;  using this software in any fashion, you are agreeing to be bound by the
;;  terms of this license.  You must not remove this notice, or any other,
;;  from this software.
;;
;;  test-database.clj
;;
;;  A Clojure implementation of Datalog -- Database
;;
;;  straszheimjeffrey (gmail)
;;  Created 12 Feburary 2009


(ns clojure.contrib.datalog.tests.test-database
  (:use clojure.contrib.test-is
	clojure.contrib.datalog.database))


(def test-db
     (make-database
      (relation :fred [:mary :sue])
      (index :fred :mary)
      (relation :sally [:jen :becky :joan])
      (index :sally :jen)
      (index :sally :becky)))

(deftest test-make-database
  (is (= (-> test-db :fred :schema) #{:mary :sue}))
  (is (= (-> test-db :sally :schema) #{:jen :becky :joan})))

(deftest test-ensure-relation
  (is (contains? (ensure-relation test-db :bob [:sam :george] [:sam]) :bob))
  (is (contains? (ensure-relation test-db :fred [:mary :sue] [:mary]) :fred))
  (is (thrown? Exception (ensure-relation test-db :fred [:bob :joe] []))))

(deftest test-add-tuple
  (let [new-db (add-tuple test-db :fred {:mary 1 :sue 2})]
    (is (= (select new-db :fred {:mary 1}) [{:mary 1 :sue 2}])))
  (is (thrown? Exception (add-tuple test-db :fred {:mary 1}))))


(def test-db-1
     (add-tuples test-db
                 [:fred :mary 1 :sue 2]
                 [:fred :mary 2 :sue 3]
                 [:sally :jen 1 :becky 2 :joan 0]
                 [:sally :jen 1 :becky 4 :joan 3]
                 [:sally :jen 1 :becky 3 :joan 0]
                 [:sally :jen 1 :becky 2 :joan 3]
                 [:fred :mary 1 :sue 1]
                 [:fred :mary 3 :sue 1]))

(deftest test-select
  (is (= (set (select test-db-1 :sally {:jen 1 :becky 2}))
         #{{:jen 1 :joan 0 :becky 2} {:jen 1 :joan 3 :becky 2}}))
  (is (= (set (select test-db-1 :fred {:sue 1})))
      #{{:mary 3 :sue 1} {:mary 1 :sue 1}})
  (is (empty? (select test-db-1 :sally {:joan 5 :jen 1}))))
         
(deftest test-any-match?
  (is (any-match? test-db-1 :fred {:mary 3}))
  (is (any-match? test-db-1 :sally {:jen 1 :becky 2 :joan 3}))
  (is (not (any-match? test-db-1 :sally {:jen 5})))
  (is (not (any-match? test-db-1 :fred {:mary 1 :sue 5}))))


(comment
  (run-tests)
)

;; End of file