ohm_util 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/test/set.rb ADDED
@@ -0,0 +1,37 @@
1
+ require_relative 'helper'
2
+
3
+ class Post < Ohm::Model
4
+ end
5
+
6
+ class User < Ohm::Model
7
+ attribute :name
8
+
9
+ index :name
10
+
11
+ set :posts, :Post
12
+ end
13
+
14
+ test '#exists? returns false if the given id is not included in the set' do
15
+ assert !User.create.posts.exists?('nonexistent')
16
+ end
17
+
18
+ test '#exists? returns true if the given id is included in the set' do
19
+ user = User.create
20
+ post = Post.create
21
+ user.posts.add(post)
22
+
23
+ assert user.posts.exists?(post.id)
24
+ end
25
+
26
+ test "#ids returns an array with the ids" do
27
+ user_ids = [
28
+ User.create(name: "John").id,
29
+ User.create(name: "Jane").id
30
+ ]
31
+
32
+ assert_equal user_ids, User.all.ids
33
+
34
+ result = User.find(name: "John").union(name: "Jane")
35
+
36
+ assert_equal user_ids, result.ids
37
+ end
@@ -0,0 +1,67 @@
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/to_hash.rb ADDED
@@ -0,0 +1,29 @@
1
+ require_relative 'helper'
2
+
3
+ class User < Ohm::Model
4
+ end
5
+
6
+ test "returns an empty hash if model doesn't have set attributes" do
7
+ assert_equal Hash.new, User.new.to_hash
8
+ end
9
+
10
+ test "returns a hash with its id if model is persisted" do
11
+ user = User.create
12
+
13
+ assert_equal Hash[id: user.id], user.to_hash
14
+ end
15
+
16
+ class Person < Ohm::Model
17
+ attribute :name
18
+
19
+ def to_hash
20
+ super.merge(name: name)
21
+ end
22
+ end
23
+
24
+ test "returns additional attributes if the method is overrided" do
25
+ person = Person.create(name: "John")
26
+ expected = { id: person.id, name: person.name }
27
+
28
+ assert_equal expected, person.to_hash
29
+ end
data/test/uniques.rb ADDED
@@ -0,0 +1,108 @@
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
99
+
100
+ test "nil doesn't count for uniques" do
101
+ u1 = User.create
102
+ u2 = User.create
103
+
104
+ assert u1.id
105
+ assert u2.id
106
+
107
+ assert u1.id != u2.id
108
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ohm_util
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Travis Liu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-03-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: redic
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.5.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.5.0
27
+ description: Ohm is a library that allows to store an object in Redis, a persistent
28
+ key-value database. It has very good performance.
29
+ email:
30
+ - travisliu.tw@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".gems"
36
+ - ".gitignore"
37
+ - CHANGELOG.md
38
+ - CONTRIBUTING
39
+ - LICENSE
40
+ - README.md
41
+ - benchmarks/common.rb
42
+ - benchmarks/create.rb
43
+ - benchmarks/delete.rb
44
+ - examples/activity-feed.rb
45
+ - examples/chaining.rb
46
+ - examples/json-hash.rb
47
+ - examples/one-to-many.rb
48
+ - examples/philosophy.rb
49
+ - examples/redis-logging.txt
50
+ - examples/slug.rb
51
+ - examples/tagging.rb
52
+ - lib/lua/delete.lua
53
+ - lib/lua/save.lua
54
+ - lib/ohm_util.rb
55
+ - makefile
56
+ - ohm-util.gemspec
57
+ - test/association.rb
58
+ - test/connection.rb
59
+ - test/core.rb
60
+ - test/counters.rb
61
+ - test/enumerable.rb
62
+ - test/filtering.rb
63
+ - test/hash_key.rb
64
+ - test/helper.rb
65
+ - test/indices.rb
66
+ - test/json.rb
67
+ - test/list.rb
68
+ - test/model.rb
69
+ - test/set.rb
70
+ - test/thread_safety.rb
71
+ - test/to_hash.rb
72
+ - test/uniques.rb
73
+ homepage: https://github.com/travisliu/ohm-util
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.6.8
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Object-hash mapping library for Redis.
97
+ test_files: []