sohm 0.0.1 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,72 +0,0 @@
1
- -- This script receives three parameters, all encoded with
2
- -- MessagePack. The decoded values are used for deleting a model
3
- -- instance in Redis and removing any reference to it in sets
4
- -- (indices) and hashes (unique indices).
5
- --
6
- -- # model
7
- --
8
- -- Table with three attributes:
9
- -- id (model instance id)
10
- -- key (hash where the attributes will be saved)
11
- -- name (model name)
12
- --
13
- -- # uniques
14
- --
15
- -- Fields and values to be removed from the unique indices.
16
- --
17
- -- # tracked
18
- --
19
- -- Keys that share the lifecycle of this model instance, that
20
- -- should be removed as this object is deleted.
21
- --
22
- local model = cmsgpack.unpack(ARGV[1])
23
- local uniques = cmsgpack.unpack(ARGV[2])
24
- local tracked = cmsgpack.unpack(ARGV[3])
25
-
26
- local function remove_indices(model)
27
- local memo = model.key .. ":_indices"
28
- local existing = redis.call("SMEMBERS", memo)
29
-
30
- for _, key in ipairs(existing) do
31
- redis.call("SREM", key, model.id)
32
- redis.call("SREM", memo, key)
33
- end
34
- end
35
-
36
- local function remove_uniques(model, uniques)
37
- local memo = model.key .. ":_uniques"
38
-
39
- for field, _ in pairs(uniques) do
40
- local key = model.name .. ":uniques:" .. field
41
-
42
- redis.call("HDEL", key, redis.call("HGET", memo, key))
43
- redis.call("HDEL", memo, key)
44
- end
45
- end
46
-
47
- local function remove_tracked(model, tracked)
48
- for _, tracked_key in ipairs(tracked) do
49
- local key = model.key .. ":" .. tracked_key
50
-
51
- redis.call("DEL", key)
52
- end
53
- end
54
-
55
- local function delete(model)
56
- local keys = {
57
- model.key .. ":counters",
58
- model.key .. ":_indices",
59
- model.key .. ":_uniques",
60
- model.key
61
- }
62
-
63
- redis.call("SREM", model.name .. ":all", model.id)
64
- redis.call("DEL", unpack(keys))
65
- end
66
-
67
- remove_indices(model)
68
- remove_uniques(model, uniques)
69
- remove_tracked(model, tracked)
70
- delete(model)
71
-
72
- return model.id
@@ -1,67 +0,0 @@
1
- require_relative "helper"
2
-
3
- class Post < Ohm::Model; end
4
- class Role < Ohm::Model; end
5
-
6
- class User < Ohm::Model
7
- list :posts, :Post
8
- set :roles, :Role
9
- end
10
-
11
- setup do
12
- User.create
13
- end
14
-
15
- test "list#replace" do |user|
16
- Post.mutex.lock
17
-
18
- thread = Thread.new { user.posts.replace([Post.create]) }
19
-
20
- sleep 0.1
21
-
22
- assert_equal true, thread.alive?
23
-
24
- Post.mutex.unlock
25
-
26
- sleep 0.1
27
-
28
- assert_equal false, thread.alive?
29
-
30
- thread.join
31
- end
32
-
33
- test "set#replace" do |user|
34
- Role.mutex.lock
35
-
36
- thread = Thread.new { user.roles.replace([Role.create]) }
37
-
38
- sleep 0.1
39
-
40
- assert_equal true, thread.alive?
41
-
42
- Role.mutex.unlock
43
-
44
- sleep 0.1
45
-
46
- assert_equal false, thread.alive?
47
-
48
- thread.join
49
- end
50
-
51
- test "collection#fetch" do
52
- User.mutex.lock
53
-
54
- thread = Thread.new { User.all.to_a }
55
-
56
- sleep 0.1
57
-
58
- assert_equal true, thread.alive?
59
-
60
- User.mutex.unlock
61
-
62
- sleep 0.1
63
-
64
- assert_equal false, thread.alive?
65
-
66
- thread.join
67
- end
data/test/uniques.rb DELETED
@@ -1,98 +0,0 @@
1
- require_relative "helper"
2
-
3
- class User < Ohm::Model
4
- attribute :email
5
- unique :email
6
- unique :provider
7
-
8
- def self.[](id)
9
- super(id.to_i)
10
- end
11
-
12
- def provider
13
- email.to_s[/@(.*?).com/, 1]
14
- end
15
- end
16
-
17
- setup do
18
- User.create(:email => "a@a.com")
19
- end
20
-
21
- test "findability" do |u|
22
- assert_equal u, User.with(:email, "a@a.com")
23
- end
24
-
25
- test "raises when it already exists during create" do
26
- assert_raise Ohm::UniqueIndexViolation do
27
- User.create(:email => "a@a.com")
28
- end
29
- end
30
-
31
- test "raises when it already exists during save" do
32
- u = User.create(:email => "b@b.com")
33
- u.email = "a@a.com"
34
-
35
- assert_raise Ohm::UniqueIndexViolation do
36
- u.save
37
- end
38
- end
39
-
40
- test "raises if the index doesn't exist" do
41
- User.create(:email => "b@b.com")
42
-
43
- assert_raise Ohm::IndexNotFound do
44
- User.with(:address, "b@b.com")
45
- end
46
- end
47
-
48
- test "doesn't raise when saving again and again" do |u|
49
- ex = nil
50
-
51
- begin
52
- User[u.id].save
53
- rescue Exception => e
54
- ex = e
55
- end
56
-
57
- assert_equal nil, ex
58
- end
59
-
60
- test "removes the previous index when changing" do
61
- u = User.create(:email => "c@c.com")
62
- u.update(:email => "d@d.com")
63
-
64
- assert_equal nil, User.with(:email, "c@c.com")
65
- assert_equal nil, User.redis.call("HGET", User.key[:uniques][:email], "c@c.com")
66
- assert_equal u, User.with(:email, "d@d.com")
67
-
68
- u.update(:email => nil)
69
-
70
- assert_equal nil, User.with(:email, "d@d.com")
71
- assert_equal nil, User.redis.call("HGET", User.key[:uniques][:email], "d@d.com")
72
- end
73
-
74
- test "removes the previous index when deleting" do |u|
75
- u.delete
76
-
77
- assert_equal nil, User.with(:email, "a@a.com")
78
- assert_equal nil, User.redis.call("HGET", User.key[:uniques][:email], "a@a.com")
79
- end
80
-
81
- test "unique virtual attribute" do
82
- u = User.create(:email => "foo@yahoo.com")
83
-
84
- assert_equal u, User.with(:provider, "yahoo")
85
-
86
- # Yahoo should be allowed because this user is the one reserved for it.
87
- u.update(:email => "bar@yahoo.com")
88
-
89
- # `a` is not allowed though.
90
- assert_raise Ohm::UniqueIndexViolation do
91
- u.update(:email => "bar@a.com")
92
- end
93
-
94
- # And so is yahoo if we try creating a different user.
95
- assert_raise Ohm::UniqueIndexViolation do
96
- User.create(:email => "baz@yahoo.com")
97
- end
98
- end