<commit_before>(ns clj.conference-rating.handler-test
  (:use [conference-rating.handler])
  (:require [clojure.test :refer [deftest is testing use-fixtures]]
            [ring.mock.request :refer [request body header]]
            [clojure.data.json :as json]
            [monger.collection :as mc]
            [conference-rating.testdata :refer [some-rating-with some-rating]])
  (:import (com.github.fakemongo Fongo)
           (org.bson.types ObjectId)))

(use-fixtures :once schema.test/validate-schemas)

(defn json-body-for [db request] (json/read-str (:body ((app db) request)) :key-fn keyword))

(defn- create-mock-db []
  (let [fongo (Fongo. "some mock mongodb")]
    (.getDB fongo "crdb")))

(deftest acceptance-test
  (testing "should have an index-page"
    (is (= 200 (:status ((app (create-mock-db)) (request :get "/"))))))
  (testing "should return all ratings of a conference as json"
    (let [db (create-mock-db)
          response ((app db) (-> (request :post "/api/conferences/someConferenceId/ratings")
                                 (body (json/write-str
                                         (some-rating-with :comment {:name "Bob" :comment "some comment"}
                                                           :rating {:overall 5
                                                                    :talks 1
                                                                    :venue 2
                                                                    :networking 3})))
                                 (header :content-type "application/json")))]
      (is (= 201 (:status response)))
      (let [ratings-response ((app db) (request :get "/api/conferences/someConferenceId/ratings"))
            rating-list (json/read-str (:body ratings-response) :key-fn keyword) ]
        (is (= 200 (:status ratings-response) ))
        (is (= 1 (count rating-list)))
        (is (= "Bob" (:name (:comment (first rating-list)))))
        (is (= "some comment" (:comment (:comment (first rating-list)))))
        (is (= 5 (:overall (:rating (first rating-list))))))))
  (testing "should add a conference to the database"
    (let [db (create-mock-db)]
      (let [response ((app db) (-> (request :post "/api/conferences/")
                                   (body (json/write-str {:name "<NAME>" :description "some description"}))
                                   (header :content-type "application/json")))]
        (is (= 201 (:status response)))
        (is (.startsWith (get-in response [:headers "Location"]) "/api/conferences/"))
        (let [conference-response (json-body-for db (request :get (get-in response [:headers "Location"])))]
          (is (= "some description" (:description conference-response)))
          (is (= "some name" (:name conference-response)))
          (is (map? (:aggregated-ratings conference-response))))
      (let [conferences (json-body-for db (request :get "/api/conferences"))]
        (is (= 1 (count conferences)))
        (is (= "some description" (:description (first conferences))))
        (is (= "some name" (:name (first conferences)))))))))


(deftest backwards-compatibility-test
  (testing "that we get valid ratings even if there is crap in the db"<commit_msg>add a test for input validation<commit_after>(ns clj.conference-rating.handler-test
  (:use [conference-rating.handler])
  (:require [clojure.test :refer [deftest is testing use-fixtures]]
            [ring.mock.request :refer [request body header]]
            [clojure.data.json :as json]
            [monger.collection :as mc]
            [conference-rating.testdata :refer [some-rating-with some-rating]])
  (:import (com.github.fakemongo Fongo)
           (org.bson.types ObjectId)))

(use-fixtures :once schema.test/validate-schemas)

(defn json-body-for [db request] (json/read-str (:body ((app db) request)) :key-fn keyword))

(defn- create-mock-db []
  (let [fongo (Fongo. "some mock mongodb")]
    (.getDB fongo "crdb")))

(deftest acceptance-test
  (testing "should have an index-page"
    (is (= 200 (:status ((app (create-mock-db)) (request :get "/"))))))
  (testing "should return all ratings of a conference as json"
    (let [db (create-mock-db)
          response ((app db) (-> (request :post "/api/conferences/someConferenceId/ratings")
                                 (body (json/write-str
                                         (some-rating-with :comment {:name "Bob" :comment "some comment"}
                                                           :rating {:overall 5
                                                                    :talks 1
                                                                    :venue 2
                                                                    :networking 3})))
                                 (header :content-type "application/json")))]
      (is (= 201 (:status response)))
      (let [ratings-response ((app db) (request :get "/api/conferences/someConferenceId/ratings"))
            rating-list (json/read-str (:body ratings-response) :key-fn keyword) ]
        (is (= 200 (:status ratings-response) ))
        (is (= 1 (count rating-list)))
        (is (= "Bob" (:name (:comment (first rating-list)))))
        (is (= "some comment" (:comment (:comment (first rating-list)))))
        (is (= 5 (:overall (:rating (first rating-list))))))))
  (testing "should add a conference to the database"
    (let [db (create-mock-db)]
      (let [response ((app db) (-> (request :post "/api/conferences/")
                                   (body (json/write-str {:name "some name" :description "some description"}))
                                   (header :content-type "application/json")))]
        (is (= 201 (:status response)))
        (is (.startsWith (get-in response [:headers "Location"]) "/api/conferences/"))
        (let [conference-response (json-body-for db (request :get (get-in response [:headers "Location"])))]
          (is (= "some description" (:description conference-response)))
          (is (= "some name" (:name conference-response)))
          (is (map? (:aggregated-ratings conference-response))))
      (let [conferences (json-body-for db (request :get "/api/conferences"))]
        (is (= 1 (count conferences)))
        (is (= "some description" (:description (first conferences))))
        (is (= "some name" (:name (first conferences))))))))
  (testing "should fail if incomplete data is written to ratings"
    (let [db (create-mock-db)
          response ((app db) (-> (request :post "/api/conferences/someConferenceId/ratings")
                                 (body (json/write-str {:some "random value"}))
                                 (header :content-type "application/json")))]
      (is (= 500 (:status response))))))

  (deftest backwards-compatibility-test
  (testing "that we get valid ratings even if there is crap in the db"
{'fraction_non_alphanumeric': 0.14916485112563543, 'fraction_numerical': 0.005954974582425563, 'mean_word_length': 2.012248468941382, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 2, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1876876', 'n_tokens_mistral': 1956, 'n_tokens_neox': 1822, 'n_words': 548}