<commit_before>require 'spec_helper'

describe "ZObject" do

  integration_test do

    after do
      # Delete the account to cleanup in case a test failed.
      @account.delete if @account
      @child.delete if @child
    end

    it "can be created, queried, updated, and destroyed" do

      # Test failed creation.
      @account = Z::Account.create
      expect(@account.new_record?).to be_truthy
      expect(@account.errors).to be_present

      # Test creation.
      @account = Z::Account.new(
        :name => "ZObject Integration Test Account",
        :currency => Tenant.currency,
        :status => "Draft",
        :bill_cycle_day => 1)
      expect(@account.changes).to be_present
      expect(@account.save).to be_truthy
      expect(@account.new_record?).to be_falsey
      expect(@account.errors).to be_blank
      expect(@account.changes).to be_blank

      # Test update.
      @account.name = "ZObject Integration Test Account 2"
      expect(@account.changes).to be_present
      expect(@account.save).to be_truthy
      expect(@account.changes).to be_blank

      # Test querying.
      expect(Z::Account.where(:name => "Some Random Name").all).to_not include(@account)
      expect(Z::Account.where(:name => "Some Random Name").or(:name => @account.name).all).to include(@account)
      expect(Z::Account.where(:created_date => { ">=" => Date.yesterday }).all).to include(@account)
      expect(Z::Account.where(:created_date => { ">" => Time.now }).or(:name => @account.name).all).to include(@account)
      Z::Account.where(:created_date => { ">=" => Date.today }).find_each do |account|
        expect(account).to be_present
      end

      # Test ordering
      unordered = Z::Account.where(:created_date => { ">=" => Date.today })
      ordered = unordered.order(:name, :desc)
      expect(unordered.order_attribute).to eq(:created_date)
      expect(unordered.order_direction).to eq(:asc)
      expect(ordered.order_attribute).to eq(:name)
      expect(ordered.order_direction).to eq(:desc)

      # Test scopes and chaining.
      Z::Account.instance_eval do
        scope :draft, :status => "Draft"
        scope :active, where(:status => "Active")
        scope :since, lambda { |datetime| where(:created_date => { ">=" => datetime }) }
      end
      expect(Z::Account.select(:id).draft.to_zql).to eq("select Id from Account where Status = 'Draft'")
      expect(Z::Account.select(:id).active.since(Date.new 2012).to_zql).to eq("select Id from Account where Status = 'Active' and CreatedDate >= '2012-01-01T00:00:00+08:00'")

      # Update all.
      expect(Z::Account.where(:name => @account.name).update_all(:name => "ZObject Integration Test Account 3")).to eq(1)
      expect(@account.reload.name).to eq("ZObject Integration Test Account 3")
      # Block-style update_all
      expect(Z::Account.where(:name => @account.name).update_all { |account| account.name += "4" }).to eq(1)
      expect(@account.reload.name).to eq("ZObject Integration Test Account 34")
      # No changes, so no records were updated.
      expect(Z::Account.where(:name => @account.name).update_all(:name => "ZObject Integration Test Account 34")).to eq(0)

      # Associations
      @child = Z::Account.create!(
        :parent_id => @account.id,
        :name => "ZObject Integration Test Child Account",
        :currency => Tenant.currency,
        :status => "Draft",
        :bill_cycle_day => 1)
      expect(@account.children).to include(@child)
      expect(@child.parent).to eq(@account)
      # Make sure that the has_many pre-loads the inverse relationship.
      @account.children.each { |child| expect(child.parent_loaded?).to be_truthy }

      # Testing batch creates
      batch_accounts = []
      batch_number = 55
      (1..batch_number).each do |i|
        batch_accounts << Z::Account.new(
          :name => @account.name,
          :currency => Tenant.currency,
          :status => "Draft",
          :bill_cycle_day => 1)
      end
      expect(Z::Account.save(batch_accounts)).to eq(batch_number)

      # Delete all
      expect(Z::Account.where(:name => @account.name).delete_all).to be >= 1
      @account = nil

    end

  end
end<commit_msg>Fix integration test by calling to_a since the rspec expect syntax doesn't seem to trigger that.<commit_after>require 'spec_helper'

