webget-active_record_mock 1.2.2
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/lib/active_record_mock.rb +71 -0
- data/test/unit/active_record_mock_test.rb +70 -0
- metadata +53 -0
@@ -0,0 +1,71 @@
|
|
1
|
+
# = ActiveRecordMock
|
2
|
+
#
|
3
|
+
# Author:: Joel Parker Henderson, joelparkerhenderson@gmail.com
|
4
|
+
# Copyright:: Copyright (c) 2006-2008 Joel Parker Henderson
|
5
|
+
# License:: CreativeCommons License, Non-commercial Share Alike
|
6
|
+
# License:: LGPL, GNU Lesser General Public License
|
7
|
+
#
|
8
|
+
# A simple mock object that provides the ActiveRecord method
|
9
|
+
# signatures read_attribute(key) and write_attribute(key,val),
|
10
|
+
# and simple record finder signatures find(id) and find(:all).
|
11
|
+
#
|
12
|
+
# Example:
|
13
|
+
# mock = ActiveRecordMock.new
|
14
|
+
# mock.write_attribute('foo','bar')
|
15
|
+
# mock.read_attribute('foo') => 'bar'
|
16
|
+
#
|
17
|
+
# Example of initialize with attributes:
|
18
|
+
# mock = ActiveRecordMock.new(:foo => 'bar', :goo => 'car', :hoo => 'dar')
|
19
|
+
# mock.read_attribute(:foo') => 'bar'
|
20
|
+
# mock.read_attribute(:goo') => 'car'
|
21
|
+
# mock.read_attribute(:hoo') => 'dar'
|
22
|
+
#
|
23
|
+
# Example of creating mock users:
|
24
|
+
# anne = ActiveRecordMock.new(:id => 123, :name => 'Anne')
|
25
|
+
# beth = ActiveRecordMock.new(:id => 456, :name => 'Beth')
|
26
|
+
# cate = ActiveRecordMock.new(:id => 789, :name => 'Cate')
|
27
|
+
#
|
28
|
+
# Example of mock finder creation:
|
29
|
+
# ActiveRecordMock.find=[anne,beth,cate]
|
30
|
+
#
|
31
|
+
# Example of mock finder retrieval of records by id:
|
32
|
+
# ActiveRecordMock.find(123) => anne
|
33
|
+
# ActiveRecordMock.find(456) => beth
|
34
|
+
# ActiveRecordMock.find(789) => cate
|
35
|
+
#
|
36
|
+
# Example of mock finder retrieval of all records:
|
37
|
+
# ActiveRecordMock.find(:all) => [anne,beth,cate]
|
38
|
+
#
|
39
|
+
##
|
40
|
+
|
41
|
+
class ActiveRecordMock
|
42
|
+
|
43
|
+
@@find=[]
|
44
|
+
|
45
|
+
def initialize(ops={})
|
46
|
+
@attributes=ops
|
47
|
+
@@find << self
|
48
|
+
end
|
49
|
+
|
50
|
+
def read_attribute(k)
|
51
|
+
@attributes[k]
|
52
|
+
end
|
53
|
+
|
54
|
+
def write_attribute(k,v)
|
55
|
+
@attributes[k]=v
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.find(id,*ops)
|
59
|
+
case id
|
60
|
+
when :all
|
61
|
+
@@find
|
62
|
+
else
|
63
|
+
@@find.each{|x| if x.read_attribute(:id)==id then return x end } or nil
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def self.find=(records)
|
68
|
+
@@find=records
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'active_record_mock'
|
3
|
+
|
4
|
+
class ActiveRecordMockTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_instantiation
|
7
|
+
mock1 = ActiveRecordMock.new
|
8
|
+
mock2 = ActiveRecordMock.new
|
9
|
+
assert_not_same(mock1,mock2,"mock1,mock2")
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_attribute_assignment
|
13
|
+
mock = ActiveRecordMock.new
|
14
|
+
key='foo'
|
15
|
+
val='bar'
|
16
|
+
mock.write_attribute(key,val)
|
17
|
+
out=mock.read_attribute(key)
|
18
|
+
assert_equal(val,out,"mock key:#{key} val:#{val} out:#{out}")
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_attribute_independence
|
22
|
+
mock = ActiveRecordMock.new
|
23
|
+
key1='foo'
|
24
|
+
val1='bar'
|
25
|
+
key2='hello'
|
26
|
+
val2='world'
|
27
|
+
mock.write_attribute(key1,val1)
|
28
|
+
mock.write_attribute(key2,val2)
|
29
|
+
out1=mock.read_attribute(key1)
|
30
|
+
out2=mock.read_attribute(key2)
|
31
|
+
assert_equal(val1,out1,"mock key1:#{key1} val1:#{val1} out:#{out1}")
|
32
|
+
assert_equal(val2,out2,"mock key2:#{key2} val2:#{val2} out:#{out2}")
|
33
|
+
assert_not_equal(key1,key2,"key1,key2")
|
34
|
+
assert_not_equal(val1,val2,"val1,val2")
|
35
|
+
assert_not_equal(out1,out2,"out1,out2")
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_initialize_attributes
|
39
|
+
mock = ActiveRecordMock.new(:foo => 'bar', :goo => 'car', :hoo => 'dar')
|
40
|
+
x = mock.read_attribute(:foo); assert_equal(x,'bar',"x:#{x},bar")
|
41
|
+
x = mock.read_attribute(:goo); assert_equal(x,'car',"x:#{x},car")
|
42
|
+
x = mock.read_attribute(:hoo); assert_equal(x,'dar',"x:#{x},dar")
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_find
|
46
|
+
|
47
|
+
anne = ActiveRecordMock.new(:id=>1, :name=>'Anne')
|
48
|
+
beth = ActiveRecordMock.new(:id=>2, :name=>'Beth')
|
49
|
+
cate = ActiveRecordMock.new(:id=>3, :name=>'Cate')
|
50
|
+
|
51
|
+
x=anne.read_attribute(:name); assert_equal(x,'Anne',"x:#{x},anne")
|
52
|
+
x=beth.read_attribute(:name); assert_equal(x,'Beth',"x:#{x},beth")
|
53
|
+
x=cate.read_attribute(:name); assert_equal(x,'Cate',"x:#{x},cate")
|
54
|
+
|
55
|
+
# Set the mock finder
|
56
|
+
all = [anne,beth,cate]
|
57
|
+
ActiveRecordMock.find=all
|
58
|
+
|
59
|
+
# Retrieve all
|
60
|
+
x = ActiveRecordMock.find(:all)
|
61
|
+
assert_equal(x,all,"find :all x:#{x}")
|
62
|
+
|
63
|
+
# Retrieve by id
|
64
|
+
x = ActiveRecordMock.find(1); assert_equal(x,anne,"find(1) x:#{x},anne")
|
65
|
+
x = ActiveRecordMock.find(2); assert_equal(x,beth,"find(2) x:#{x},beth")
|
66
|
+
x = ActiveRecordMock.find(3); assert_equal(x,cate,"find(3) x:#{x},cate")
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
metadata
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: webget-active_record_mock
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.2.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- WebGet
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-02-05 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: webget@webget.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- lib/active_record_mock.rb
|
26
|
+
has_rdoc: true
|
27
|
+
homepage: http://webget.com/
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: "0"
|
38
|
+
version:
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
requirements: []
|
46
|
+
|
47
|
+
rubyforge_project:
|
48
|
+
rubygems_version: 1.2.0
|
49
|
+
signing_key:
|
50
|
+
specification_version: 2
|
51
|
+
summary: "active_record_mock: mimic the ActiveRecord methods read_attribute and write_attribute"
|
52
|
+
test_files:
|
53
|
+
- test/unit/active_record_mock_test.rb
|