Skip to content

Instantly share code, notes, and snippets.

@grzm
Last active February 12, 2016 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grzm/3d17f353224ce329855f to your computer and use it in GitHub Desktop.
Save grzm/3d17f353224ce329855f to your computer and use it in GitHub Desktop.
(ns om-tutorial.X-Query-Exercises
(:require-macros
[cljs.test :refer [is]])
(:require [om.next :as om]
[om-tutorial.queries.query-editing :as qe]
[devcards.core :as dc :refer-macros [defcard defcard-doc deftest]]))
(def bird-db
{:birds/by-id {1 {:bird/id 1 :bird/name "Robin"}
2 {:bird/id 2 :bird/name "Blackbird"}
3 {:bird/id 3 :bird/name "Big Bird"}}
:birds [[:birds/by-id 1]
[:birds/by-id 2]
[:birds/by-id 3]]
:observations [{:bird [:birds/by-id 2]
:observation/at #inst "2016-02-10T19:00-05:00"}
{:bird [:birds/by-id 1]
:observation/at #inst "2016-02-10T18:00-05:00"}]})
(def desired-ui-data
{:birds [{:bird/id 1 :bird/name "Robin"}
{:bird/id 2 :bird/name "Blackbird"}
{:bird/id 3 :bird/name "Big Bird"}]
:observations [{:bird {:bird/id 2 :bird/name "Blackbird"}
:observation/at #inst "2016-02-10T19:00-05:00"}
{:bird {:bird/id 1 :bird/name "Robin"}
:observation/at #inst "2016-02-10T18:00-05:00"}]})
(defcard bird-db-example
"
Query editor for bird-db
"
qe/query-editor
{:query "[{:birds [:bird/id :bird/name]}
{:observations [{:bird [:bird/id :bird/name]}
:observation/at]}]"
:query-result {}
;; observed results
;; {:birds [{:bird/id 1 :bird/name "Robin"}
;; {:bird/id 2 :bird/name "Blackbird"}
;; {:bird/id 3 :bird/name "Big Bird"}]
;; :observations [{} {}]}
:db bird-db
:id "query-example-1"}
{:inspect-data false})
(deftest bird-db-query-test
"Trying to figure out normalization with augmented/composite components"
(is (= {:birds [{:bird/id 1 :bird/name "Robin"}
{:bird/id 2 :bird/name "Blackbird"}
{:bird/id 3 :bird/name "Big Bird"}]
:observations [{:bird {:bird/id 2 :bird/name "Blackbird"}
:observation/at #inst "2016-02-10T19:00-05:00"}
{:bird {:bird/id 1 :bird/name "Robin"}
:observation/at #inst "2016-02-10T18:00-05:00"}]}
(om/db->tree [{:birds [:bird/id :bird/name]}
{:observations [{:bird [:bird/id :bird/name]} :observation/at]}]
bird-db bird-db))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment