File: hyper_log_logs_spec.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 (50 lines) | stat: -rw-r--r-- 1,631 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
require "spec_helper"

module FakeRedis
  describe "HyperLogLogsMethods" do
    let(:redis) { Redis.new }

    it "should add item to hyperloglog" do
      expect(redis.pfadd("hll", "val")).to eq(true)
      expect(redis.pfcount("hll")).to eq(1)
    end

    it "should not add duplicated item to hyperloglog" do
      redis.pfadd("hll", "val")
      expect(redis.pfadd("hll", "val")).to eq(false)
      expect(redis.pfcount("hll")).to eq(1)
    end

    it "should not add multiple items to hyperloglog" do
      expect(redis.pfadd("hll", ["val1", "val2"])).to eq(true)
      expect(redis.pfcount("hll")).to eq(2)
    end

    it "should return zero as cardinality for nonexistent key" do
      expect(redis.pfcount("nonexistent")).to eq(0)
    end

    it "should return cardinality of union of hyperloglogs" do
      redis.pfadd("hll1", ["val1", "val2"])
      redis.pfadd("hll2", ["val2", "val3"])
      expect(redis.pfcount("hll1", "hll2")).to eq(3)
    end

    it "should error if an empty list of keys is given" do
      expect { redis.pfcount([]) }.to raise_error(Redis::CommandError, "ERR wrong number of arguments for 'pfcount' command")
    end

    it "should merge hyperloglogs" do
      redis.pfadd("hll1", ["val1", "val2"])
      redis.pfadd("hll2", ["val2", "val3"])
      expect(redis.pfmerge("hll3", "hll1", "hll2")).to eq(true)
      expect(redis.pfcount("hll3")).to eq(3)
    end

    it "should merge nonexistent hyperloglogs with others" do
      redis.pfadd("hll1", "val")
      expect(redis.pfmerge("hll3", "hll1", "nonexistent")).to eq(true)
      expect(redis.pfcount("hll3")).to eq(1)
    end
  end
end