<commit_before>require File.join(File.dirname(__FILE__), "..", "spec_helper.rb")

describe 'queries', :queries => true do
  before :each do
    Redis.new.flushall
    Ampere.connect
    
    class Motorcycle < Ampere::Model
      field :make
      field :model
      field :year
    end
    
    @bike = Motorcycle.create make: "Honda", model: "CB400", year: "1990"
  end
  
  ###
  
  it 'should openly refuse to set one field that does not exist' do
    (->{@bike.update_attribute :this_field_does_not_exist, "This is never gonna get stored."}).should raise_error
  end
  
  it 'should be able to update one field atomically' do
    @bike.model.should == "CB400"
    @bike.update_attribute :model, "CB450SC"
    @bike.model.should == "CB450SC"
    @bike.reload.model.should == "CB450SC"
    
    @bike.year.should == "1990"
    @bike.update_attribute :year, "1986"
    @bike.year.should == "1986"
    @bike.reload
    @bike.year.should == "1986"
  end
  
  it 'should be able to update multiple fields atomically' do
    @bike.update_attributes model: "CB750",
                             year:  "1996"
    @bike.model.should == "CB750"
    @bike.year.should == "1996"
    @bike.reload.model.should == "CB750"
    @bike.reload.year.should == "1996"
  end
  
  ###
  
  after :all do
    Redis.new.flushall
    Ampere.connect
  end
end<commit_msg>Made a little clearer that the update_attributes function isn't yet atomic.<commit_after>require File.join(File.dirname(__FILE__), "..", "spec_helper.rb")

describe 'queries', :queries => true do
  before :each do
    Redis.new.flushall
    Ampere.connect
    
    class Motorcycle < Ampere::Model
      field :make
      field :model
      field :year
    end
    
    @bike = Motorcycle.create make: "Honda", model: "CB400", year: "1990"
  end
  
  ###
  
  it 'should be able to update one field atomically' do
    @bike.model.should == "CB400"
    @bike.update_attribute :model, "CB450SC"
    @bike.model.should == "CB450SC"
    @bike.reload.model.should == "CB450SC"
    
    @bike.year.should == "1990"
    @bike.update_attribute :year, "1986"
    @bike.year.should == "1986"
    @bike.reload
    @bike.year.should == "1986"
  end
  
  it 'should be able to update multiple fields atomically' do
    @bike.update_attributes model: "CB750",
                             year:  "1996"
    @bike.model.should == "CB750"
    @bike.year.should == "1996"
    @bike.reload.model.should == "CB750"
    @bike.reload.year.should == "1996"
  end

  it 'should openly refuse to set fields that do not exist' do
    (->{
      @bike.update_attribute :this_field_does_not_exist, "Lorem ipsum dolor sit amet."
    }).should raise_error
    (->{
      # TODO Once this operation is made atomic, this test needs to be updated to also
      # include keys that do exist, in various orders. Even if real fields are included,
      # no fields should be updated if any part of this query would fail. To emulate this
      # behaviour in the meantime, possibly check all keys of attributes hash to see if 
      # they exist before performing the queries or synchronize during.
      @bike.update_attributes this_field_does_not_exist: "Ut enim ad minim veniam.",
                              neither_does_this_one:     "Duis aute irure dolor in."
    }).should raise_error
  end
  
  ###
  
  after :all do
    Redis.new.flushall
    Ampere.connect
  end
end
{'fraction_non_alphanumeric': 0.11199528440907751, 'fraction_numerical': 0.03300913645741232, 'mean_word_length': 2.6416309012875536, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3280182', 'n_tokens_mistral': 1206, 'n_tokens_neox': 1111, 'n_words': 324}