# # Some basic tests... feel free to add more :) # $:.unshift File.join(File.dirname(__FILE__),'..','lib') require 'test/unit' require 'rtm' class BaseTest < Test::Unit::TestCase # There are a bunch of global assertions in here so it cannot be parallelized # with other tests, that's why it is all in one. What can we do about that? def test_connect assert RTM.connect, "Connection to memory backend failed" assert_equal 0, RTM[].size, "There should be no topic maps in a newly created environment." base_locator = "http://rtm.rubyforge.org/tests/base_test" m = RTM.create base_locator assert_equal 1, RTM[].size, "After creation of a topic map, there should be one." assert_kind_of RTM::TopicMap, m, "RTM.create should create something which includes RTM::TopicMap" assert_equal base_locator, m.base_locator, "The base_locator should be initialized properly." assert m.respond_to?(:topics), "The topic map should respond to :topics" assert m.respond_to?(:associations), "The topic map should respond to :topics" assert_equal 0, m.topics.size, "A new topic map should have no topics." assert_equal 0, m.associations.size, "A new topic map should have no associations." assert m.reifier.nil?, "A new topic map should not be reified yet." assert_equal 0, m.item_identifiers.size, "A new topic map should " # assigning a reifier m.reifier = "a_topic_which_reifies_the_topic_map" assert_equal 1, m.topics.size, "A topic should have been created to reify the topic map." assert_kind_of RTM::Topic, m.reifier, "A reifier should be a Topic." assert_equal m, m.topics.first.reified, "The only topic out there should reify the map (not generally, but here)." assert_equal m.base_locator, m.topics.first.reified.base_locator, "The only topic out there should reify the map. Check base locator." assert_equal m.__getobj__, m.topics.first.reified.__getobj__, "The only topic out there should reify the map. Look inside." t = m.create_topic assert_equal 2, m.topics.size, "There should be 2 topics after creating the second one." assert_kind_of RTM::Topic, t, "The created topic should really be one." assert_kind_of RTM::TopicMap, m assert_equal m, t.parent, "The Topic Map should be the parent of our new topic." # The next tests may look a bit unneeded, but they at least also test the properties be available and initialized somehow. assert_equal 0, t.item_identifiers.size, "New topic has no item_identifiers." assert_equal 0, t.subject_identifiers.size, "New topic has no subject_identifiers." assert_equal 0, t.subject_locators.size, "New topic has no subject_locators." assert !t.valid?, "Without item identifiers nor subject identifiers the topic must not be valid." assert_equal 0, t.names.size, "New topic has no names." assert_equal 0, t.occurrences.size, "New topic has no occurrences." assert_equal 0, t.roles_played.size, "New topic plays no roles." assert_nil t.reified, "New topic does not reify a statement." n = t.create_name assert_equal 1, t.names.size, "The topic should have one name now." assert_kind_of RTM::TopicName, n assert_kind_of RTM::Topic, n.parent, "You won't believe but this should be a Topic and right now it not..." # TODO the following assertion does not work - it crashes pp... but why? #assert_equal t, n.parent, "The parent of the name should be the topic on which it was created." assert_equal 0, n.item_identifiers.size, "I wonder if someone will ever read that." assert_kind_of RTM::TopicName, n, "The topic name shoud be a TopicName... makes sence, he?" assert_equal 0, n.variants.size, "According to Lars Heuer and Robert Barta that should always be 0, not only now." assert_nil n.value, "We haven't set a value yet, where should it come from." assert_nil n.type, "I am really tired of writing this stuff." assert_equal 0, n.scope.size, "Should I really be more serious with this stuff?" assert_nil n.reifier, "No, I guess not... who could ever stand that." v = n.create_variant assert_equal 1, n.variants.size, "The topic should have one name now." assert_kind_of RTM::Variant, v assert_kind_of RTM::TopicName, v.parent, "You won't believe but this should be a Topic and right now it not..." # TODO the following assertion does not work - it crashes pp... but why? #assert_equal t, v.parent, "The parent of the name should be the topic on which it was created." assert_equal 0, v.item_identifiers.size, "I wonder if someone will ever read that." assert_kind_of RTM::Variant, v, "The variant should be a Variant... makes sence, he?" assert_nil v.value, "We haven't set a value yet, where should it come from." assert_nil v.datatype, "No datatype set, yet." assert_equal 0, v.scope.size, "Should I really be more serious with this stuff?" assert_nil v.reifier, "No, I guess not... who could ever stand that." assert !v.valid? o = t.create_occurrence assert_equal 1, t.occurrences.size, "The topic should have one name now." assert_kind_of RTM::Occurrence, o assert_kind_of RTM::Topic, o.parent, "You won't believe but this should be a Topic and right now it not..." # TODO the following assertion does not work - it crashes pp... but why? #assert_equal t, o.parent, "The parent of the name should be the topic on which it was created." assert_equal 0, o.item_identifiers.size, "I wonder if someone will ever read that." assert_kind_of RTM::Occurrence, o, "The occurrence shoud be a Occurrence... makes sence, he?" assert_nil o.value, "We haven't set a value yet, where should it come from." assert_nil o.type, "I am really tired of writing this stuff." assert_nil o.datatype assert_equal 0, n.scope.size, "Should I really be more serious with this stuff?" assert_nil n.reifier, "No, I guess not... who could ever stand that." a = m.create_association assert_nil a.type assert_equal 0, a.roles.size assert_kind_of RTM::Association, a assert_kind_of RTM::TopicMap, a.parent assert_equal m, a.parent assert_equal 0, a.scope.size assert !a.valid? r1 = a.create_role assert_nil r1.type assert_nil r1.player assert_kind_of RTM::AssociationRole, r1 assert_kind_of RTM::Association, r1.parent assert_equal 1, a.roles.size t.item_identifiers << "nr1" assert_equal 1, t.item_identifiers.size fii = t.item_identifiers.first assert_equal "http://rtm.rubyforge.org/tests/base_test#nr1", fii.to_s assert_kind_of RTM::Locator, fii assert_kind_of RTM::ItemIdentifier, fii #needed? # TODO: invalidate cache/reload value somehow #assert_equal 1, m.item_identifiers.size, "If you see this, it is probably because TopicMap#item_identifiers.size is cached which is wrong in this case." t.subject_identifiers << "si1" fsi = t.subject_identifiers.first assert_equal "si1", fsi.to_s assert_kind_of RTM::Locator, fsi assert_kind_of RTM::SubjectIdentifier, fsi #needed? t.subject_locators << "sl1" fsl = t.subject_locators.first assert_equal "sl1", fsl.to_s assert_kind_of RTM::Locator, fsl assert_kind_of RTM::SubjectLocator, fsl #needed? # create roles with topics # some more stuff. # to be in associations_test later: tm = RTM.create "urn:/associations_test" a1 = tm.create_association "urn:/assoc_type_with_absolute_uri" assert_not_nil a1.type assert_equal a1.type, tm.get("urn:/assoc_type_with_absolute_uri") a2 = tm.create_association :type => "urn:/assoc_type_with_absolute_uri_using_hash" assert_not_nil a2.type assert_equal a2.type, tm.get("urn:/assoc_type_with_absolute_uri_using_hash") a3 = tm.create_association "relative_type" assert_not_nil a3.type assert_equal a3.type, tm.get("relative_type") end def test_sugar_hash_access RTM.connect tm = RTM.create "a:b" bla = tm.get!("bla") bla["-name"] = "test" n = bla["-name"].first end end