File: uuid_spec.rb

package info (click to toggle)
ruby-sequel 5.63.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,408 kB
  • sloc: ruby: 113,747; makefile: 3
file content (103 lines) | stat: -rw-r--r-- 2,975 bytes parent folder | download | duplicates (3)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
require_relative "spec_helper"

describe "Sequel::Plugins::Uuid" do
  before do
    uuid = @uuid = '57308544-4e83-47b8-b87f-6f68b987f4f9'
    @alt_uuid = 'd5d1ec46-5e8e-4a7b-adc9-50e76b819e19'
    @c = Class.new(Sequel::Model(:t))
    @c.class_eval do
      columns :id, :uuid
      plugin :uuid
      private
      def _save_refresh(*) end
      define_method(:create_uuid) do
        uuid
      end
      db.reset
    end
  end
  
  it "should handle validations on the uuid field for new objects" do
    @c.plugin :uuid, :force=>true
    o = @c.new
    def o.validate
      errors.add(model.uuid_field, 'not present') unless send(model.uuid_field)
    end
    o.valid?.must_equal true
  end

  it "should set uuid field when skipping validations" do
    @c.plugin :uuid
    @c.new.save(:validate=>false)
    @c.db.sqls.must_equal ["INSERT INTO t (uuid) VALUES ('#{@uuid}')"]
  end

  it "should set the uuid field on creation" do
    o = @c.create
    @c.db.sqls.must_equal ["INSERT INTO t (uuid) VALUES ('#{@uuid}')"]
    o.uuid.must_equal @uuid
  end

  it "should allow specifying the uuid field via the :field option" do
    c = Class.new(Sequel::Model(:t))
    c.class_eval do
      columns :id, :u
      plugin :uuid, :field=>:u
      private
      def _save_refresh(*) end
    end
    o = c.create
    c.db.sqls.must_equal ["INSERT INTO t (u) VALUES ('#{o.u}')"]
  end

  it "should not raise an error if the model doesn't have the uuid column" do
    @c.columns :id, :x
    @c.send(:undef_method, :uuid)
    @c.create(:x=>2)
    @c.load(:id=>1, :x=>2).save
    @c.db.sqls.must_equal ["INSERT INTO t (x) VALUES (2)", "UPDATE t SET x = 2 WHERE (id = 1)"]
  end

  it "should not overwrite an existing uuid value" do
    o = @c.create(:uuid=>@alt_uuid)
    @c.db.sqls.must_equal ["INSERT INTO t (uuid) VALUES ('#{@alt_uuid}')"]
    o.uuid.must_equal @alt_uuid
  end

  it "should overwrite an existing uuid if the :force option is used" do
    @c.plugin :uuid, :force=>true
    o = @c.create(:uuid=>@alt_uuid)
    @c.db.sqls.must_equal ["INSERT INTO t (uuid) VALUES ('#{@uuid}')"]
    o.uuid.must_equal @uuid
  end

  it "should have uuid_field give the uuid field" do
    @c.uuid_field.must_equal :uuid
    @c.plugin :uuid, :field=>:u
    @c.uuid_field.must_equal :u
  end

  it "should have uuid_overwrite? give the whether to overwrite an existing uuid" do
    @c.uuid_overwrite?.must_equal false
    @c.plugin :uuid, :force=>true
    @c.uuid_overwrite?.must_equal true
  end

  it "should work with subclasses" do
    c = Class.new(@c)
    o = c.create
    o.uuid.must_equal @uuid
    c.db.sqls.must_equal ["INSERT INTO t (uuid) VALUES ('#{@uuid}')"]
    c.create(:uuid=>@alt_uuid).uuid.must_equal @alt_uuid

    c.class_eval do
      columns :id, :u
      plugin :uuid, :field=>:u, :force=>true
    end
    c2 = Class.new(c)
    c2.db.reset
    o = c2.create
    o.u.must_equal @uuid
    c2.db.sqls.must_equal ["INSERT INTO t (u) VALUES ('#{@uuid}')"]
  end
end