<commit_before>(ns clojurewerkz.cassaforte.collections-test
  (:refer-clojure :exclude [update])
  (:require [clojurewerkz.cassaforte.test-helper :as th]
            [clojurewerkz.cassaforte.client :as client]
            [clojurewerkz.cassaforte.cql :as cql :refer :all]
            [clojure.test :refer :all]
            [clojurewerkz.cassaforte.query :refer :all]))

(let [s (client/connect ["127.0.0.1"])]
  (use-fixtures :each (fn [f]
                        (th/with-temporary-keyspace s f)))
  (deftest test-collection-conversion-on-load
    (let [t :users_collections]
      (create-table s t
                  (column-definitions
                   {:name :varchar
                    :test_map (map-type :varchar :varchar)
                    :test_set (set-type :int)
                    :test_list (list-type :varchar)
                    :primary-key [:name]}))

    (insert s t
            {:name "user1"
             :test_map {"a" "b" "c" "d"}
             :test_set #{1 2 3}
             :test_list ["clojure" "cassandra"]})
    (let [m  (-> (select s t) first)
          mv (:test_map m)
          ms (:test_set m)
          ml (:test_list m)]
      (is (map? mv))
      (is (= {"a" "b" "c" "d"} mv))
      (is (set? ms))
      (is (= #{1 2 3} ms))
      (is (vector? ml))
      (is (= ["clojure" "cassandra"] ml)))
    (truncate s t)
    (drop-table s t)))


    (deftest test-list-operations
      (create-table s :users_list
                    (column-definitions
                     {:name :varchar
                      :test_list (list-type :varchar)
                      :primary-key [:name]}))

      (testing "Inserting"
        (th/test-combinations
         (insert s :users_list
                 {:name "user1"
                  :test_list ["str1" "str2" "str3"]})
         (is (= ["str1" "str2" "str3"] (get-in (select s :users_list)
                                               [0 :test_list])))
         (truncate s :users_list)))

      (testing "Updating"
      (th/test-combinations
       (insert s :users_list
               {:name "user1"
                :test_list []})
       (dotimes [i 3]
         (update s :users_list
                 {:test_list [+ [(str "str" i)]]}
                 (where {:name "user1"})))

       (is (= ["str0" "str1" "str2"] (get-in (select s :users_list)
                                             [0 :test_list])))
       (truncate s :users_list)))

    (testing "Deleting"
      (th/test-combinations
       (insert s :users_list
               {:name "user1"
                :test_list ["str0" "str1" "str2"]})
       (update s :users_list
               {:test_list [- ["str0" "str1"]]}
               (where {:name "user1"}))

       (is (= ["str2"] (get-in (select s :users_list)
                               [0 :test_list])))))

    (drop-table s :users_list))

    (deftest test-map-operations
      (create-table s :users_map
                    (column-definitions
                     {:name :varchar
                      :test_map (map-type :varchar :varchar)
                      :primary-key [:name]}))

      (testing "Inserting"
        (th/test-combinations
         (insert s :users_map
                 {:name "user1"
                  :test_map {"a" "b" "c" "d"}})
         (is (= {"a" "b" "c" "d"} (get-in (select s :users_map)
                                          [0 :test_map])))
         (truncate s :users_map)))

      (testing "Updating"
        (th/test-combinations
         (insert s :users_map
                 {:name "user1"
                  :test_map {}})
         (dotimes [i 3]
           (update s :users_map
                   {:test_map [+ {"a" "b" "c" "d"}]}
                   (where {:name "user1"})))

         (is (= {"a" "b" "c" "d"} (get-in (select s :users_map)
                                          [0 :test_map])))
         (truncate s :users_map)))
      (drop-table s :users_map))
<commit_msg>Add a test that demonstrates how to map entries are deleted<commit_after>(ns clojurewerkz.cassaforte.collections-test
  (:refer-clojure :exclude [update])
  (:require [clojurewerkz.cassaforte.test-helper :as th]
            [clojurewerkz.cassaforte.client :as client]
            [clojurewerkz.cassaforte.cql :as cql :refer :all]
            [clojure.test :refer :all]
            [clojurewerkz.cassaforte.query :refer :all]))

(let [s (client/connect ["127.0.0.1"])]
  (use-fixtures :each (fn [f]
                        (th/with-temporary-keyspace s f)))
  (deftest test-collection-conversion-on-load
    (let [t :users_collections]
      (create-table s t
                  (column-definitions
                   {:name :varchar
                    :test_map (map-type :varchar :varchar)
                    :test_set (set-type :int)
                    :test_list (list-type :varchar)
                    :primary-key [:name]}))

    (insert s t
            {:name "user1"
             :test_map {"a" "b" "c" "d"}
             :test_set #{1 2 3}
             :test_list ["clojure" "cassandra"]})
    (let [m  (-> (select s t) first)
          mv (:test_map m)
          ms (:test_set m)
          ml (:test_list m)]
      (is (map? mv))
      (is (= {"a" "b" "c" "d"} mv))
      (is (set? ms))
      (is (= #{1 2 3} ms))
      (is (vector? ml))
      (is (= ["clojure" "cassandra"] ml)))
    (truncate s t)
    (drop-table s t)))


    (deftest test-list-operations
      (create-table s :users_list
                    (column-definitions
                     {:name :varchar
                      :test_list (list-type :varchar)
                      :primary-key [:name]}))

      (testing "Inserting"
        (th/test-combinations
         (insert s :users_list
                 {:name "user1"
                  :test_list ["str1" "str2" "str3"]})
         (is (= ["str1" "str2" "str3"] (get-in (select s :users_list)
                                               [0 :test_list])))
         (truncate s :users_list)))

      (testing "Updating"
      (th/test-combinations
       (insert s :users_list
               {:name "user1"
                :test_list []})
       (dotimes [i 3]
         (update s :users_list
                 {:test_list [+ [(str "str" i)]]}
                 (where {:name "user1"})))

       (is (= ["str0" "str1" "str2"] (get-in (select s :users_list)
                                             [0 :test_list])))
       (truncate s :users_list)))

    (testing "Deleting"
      (th/test-combinations
       (insert s :users_list
               {:name "user1"
                :test_list ["str0" "str1" "str2"]})
       (update s :users_list
               {:test_list [- ["str0" "str1"]]}
               (where {:name "user1"}))

       (is (= ["str2"] (get-in (select s :users_list)
                               [0 :test_list])))))

    (drop-table s :users_list))

    (deftest test-map-operations
      (create-table s :users_map
                    (column-definitions
                     {:name :varchar
                      :test_map (map-type :varchar :varchar)
                      :primary-key [:name]}))

      (testing "Inserting"
        (th/test-combinations
         (insert s :users_map
                 {:name "user1"
                  :test_map {"a" "b" "c" "d"}})
         (is (= {"a" "b" "c" "d"} (get-in (select s :users_map)
                                          [0 :test_map])))
         (truncate s :users_map)))

      (testing "Updating"
        (th/test-combinations
         (insert s :users_map
                 {:name "user1"
                  :test_map {}})
         (dotimes [i 3]
           (update s :users_map
                   {:test_map [+ {"a" "b" "c" "d"}]}
                   (where {:name "user1"})))

         (is (= {"a" "b" "c" "d"} (get-in (select s :users_map)
                                          [0 :test_map])))
         (truncate s :users_map)))

      (testing "Deleting"
        (th/test-combinations
         (insert s :users_map
                 {:name "user1"
                  :test_map {"a" "b" "c" "d"}})
         (delete s :users_map
                 (columns {:test_map "c"})
                 (where [[= :name "user1"]]))
         (is (= {"a" "b"} (get-in (select s :users_map)
                                  [0 :test_map])))
         (truncate s :users_map)))
      (drop-table s :users_map))

{'fraction_non_alphanumeric': 0.15008391273076002, 'fraction_numerical': 0.010668904339486933, 'mean_word_length': 1.3987924094307074, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 19, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3864640', 'n_tokens_mistral': 2803, 'n_tokens_neox': 2686, 'n_words': 654}