<commit_before>require "spec_helper"

describe "attribute handling" do
  before :all do
    class AttributeTestOilRig < Kalimba::Resource
      type "http://schema.org/OilRig"
      base_uri "http://example.org/oil_rigs"

      property :safe, :predicate => "http://example.org/safe", :datatype => NS::XMLSchema["boolean"]
      property :name, :predicate => "http://example.org/name", :datatype => NS::XMLSchema["string"]
      property :local_time, :predicate => "http://example.org/time", :datatype => NS::XMLSchema["time"]
      property :created_at, :predicate => "http://example.org/date", :datatype => NS::XMLSchema["date"]
    end
  end

  let(:rig) { AttributeTestOilRig.new }

  describe "boolean value" do
    subject { rig.safe }

    context "when not set" do
      it { should be_a NilClass }
    end

    context "when set to 'false'" do
      before { rig.safe = false }

      it { should be_a FalseClass }
    end
  end

  describe "multiparameter attribute" do
    describe "localized string" do<commit_msg>failing specs for localized string assignment<commit_after>require "spec_helper"

describe "attribute handling" do
  before :all do
    class AttributeTestOilRig < Kalimba::Resource
      type "http://schema.org/OilRig"
      base_uri "http://example.org/oil_rigs"

      property :safe, :predicate => "http://example.org/safe", :datatype => NS::XMLSchema["boolean"]
      property :name, :predicate => "http://example.org/name", :datatype => NS::XMLSchema["string"]
      property :local_time, :predicate => "http://example.org/time", :datatype => NS::XMLSchema["time"]
      property :created_at, :predicate => "http://example.org/date", :datatype => NS::XMLSchema["date"]
    end
  end

  let(:rig) { AttributeTestOilRig.for("bp1") }

  describe "boolean value" do
    subject { rig.safe }

    context "when not set" do
      it { should be_a NilClass }
    end

    context "when set to 'false'" do
      before { rig.safe = false }

      it { should be_a FalseClass }
    end
  end

  describe "localized string" do
    before do
      # populate the storage with literals in different locales
      { en: "Quarter Pounder with Cheese",
        fr: "Le Big Mac" }.each do |lang, text|
        Kalimba.repository.statements.create(subject: rig.subject,
                                             predicate: rig.class.properties["name"][:predicate],
                                             object: text.with_lang(lang))
      end
    end

    subject { rig.name }

    context "when retrieved" do
      it "should be retrieved in the language of the current locale" do
        I18n.with_locale(:fr) do
          expect(subject.lang).to eql :fr
        end

        I18n.with_locale(:en) do
          expect(subject.lang).to eql :en
        end
      end
    end

    context "when stored" do
      it "should not be overwritten by a localized string in another language" do
        rig.update_attributes(name: "Burger")

        s1 = Redlander::Statement.new(subject: rig.subject,
                                      predicate: rig.class.properties["name"][:predicate],
                                      object: "Quarter Pounder with Cheese".with_lang(:en))
        s2 = Redlander::Statement.new(subject: rig.subject,
                                      predicate: rig.class.properties["name"][:predicate],
                                      object: "Le Big Mac".with_lang(:fr))
        s3 = Redlander::Statement.new(subject: rig.subject,
                                      predicate: rig.class.properties["name"][:predicate],
                                      object: "Burger")

        Kalimba.repository.statements.to_a.should include s1
        Kalimba.repository.statements.to_a.should include s2
      end
    end
  end

  describe "multiparameter attribute" do
    describe "localized string" do
{'fraction_non_alphanumeric': 0.11586314152410575, 'fraction_numerical': 0.002592016588906169, 'mean_word_length': 2.335350043215212, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6797287', 'n_tokens_mistral': 1139, 'n_tokens_neox': 1090, 'n_words': 291}