Skip to content

Instantly share code, notes, and snippets.

func insertOne(params):
tr = db.OpenTransaction()
resultRow = tr.Exec("select .... ")
if resultRow == nil
resultRow = tr.Exec("insert into .... ")
tr.Close()
return resultRow
@erez-rabih
erez-rabih / s3_test.clj
Created June 3, 2019 06:52
tighter mock
(deftest get-file-metadata-test
(let [example-bucket "some-bucket"
example-filename "some-file"
example-metadata {:example "valid metadata"}]
(testing "should return file metadata in case file exists"
(with-redefs [s3/get-object (fn [& {:keys [bucket-name key]}]
(when (and
(= bucket-name example-bucket)
(= key example-filename))
example-metadata))]
@erez-rabih
erez-rabih / s3.clj
Created June 3, 2019 06:30
s3 get file metadata broken
(defn get-file-metadata [bucket filename]
(try
(s3/get-object :bucket-name "bucket" :key filename)
(catch com.amazonaws.services.s3.model.AmazonS3Exception e nil)))
(deftest get-file-metadata-test
(let [example-metadata {:example "valid metadata"}]
(testing "should return file metadata in case file exists"
(with-redefs [s3/get-object (fn [& args] example-metadata)]
(is (= example-metadata (get-file-metadata "some-bucket" "some-file")))))))
@erez-rabih
erez-rabih / s3.clj
Created June 3, 2019 06:29
s3 get file metadata
(ns example
(:require [amazonica.aws.s3 :as s3]))
(defn get-file-metadata [bucket filename]
(try
(s3/get-object :bucket-name bucket :key filename)
(catch com.amazonaws.services.s3.model.AmazonS3Exception e nil)))
(deftest register-test
(let [email "[email protected]"
response (request :post "/register" {:email email})]
(testing "http response"
(testing "should return 201 status code"
(is (= 201 (:status response))))
(testing "should return the created user in response body"
(is (= email (-> response :body :email)))
(is (integer? (-> response :body :id))))
(testing "should not return the confirmation token in the response body"
(ns test.redis-mock)
(defn mock-redis [f]
(let [redis-storage (atom {})]
(with-redefs [redis/set (fn [key value] (swap! redis-storage assoc key value))
redis/get (fn [key] (get @redis-storage key))]
(f))))
@erez-rabih
erez-rabih / user_authentication.clj
Created April 21, 2019 06:16
user authentication module
(ns user-authentication)
(defn get-user-by-token [token]
(if-let [user-id (redis/get token)]
(first (db/select users (where {:id user-id})))
:unauthorized))
@erez-rabih
erez-rabih / user_authentication_test.clj
Last active April 21, 2019 11:32
user auth test with redis mock
(deftest get-user-by-token-test
(let [user-fix (db/insert users {:id 1 :name "username"})]
(testing "authorized token"
(redis/set "authorized-token" 1)
(testing "should return its respective user from the database"
(is (= user-fix (get-user-by-token "authorized-token")))))
(testing "unauthorized token"
(testing "should return unauthorized"
(is (= :unauthorized (get-user-by-token "unauthorized-token")))))))
(def millis-in-year (* 1000 60 60 24 365))
(defn serve-vodka [user-id]
(let [user (user/find-by-id user-id)
millis-since-born (- (System/currentTimeMillis) (:birthtime user))
years-since-born (/ millis-since-born millis-in-year)]
(if (>= years-since-born 21)
"Here's your vodka!"
"Too Young!")))