forked from KensoDev/perform_later
/
args_parser_spec.rb
80 lines (65 loc) · 2.38 KB
/
args_parser_spec.rb
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
require 'spec_helper'
describe PerformLater::ArgsParser do
subject { PerformLater::ArgsParser }
let(:user) { User.create }
context "args to resque" do
it "should convert array of hashes correctly" do
arr = [
{ something: "aaa" },
{ something: "bbb" },
{ something: "ccc" },
{ something: "ddd" },
{ something: "eee" }
]
subject.args_to_resque(arr).class.should == Array
end
it "should translate array of hashes back and forth again" do
arr = [
{ something: "aaa" },
{ something: "bbb" },
{ something: "ccc" },
{ something: "ddd" },
{ something: "eee" }
]
to_resque = subject.args_to_resque(arr).to_json
puts to_resque
a = JSON.parse(to_resque)
from_resque = subject.args_from_resque(a)
from_resque.should == arr
end
it "should convert the AR object to the proper string" do
user_id = user.id
subject.args_to_resque(user).length.should == 1
subject.args_to_resque(user)[0].should == "AR:User:#{user_id}"
end
it "should convert a hash into YAML string so that Resque will be able to JSON convert it" do
hash = { name: "something", other: "something else" }
subject.args_to_resque(hash)[0].class.name.should == "String"
end
it "should be able to load a yaml from the string and translate it into the same hash again" do
hash = { name: "something", other: "something else" }
yaml = subject.args_to_resque(hash)[0]
loaded_yaml = YAML.load(yaml)
loaded_yaml[:name].should == "something"
loaded_yaml[:other].should == "something else"
end
it "should convert a class to the proper string representation" do
klass = User
subject.args_to_resque(klass)[0].should == "CLASS:User"
end
end
context "args from resque" do
it "should give me a hash back when I pass a yaml representation of it" do
hash = { name: "something", other: "something else" }
yaml = hash.to_yaml
args = subject.args_from_resque(yaml)
args[0].class.name.should == "Hash"
args[0][:name].should == "something"
args[0][:other].should == "something else"
end
it "Should give me a user model back when I pass the proper string" do
args = subject.args_from_resque("AR:User:#{user.id}")
args[0].should == user
end
end
end