# encoding: UTF-8 # Copyright 2011 innoQ Deutschland GmbH # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. require File.join(File.expand_path(File.dirname(__FILE__)), '../test_helper') require 'iqvoc/skos_importer' class SkosImportTest < ActiveSupport::TestCase setup do Iqvoc::Concept.pref_labeling_class_name = 'Labeling::SKOS::PrefLabel' Iqvoc.config.register_setting("languages.pref_labeling", ["de", "en"]) Iqvoc.config.register_setting("languages.further_labelings.Labeling::SKOS::AltLabel", ["de", "en"]) end TEST_DATA = (<<-DATA . "Tier"@de . "Animal"@en . "Viehzeug"@de . "Ein Tier ist kein Mensch."@de . . . "Kuh"@de . "Cow"@en . "Rind"@de . . . "Esel"@de . "Donkey"@en . . "Affe"@de . "Monkey"@en . "\u00C4ffle"@de . "Ape"@en . . . . . DATA ).split("\n") test "unicode json decoding trick" do encoded_val = "\\u00C4ffle" decoded_val = JSON.parse(%Q{["#{encoded_val}"]})[0].gsub("\\n", "\n") assert_equal decoded_val, "Äffle" end test "basic_importer_functionality" do assert_difference('Concept::SKOS::Base.count', 4) do Iqvoc::SkosImporter.new(TEST_DATA, "http://www.example.com/") end concepts = {} ["animal", "cow", "donkey", "monkey"].each do |origin| concepts[origin] = Iqvoc::Concept.base_class.by_origin(origin).last assert_not_nil(concepts[origin], "Couldn't find concept '#{origin}'.") assert concepts[origin].published?, "Concept '#{origin}' wasn't published." end assert_equal "Animal", concepts["animal"].pref_label.to_s broader_relation = concepts["cow"].broader_relations.first assert_not_nil broader_relation assert_not_nil broader_relation.target assert_equal concepts["animal"].origin, broader_relation.target.origin narrower_relations = concepts["animal"].narrower_relations assert_equal 3, narrower_relations.count note = concepts["animal"].note_skos_definitions.first assert_not_nil note assert_equal "Ein Tier ist kein Mensch.", note.value match = concepts["monkey"].match_skos_exact_matches.first assert_not_nil match assert_equal "http://dbpedia.org/page/Monkey", match.value end test "incorrect origin" do assert_difference('Concept::SKOS::Base.count', 1) do Iqvoc::SkosImporter.new([" ."], "http://www.example.com/") end assert_nil Iqvoc::Concept.base_class.by_origin("1").last assert_not_nil Iqvoc::Concept.base_class.by_origin("_1").last end test "blank nodes" do test_data = (<<-DATA . "Car"@en . _:A01 . _:A01 "2012-02-13T08:56:13+01:00" . _:A01 "Arnulf Beckenbauer" . DATA ).split("\n") assert_difference('Note::SKOS::ChangeNote.count', 1) do Iqvoc::SkosImporter.new(test_data, "http://www.example.com/") end assert_difference('Note::Annotated::Base.count', 2) do Iqvoc::SkosImporter.new(test_data, "http://www.example.com/") end end test "notations" do test_data = (<<-DATA . "ME-IQ 1234"^^ . "12345"^^ . DATA ).split("\n") assert_difference('Notation::Base.count', 2) do Iqvoc::SkosImporter.new(test_data, "http://www.example.com/") end end test "top concepts for concept scheme" do test_data = (<<-DATA . . . . DATA ).split("\n") assert_difference('Concept::SKOS::Base.tops.count', 1) do Iqvoc::SkosImporter.new(test_data, "http://www.example.com/") end end end class SkosCollectionImportTest < ActiveSupport::TestCase setup do Iqvoc::Concept.pref_labeling_class_name = 'Labeling::SKOS::PrefLabel' Iqvoc.config.register_setting("languages.pref_labeling", ["de", "en"]) Iqvoc.config.register_setting("languages.further_labelings.Labeling::SKOS::AltLabel", ["de", "en"]) end TEST_DATA = (<<-DATA . "Landtier"@de . "Land animal"@en . . "Vierbeinige Tier"@de . "Four legged animal"@en . . "Kuh"@de . "Cow"@en . . "Schlange"@de . "Snake"@en . . "Esel"@de . "Donkey"@en . . . . . DATA ).split("\n") test "basic importer functionality" do assert_difference('Collection::Base.count', 2) do Iqvoc::SkosImporter.new(TEST_DATA, "http://www.example.com/") end concepts = {} ["cow", "donkey", "snake"].each do |origin| concepts[origin] = Iqvoc::Concept.base_class.by_origin(origin).last assert_not_nil(concepts[origin], "Couldn't find concept '#{origin}'.") assert concepts[origin].published?, "Concept '#{origin}' wasn't published." end collections = {} ["land-animal", "legged-animal"].each do |origin| collections[origin] = Iqvoc::Collection.base_class.by_origin(origin).last assert_not_nil(collections[origin], "Couldn't find collections '#{origin}'.") assert collections[origin].published?, "collections '#{origin}' wasn't published." end collection_with_member = concepts["cow"].collections.first assert_not_nil collection_with_member concept_member = collections["land-animal"].members.first assert_not_nil concept_member end test "subcollections importer functionality" do assert_difference('Collection::Base.count', 2) do Iqvoc::SkosImporter.new(TEST_DATA, "http://www.example.com/") end collection_with_subcollections = Iqvoc::Collection.base_class.by_origin("land-animal").last assert_not_nil collection_with_subcollections assert_not_nil collection_with_subcollections.subcollections.first end end