<commit_before>;   Copyright (c) <NAME>. All rights reserved.
;   The use and distribution terms for this software are covered by the
;   Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;   which can be found in the file LICENSE at the root of this distribution.
;   By using this software in any fashion, you are agreeing to be bound by
;   the terms of this license.
;   You must not remove this notice, or any other, from this software.


(ns asphalt.core-test
  (:require
    [clojure.test :refer :all]
    [asphalt.test-util :as u]
    [asphalt.core      :as a]
    [asphalt.param     :as p]
    [asphalt.result    :as r]
    [asphalt.type      :as t]
    [asphalt.transaction :as x])
  (:import
    [java.sql Date SQLTimeoutException]
    [clojure.lang ExceptionInfo]))


(defn test-fixture
  [f]
  (u/create-db)
  (f)
  (u/drop-db))


(use-fixtures :each test-fixture)


;"CREATE TABLE emp
;id     INT PRIMARY KEY AUTO_INCREMENT,
;name   VARCHAR(50) NOT NULL,
;salary INT NOT NULL,
;dept   VARCHAR(50),
;j_date DATE NOT NULL"


;; ----- templates -----


(a/defsql t-count  "SELECT ^int COUNT(*) FROM emp")

(a/defsql t-insert "INSERT INTO emp (name, salary, dept, j_date)
VALUES (^string $name, ^int $salary, ^string $dept, ^date $joined)")

(a/defsql t-select "SELECT ^string name, -- ^int age,
-- ^boolean gender,
^int salary, ^string dept, ^date j_date FROM emp")

(a/defsql t-crosstype "SELECT ^string j_date FROM emp")

(a/defsql t-selfew "SELECT ^string name, ^int salary, ^string dept, ^date j_date FROM emp WHERE name = ?")

(a/defsql t-qfetch "SELECT ^string name, ^int salary, ^string dept, ^date j_date FROM emp WHERE name = ?"
  {:make-conn-worker (constantly (partial a/query a/fetch-single-row))})

(a/defsql t-update "UPDATE emp SET salary = ^int $new-salary WHERE dept = ^string $dept")

(a/defsql t-delete "DELETE FROM emp")

(a/defsql t-batch-update "UPDATE emp SET salary = $new-salary WHERE id = $id")


;; ----- tests -----


(deftest test-crosstype
  (let [jd1 (u/make-date)
        vs1 ["<NAME>" 100000 "Accounts" jd1]
        row (zipmap [:name :salary :dept :joined] vs1)]
    (let [generated-key (a/genkey u/ds t-insert row)]
      (is (= 1 generated-key) "Verify that insertion generated a key"))
    (is (= 1 (a/query a/fetch-single-value
               u/ds t-count [])) "Verify that row was inserted")
    (is (= (str jd1) (a/query a/fetch-single-value u/ds t-crosstype [])))
    (is (= [(str jd1)] (a/query a/fetch-single-row u/ds t-crosstype [])))
    (is (= [[(str jd1)]] (a/query a/fetch-rows u/ds t-crosstype [])))))


(deftest test-params
  (let [jd1 (u/make-date)
        vs1 ["<NAME>" 100000 "Accounts" jd1]
        row (zipmap [:name :salary :dept :joined] vs1)
        vs2 ["<NAME>" 110000 "Accounts" jd1]
        upa {:new-salary 110000
             :dept "Accounts"}
        upb [false "Accounts"]  ; bad params, first param should be int
        upv [110000 "Accounts"]]
    ;; create
    (let [params-setter (fn [sql-source prepared-statement params]
                          (p/lay-params prepared-statement [:name   :string<commit_msg>add cross-type test for update (param-setting)<commit_after>;   Copyright (c) <NAME>. All rights reserved.
;   The use and distribution terms for this software are covered by the
;   Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;   which can be found in the file LICENSE at the root of this distribution.
;   By using this software in any fashion, you are agreeing to be bound by
;   the terms of this license.
;   You must not remove this notice, or any other, from this software.


(ns asphalt.core-test
  (:require
    [clojure.test :refer :all]
    [asphalt.test-util :as u]
    [asphalt.core      :as a]
    [asphalt.param     :as p]
    [asphalt.result    :as r]
    [asphalt.type      :as t]
    [asphalt.transaction :as x])
  (:import
    [java.sql Date SQLTimeoutException]
    [clojure.lang ExceptionInfo]))


(defn test-fixture
  [f]
  (u/create-db)
  (f)
  (u/drop-db))


(use-fixtures :each test-fixture)


;"CREATE TABLE emp
;id     INT PRIMARY KEY AUTO_INCREMENT,
;name   VARCHAR(50) NOT NULL,
;salary INT NOT NULL,
;dept   VARCHAR(50),
;j_date DATE NOT NULL"


;; ----- templates -----


(a/defsql t-count  "SELECT ^int COUNT(*) FROM emp")

(a/defsql t-insert "INSERT INTO emp (name, salary, dept, j_date)
VALUES (^string $name, ^int $salary, ^string $dept, ^date $joined)")

(a/defsql t-select "SELECT ^string name, -- ^int age,
-- ^boolean gender,
^int salary, ^string dept, ^date j_date FROM emp")

(a/defsql t-crosstype-select "SELECT ^string j_date FROM emp")
(a/defsql t-crosstype-update "UPDATE emp SET j_date = ^string $jd")

(a/defsql t-selfew "SELECT ^string name, ^int salary, ^string dept, ^date j_date FROM emp WHERE name = ?")

(a/defsql t-qfetch "SELECT ^string name, ^int salary, ^string dept, ^date j_date FROM emp WHERE name = ?"
  {:make-conn-worker (constantly (partial a/query a/fetch-single-row))})

(a/defsql t-update "UPDATE emp SET salary = ^int $new-salary WHERE dept = ^string $dept")

(a/defsql t-delete "DELETE FROM emp")

(a/defsql t-batch-update "UPDATE emp SET salary = $new-salary WHERE id = $id")


;; ----- tests -----


(deftest test-crosstype
  (let [jd1 (u/make-date)
        vs1 ["Jo<NAME>" 100000 "Accounts" jd1]
        row (zipmap [:name :salary :dept :joined] vs1)]
    (let [generated-key (a/genkey u/ds t-insert row)]
      (is (= 1 generated-key) "Verify that insertion generated a key"))
    ;; retrieval test
    (is (= 1 (a/query a/fetch-single-value
               u/ds t-count [])) "Verify that row was inserted")
    (is (= (str jd1) (a/query a/fetch-single-value u/ds t-crosstype-select [])))
    (is (= [(str jd1)] (a/query a/fetch-single-row u/ds t-crosstype-select [])))
    (is (= [[(str jd1)]] (a/query a/fetch-rows u/ds t-crosstype-select [])))
    ;; update test
    (let [strdate "2016-02-01"]
      (t-crosstype-update u/ds [strdate])
      (is (= strdate (a/query a/fetch-single-value u/ds t-crosstype-select [])))
      (is (= [strdate] (a/query a/fetch-single-row u/ds t-crosstype-select [])))
      (is (= [[strdate]] (a/query a/fetch-rows u/ds t-crosstype-select []))))))


(deftest test-params
  (let [jd1 (u/make-date)
        vs1 ["<NAME>" 100000 "Accounts" jd1]
        row (zipmap [:name :salary :dept :joined] vs1)
        vs2 ["<NAME>" 110000 "Accounts" jd1]
        upa {:new-salary 110000
             :dept "Accounts"}
        upb [false "Accounts"]  ; bad params, first param should be int
        upv [110000 "Accounts"]]
    ;; create
    (let [params-setter (fn [sql-source prepared-statement params]
                          (p/lay-params prepared-statement [:name   :string
{'fraction_non_alphanumeric': 0.15055043141922048, 'fraction_numerical': 0.016959238321927996, 'mean_word_length': 2.9523809523809526, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2730928', 'n_tokens_mistral': 2540, 'n_tokens_neox': 2403, 'n_words': 756}