Skip to content

Instantly share code, notes, and snippets.

@pricees
Created September 7, 2012 18:32
Show Gist options
  • Save pricees/3668414 to your computer and use it in GitHub Desktop.
Save pricees/3668414 to your computer and use it in GitHub Desktop.
PersistentJhash Test (meh)
describe Ss::PersistentJhash do
before do
tmp = Ss::PersistentJhash.new
tmp.clear.save
assert tmp.send(:conn)
tmp.load_data.must_be :empty?
end
it "is a hash" do
hsh = Ss::PersistentJhash.new
assert hsh.is_a? Hash
end
it "saves" do
hsh = Ss::PersistentJhash.new
hsh.send(:raw).must_equal Hash.new.to_json
hsh += { name: :John, age: 32, fav_nums: [ 1, 2, 42 ] }
hsh.save
hsh.clear
hsh.must_be :empty?
hsh.send(:raw).wont_equal Hash.new.to_json
end
it "takes default params hash" do
klass = Ss::PersistentJhash
p1 = { ns: :a_ns }
klass.new(p1).namespace.must_equal "a_ns"
p2 = { rk: :a_rk }
klass.new(p2).namespace.must_equal "ss:persistent_jhash"
end
it "has a default namespace" do
Ss::PersistentJhash.new.namespace.must_equal "ss:persistent_jhash"
end
it "overrides default namespace" do
ns = "foo_bar"
ph = Ss::PersistentJhash.new
ph.send(:namespace=, ns)
ph.namespace.must_equal ns
end
it "has a default rd_key" do
Ss::PersistentJhash.new.rd_key.must_equal "persistent_jhash"
end
it "overrides default rd_key" do
ns = "foo_bar"
ph = Ss::PersistentJhash.new
ph.send(:rd_key=, ns)
ph.rd_key.must_equal ns
Ss::PersistentJhash.new.reset
end
it "loads data" do
hsh = Ss::PersistentJhash.new.load_data
hsh.must_be :empty?
cn = hsh.send(:conn)
key = Ss::PersistentJhash.new.rd_key
cn[key] = Hash[:name, :John, :nums, [1, 42]].to_json
hsh.must_be :empty?
hsh.load_data
hsh["name"].must_equal "John"
hsh["nums"].must_equal [1, 42]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment