 
     (testing "...with URI, user, password, content-base"
       (is (= "Hello world"
-             (let [session (create-session db)]
+             (with-open [session (create-session db)]
                (-> session
                    (.submitRequest (.newAdhocQuery session
                                                    "\"Hello world\""))

    ))




;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; Type tests
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; (def session
;;   (create-session {:uri "xdbc://localhost:8383/"
;;                    :user "rest-admin" :password "x"
;;                    :content-base "TutorialDB"}))

(deftest types
  (with-open [session (create-session {:uri "xdbc://localhost:8383/"
                                       :user "rest-admin" :password "x"
                                       :content-base "TutorialDB"})]
    (testing "Types roundtrip through XQuery with expected conversions"
      (testing "...JSON interface types"
        (testing "......array-node type"
          (is (= "array-node()"
                 (result->type (execute-xquery session "array-node {1,2,3}"
                                               {:types :raw}))))
          (is (= [1 2 3]
                 (execute-xquery session "array-node {1,2,3}"
                                 {:shape :single!}))))

        (testing "......boolean-node type"
          (is (= "boolean-node()"
                 (result->type (execute-xquery session "boolean-node{false()}"
                                               {:types :raw}))))
          (is (false? (execute-xquery session "boolean-node{false()}"
                                      {:shape :single!}))))

        ;; TODO produce and test JsonItem

        (testing "......null-node type"
          (is (= "null-node()"
                 (result->type (execute-xquery session "null-node {}"
                                               {:types :raw}))))
          (is (nil? (execute-xquery session "null-node {}"
                                    {:shape :single!}))))

        (testing "......number-node type"
          (is (= "number-node()"
                 (result->type (execute-xquery session "number-node {1}"
                                               {:types :raw}))))
          (is (= 1 (execute-xquery session "number-node {1}"
                                   {:shape :single!}))))

        (testing "......object-node type"
          (is (= "object-node()"
                 (result->type (execute-xquery session "xquery version \"1.0-ml\";
                                      let $object := json:object()
                                      let $_ := map:put($object,\"a\",111)
                                      return xdmp:to-json($object)" {:types :raw}))))
          (is (= {"a" 111}
                 (execute-xquery session "xquery version \"1.0-ml\";
                                      let $object := json:object()
                                      let $_ := map:put($object,\"a\",111)
                                      return xdmp:to-json($object)"
                                 {:shape :single!})))))

      
      (testing "...JSON types"
        (testing "......JSON array type"
          (is (= "json:array"
                 (result->type (execute-xquery session "xquery version \"1.0-ml\";
                            json:array(

        (is (= "xs:boolean"
               (result->type (execute-xquery session "xdmp:exists(collection())"
                                             {:types :raw}))))
        (is (true? (execute-xquery session "xdmp:exists(collection())" {:shape :single!})))))

    (testing "......XSDate"
      (with-open [session (create-session db)]
        (is (= "xs:date"
               (result->type (execute-xquery session "fn:adjust-date-to-timezone(xs:date(\"2002-03-07-07:00\"),())"
                                             {:types :raw}))))
        (is (= "2002-03-07"
               (execute-xquery session "fn:adjust-date-to-timezone(xs:date(\"2002-03-07-07:00\"),
                                                           ())"
                               {:shape :single!})))))

    (testing "......XSDateTime"
      (with-open [session (create-session db)]
        (is (= "xs:dateTime"
               (result->type (execute-xquery session "fn:adjust-dateTime-to-timezone(xs:dateTime(\"2002-03-07T10:00:00\"), ())"
                                             {:types :raw}))))
        (is (= "2002-03-07T10:00:00"
               (execute-xquery session "fn:adjust-dateTime-to-timezone(xs:dateTime(\"2002-03-07T10:00:00\"), ())"
                               {:shape :single!})))))

    (testing "......XSDayTimeDuration"
      (with-open [session (create-session db)]
        (is (= "xs:dayTimeDuration"
               (result->type (execute-xquery session "xquery version \"0.9-ml\" 
                                                  fn:subtract-dateTimes-yielding-dayTimeDuration(fn:adjust-dateTime-to-timezone(xs:dateTime(\"2002-03-07T10:00:00\"), ()), xs:dateTime(\"2000-01-11T12:01:00.000Z\"))" ;; this fn removed in version 1.0; only used to get correct response type
                                             {:types :raw}))))
        (is (= "P785DT20H59M"
               (execute-xquery session "xquery version \"0.9-ml\"
                                    fn:subtract-dateTimes-yielding-dayTimeDuration(fn:adjust-dateTime-to-timezone(xs:dateTime(\"2002-03-07T10:00:00\"),
                                                                                                                  ()),
                                                                                   xs:dateTime(\"2000-01-11T12:01:00.000Z\"))"
                               {:shape :single!})))))
    
    (testing "......XSDecimal"
      (with-open [session (create-session db)]
        (is (= "xs:decimal" (result->type (execute-xquery session "fn:abs(-1.2)"
                                                          {:types :raw}))))
        (is (= 1.2 (execute-xquery session "fn:abs(-1.2)" {:shape :single!})))))

    (testing "......XSDouble"
      (with-open [session (create-session db)]
        (is (= "xs:double" (result->type (execute-xquery session "fn:number(-1.2)"
                                                         {:types :raw}))))
        (is (= -1.2 (execute-xquery session "fn:number(-1.2)" {:shape :single!})))
        (is (= "xs:double" (result->type (execute-xquery session "xs:double(-1.2)"
                                                         {:types :raw}))))
        (is (= -1.2 (execute-xquery session "xs:double(-1.2)" {:shape :single!})))))

    (testing "......XSDuration"
      (with-open [session (create-session db)]
        (is (= "xs:duration"
               (result->type (execute-xquery session "xs:duration(\"P3DT10H\")"
                                             {:types :raw}))))
        (is (= "P3DT10H"
               (execute-xquery session "xs:duration(\"P3DT10H\")" {:shape :single!})))))

    (testing "......XSFloat"
      (with-open [session (create-session db)]
        (is (= "xs:float" (result->type (execute-xquery session "xs:float(\"1\")"
                                                        {:types :raw}))))
        (is (= 1 (execute-xquery session "xs:float(\"1\")" {:shape :single!})))))

    (testing "......Gregorians!"
      (testing ".........XSGDay"
        (with-open [session (create-session db)]
          (is (= "xs:gDay" (result->type (execute-xquery session "xs:gDay('---08')"
                                                         {:types :raw}))))
          (is (= "---08" (execute-xquery session "xs:gDay('---08')" {:shape :single!})))))

      (testing ".........XSGMonth"
        (with-open [session (create-session db)]
          (is (= "xs:gMonth"
                 (result->type (execute-xquery session "xs:gMonth('--08')" {:types :raw}))))
          (is (= "--08" (execute-xquery session "xs:gMonth('--08')" {:shape :single!})))))

      (testing ".........XSGMonthDay"
        (with-open [session (create-session db)]
          (is (= "xs:gMonthDay"
                 (result->type (execute-xquery session "xs:gMonthDay('--08-20')"
                                               {:types :raw}))))
          (is (= "--08-20"
                 (execute-xquery session "xs:gMonthDay('--08-20')" {:shape :single!})))))

      (testing ".........XSGYear"
        (with-open [session (create-session db)]
          (is (= "xs:gYear"
                 (result->type (execute-xquery session "xs:gYear('2016')"
                                               {:types :raw}))))
          (is (= "2016" (execute-xquery session "xs:gYear('2016')" {:shape :single!})))))

      (testing ".........XSGYearMonth"
        (with-open [session (create-session db)]
          (is (= "xs:gYearMonth"
                 (result->type (execute-xquery session "xs:gYearMonth('2016-02')"
                                               {:types :raw}))))
          (is (= "2016-02" (execute-xquery session "xs:gYearMonth('2016-02')"
                                           {:shape :single!}))))))

    (testing ".........XSHexBinary"
      (with-open [session (create-session db)]
        (is (= "xs:hexBinary"
               (result->type (execute-xquery session "xs:hexBinary(\"74657374\")"
                                             {:types :raw}))))