describe "ZObject" do

  integration_test do

    after do
      # Delete the account to cleanup in case a test failed.
      @account.delete if @account
      @child.delete if @child
    end

    it "can be created, queried, updated, and destroyed" do

      # Test failed creation.
      @account = Z::Account.create
      expect(@account.new_record?).to be_truthy
      expect(@account.errors).to be_present

      # Test creation.
      @account = Z::Account.new(
        :name => "ZObject Integration Test Account",
        :currency => Tenant.currency,
        :status => "Draft",
        :bill_cycle_day => 1)
      expect(@account.changes).to be_present
      expect(@account.save).to be_truthy
      expect(@account.new_record?).to be_falsey
      expect(@account.errors).to be_blank
      expect(@account.changes).to be_blank

      # Test update.
      @account.name = "ZObject Integration Test Account 2"
      expect(@account.changes).to be_present
      expect(@account.save).to be_truthy
      expect(@account.changes).to be_blank

      # Test querying.
      expect(Z::Account.where(:name => "Some Random Name").all.to_a).to_not include(@account)
      expect(Z::Account.where(:name => "Some Random Name").or(:name => @account.name).all.to_a).to include(@account)
      expect(Z::Account.where(:created_date => { ">=" => Date.yesterday }).all.to_a).to include(@account)
      expect(Z::Account.where(:created_date => { ">" => Time.now }).or(:name => @account.name).all.to_a).to include(@account)
      Z::Account.where(:created_date => { ">=" => Date.today }).find_each do |account|
        expect(account).to be_present
      end

      # Test ordering
      unordered = Z::Account.where(:created_date => { ">=" => Date.today })
      ordered = unordered.order(:name, :desc)
      expect(unordered.order_attribute).to eq(:created_date)
      expect(unordered.order_direction).to eq(:asc)
      expect(ordered.order_attribute).to eq(:name)
      expect(ordered.order_direction).to eq(:desc)

      # Test scopes and chaining.
      Z::Account.instance_eval do
        scope :draft, :status => "Draft"
        scope :active, where(:status => "Active")
        scope :since, lambda { |datetime| where(:created_date => { ">=" => datetime }) }
      end
      expect(Z::Account.select(:id).draft.to_zql).to eq("select Id from Account where Status = 'Draft'")
      expect(Z::Account.select(:id).active.since(Date.new 2012).to_zql).to eq("select Id from Account where Status = 'Active' and CreatedDate >= '2012-01-01T00:00:00+08:00'")

      # Update all.
      expect(Z::Account.where(:name => @account.name).update_all(:name => "ZObject Integration Test Account 3")).to eq(1)
      expect(@account.reload.name).to eq("ZObject Integration Test Account 3")
      # Block-style update_all
      expect(Z::Account.where(:name => @account.name).update_all { |account| account.name += "4" }).to eq(1)
      expect(@account.reload.name).to eq("ZObject Integration Test Account 34")
      # No changes, so no records were updated.
      expect(Z::Account.where(:name => @account.name).update_all(:name => "ZObject Integration Test Account 34")).to eq(0)

      # Associations
      @child = Z::Account.create!(
        :parent_id => @account.id,
        :name => "ZObject Integration Test Child Account",
        :currency => Tenant.currency,
        :status => "Draft",
        :bill_cycle_day => 1)
      expect(@account.children.to_a).to include(@child)
      expect(@child.parent).to eq(@account)
      # Make sure that the has_many pre-loads the inverse relationship.
      @account.children.each { |child| expect(child.parent_loaded?).to be_truthy }

      # Testing batch creates
      batch_accounts = []
      batch_number = 55
      (1..batch_number).each do |i|
        batch_accounts << Z::Account.new(
          :name => @account.name,
          :currency => Tenant.currency,
          :status => "Draft",
          :bill_cycle_day => 1)
      end
      expect(Z::Account.save(batch_accounts)).to eq(batch_number)

      # Delete all
      expect(Z::Account.where(:name => @account.name).delete_all).to be >= 1
      @account = nil

    end

  end
end
{'fraction_non_alphanumeric': 0.14509296305012945, 'fraction_numerical': 0.009413979759943516, 'mean_word_length': 3.031783681214421, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 87, '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': '3081984', 'n_tokens_mistral': 2641, 'n_tokens_neox': 2515, 'n_words': 580}