Skip to content

Step #7 : Improve Entity syntax

mandubian edited this page Nov 5, 2012 · 5 revisions
  "Datomic" should {
    "accept syntactic sugar" in {
      import Datomic._
      import DatomicData._

      implicit val uri = "datomic:mem://datomicschemaspec"

      DatomicBootstrap(uri)
      println("created DB with uri %s: %s".format(uri, createDatabase(uri)))

      val person = new Namespace("person") {
        val character = Namespace("person.character")
      }

      val weak = AddIdent(Keyword(person.character, "weak"))
      val dumb = AddIdent(Keyword(person.character, "dumb"))

      val id = DId(Partition.USER)

      Datomic.addEntity(id)(
        person / "name" -> "toto",
        person / "age" -> 30L,
        person / "character" -> Seq(weak, dumb)
      ) must beEqualTo(
        AddEntity(id)(
          Keyword(person, "name") -> DString("toto"),
          Keyword(person, "age") -> DLong(30L),
          Keyword(person, "character") -> DSeq(weak.ident, dumb.ident)
        )
      )

      Datomic.addEntity(id)(
        KW(":person/name") -> "toto",
        KW(":person/age") -> 30L,
        KW(""":person/character""") -> Seq(weak, dumb)
      ) must beEqualTo(
        AddEntity(id)(
          Keyword(person, "name") -> DString("toto"),
          Keyword(person, "age") -> DLong(30L),
          Keyword(person, "character") -> DSeq(weak.ident, dumb.ident)
        )
      )

      Datomic.addEntity("""{
        :db/id $id
        :person/name "toto"
        :person/age 30
        :person/character [ $weak $dumb ]
      }""") must beEqualTo(
        AddEntity(id)(
          Keyword(person, "name") -> DString("toto"),
          Keyword(person, "age") -> DLong(30L),
          Keyword(person, "character") -> DSeq(weak.ident, dumb.ident)
        )
      )

      Datomic.transact("""{
        :db/id ${DId(Partition.USER)}
        :person/name "toto"
        :person/age 30
        :person/character [ $weak $dumb ]
      }""").map{ tx =>
        println("Provisioned data... TX:%s".format(tx))
      }.recover{
        case e => println(e.getMessage)
      }

      success
    }
  }
}