Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Introduce datastore and test-utils namespaces, with test-utils now pr…

…oviding dstest macro for out-of-appserver testing of datastore stuff.
  • Loading branch information...
commit 432c6a01ad7735131bb0637451bbc19a32c3a949 1 parent 9ba833c
@duelinmarkers authored
View
23 build.xml
@@ -4,6 +4,7 @@
<property name="classes.dir" value="classes" />
<property name="lib.dir" value="lib" />
<property name="src.dir" value="src" />
+ <property name="test.dir" value="test" />
<property name="jar.file" value="appengine-clj.jar" />
<path id="project.classpath">
@@ -30,10 +31,30 @@
<classpath path="${src.dir}" />
<sysproperty key="clojure.compile.path" value="${classes.dir}" />
<arg value="appengine-clj.users" />
+ <arg value="appengine-clj.datastore" />
+ <arg value="appengine-clj.test-utils" />
</java>
</target>
- <target name="jar" depends="compile">
+ <target name="repl">
+ <java classname="clojure.main" classpathref="project.classpath" fork="true" failonerror="true">
+ <classpath path="${src.dir}" />
+ <classpath path="${test.dir}" />
+ </java>
+ </target>
+
+ <target name="test" depends="compile">
+ <java classname="clojure.main" classpathref="project.classpath" fork="true" failonerror="true">
+ <classpath path="${test.dir}" />
+ <arg value="-e" />
+ <arg value="
+ (use 'clojure.contrib.test-is)
+ (require 'appengine-clj.datastore-test)
+ (run-tests 'appengine-clj.datastore-test)" />
+ </java>
+ </target>
+
+ <target name="jar" depends="compile,test">
<jar jarfile="${jar.file}" basedir="${classes.dir}" />
</target>
View
12 src/appengine_clj/datastore.clj
@@ -0,0 +1,12 @@
+(ns appengine-clj.datastore)
+
+
+(defn entity-to-map
+ "Converts an instance of com.google.appengine.api.datastore.Entity
+ to a PersistentHashMap with properties stored under keyword keys,
+ plus the entity's kind stored under :kind."
+ [entity]
+ (reduce #(assoc %1 (keyword (key %2)) (val %2))
+ {:kind (.getKind entity)}
+ (.entrySet (.getProperties entity))))
+
View
28 src/appengine_clj/test_utils.clj
@@ -0,0 +1,28 @@
+(ns appengine-clj.test-utils
+ (:require [clojure.contrib.test-is :as test-is])
+ (:import
+ (com.google.appengine.tools.development ApiProxyLocalFactory)
+ (com.google.appengine.api.datastore.dev LocalDatastoreService)
+ (com.google.apphosting.api ApiProxy)))
+
+
+(defn ds-setup []
+ (let [proxy-factory (doto (ApiProxyLocalFactory.) (.setApplicationDirectory (java.io.File. ".")))
+ api-proxy (doto (.create proxy-factory) (.setProperty LocalDatastoreService/NO_STORAGE_PROPERTY "true"))]
+ (ApiProxy/setDelegate api-proxy))
+ (ApiProxy/setEnvironmentForCurrentThread
+ (proxy [com.google.apphosting.api.ApiProxy$Environment] []
+ (getAppId [] "test")
+ (getVersionId [] "1.0"))))
+
+(defn ds-teardown []
+ (ApiProxy/clearEnvironmentForCurrentThread)
+ (.stop (ApiProxy/getDelegate)))
+
+(defmacro dstest [name & body]
+ `(test-is/deftest ~name
+ (ds-setup)
+ (try
+ ~@body
+ (finally (ds-teardown)))))
+
View
16 test/appengine_clj/datastore_test.clj
@@ -0,0 +1,16 @@
+(ns appengine-clj.datastore-test
+ (:require [appengine-clj.datastore :as ds])
+ (:use
+ clojure.contrib.test-is
+ appengine-clj.test-utils)
+ (:import (com.google.appengine.api.datastore Entity DatastoreServiceFactory)))
+
+
+(dstest converts-entity-to-persistent-map
+ (let [entity (doto (Entity. "MyKind")
+ (.setProperty "foo" "Foo")
+ (.setProperty "bar" "Bar"))]
+ (.put (DatastoreServiceFactory/getDatastoreService) entity)
+ (is (= {:foo "Foo" :bar "Bar" :kind "MyKind" :key (.getKey entity)}
+ (ds/entity-to-map entity)))))
+

0 comments on commit 432c6a0

Please sign in to comment.
Something went wrong with that request. Please try again.