Skip to content

Commit

Permalink
Added counter with unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
David Krisch committed Nov 13, 2011
1 parent e2548f3 commit ea5d3cd
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 9 deletions.
9 changes: 8 additions & 1 deletion README.md
Expand Up @@ -3,6 +3,8 @@
A link shortener written as a learning tool for clojure, ring, noir,
drift (migrations), and hsqldb

This is very much a work in progress. There is nothing functional about it, yet.

## Prerequisites

1. Download hsqldb 2.2.5
Expand All @@ -22,12 +24,17 @@ drift (migrations), and hsqldb
```bash
lein deps
```
2. Run the tests

```bash
lein test
```
2. Run the migrations

```bash
lein migrate -version 1
```
3. Download dependencies and run the server
3. Run the server

```bash
lein run
Expand Down
4 changes: 2 additions & 2 deletions src/config/migrate_config.clj
Expand Up @@ -3,7 +3,7 @@
(:use [link-shortener.db :only (db)]))

(defn
#^{:doc "Get the current schema migration number"}
^{:doc "Get the current schema migration number"}
db-version []
(try
(sql/with-connection db ;; Pull the version number from the schema_migrations table
Expand All @@ -13,7 +13,7 @@
(catch Exception _ 0))) ;; If there is an exception, return version=0

(defn
#^{:doc "Set the current schema migration version"}
^{:doc "Set the current schema migration version"}
update-db-version [new-version]
(if (not= 0 new-version)
(sql/with-connection db
Expand Down
25 changes: 25 additions & 0 deletions src/link_shortener/counter.clj
@@ -0,0 +1,25 @@
(ns link-shortener.counter)

(def
^{:private true}
alpha "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")

(def alpha-length (.length alpha))

(defn allButLast [s]
(if (= (.length s) 0)
""
(.substring s 0 (- (.length s) 1))))

(defn nextInAlpha [c]
(let [index (inc (.indexOf alpha (str c)))
index (if (= alpha-length index) 0 index)]
(str (.charAt alpha index))))

(defn n [s]
(loop [f (allButLast s)
l (nextInAlpha (last s))
acc ""]
(if (not= l "0")
(str f l acc)
(recur (allButLast f) (nextInAlpha (last f)) (str l acc)))))
10 changes: 4 additions & 6 deletions src/migrations/001_schema_migrations.clj
Expand Up @@ -3,29 +3,27 @@
(:use [link-shortener.db :only (db)]))

(defn
#^{:doc "Create the schema_migrations table"}
^{:doc "Create the schema_migrations table"}
create-migrations-table []
(sql/create-table
:schema_migrations
[:version :int]))

(defn
#^{:doc "Drop the schema_migrations table"}
^{:doc "Drop the schema_migrations table"}
drop-migrations-table []
(try
(sql/drop-table :schema_migrations)
(catch Exception _)))

(defn
#^{:doc "Migrates the database up to version 1."}
^{:doc "Migrates the database up to version 1."}
up []
(println "migrations.001-schema-migrations up...")
(sql/with-connection db
(sql/transaction (create-migrations-table))))

(defn
#^{:doc "Migrates the database down from version 1."}
^{:doc "Migrates the database down from version 1."}
down []
(println "migrations.001-schema-migrations down...")
(sql/with-connection db
(sql/transaction (drop-migrations-table))))
22 changes: 22 additions & 0 deletions test/link_shortener/test/counter.clj
@@ -0,0 +1,22 @@
(ns link-shortener.test.counter
(:use [link-shortener.counter])
(:use clojure.test))

(deftest test_nextInAlpha
(is (= (nextInAlpha "z") "A") "Should have been A.")
(is (= (nextInAlpha "Z") "0") "Should have been 0.")
(is (= (nextInAlpha "0") "1") "Should have been 1.")
(is (= (nextInAlpha \Z) "0") "Didn't handle char correctly.")
(is (= (nextInAlpha "") "1") "Didn't handle emptry string correctly."))

(deftest test_allButLast
(is (= (allButLast "asdf") "asd") "Why didn't it return asd?")
(is (= (allButLast "a") "") "This shouldn't return anything.")
(is (= (allButLast "") "") "This shouldn't return anything."))

(deftest test_multichar
(is (= (n "ab") "ac") "Why didn't it return ac?")
(is (= (n "Z") "10") "Why didn't it return 10?")
(is (= (n "ZZ") "100") "Why didn't it return 100?")
(is (= (n "zZ") "A0") "Why didn't it return A0?")
(is (= (n "asdfZZ") "asdg00") "Why didn't it return asdg00?"))

0 comments on commit ea5d3cd

Please sign in to comment.