Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing #23

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ pom.xml
pom.xml.asc
*.jar
*.class
*#
*~
/.lein-*
/.nrepl-port
resources/generated/
resources/generated/
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: clojure
lein: lein2
jdk:
- openjdk7
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![Build Status](https://travis-ci.org/DeltaAlphaRho/groops.svg?branch=master)](https://travis-ci.org/DeltaAlphaRho/groops)

groops
======

Expand Down
10 changes: 7 additions & 3 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
(defproject groops "0.1.0-SNAPSHOT"
:description "Austin Clojure Meetup groops project"
:url "https://github.com/AustinClojure/groops"

:dependencies [[org.clojure/clojure "1.6.0"]

;; server
Expand All @@ -24,11 +23,16 @@
;; dev
[org.clojure/tools.nrepl "0.2.3"]
[cider/cider-nrepl "0.7.0"]
[cljs-hash "0.0.2"]]
[cljs-hash "0.0.2"]

;; testing
[org.clojure/data.json "0.2.5"]
[http.async.client "0.5.2"]]

:plugins [[com.cemerick/austin "0.1.5"]
[lein-cljsbuild "1.0.3"]
[lein-ring "0.8.8"]]
[lein-ring "0.8.8"]
[com.cemerick/clojurescript.test "0.3.1"]]
:resource-paths ["resources"]
:cljsbuild {:builds
[{:source-paths ["src-cljs"]
Expand Down
4 changes: 1 addition & 3 deletions src/groops/async.clj
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
(swap! chat-clients dissoc channel)
(println channel "disconnected. status: " status)))
(on-receive channel (fn [data]
(println "on-receive channel:" channel " data:" data)
(swap! chat-clients assoc-in [channel] (read-string data))
(println "chat-ws chat-clients" @chat-clients)))))
(swap! chat-clients assoc-in [channel] (read-string data))))))

(defn send-level []
(let [level (int (rand 100))
Expand Down
94 changes: 94 additions & 0 deletions test/test_groops/api.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
(ns test-groops.api
(:require [groops.api :as api]
[groops.data :as data]
[clojure.data.json :as json])
(:use clojure.test
ring.mock.request))

(comment
(ns test-groops.api)
(require '[groops.api :as api])
(require '[groops.data :as data])
(require '[clojure.data.json :as json])
(use 'clojure.test)
(use 'ring.mock.request))

#_(defn request [method params]
(hash-map :request-method method :headers {} :params params))

(defn user-map [name email twitter]
(hash-map :name name :email email :twitter twitter))

(def test-users #{(user-map "John McCarthy" "[email protected]" "@JohnMcCarthy")
(user-map "Steve Russell" "[email protected]" "@SteveRussell")
(user-map "Guy Steele" "[email protected]" "@GuySteele")
(user-map "Rich Hickey" "[email protected]" " @RickHickey")})

(defn post-test-user [user]
(api/api-routes (-> (request :post "/api/user")
(assoc :params user))))

(doall (map post-test-user test-users))

(deftest test-post-user-count
(is (= 4 (count (deref data/registry-set)))))

(comment
(api/post-room (request :post {:room-name "Alpha"}))
(api/post-room (request :post {:room-name "Beta"}))
(api/post-room (request :post {:room-name "Gamma"}))
(api/post-room (request :post {:room-name "Delta"})))

(defn post-test-room [room]
(api/api-routes (-> (request :post "/api/room")
(assoc :params {:room-name room}))))

(def test-rooms #{"Alpha" "Beta" "Gamma" "Delta"})

(doall (map post-test-room test-rooms))

(deftest test-post-room-count
(is (= 4 (count (deref data/room-set)))))


(defn get-user-in-room-count []
(let [room-count-map (json/read-str
(:body
(api/api-routes (request :get "/api/rooms"))))]
(apply + (vals (first (vals room-count-map))))))

(deftest test-get-rooms-initially-zero
(is (= 0 (get-user-in-room-count))))

(defn get-message-vect-from-room [room]
(first
(vals
(json/read-str
(:body (api/api-routes
(request :get (str "/api/room/messages/" room))))))))

(defn message-map [room user message gravatar-url]
(hash-map :room room :user user :message message
:gravatar-url gravatar-url))

(def test-messages #{(message-map "Alpha" "John McCarthy"
"This is the first room"
nil)
(message-map "Alpha" "Steve Russell"
"This is the second message in the first room"
nil)})

(defn post-test-message [message]
(api/api-routes (-> (request :post "/api/room/message")
(assoc :params message))))

(doall (map post-test-message test-messages))

(deftest count-message-vect
(is (= 2 (count (get-message-vect-from-room "Alpha"))))
;; messages loaded into Beta froom in async testing
(is (= 1 (count (get-message-vect-from-room "Beta"))))
(is (empty? (get-message-vect-from-room "Gamma")))
(is (empty? (get-message-vect-from-room "Delta"))))


52 changes: 52 additions & 0 deletions test/test_groops/async.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
(ns test-groops.async
(:require [groops.async :as async]
[groops.server :as server]
[http.async.client :as http]
[clojure.data.json :as json]
[test-groops.api :as api-test :only post-test-message])
(:use clojure.test
ring.mock.request)
)

(comment
(ns test-groops.async)
(require '[groops.async :as async])
(require '[http.async.client :as http])
(require '[clojure.data.json :as json])
(require '[test-groops.api :as api-test :only post-test-message])
(use 'clojure.test)
(use 'ring.mock.request))

(server/start-webserver)

(def client (http/create-client))

(def received-msg (atom nil))

(def ws (http/websocket client "ws://localhost:8080/chat-ws"
:text (fn [con msg]
(reset! received-msg msg)
(println "test-groops.async: ws text: connection " con)
(println "test-groops.async: ws text: message " msg))
:close (fn [con status]
(println "test-groops.async: ws close:" con status))
:open (fn [con]
(println "test-groops.async: ws opened:" con))))

(http/send ws :text (pr-str {:name "Rich Hickey" :email "[email protected]" :room "Beta"}))

(deftest websocket-populates-chat-client
(let [chat-client (deref async/chat-clients)
ws-msg (first (vals chat-client))]
(is (> (count chat-client) 0))
(is (= "Rich Hickey" (:name ws-msg)))
(is (= "[email protected]" (:email ws-msg)))
(is (= "Beta" (:room ws-msg)))))

(api-test/post-test-message {:room "Beta" :user "Rich Hickey" :message "You're doing it wrong." :gravatar-url nil})

(deftest websocket-sends-to-client
(let [msg-rec (first (vals (json/read-str @received-msg)))]
(println "websocket-sends-to-client msg:" )
(is (= "Rich Hickey" (get-in msg-rec ["author"])))
(is (= "You're doing it wrong." (get-in msg-rec ["message"])))))
21 changes: 21 additions & 0 deletions test/test_groops/web.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
(ns test-groops.web
(:require [groops.web :as web])
(:use clojure.test
ring.mock.request))

(comment
(ns test-groops.web)
(require '[groops.web :as web])
(use 'clojure.test)
(use 'ring.mock.request))

(def basic-req {:get "/"})

(deftest landing-template-not-blank
(is (< 0 (count (web/landing-page basic-req)))))

(deftest landing-template-contains-groops-js
(is (some #(.contains % "groops.js") (web/landing-page basic-req))))

(deftest root-route-okay
(is (= 200 (:status (web/app-routes (request :get "/"))))))