created tests for finding rain periods.
This commit is contained in:
parent
89561ec163
commit
cc80b04feb
|
@ -3,4 +3,5 @@
|
|||
:url "http://example.com/FIXME"
|
||||
:license {:name "Eclipse Public License"
|
||||
:url "http://www.eclipse.org/legal/epl-v10.html"}
|
||||
:dependencies [[org.clojure/clojure "1.6.0"] [org.clojure/data.csv "0.1.2"] [clj-time "0.8.0"]])
|
||||
:dependencies [[org.clojure/clojure "1.6.0"] [org.clojure/data.csv "0.1.2"] [clj-time "0.8.0"]]
|
||||
:profiles {:dev {:plugins [[com.jakemccrary/lein-test-refresh "0.5.2"]]}})
|
||||
|
|
|
@ -23,68 +23,35 @@
|
|||
(defn rainy [entry]
|
||||
(if (> (rainLevel entry) 0) true false))
|
||||
|
||||
(defn addToLastPeriod [x y]
|
||||
[(conj (butlast x) (conj (last x) y))])
|
||||
|
||||
(defn addToEmptyPeriod [x y]
|
||||
[(conj (butlast x) (conj (last (last x)) y))])
|
||||
|
||||
(addToLastPeriod [[[1]]] [2])
|
||||
(addToEmptyPeriod [[[]]] [2])
|
||||
|
||||
(defn createNewPeriod [x y]
|
||||
(conj x [y]))
|
||||
|
||||
(createNewPeriod [[[1] [2]]] [3])
|
||||
|
||||
;Create new periods if x y is different places. Or not raining
|
||||
(defn noCreatedPeriods [x] (empty? (last (last x))))
|
||||
|
||||
(empty? (last (last [[[]]])))
|
||||
|
||||
(def rolldal ["RØLDAL" "01.01.1960" "-" "-" "-" "19.9" "25" "4" "Regn.snø.sludd"])
|
||||
(def moss ["MOSS" "01.01.1960" "-" "-" "-" "19.9" "25" "4" "Regn.snø.sludd"])
|
||||
|
||||
(defn lastEntry [x] (last (last x)))
|
||||
|
||||
(defn newPlace [x y] (= (location (lastEntry x))
|
||||
(location y)))
|
||||
|
||||
(location (lastEntry [[rolldal]]))
|
||||
|
||||
(newPlace [[rolldal]] moss)
|
||||
|
||||
(defn compareFunc [x y]
|
||||
(do (println (str "processing" (lastEntry x))
|
||||
(if(noCreatedPeriods x)
|
||||
(do
|
||||
(println "add to empty period")
|
||||
(println (str "created:" (addToEmptyPeriod x y)))
|
||||
(addToEmptyPeriod x y))
|
||||
;if new place and raining start building new period
|
||||
(if(and (rainy (lastEntry x)) (rainy y))
|
||||
(do (println "They're consecutive rainy days.")(addToLastPeriod x y))
|
||||
;if it's not consecutive and raining create new period
|
||||
(if(and (not (consecutiveDay (lastEntry x) y)) (rainy y))
|
||||
(do
|
||||
(println "not consecutive, but raining")
|
||||
(createNewPeriod x y)) x))))))
|
||||
|
||||
(defn parseTime [entry]
|
||||
(let [custom-formatter (f/formatter "dd.MM.YYYY")]
|
||||
(f/parse custom-formatter (nth entry 1))))
|
||||
|
||||
(defn consecutiveDay [entry1 entry2]
|
||||
(= (t/plus (parseTime entry1) (t/days 1)) (parseTime entry2)))
|
||||
(or (= (t/plus (parseTime entry1) (t/days 1)) (parseTime entry2))
|
||||
(= (t/minus (parseTime entry1) (t/days 1)) (parseTime entry2))))
|
||||
|
||||
(def data [["RØLDAL" "01.01.1960" "-" "-" "-" "19.9" "25" "4" "Regn.snø.sludd"]
|
||||
["RØLDAL" "02.01.1960" "-" "-" "-" "19.9" "25" "4" "Regn.snø.sludd"]
|
||||
["RØLDAL" "05.01.1960" "-" "-" "-" "19.9" "25" "4" "Regn.snø.sludd"]])
|
||||
(def listOfPlaces (partition-by (fn [entry] (nth entry 0)) (rest csv)))
|
||||
|
||||
(def data (lazy-seq [["RØLDAL" "01.01.1960" "-" "-" "-" "19.9" "25" "4" "Regn.snø.sludd"]
|
||||
["RØLDAL" "02.01.1960" "-" "-" "-" "15" "25" "4" "Regn.snø.sludd"]
|
||||
["RØLDAL" "03.01.1960" "-" "-" "-" "19.9" "25" "4" "Regn.snø.sludd"]]))
|
||||
(def rolldal (nth listOfPlaces 0))
|
||||
|
||||
(reduce compareFunc [[[]]] data)
|
||||
(defn rainyEntries [place] (filter #(> (convertToNumeric (rainLevel %)) 0) place))
|
||||
|
||||
(consecutiveDay (first data) (second data))
|
||||
(defn addToLastPeriod [x y]
|
||||
(vec (conj (butlast x) (vec (conj (last x) y)))))
|
||||
|
||||
(defn createNewPeriod [x y]
|
||||
(vec (conj x [y])))
|
||||
|
||||
(defn rainyPeriods [raindata] (reduce
|
||||
(fn [x y]
|
||||
(if(= [[[]]] x)
|
||||
(addToLastPeriod (last (last x)) y)
|
||||
(if(consecutiveDay (last (last x)) y)
|
||||
(do (println "addingToLastperiod")
|
||||
(addToLastPeriod x y))
|
||||
(createNewPeriod x y))))
|
||||
[[[]]]
|
||||
raindata))
|
||||
|
||||
(rainyPeriods (take 5 (rainyEntries rolldal)))
|
||||
|
|
|
@ -1,16 +0,0 @@
|
|||
;; Anything you type in here will be executed
|
||||
;; immediately with the results shown on the
|
||||
;; right.
|
||||
|
||||
(def init [[["test"]][["hello"]]])
|
||||
|
||||
(defn nested-array [x] (last (last x)))
|
||||
|
||||
(def x (conj (butlast init) [(conj (nested-array init) ["world"])]))
|
||||
|
||||
(conj (conj (nested-array init) ["world"]) (butlast init))
|
||||
|
||||
(conj x [["another world"]])
|
||||
|
||||
[[[1] [2]]
|
||||
[[1] [3] [4]]]
|
|
@ -1,3 +0,0 @@
|
|||
(ns weather-data.core-test
|
||||
(:require [clojure.test :refer :all]
|
||||
[weather-data.rainiestperiod :refer :all :as weather]))
|
|
@ -1,30 +0,0 @@
|
|||
(ns weather-data.rainiest-period-test
|
||||
(:require [clojure.test :refer :all])
|
||||
(:use [weather-data.rainiestperiod :refer :all]))
|
||||
|
||||
(deftest test-rainy
|
||||
(testing "rainy returns true for raininess."
|
||||
(let [x ["RØLDAL" "01.01.1960" "-" "-" "-" "19.9" "25" "4" "Regn.snø.sludd"]]
|
||||
(is (= true (rainy x))))))
|
||||
|
||||
(deftest test-add-to-period
|
||||
(testing "is able to build rainy period."
|
||||
(let [x [1]
|
||||
y [2]]
|
||||
(is (= (addToLastPeriod [[x]] y)
|
||||
[[x y]])))))
|
||||
|
||||
(= (addToLastPeriod [[[1]]] 2) [[[1] [2]]])
|
||||
|
||||
(deftest test-add-new-period
|
||||
(testing "is able to build new rainy period."
|
||||
(let [x [1]
|
||||
y [2]
|
||||
z [3]]
|
||||
(is (=
|
||||
(createNewPeriod (addToLastPeriod [[[x]]] y) z)
|
||||
[[[x y]] [[z]]])))))
|
||||
|
||||
|
||||
|
||||
(run-tests 'weather-data.rainiest-period-test)
|
35
test/weather_data/rainiestperiod_test.clj
Normal file
35
test/weather_data/rainiestperiod_test.clj
Normal file
|
@ -0,0 +1,35 @@
|
|||
(ns weather-data.rainiestperiod-test
|
||||
(:require [clojure.test :refer :all][weather-data.rainiestperiod :refer :all :as weather]))
|
||||
|
||||
(deftest add-1-to-1
|
||||
(is (= 2 (+ 1 1))))
|
||||
|
||||
(deftest testAddToLastPeriod
|
||||
(let [x [[["entry1"]["entry2"]]]
|
||||
y ["entry3"]
|
||||
result [[["entry1"]["entry2"]["entry3"]]]]
|
||||
(is (= (weather/addToLastPeriod x y) result))))
|
||||
|
||||
(deftest testCreateNewPeriod
|
||||
(let [x [[["entry1"]["entry2"]]]
|
||||
y ["entry3"]
|
||||
result [[["entry1"]["entry2"]][["entry3"]]]]
|
||||
(is (= (weather/createNewPeriod x y) result))))
|
||||
|
||||
(deftest testConsecutiveDay
|
||||
(let [x [[["RØLDAL" "26.10.2014" "-" "-" "-" "42.7" "0" "0" "Regn"]]]
|
||||
y ["RØLDAL" "27.10.2014" "-" "-" "-" "57.0" "0" "0" "Regn"]]
|
||||
(is (weather/consecutiveDay (last (last x)) y))))
|
||||
|
||||
|
||||
(def initData (take 5 (weather/rainyEntries weather/rolldal)))
|
||||
(def result [[["RØLDAL" "01.01.1960" "-" "-" "-" "19.9" "25" "4" "Regn.snø.sludd"]
|
||||
["RØLDAL" "02.01.1960" "-" "-" "-" "2.0" "19" "4" "Regn"]]
|
||||
[["RØLDAL" "05.01.1960" "-" "-" "-" "3.0" "15" "4" "Regn"]
|
||||
["RØLDAL" "06.01.1960" "-" "-" "-" "1.4" "17" "4" "Regn.snø.sludd"]
|
||||
["RØLDAL" "07.01.1960" "-" "-" "-" "0.2" "15" "4" "Regn"]]])
|
||||
|
||||
(deftest testRainyPeriods
|
||||
(is (= (weather/rainyPeriods initData) result)))
|
||||
|
||||
(run-tests)
|
Loading…
Reference in a new issue