spec/lib/zermelo/associations/multiple_spec.rb in zermelo-1.3.0 vs spec/lib/zermelo/associations/multiple_spec.rb in zermelo-1.4.0

- old
+ new

@@ -210,17 +210,17 @@ let(:redis) { Zermelo.redis } module ZermeloExamples class AssociationsHasManyParentRedis - include Zermelo::Records::Redis + include Zermelo::Records::RedisSet has_many :children, :class_name => 'ZermeloExamples::AssociationsHasManyChildRedis', :inverse_of => :parent end class AssociationsHasManyChildRedis - include Zermelo::Records::Redis + include Zermelo::Records::RedisSet define_attributes :important => :boolean index_by :important belongs_to :parent, :class_name => 'ZermeloExamples::AssociationsHasManyParentRedis', :inverse_of => :children end @@ -669,20 +669,20 @@ let(:redis) { Zermelo.redis } module ZermeloExamples class AssociationsHasAndBelongsToManyPrimaryRedis - include Zermelo::Records::Redis + include Zermelo::Records::RedisSet define_attributes :active => :boolean index_by :active has_and_belongs_to_many :secondaries, :class_name => 'ZermeloExamples::AssociationsHasAndBelongsToManySecondaryRedis', :inverse_of => :primaries end class AssociationsHasAndBelongsToManySecondaryRedis - include Zermelo::Records::Redis + include Zermelo::Records::RedisSet # define_attributes :important => :boolean # index_by :important has_and_belongs_to_many :primaries, :class_name => 'ZermeloExamples::AssociationsHasAndBelongsToManyPrimaryRedis', :inverse_of => :secondaries @@ -771,23 +771,23 @@ let(:redis) { Zermelo.redis } module ZermeloExamples class AssociationsHasSortedSetParentRedis - include Zermelo::Records::Redis + include Zermelo::Records::RedisSet has_sorted_set :children, :class_name => 'ZermeloExamples::AssociationsHasSortedSetChildRedis', :inverse_of => :parent, :key => :timestamp has_sorted_set :reversed_children, :class_name => 'ZermeloExamples::AssociationsHasSortedSetChildRedis', :inverse_of => :reversed_parent, :key => :timestamp, :order => :desc end class AssociationsHasSortedSetChildRedis - include Zermelo::Records::Redis + include Zermelo::Records::RedisSortedSet define_attributes :emotion => :string, :timestamp => :timestamp + define_sort_attribute :timestamp index_by :emotion - range_index_by :timestamp belongs_to :parent, :class_name => 'ZermeloExamples::AssociationsHasSortedSetParentRedis', :inverse_of => :children belongs_to :reversed_parent, :class_name => 'ZermeloExamples::AssociationsHasSortedSetParentRedis', :inverse_of => :reversed_children end @@ -809,27 +809,25 @@ redis.hmset("#{ck}:#{attrs[:id]}:attrs", {:emotion => attrs[:emotion], 'timestamp' => attrs[:timestamp].to_f}.to_a.flatten) redis.sadd("#{ck}::indices:by_emotion:string:#{attrs[:emotion]}", attrs[:id]) - redis.zadd("#{ck}::indices:by_timestamp", attrs[:timestamp].to_f, attrs[:id]) redis.hmset("#{ck}:#{attrs[:id]}:assocs:belongs_to", {'parent_id' => parent.id}.to_a.flatten) unless parent.nil? - redis.sadd("#{ck}::attrs:ids", attrs[:id]) + redis.zadd("#{ck}::attrs:ids", attrs[:timestamp].to_f, attrs[:id]) end def create_reversed_child(parent, attrs = {}) redis.zadd("#{pk}:#{parent.id}:assocs:reversed_children_ids", attrs[:timestamp].to_f, attrs[:id]) unless parent.nil? redis.hmset("#{ck}:#{attrs[:id]}:attrs", {:emotion => attrs[:emotion], 'timestamp' => attrs[:timestamp].to_f}.to_a.flatten) redis.sadd("#{ck}::indices:by_emotion:string:#{attrs[:emotion]}", attrs[:id]) - redis.zadd("#{ck}::indices:by_timestamp", attrs[:timestamp].to_f, attrs[:id]) redis.hmset("#{ck}:#{attrs[:id]}:assocs:belongs_to", {'reversed_parent_id' => parent.id}.to_a.flatten) unless parent.nil? - redis.sadd("#{ck}::attrs:ids", attrs[:id]) + redis.zadd("#{ck}::attrs:ids", attrs[:timestamp].to_f, attrs[:id]) end let(:parent) { create_parent(:id => '8') parent_class.find_by_id('8') @@ -844,15 +842,14 @@ expect(redis.keys('*')).to match_array(["#{pk}::attrs:ids", "#{pk}:8:assocs:children_ids", "#{ck}::attrs:ids", "#{ck}::indices:by_emotion:string:indifferent", - "#{ck}::indices:by_timestamp", "#{ck}:4:attrs", "#{ck}:4:assocs:belongs_to"]) - expect(redis.smembers("#{ck}::attrs:ids")).to eq(['4']) + expect(redis.zrange("#{ck}::attrs:ids", 0, -1)).to eq(['4']) expect(redis.hgetall("#{ck}:4:attrs")).to eq( {'emotion' => 'indifferent', 'timestamp' => time.to_f.to_s} ) expect(redis.hgetall("#{ck}:4:assocs:belongs_to")).to eq( {'parent_id' => '8'} @@ -880,34 +877,32 @@ it "removes a parent/child has_sorted_set relationship between two records" do create_child(parent, :id => '4', :emotion => 'indifferent', :timestamp => time) child = child_class.find_by_id('4') - expect(redis.smembers("#{ck}::attrs:ids")).to eq(['4']) + expect(redis.zrange("#{ck}::attrs:ids", 0, -1)).to eq(['4']) expect(redis.zrange("#{pk}:8:assocs:children_ids", 0, -1)).to eq(['4']) parent.children.remove(child) - expect(redis.smembers("#{ck}::attrs:ids")).to eq(['4']) # child not deleted + expect(redis.zrange("#{ck}::attrs:ids", 0, -1)).to eq(['4']) # child not deleted expect(redis.zrange("#{pk}:8:assocs:children_ids", 0, -1)).to eq([]) # but association is end it "clears the belongs_to association when the parent record is deleted" do create_child(parent, :id => '6', :timestamp => time, :emotion => 'upset') expect(redis.keys).to match_array(["#{pk}::attrs:ids", "#{pk}:8:assocs:children_ids", "#{ck}::attrs:ids", - "#{ck}::indices:by_timestamp", "#{ck}::indices:by_emotion:string:upset", "#{ck}:6:attrs", "#{ck}:6:assocs:belongs_to"]) parent.destroy expect(redis.keys).to match_array(["#{ck}::attrs:ids", - "#{ck}::indices:by_timestamp", "#{ck}::indices:by_emotion:string:upset", "#{ck}:6:attrs"]) end it 'sets the score in a sorted set appropriately when assigned from the belongs_to' do \ No newline at end of file