table.lisp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. ;; orizuru-orm: a simple orm for Common lisp Copyright (C) 2019
  2. ;; Universita' degli Studi di Palermo
  3. ;; This program is free software: you can redistribute it and/or
  4. ;; modify it under the terms of the GNU General Public License as
  5. ;; published by the Free Software Foundation, version 3 of the
  6. ;; License, or (at your option) any later version.
  7. ;; This program is distributed in the hope that it will be useful, but
  8. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  9. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. ;; General Public License for more details.
  11. ;; You should have received a copy of the GNU General Public License
  12. ;; along with this program. If not, see
  13. ;; <http://www.gnu.org/licenses/>.
  14. (in-package :orizuru-orm-test.postgres)
  15. (defsuite table-suite (all-suite))
  16. (defmacro make-simple-table-1 ()
  17. `(deftable table-1 ()
  18. (field-1 :type text :nullp t))
  19. t)
  20. (deftest create-simple-table (table-suite)
  21. (with-db-connection
  22. (assert-true
  23. (with-nil-if-signal
  24. (make-simple-table-1)))))
  25. (defmacro make-table-2 ()
  26. `(progn
  27. (deftable table-2 ()
  28. (field-2 :type text :nullp t))))
  29. (defmacro make-table-3 ()
  30. `(progn
  31. (deftable table-3 (table-2)
  32. (:abstractp t))))
  33. (deftest create-inherited-table (table-suite)
  34. (with-db-connection
  35. (assert-true
  36. (with-nil-if-signal
  37. (progn
  38. (make-table-2)
  39. (make-table-3)
  40. t)))))
  41. (deftest create-another-table (table-suite)
  42. (with-db-connection
  43. (assert-true
  44. (with-nil-if-signal
  45. (progn
  46. #.(with-db-connection
  47. (deftable table-4 ()
  48. (field-4 :type text :nullp t)))
  49. (deftable table-5 (table-4)
  50. (field-5 :type int :nullp nil)))))))
  51. (deftest find-tables (table-suite)
  52. (with-db-connection
  53. (assert-true
  54. (with-nil-if-signal
  55. (progn
  56. #.(with-db-connection
  57. (deftable table-6 ()
  58. (field-6 :type text :nullp t)))
  59. (deftable table-7 (table-6)
  60. (:abstractp t))
  61. (deftable table-8 (table-6)
  62. (field-b :type int :nullp nil))
  63. (find-class 'table-6)
  64. (find-class 'table-7)
  65. (find-class 'table-8))))))
  66. (deftest column-options (table-suite)
  67. (assert-true
  68. (with-nil-if-signal
  69. #.(with-db-connection
  70. (deftable table-9 ()
  71. (field-9 :type text :nullp t)))))
  72. (with-db-connection
  73. (assert-true
  74. (with-nil-if-signal
  75. (deftable table-10 (table-9)
  76. (field-10
  77. :type int
  78. :default 1
  79. :unique-p t))))
  80. (assert-true
  81. (with-nil-if-signal
  82. (closer-mop:class-slots (find-class 'table-10))))
  83. (assert-true
  84. (with-nil-if-signal (orizuru-orm.meta:digest (find-class 'table-10))))))
  85. (deftest creating-related-tables (table-suite)
  86. (with-db-connection
  87. (assert-true
  88. (with-nil-if-signal
  89. (progn
  90. #.(with-db-connection
  91. (deftable parent-table ()
  92. (something :type int)))
  93. (deftable child-table ()
  94. (something-else :type text :initform "Foo")
  95. (ref :type int :foreign (parent-table :cascade :cascade))))))))
  96. (defsuite dereferencing-suite (table-suite))
  97. (deftest dereferencing (dereferencing-suite)
  98. (assert-true
  99. #.(with-db-connection
  100. (deftable parent-table-1 ()
  101. (something :type int)))
  102. #.(with-db-connection
  103. (deftable child-table-1 ()
  104. (something-else :type text :initform "Foo")
  105. (ref :type int :foreign (parent-table-1 :cascade :cascade))))
  106. (let* ((parent-instance (with-db-connection-body-value
  107. (create 'parent-table-1
  108. :something 99)))
  109. (child-instance (with-db-connection-body-value
  110. (create 'child-table-1
  111. :ref (id parent-instance))))
  112. (res (with-db-connection-body-value
  113. (something (deref child-instance 'ref)))))
  114. (= res 99))))