test/post.lisp

DOWNLOAD
(defpackage murja.tests.posts
  (:use :cl :fiveam :murja.models.post :murja.models.user)
  (:import-from :murja.tests :prepare-db-and-server :drakma->string :url :main-suite :prepare-db-and-server))

(in-package :murja.tests.posts)

(in-suite main-suite)

(defparameter *test-user* (make-instance 'User :id 1 :username "test" :password "testpass" :nickname "Test User"))

(defparameter *test-posts*
  (list (make-instance 'Post
		       :id 1
		       :title "Test"
		       :content "lol"
		       :creator-id 1
		       :created-at (simple-date:encode-timestamp 2002 12 12 1 2 3)
		       :hidden? nil
		       :unlisted? nil
		       :next nil
		       :previous nil
		       :tags "[]")
	(make-instance 'Post
		       :id 2
		       :title "Test2"
		       :content "lol"
		       :creator-id 1
		       :created-at (simple-date:encode-timestamp 2004 1 12 2 1 3)
		       :hidden? t
		       :unlisted? t
		       :next nil
		       :previous nil
		       :tags "[]")
	(make-instance 'Post
		       :id 3
		       :title "Test3"
		       :content "lol"
		       :creator-id 1
		       :created-at (simple-date:encode-timestamp 2005 1 12 2 1 3)
		       :hidden? nil
		       :unlisted? t
		       :next nil
		       :previous nil
		       :tags "[]")

	(make-instance 'Post
		       :id 4
		       :title "This shouldn't be visible"
		       :content "lol"
		       :creator-id 1
		       :created-at (simple-date:encode-timestamp 2004 1 12 2 1 3)
		       :hidden? t
		       :unlisted? t
		       :next nil
		       :previous nil
		       :tags "[]")
	(make-instance 'Post
		       :id 5
		       :title "Newst test "
		       :content "lol"
		       :creator-id 1
		       :created-at (simple-date:encode-timestamp 2010 1 12 2 1 3)
		       :hidden? nil
		       :unlisted? nil
		       :next nil
		       :previous nil
		       :tags "[]")))

(defun setup-previouslies ()
  "5 refers to 3 and 4 (so should be fetched from db without any previouslies.
3 refers to 1 and 2 and should contain those"
  (postmodern:execute "INSERT INTO blog.previously_link VALUES (5, 3), (5,4), (3, 1), (3, 2)"))

(defmacro within-post-test-db (&body body)
  `(progn
     (setf murja.middleware.db:*automatic-tests-on?* t)
     (setf lisp-fixup:*dev?* t)
     ;; with-db connects automatically to the test-db if *automatic-tests-on?* != nil 
     (murja.middleware.db:with-db 
	 (unwind-protect 
	      (progn 
		(postmodern:execute "DROP SCHEMA IF EXISTS blog CASCADE;")
		(postmodern:execute "DROP TABLE IF EXISTS public.ragtime_migrations")
		(postmodern:execute "DROP TABLE IF EXISTS public.migrations_tracker")
		(murja.migrations:migrate)
		(postmodern:upsert-dao *test-user*)
		(mapcar #'postmodern:insert-dao *test-posts*)
		(setup-previouslies)
		
		,@body)

	(postmodern:execute "DROP SCHEMA IF EXISTS blog CASCADE;")
	(postmodern:execute "DROP TABLE IF EXISTS public.ragtime_migrations")
	(postmodern:execute "DROP TABLE IF EXISTS public.migrations_tracker")
	(setf lisp-fixup:*dev?* nil)
	(setf murja.middleware.db:*automatic-tests-on?* nil)))))
  

(def-test post-model-test ()
  (within-post-test-db
    ;; test data is inserted as expected 
    (is (equalp 1
		(caar (postmodern:query "SELECT count(*) FROM blog.Users"))))
    (is (equalp 5
		(caar (postmodern:query "SELECT count(*) FROM blog.Post"))))
    (is (equalp 3
		(caar (postmodern:query "SELECT count(*) FROM blog.Post WHERE hidden or unlisted"))))

    ;; test that previous and next links are calculated correctly
    (let ((post-3 (murja.models.post:get-post 3)))
      (is-true post-3)
      (is (equalp
	   1
	   (previous-post-id post-3)))

      (is (equalp
	   5
	   (next-post-id post-3))))

    ;; test that completely public post-1 skips both hidden posts and the unlisted post-3
    (let ((post-1 (murja.models.post:get-post 1)))
      (is-true post-1)
      ;; no previous-id 
      (is (equalp -1
		  (previous-post-id post-1)))

      ;; the next link skips correctly hiddens and unlisteds
      (is (equalp 5
		  (next-post-id post-1))))

    ;; test that previouslies honour unlisted and hidden
    (let ((post-5 (murja.models.post:get-post 5))
	  (post-3 (murja.models.post:get-post 3)))
      (is-false (previouslies post-5))

      (is (equalp 1
		  (length (previouslies post-3))))
      (is (equalp 1
		  (gethash "id" (first (previouslies post-3)))))))) ;; 2 is hidden
		  

;; (fiveam:explain! 
;;  (fiveam:run 'post-model-test))