sohm 0.0.1

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.
data/test/json.rb ADDED
@@ -0,0 +1,62 @@
1
+ require_relative 'helper'
2
+ require_relative "../lib/ohm/json"
3
+
4
+ class Venue < Ohm::Model
5
+ attribute :name
6
+ list :programmers, :Programmer
7
+ end
8
+
9
+ class Programmer < Ohm::Model
10
+ attribute :language
11
+
12
+ index :language
13
+
14
+ def to_hash
15
+ super.merge(language: language)
16
+ end
17
+ end
18
+
19
+ test "exports model.to_hash to json" do
20
+ assert_equal Hash.new, JSON.parse(Venue.new.to_json)
21
+
22
+ venue = Venue.create(name: "foo")
23
+ json = JSON.parse(venue.to_json)
24
+ assert_equal venue.id, json["id"]
25
+ assert_equal nil, json["name"]
26
+
27
+ programmer = Programmer.create(language: "Ruby")
28
+ json = JSON.parse(programmer.to_json)
29
+
30
+ assert_equal programmer.id, json["id"]
31
+ assert_equal programmer.language, json["language"]
32
+ end
33
+
34
+ test "exports a set to json" do
35
+ Programmer.create(language: "Ruby")
36
+ Programmer.create(language: "Python")
37
+
38
+ expected = [{ id: "1", language: "Ruby" }, { id: "2", language: "Python"}].to_json
39
+
40
+ assert_equal expected, Programmer.all.to_json
41
+ end
42
+
43
+ test "exports a multiset to json" do
44
+ Programmer.create(language: "Ruby")
45
+ Programmer.create(language: "Python")
46
+
47
+ expected = [{ id: "1", language: "Ruby" }, { id: "2", language: "Python"}].to_json
48
+ result = Programmer.find(language: "Ruby").union(language: "Python").to_json
49
+
50
+ assert_equal expected, result
51
+ end
52
+
53
+ test "exports a list to json" do
54
+ venue = Venue.create(name: "Foo")
55
+
56
+ venue.programmers.push(Programmer.create(language: "Ruby"))
57
+ venue.programmers.push(Programmer.create(language: "Python"))
58
+
59
+ expected = [{ id: "1", language: "Ruby" }, { id: "2", language: "Python"}].to_json
60
+
61
+ assert_equal expected, venue.programmers.to_json
62
+ end
data/test/list.rb ADDED
@@ -0,0 +1,83 @@
1
+ require_relative "helper"
2
+
3
+ class Post < Ohm::Model
4
+ list :comments, :Comment
5
+ end
6
+
7
+ class Comment < Ohm::Model
8
+ end
9
+
10
+ setup do
11
+ post = Post.create
12
+
13
+ post.comments.push(c1 = Comment.create)
14
+ post.comments.push(c2 = Comment.create)
15
+ post.comments.push(c3 = Comment.create)
16
+
17
+ [post, c1, c2, c3]
18
+ end
19
+
20
+ test "include?" do |p, c1, c2, c3|
21
+ assert p.comments.include?(c1)
22
+ assert p.comments.include?(c2)
23
+ assert p.comments.include?(c3)
24
+ end
25
+
26
+ test "first / last / size / empty?" do |p, c1, c2, c3|
27
+ assert_equal 3, p.comments.size
28
+ assert_equal c1, p.comments.first
29
+ assert_equal c3, p.comments.last
30
+ assert ! p.comments.empty?
31
+ end
32
+
33
+ test "replace" do |p, c1, c2, c3|
34
+ c4 = Comment.create
35
+
36
+ p.comments.replace([c4])
37
+
38
+ assert_equal [c4], p.comments.to_a
39
+ end
40
+
41
+ test "push / unshift" do |p, c1, c2, c3|
42
+ c4 = Comment.create
43
+ c5 = Comment.create
44
+
45
+ p.comments.unshift(c4)
46
+ p.comments.push(c5)
47
+
48
+ assert_equal c4, p.comments.first
49
+ assert_equal c5, p.comments.last
50
+ end
51
+
52
+ test "delete" do |p, c1, c2, c3|
53
+ p.comments.delete(c1)
54
+ assert_equal 2, p.comments.size
55
+ assert ! p.comments.include?(c1)
56
+
57
+ p.comments.delete(c2)
58
+ assert_equal 1, p.comments.size
59
+ assert ! p.comments.include?(c2)
60
+
61
+ p.comments.delete(c3)
62
+ assert p.comments.empty?
63
+ end
64
+
65
+ test "deleting main model cleans up the collection" do |p, _, _, _|
66
+ p.delete
67
+
68
+ assert_equal 0, Ohm.redis.call("EXISTS", p.key[:comments])
69
+ end
70
+
71
+ test "#ids returns an array with the ids" do |post, *comments|
72
+ assert_equal comments.map(&:id), post.comments.ids
73
+ end
74
+
75
+ test "range" do |p, c1, c2, c3|
76
+ assert_equal 3, p.comments.range(0, 100).size
77
+ assert_equal [c1, c2, c3], p.comments.range(0, 2)
78
+ assert_equal [c1, c2], p.comments.range(0, 1)
79
+ assert_equal [c2, c3], p.comments.range(1, 2)
80
+ assert_equal [c1, c2, c3], p.comments.range(0, -1)
81
+ assert_equal [c1, c2], p.comments.range(0, -2)
82
+ assert_equal [c2, c3], p.comments.range(1, -1)
83
+ end