<commit_before>require "spec_helper"

describe "Spira resources" do

  before :all do
    
    class ::CDs < RDF::Vocabulary('http://example.org/')
      property :artist
      property :cds
      property :artists
      property :has_cd
    end
    
    class ::CD < Spira::Base
      configure :base_uri => CDs.cds
      property :name,   :predicate => DC.title,   :type => String
      property :artist, :predicate => CDs.artist, :type => 'Artist'
    end
    
    class ::Artist < Spira::Base
      configure :base_uri => CDs.artists
      property :name, :predicate => DC.title, :type => String
      has_many :cds, :predicate => CDs.has_cd, :type => :CD
    end
  end

  context "when referencing relationships" do
    context "in the root namespace" do
      before :all do
        class ::RootNSTest < Spira::Base
          property :name, :predicate => DC.title, :type => 'RootNSTest'
        end
        Spira.add_repository!(:default, RDF::Repository.new)
      end

      it "should find a class based on the string version of the name" do
        test = RootNSTest.new
        subject = test.subject
        test.name = RootNSTest.new
        test.name.save!
        test.save!
        
        test = subject.as(RootNSTest)
        test.name.should be_a RootNSTest
      end
    end

    context "in the same namespace" do
      before :all do
        module ::NSTest
          class X < Spira::Base
            property :name, :predicate => DC.title, :type => 'Y'
          end
          class Y < Spira::Base
          end
        end
        Spira.add_repository!(:default, RDF::Repository.new)
      end

      it "should find a class based on the string version of the name" do
        test = NSTest::X.new
        subject = test.subject
        test.name = NSTest::Y.new

        test.save!

        test = NSTest::X.for(subject)
        test.name.should be_a NSTest::Y
      end
    end

    context "in another namespace" do
      before :all do
        module ::NSTest
          class Z < Spira::Base
            property :name, :predicate => DC.title, :type => 'NSTestA::A'
          end
        end
        module ::NSTestA
          class A < Spira::Base
          end
        end
      end 

      it "should find a class based on the string version of the name" do
        test = NSTest::Z.new
        subject = test.subject
        test.name = NSTestA::A.new

        test.save!

        test = NSTest::Z.for(subject)
        test.name.should be_a NSTestA::A
      end
    end
  end


  context "with a one-to-many relationship" do
  
    before :each do
      require 'rdf/ntriples'
      @cds_repository = RDF::Repository.load(fixture('relations.nt'))
      Spira.add_repository(:default, @cds_repository)
      @cd = CD.for 'nevermind'
      @artist = Artist.for 'nirvana'
    end

    it "should find a cd" do
      @cd.should be_a CD
    end

    it "should find the artist" do
      @artist.should be_a Artist
    end

    it "should find an artist for a cd" do
      @cd.artist.should be_a Artist
    end

    it "should find the correct artist for a cd" do
      @cd.artist.uri.should == @artist.uri
    end

    it "should find CDs for an artist" do
      cds = @artist.cds
      cds.should be_a Set
      cds.find { |cd| cd.name == 'Nevermind' }.should be_true
      cds.find { |cd| cd.name == 'In Utero' }.should be_true
    end

    it "should not reload an object for a simple reverse relationship" do
      @artist.cds.first.artist.should equal @artist
      artist_cd = @cd.artist.cds.find { | list_cd | list_cd.uri == @cd.uri }
      @cd.should equal artist_cd
    end

    it "should find a model object for a uri" do
      @cd.artist.should == @artist
    end

    it "should make a valid statement referencing the assigned objects URI" do
      @kurt = Artist.for('<NAME>')
      @cd.artist = @kurt
      statement = @cd.query(:predicate => CDs.artist).first
      statement.subject.should == @cd.uri
      statement.predicate.should == CDs.artist
      statement.object.should == @kurt.uri
    end

  end

  context "with invalid relationships" do

    before :all do
      @invalid_repo = RDF::Repository.new
      Spira.add_repository(:default, @invalid_repo)
    end<commit_msg>no more global caching, sorry<commit_after>require "spec_helper"

