<commit_before>(ns metabase.api.meta.field-test
  (:require [expectations :refer :all]
            [metabase.db :refer :all]
            (metabase.models [field :refer [Field]]
                             [field-values :refer [FieldValues]]
                             [table :refer [Table]])
            [metabase.test.data :refer :all]
            [metabase.test.data.users :refer :all]
            [metabase.test.util :refer [match-$ expect-eval-actual-first]]))



;; ## GET /api/meta/field/:id
(expect
    (match-$ (Field (id :users :name))
      {:description nil
       :table_id (id :users)
       :table (match-$ (Table (id :users))
                {:description nil
                 :entity_type nil
                 :db (match-$ (db)
                       {:created_at $
                        :engine "h2"
                        :id $
                        :updated_at $
                        :name "Test Database"
                        :organization_id nil
                        :description nil})
                 :name "USERS"
                 :display_name "Users"
                 :rows 15
                 :updated_at $
                 :entity_name nil
                 :active true
                 :id (id :users)
                 :db_id (db-id)
                 :created_at $})
       :special_type "category" ; metabase.driver.generic-sql.sync/check-for-low-cardinality should have marked this as such because it had no other special_type
       :name "NAME"
       :display_name "Name"
       :updated_at $
       :active true
       :id (id :users :name)
       :field_type "info"
       :position 0
       :preview_display true
       :created_at      $
       :base_type       "TextField"
       :parent_id       nil
       :parent          nil})
  ((user->client :rasta) :get 200 (format "meta/field/%d" (id :users :name))))


;; ## GET /api/meta/field/:id/summary
(expect [["count" 75]      ; why doesn't this come back as a dictionary ?
         ["distincts" 75]]
  ((user->client :rasta) :get 200 (format "meta/field/%d/summary" (id :categories :name))))


;; ## PUT /api/meta/field/:id
;; Check that we can update a Field
;; TODO - this should NOT be modifying a field from our test data, we should create new data to mess with
(expect-eval-actual-first
    (match-$ (let [field (Field (id :venues :latitude))]
               ;; this is sketchy. But return the Field back to its unmodified state so it won't affect other unit tests
               (upd Field (id :venues :latitude) :special_type "latitude")
               ;; match against the modified Field
               field)
             {:description     nil
              :table_id        (id :venues)
              :special_type    "fk"
              :name            "LATITUDE"
              :display_name    nil
              :updated_at      $
              :active          true
              :id              $
              :field_type      "info"
              :position        0
              :preview_display true
              :created_at      $
              :base_type       "FloatField"
              :parent_id       nil
              :parent          nil})
  ((user->client :crowberto) :put 200 (format "meta/field/%d" (id :venues :latitude)) {:special_type :fk}))

(defn- field->field-values
  "Fetch the `FieldValues` object that corresponds to a given `Field`."
  [table-kw field-kw]
  (sel :one FieldValues :field_id (id table-kw field-kw)))

;; ## GET /api/meta/field/:id/values<commit_msg>expect "Latitude" display_name<commit_after>(ns metabase.api.meta.field-test
  (:require [expectations :refer :all]
            [metabase.db :refer :all]
            (metabase.models [field :refer [Field]]
                             [field-values :refer [FieldValues]]
                             [table :refer [Table]])
            [metabase.test.data :refer :all]
            [metabase.test.data.users :refer :all]
            [metabase.test.util :refer [match-$ expect-eval-actual-first]]))



;; ## GET /api/meta/field/:id
(expect
    (match-$ (Field (id :users :name))
      {:description nil
       :table_id (id :users)
       :table (match-$ (Table (id :users))
                {:description nil
                 :entity_type nil
                 :db (match-$ (db)
                       {:created_at $
                        :engine "h2"
                        :id $
                        :updated_at $
                        :name "Test Database"
                        :organization_id nil
                        :description nil})
                 :name "USERS"
                 :display_name "Users"
                 :rows 15
                 :updated_at $
                 :entity_name nil
                 :active true
                 :id (id :users)
                 :db_id (db-id)
                 :created_at $})
       :special_type "category" ; metabase.driver.generic-sql.sync/check-for-low-cardinality should have marked this as such because it had no other special_type
       :name "NAME"
       :display_name "Name"
       :updated_at $
       :active true
       :id (id :users :name)
       :field_type "info"
       :position 0
       :preview_display true
       :created_at      $
       :base_type       "TextField"
       :parent_id       nil
       :parent          nil})
  ((user->client :rasta) :get 200 (format "meta/field/%d" (id :users :name))))


;; ## GET /api/meta/field/:id/summary
(expect [["count" 75]      ; why doesn't this come back as a dictionary ?
         ["distincts" 75]]
  ((user->client :rasta) :get 200 (format "meta/field/%d/summary" (id :categories :name))))


;; ## PUT /api/meta/field/:id
;; Check that we can update a Field
;; TODO - this should NOT be modifying a field from our test data, we should create new data to mess with
(expect-eval-actual-first
    (match-$ (let [field (Field (id :venues :latitude))]
               ;; this is sketchy. But return the Field back to its unmodified state so it won't affect other unit tests
               (upd Field (id :venues :latitude) :special_type "latitude")
               ;; match against the modified Field
               field)
             {:description     nil
              :table_id        (id :venues)
              :special_type    "fk"
              :name            "LATITUDE"
              :display_name    "Latitude"
              :updated_at      $
              :active          true
              :id              $
              :field_type      "info"
              :position        0
              :preview_display true
              :created_at      $
              :base_type       "FloatField"
              :parent_id       nil
              :parent          nil})
  ((user->client :crowberto) :put 200 (format "meta/field/%d" (id :venues :latitude)) {:special_type :fk}))

(defn- field->field-values
  "Fetch the `FieldValues` object that corresponds to a given `Field`."
  [table-kw field-kw]
  (sel :one FieldValues :field_id (id table-kw field-kw)))

;; ## GET /api/meta/field/:id/values
{'fraction_non_alphanumeric': 0.11148936170212766, 'fraction_numerical': 0.005106382978723404, 'mean_word_length': 1.4888810448288035, 'pattern_counts': {'":': 0, '<': 3, '<?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': '3937561', 'n_tokens_mistral': 2003, 'n_tokens_neox': 1954, 'n_words': 573}