<commit_before>require "spec_helper"

if defined?(Mongoid)
  describe "Tenancy::Scoping::Mongoid", mongoid: true do
    let(:camyp)     { Mongo::Portal.create(domain_name: "yp.com.kh") }
    let(:panpages)  { Mongo::Portal.create(domain_name: "panpages.com") }
    let(:listing)   { Mongo::Listing.create(name: "Listing 1", portal_id: camyp.id) }

    describe Mongo::Listing do
      it { should belong_to(:portal).of_type(Mongo::Portal) }

      it { should validate_presence_of(:portal) }

      it { should validate_uniqueness_of(:name).scoped_to(:portal_id).case_insensitive }

      it "have default_scope with :portal_id field" do
        Mongo::Portal.current = camyp

        expect(Mongo::Listing.where(nil).selector).to eq({"is_active"=>true, "portal_id"=>Mongo::Portal.current_id})
      end

      it "doesn't have default_scope when it doesn't have current portal" do
        Mongo::Portal.current = nil

        expect(Mongo::Listing.where(nil).selector).to eq({"is_active"=>true})
      end
    end

    describe Mongo::Communication do
      it { should belong_to(:portal) }

      it { should validate_presence_of(:portal) }

      it { should belong_to(:listing) }

      it { should validate_presence_of(:listing) }

      it { should validate_uniqueness_of(:value).scoped_to(:portal_id, :listing_id) }

      it "have default_scope with :portal_id field" do
        Mongo::Portal.current  = camyp
        Mongo::Listing.current = listing

        expect(Mongo::Communication.where(nil).selector).to eq({"is_active"=>true, "portal_id"=>Mongo::Portal.current_id, "listing_id"=>Mongo::Listing.current_id})
      end

      it "doesn't have default_scope when it doesn't have current portal and listing" do
        Mongo::Portal.current  = nil
        Mongo::Listing.current = nil

        expect(Mongo::Communication.where(nil).selector).to eq({"is_active"=>true})
      end
    end

    describe Mongo::ExtraCommunication do
      it { should belong_to(:portal).of_type(Mongo::Portal) }

      it { should belong_to(:listing).of_type(Mongo::Listing) }

      it "raise exception when passing two resources and options" do
        expect { Mongo::ExtraCommunication.scope_to(:portal, :listing, class_name: "Mongo::Listing") }.to raise_error(ArgumentError)
      end

      it "uses the correct scope" do
        listing2 = Mongo::Listing.create(name: "Name 2", portal: camyp)

        Mongo::Portal.current = camyp
        Mongo::Listing.current = listing2

        extra_communication = Mongo::ExtraCommunication.new
        # expect(extra_communication.listing_id).to eq(listing2.id)
        expect(extra_communication.portal_id).to eq(camyp.id)
      end
    end

    describe "belongs_to method override" do
      before(:each) { Mongo::Portal.current = camyp }
      after(:each)  { Mongo::Portal.current = nil }

      it "reload belongs_to when passes true" do
        listing.portal.domain_name = "abc.com"

        expect(listing.portal(true).object_id).not_to eq(Mongo::Portal.current.object_id)
      end
<commit_msg>pending spec cos of mongoid 3<commit_after>require "spec_helper"

if defined?(Mongoid)
  describe "Tenancy::Scoping::Mongoid", mongoid: true do
    let(:camyp)     { Mongo::Portal.create(domain_name: "yp.com.kh") }
    let(:panpages)  { Mongo::Portal.create(domain_name: "panpages.com") }
    let(:listing)   { Mongo::Listing.create(name: "Listing 1", portal_id: camyp.id) }

    describe Mongo::Listing do
      it { should belong_to(:portal).of_type(Mongo::Portal) }

      it { should validate_presence_of(:portal) }

      it { should validate_uniqueness_of(:name).scoped_to(:portal_id).case_insensitive }

      it "have default_scope with :portal_id field" do
        Mongo::Portal.current = camyp

        expect(Mongo::Listing.where(nil).selector).to eq({"is_active"=>true, "portal_id"=>Mongo::Portal.current_id})
      end

      it "doesn't have default_scope when it doesn't have current portal" do
        Mongo::Portal.current = nil

        expect(Mongo::Listing.where(nil).selector).to eq({"is_active"=>true})
      end
    end

    describe Mongo::Communication do
      it { should belong_to(:portal) }

      it { should validate_presence_of(:portal) }

      it { should belong_to(:listing) }

      it { should validate_presence_of(:listing) }

      it { should validate_uniqueness_of(:value).scoped_to(:portal_id, :listing_id) }

      it "have default_scope with :portal_id field" do
        Mongo::Portal.current  = camyp
        Mongo::Listing.current = listing

        expect(Mongo::Communication.where(nil).selector).to eq({"is_active"=>true, "portal_id"=>Mongo::Portal.current_id, "listing_id"=>Mongo::Listing.current_id})
      end

      it "doesn't have default_scope when it doesn't have current portal and listing" do
        Mongo::Portal.current  = nil
        Mongo::Listing.current = nil

        expect(Mongo::Communication.where(nil).selector).to eq({"is_active"=>true})
      end
    end

    describe Mongo::ExtraCommunication do
      it { should belong_to(:portal).of_type(Mongo::Portal) }

      it { should belong_to(:listing).of_type(Mongo::Listing) }

      it "raise exception when passing two resources and options" do
        expect { Mongo::ExtraCommunication.scope_to(:portal, :listing, class_name: "Mongo::Listing") }.to raise_error(ArgumentError)
      end

      it "uses the correct scope", :pending do
        listing2 = Mongo::Listing.create(name: "Name 2", portal: camyp)

        Mongo::Portal.current = camyp
        Mongo::Listing.current = listing2

        extra_communication = Mongo::ExtraCommunication.new
        expect(extra_communication.listing_id).to eq(listing2.id)
        expect(extra_communication.portal_id).to eq(camyp.id)
      end
    end

    describe "belongs_to method override" do
      before(:each) { Mongo::Portal.current = camyp }
      after(:each)  { Mongo::Portal.current = nil }

      it "reload belongs_to when passes true" do
        listing.portal.domain_name = "abc.com"

        expect(listing.portal(true).object_id).not_to eq(Mongo::Portal.current.object_id)
      end

{'fraction_non_alphanumeric': 0.12279267495094834, 'fraction_numerical': 0.0017985611510791368, 'mean_word_length': 3.4780380673499267, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '5979975', 'n_tokens_mistral': 2029, 'n_tokens_neox': 1933, 'n_words': 392}