describe "Spira resources" do

  before :all do
    
    class ::CDs < RDF::Vocabulary('http://example.org/')
      property :artist
      property :cds
      property :artists
      property :has_cd
    end
    
    class ::CD < Spira::Base
      configure :base_uri => CDs.cds
      property :name,   :predicate => DC.title,   :type => String
      property :artist, :predicate => CDs.artist, :type => 'Artist'
    end
    
    class ::Artist < Spira::Base
      configure :base_uri => CDs.artists
      property :name, :predicate => DC.title, :type => String
      has_many :cds, :predicate => CDs.has_cd, :type => :CD
    end
  end

  context "when referencing relationships" do
    context "in the root namespace" do
      before :all do
        class ::RootNSTest < Spira::Base
          property :name, :predicate => DC.title, :type => 'RootNSTest'
        end
        Spira.add_repository!(:default, RDF::Repository.new)
      end

      it "should find a class based on the string version of the name" do
        test = RootNSTest.new
        subject = test.subject
        test.name = RootNSTest.new
        test.name.save!
        test.save!
        
        test = subject.as(RootNSTest)
        test.name.should be_a RootNSTest
      end
    end

    context "in the same namespace" do
      before :all do
        module ::NSTest
          class X < Spira::Base
            property :name, :predicate => DC.title, :type => 'Y'
          end
          class Y < Spira::Base
          end
        end
        Spira.add_repository!(:default, RDF::Repository.new)
      end

      it "should find a class based on the string version of the name" do
        test = NSTest::X.new
        subject = test.subject
        test.name = NSTest::Y.new

        test.save!

        test = NSTest::X.for(subject)
        test.name.should be_a NSTest::Y
      end
    end

    context "in another namespace" do
      before :all do
        module ::NSTest
          class Z < Spira::Base
            property :name, :predicate => DC.title, :type => 'NSTestA::A'
          end
        end
        module ::NSTestA
          class A < Spira::Base
          end
        end
      end 

      it "should find a class based on the string version of the name" do
        test = NSTest::Z.new
        subject = test.subject
        test.name = NSTestA::A.new

        test.save!

        test = NSTest::Z.for(subject)
        test.name.should be_a NSTestA::A
      end
    end
  end


  context "with a one-to-many relationship" do
  
    before :each do
      require 'rdf/ntriples'
      @cds_repository = RDF::Repository.load(fixture('relations.nt'))
      Spira.add_repository(:default, @cds_repository)
      @cd = CD.for 'nevermind'
      @artist = Artist.for 'nirvana'
    end

    it "should find a cd" do
      @cd.should be_a CD
    end

    it "should find the artist" do
      @artist.should be_a Artist
    end

    it "should find an artist for a cd" do
      @cd.artist.should be_a Artist
    end

    it "should find the correct artist for a cd" do
      @cd.artist.uri.should == @artist.uri
    end

    it "should find CDs for an artist" do
      cds = @artist.cds
      cds.should be_a Set
      cds.find { |cd| cd.name == 'Nevermind' }.should be_true
      cds.find { |cd| cd.name == 'In Utero' }.should be_true
    end

    it "should not reload an object for a simple reverse relationship" do
      pending "no longer applies, but the current implementation should be reconsidered"

      @artist.cds.first.artist.should equal @artist
      artist_cd = @cd.artist.cds.find { | list_cd | list_cd.uri == @cd.uri }
      @cd.should equal artist_cd
    end

    it "should find a model object for a uri" do
      @cd.artist.should == @artist
    end

    it "should make a valid statement referencing the assigned objects URI" do
      @kurt = Artist.for('<NAME>')
      @cd.artist = @kurt
      statement = @cd.query(:predicate => CDs.artist).first
      statement.subject.should == @cd.uri
      statement.predicate.should == CDs.artist
      statement.object.should == @kurt.uri
    end

  end

  context "with invalid relationships" do

    before :all do
      @invalid_repo = RDF::Repository.new
      Spira.add_repository(:default, @invalid_repo)
    end
{'fraction_non_alphanumeric': 0.09899603081951903, 'fraction_numerical': 0.0, 'mean_word_length': 2.069509136510211, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 39, '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': '5249704', 'n_tokens_mistral': 2757, 'n_tokens_neox': 2690, 'n_words': 820}