|
| 1 | +(ns cmr.indexer.test.data.concepts.generic |
| 2 | + (:require |
| 3 | + [clojure.test :refer :all] |
| 4 | + [cmr.indexer.data.concepts.generic :as generic])) |
| 5 | + |
| 6 | +(def sample-citation-data |
| 7 | + {:RelatedIdentifiers [{:RelationshipType "Cites" |
| 8 | + :RelatedIdentifier "10.5067/MODIS/MOD08_M3.061" |
| 9 | + :RelatedIdentifierType "DOI"} |
| 10 | + {:RelationshipType "Describes" |
| 11 | + :RelatedIdentifier "ark:/13030/tf1p17542" |
| 12 | + :RelatedIdentifierType "ARK"}] |
| 13 | + :CitationMetadata {:Title "Global Climate Study" |
| 14 | + :Year 2021 |
| 15 | + :Author [{:Given "John" :Family "Smith" :ORCID "0000-0002-1825-0097"}]}}) |
| 16 | + |
| 17 | +(def sample-single-object-data |
| 18 | + {:CitationMetadata {:Title "Climate Research" |
| 19 | + :Year 2020}}) |
| 20 | + |
| 21 | +(def empty-data {}) |
| 22 | + |
| 23 | +(defn assert-collections-equal-unordered |
| 24 | + "Asserts two collections contain the same elements, ignoring order" |
| 25 | + [expected actual] |
| 26 | + (is (= (set expected) (set actual)))) |
| 27 | + |
| 28 | +(deftest field->index-complex-field-test |
| 29 | + (testing "Complex field indexer with field names in format" |
| 30 | + (let [settings {:Field ".CitationMetadata" |
| 31 | + :Name "Citation-Info" |
| 32 | + :Configuration {:sub-fields ["Title" "Year"] |
| 33 | + :format "%s=%s"}} |
| 34 | + result (generic/field->index-complex-field settings sample-single-object-data)] |
| 35 | + (is (= {:citation-info "Title=Climate Research, Year=2020" |
| 36 | + :citation-info-lowercase "title=climate research, year=2020"} |
| 37 | + result)))) |
| 38 | + |
| 39 | + (testing "Complex field with missing data" |
| 40 | + (let [settings {:Field ".NonExistentField" |
| 41 | + :Name "Missing-Field" |
| 42 | + :Configuration {:sub-fields ["Title" "Year"] |
| 43 | + :format "%s=%s"}} |
| 44 | + result (generic/field->index-complex-field settings sample-single-object-data)] |
| 45 | + (is (= {:missing-field "Title=null, Year=null" |
| 46 | + :missing-field-lowercase "title=null, year=null"} |
| 47 | + result))))) |
| 48 | + |
| 49 | +(deftest field->index-complex-field-with-values-only-test |
| 50 | + (testing "Complex field with values only, single object" |
| 51 | + (let [settings {:Field ".CitationMetadata" |
| 52 | + :Name "Citation-Values" |
| 53 | + :Configuration {:sub-fields ["Title" "Year"] |
| 54 | + :format "%s:%s"}} |
| 55 | + result (generic/field->index-complex-field-with-values-only settings sample-single-object-data)] |
| 56 | + (is (= {:citation-values "Climate Research:2020" |
| 57 | + :citation-values-lowercase "climate research:2020"} |
| 58 | + result)))) |
| 59 | + |
| 60 | + (testing "Complex field with values only, array data" |
| 61 | + (let [settings {:Field ".RelatedIdentifiers" |
| 62 | + :Name "Related-Identifier-With-Type" |
| 63 | + :Configuration {:sub-fields ["RelationshipType" "RelatedIdentifier"] |
| 64 | + :format "%s:%s"}} |
| 65 | + result (generic/field->index-complex-field-with-values-only settings sample-citation-data)] |
| 66 | + (is (= {:related-identifier-with-type ["Cites:10.5067/MODIS/MOD08_M3.061" |
| 67 | + "Describes:ark:/13030/tf1p17542"] |
| 68 | + :related-identifier-with-type-lowercase ["cites:10.5067/modis/mod08_m3.061" |
| 69 | + "describes:ark:/13030/tf1p17542"]} |
| 70 | + result)))) |
| 71 | + |
| 72 | + (testing "Complex field with empty array" |
| 73 | + (let [settings {:Field ".EmptyArray" |
| 74 | + :Name "Empty-Field" |
| 75 | + :Configuration {:sub-fields ["Title" "Year"] |
| 76 | + :format "%s:%s"}} |
| 77 | + result (generic/field->index-complex-field-with-values-only settings {:EmptyArray []})] |
| 78 | + (is (= {:empty-field [] |
| 79 | + :empty-field-lowercase []} |
| 80 | + result))))) |
| 81 | + |
| 82 | +(deftest field->index-simple-array-field-test |
| 83 | + (testing "Simple array field extracts values from array elements" |
| 84 | + (let [settings {:Field ".RelatedIdentifiers" |
| 85 | + :Name "Related-Identifier" |
| 86 | + :Configuration {:sub-fields ["RelatedIdentifier"]}} |
| 87 | + result (generic/field->index-simple-array-field settings sample-citation-data)] |
| 88 | + (assert-collections-equal-unordered |
| 89 | + ["10.5067/MODIS/MOD08_M3.061" "ark:/13030/tf1p17542"] |
| 90 | + (:related-identifier result)) |
| 91 | + (assert-collections-equal-unordered |
| 92 | + ["10.5067/modis/mod08_m3.061" "ark:/13030/tf1p17542"] |
| 93 | + (:related-identifier-lowercase result)))) |
| 94 | + |
| 95 | + (testing "Simple array field with multiple sub-fields" |
| 96 | + (let [settings {:Field ".RelatedIdentifiers" |
| 97 | + :Name "Relationship-Info" |
| 98 | + :Configuration {:sub-fields ["RelationshipType" "RelatedIdentifier"]}} |
| 99 | + result (generic/field->index-simple-array-field settings sample-citation-data)] |
| 100 | + (assert-collections-equal-unordered |
| 101 | + #{"Cites" "10.5067/MODIS/MOD08_M3.061" "Describes" "ark:/13030/tf1p17542"} |
| 102 | + (set (:relationship-info result))))) |
| 103 | + |
| 104 | + (testing "Simple array field with nested data" |
| 105 | + (let [settings {:Field ".CitationMetadata.Author" |
| 106 | + :Name "Author-Info" |
| 107 | + :Configuration {:sub-fields ["Given" "Family"]}} |
| 108 | + result (generic/field->index-simple-array-field settings sample-citation-data)] |
| 109 | + (assert-collections-equal-unordered ["John" "Smith"] (:author-info result)) |
| 110 | + (assert-collections-equal-unordered ["john" "smith"] (:author-info-lowercase result)))) |
| 111 | + |
| 112 | + (testing "Simple array field with empty data" |
| 113 | + (let [settings {:Field ".NonExistent" |
| 114 | + :Name "Missing-Array" |
| 115 | + :Configuration {:sub-fields ["Field1"]}} |
| 116 | + result (generic/field->index-simple-array-field settings empty-data)] |
| 117 | + (is (= {:missing-array [] |
| 118 | + :missing-array-lowercase []} |
| 119 | + result)))) |
| 120 | + |
| 121 | +(deftest field->index-default-field-test |
| 122 | + (testing "Default field indexer for simple values" |
| 123 | + (let [settings {:Field ".CitationMetadata.Title" |
| 124 | + :Name "Title"} |
| 125 | + result (generic/field->index-default-field settings sample-citation-data)] |
| 126 | + (is (= {:title "Global Climate Study" |
| 127 | + :title-lowercase "global climate study"} |
| 128 | + result)))) |
| 129 | + |
| 130 | + (testing "Default field indexer with nested path" |
| 131 | + (let [settings {:Field ".CitationMetadata.Author.0.Given" |
| 132 | + :Name "First-Author-Given"} |
| 133 | + result (generic/field->index-default-field settings sample-citation-data)] |
| 134 | + (is (= {:first-author-given "John" |
| 135 | + :first-author-given-lowercase "john"} |
| 136 | + result)))) |
| 137 | + |
| 138 | + (testing "Default field indexer with missing field" |
| 139 | + (let [settings {:Field ".NonExistent.Field" |
| 140 | + :Name "Missing-Field"} |
| 141 | + result (generic/field->index-default-field settings empty-data)] |
| 142 | + (is (= {:missing-field nil |
| 143 | + :missing-field-lowercase nil} |
| 144 | + result)))))) |
0 commit comments