File: sortable.rb

package info (click to toggle)
ruby-fakeredis 0.8.0-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 628 kB
  • sloc: ruby: 4,868; makefile: 2
file content (73 lines) | stat: -rw-r--r-- 2,459 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
shared_examples_for "a sortable" do
  it 'returns empty array on nil' do
    expect(@client.sort(nil)).to eq([])
  end

  context 'ordering' do
    it 'orders ascending by default' do
      expect(@client.sort(@key)).to eq(['1', '2'])
    end

    it 'orders by ascending when specified' do
      expect(@client.sort(@key, :order => "ASC")).to eq(['1', '2'])
    end

    it 'orders by descending when specified' do
      expect(@client.sort(@key, :order => "DESC")).to eq(['2', '1'])
    end

    it "orders by ascending when alpha is specified" do
      expect(@client.sort(@key, :order => "ALPHA")).to eq(["1", "2"])
    end
  end

  context 'projections' do
    it 'projects element when :get is "#"' do
      expect(@client.sort(@key, :get => '#')).to eq(['1', '2'])
    end

    it 'projects through a key pattern' do
      expect(@client.sort(@key, :get => 'fake-redis-test:values_*')).to eq(['a', 'b'])
    end

    it 'projects through a key pattern and reflects element' do
      expect(@client.sort(@key, :get => ['#', 'fake-redis-test:values_*'])).to eq([['1', 'a'], ['2', 'b']])
    end

    it 'projects through a hash key pattern' do
      expect(@client.sort(@key, :get => 'fake-redis-test:hash_*->key')).to eq(['x', 'y'])
    end
  end

  context 'weights' do
    it 'weights by projecting through a key pattern' do
      expect(@client.sort(@key, :by => "fake-redis-test:weight_*")).to eq(['2', '1'])
    end

    it 'weights by projecting through a key pattern and a specific order' do
      expect(@client.sort(@key, :order => "DESC", :by => "fake-redis-test:weight_*")).to eq(['1', '2'])
    end
  end

  context 'limit' do
    it 'only returns requested window in the enumerable' do
      expect(@client.sort(@key, :limit => [0, 1])).to eq(['1'])
    end

    it 'returns an empty array if the offset if more than the length of the list' do
      expect(@client.sort(@key, :limit => [3, 1])).to eq([])
    end
  end

  context 'store' do
    it 'stores into another key' do
      expect(@client.sort(@key, :store => "fake-redis-test:some_bucket")).to eq(2)
      expect(@client.lrange("fake-redis-test:some_bucket", 0, -1)).to eq(['1', '2'])
    end

    it "stores into another key with other options specified" do
      expect(@client.sort(@key, :store => "fake-redis-test:some_bucket", :by => "fake-redis-test:weight_*")).to eq(2)
      expect(@client.lrange("fake-redis-test:some_bucket", 0, -1)).to eq(['2', '1'])
    end
  end
end