bogusdb 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7e3d1c157db10e23b2e98c90a224e08d811ddeee
4
+ data.tar.gz: 5c338ab15a1c7307a7c7360fd6bebc4a070bb3cf
5
+ SHA512:
6
+ metadata.gz: da134c1387cae6e15c3d6af75e0a34cb9a9b1c29059161d0be17a778852c070fb914df22f81dcb31fd228d87ff4a120951678428ab0eed7909df8db0525ea57e
7
+ data.tar.gz: f72155774a43954fa5361a4a2c1004c00a287e6429336d8463e544b4162fc0875972d55406100f58319167143a1797042ae98c30b1aafd6814709bd880a59665
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bogusdb.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Bradley Smith
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,90 @@
1
+ # Bogusdb
2
+
3
+ `Bogusdb` is a `fake` ORM database object--Nothing more, nothing less.
4
+ There are great tools out there that do the similar things--and do it much better. If you need more
5
+ features you are probably better off using one of those.
6
+ The problem is most require heavy dependencies with them. Also, most tools assume you are using active record and or in a rails environment.
7
+
8
+ `Bogusdb` can be used in tests where you do not want to actually create any data,
9
+ but need to `unit` test your logic against a object that quacks like a ORM object.
10
+
11
+
12
+ * I use this when testing daemons that have DB dependencies
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ gem 'bogusdb'
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install bogusdb
27
+
28
+ ## Usage
29
+
30
+ So, lets say you need a table named `:user` with these column names and values
31
+ ```
32
+ first_name: 'foo'
33
+ last_name: 'bar'
34
+ ```
35
+
36
+ ## Create a row
37
+ * Note the id is optional
38
+
39
+ ```ruby
40
+ @user = Bogusdb::Record.new(id: 10, first_name: 'foo', last_name: 'bar')
41
+ @user.id #=> 10
42
+ @user.first_name #=> 'foo'
43
+ @user.last_name #=> 'bar'
44
+ @user.attributes #=> {:last_name=>"bar", :id=>10, :first_name=>"foo"}
45
+ @user.inspect #=> "#<Bogusdb::Record: last_name: bar, id: 10, first_name: foo"
46
+ ```
47
+
48
+ ## Create multiple records at the same time
49
+ ```ruby
50
+ @user = Bogusdb::Record.create([ { first_name: 'foo', last_name: 'bar' },
51
+ { first_name: 'Fizz', last_name: 'Buzz' } ])
52
+ @user #=> [#<Bogusdb::Record: id: 10, first_name: 'foo', last_name: 'bar',
53
+ #<Bogusdb::Record: id: 23, first_name: 'Fizz', last_name: 'Buzz']
54
+ ```
55
+
56
+ ## Join table
57
+ ```ruby
58
+ @user = Bogusdb::Record.new(first_name: 'foo', last_name: 'bar')
59
+ @user.join_table(:profile, {id: 1, avatar: 'image.jpg', gender: 'M'})
60
+ @user.profile #=> #<Bogusdb::Record: avatar: image.jpg, id: 1, gender: m
61
+ @user.profile.attributes #=> {id: 1, avatar: 'image.jpg', gender: 'M'}
62
+ ```
63
+
64
+ ## Join a table with more familiar syntax
65
+ ```ruby
66
+ @user = Bogusdb::Record.new(first_name: 'foo', last_name: 'bar')
67
+ @user.has_one(:profile, {id: 1, avatar: 'image.jpg', gender: 'M'})
68
+ @user.profile #=> #<Bogusdb::Record: avatar: image.jpg, id: 1, gender: m
69
+ @user.profile.attributes #=> {id: 1, avatar: 'image.jpg', gender: 'M'}
70
+ ```
71
+
72
+ ```ruby
73
+ @user = Bogusdb::Record.new(id: 10, first_name: 'foo', last_name: 'bar')
74
+ @user.has_many(:addresses, [{street: '123 main', city: 'Denver'},
75
+ {street: '456 boulder dr', city: 'Boulder'}])
76
+
77
+ @user.addresses #=> [#<Bogusdb::Record: street: '123 main', city: 'Denver',
78
+ #<Bogusdb::Record: street: '456 boulder dr', city: 'Boulder']
79
+ ```
80
+
81
+
82
+
83
+
84
+ ## Contributing
85
+
86
+ 1. Fork it
87
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
88
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
89
+ 4. Push to the branch (`git push origin my-new-feature`)
90
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.options = "-v"
6
+ t.libs << "test"
7
+ t.test_files = FileList["test/*_test.rb"]
8
+ end
data/bogusdb.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bogusdb/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bogusdb"
8
+ spec.version = Bogusdb::VERSION
9
+ spec.authors = ["Bradley Smith"]
10
+ spec.email = ["bradleydsmith@gmail.com"]
11
+ spec.description = %q{ A simple fake database object for testing }
12
+ spec.summary = %q{A simple fake ORM databsase object used in testing}
13
+ spec.homepage = "https://github.com/bradleyd/bogusdb.git"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "minitest"
24
+ spec.add_development_dependency "mocha"
25
+ end
data/lib/bogusdb.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "bogusdb/version"
2
+ require "bogusdb/record"
3
+
4
+ module Bogusdb
5
+ end
@@ -0,0 +1,82 @@
1
+ require "securerandom"
2
+
3
+ module Bogusdb
4
+ # simulate a basic database record ORM style
5
+ # @param [Hash] table_data column names and values
6
+ class Record
7
+ def initialize(table_data)
8
+ raise TypeError unless Hash(table_data)
9
+ @table_data = table_data
10
+ set_id
11
+ @attributes = initialize_attributes(@table_data)
12
+ end
13
+
14
+ # @note can pass array of hashes to build records
15
+ # @param [Array, Hash] args
16
+ # @retrun [Array, Bogusdb::Record]
17
+ def self.create(args)
18
+ args.is_a?(Array) ? Array.new(args.length) { |i| new(args[i]) } : new(args)
19
+ end
20
+
21
+ def attributes
22
+ @attributes
23
+ end
24
+
25
+ # @param [String, Symbol] table_name joined table name
26
+ # @param [Hash, Array] opts column names and values
27
+ # @return [Bogusdb::Record, Array] joined table rows
28
+ def join_table(table_name, opts)
29
+ self.class.send(:define_method, table_name.to_sym) do
30
+ self.class.create([opts].flatten)
31
+ end
32
+ self.send(table_name)
33
+ end
34
+
35
+ alias_method :has_many, :join_table
36
+
37
+ # @param [String, Symbol] table_name joined table name
38
+ # @param [Hash] opt column names and values
39
+ # @return [Bogusdb::Record] joined table
40
+ def has_one(table_name, opt)
41
+ self.class.send(:define_method, table_name.to_sym) do
42
+ self.class.new(opt)
43
+ end
44
+ self.send(table_name)
45
+ end
46
+
47
+ def inspect
48
+ nice_format = attributes.collect { |k,v|
49
+ "#{k}: #{v}"
50
+ }.compact.join(", ")
51
+ "#<#{self.class.name}: #{nice_format}"
52
+ end
53
+
54
+ private
55
+
56
+ def id
57
+ SecureRandom.random_number(100)
58
+ end
59
+
60
+
61
+ def column_names
62
+ @table_data.keys
63
+ end
64
+
65
+ def set_id
66
+ unless @table_data.include?("id" || :id)
67
+ @table_data.merge!(id: id)
68
+ end
69
+ end
70
+
71
+ def initialize_attributes(options)
72
+ options.each do |key,value|
73
+ if value.is_a?(Hash) || value.is_a?(Array)
74
+ self.define_singleton_method(key) { Record.create(value) }
75
+ else
76
+ self.define_singleton_method(key) { options[key] }
77
+ self.define_singleton_method("#{key}=") { |val| options[key]=val }
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,3 @@
1
+ module Bogusdb
2
+ VERSION = "0.1.2"
3
+ end
@@ -0,0 +1,11 @@
1
+ require_relative "test_helper"
2
+
3
+ class BogusdbTest < MiniTest::Test
4
+ def setup
5
+
6
+ end
7
+
8
+ def test_bogusdb_responds_to_version
9
+ assert Bogusdb::VERSION
10
+ end
11
+ end
@@ -0,0 +1,108 @@
1
+ require_relative "test_helper"
2
+
3
+ class RecordTest < MiniTest::Test
4
+ def setup
5
+ @bogusdb = Bogusdb::Record.new(name: 'akira', dob: '04/14/75')
6
+ end
7
+
8
+ def test_responds_to_new
9
+ assert_respond_to Bogusdb::Record, :new
10
+ end
11
+
12
+ def test_responds_to_create
13
+ assert_respond_to Bogusdb::Record, :create
14
+ end
15
+
16
+ def test_bogusdb_does_not_accept_string_params
17
+ assert_raises(TypeError) {
18
+ Bogusdb::Record.new("some_param")
19
+ }
20
+ end
21
+
22
+ def test_multiple_records_on_create
23
+ @bogusdb = Bogusdb::Record.create([{name: 'foo', dob: '04/14/75'},
24
+ {name: 'baz', dob: '01/01/00'} ])
25
+
26
+ assert_equal @bogusdb.first.name, 'foo'
27
+ end
28
+
29
+ def test_join_table_on_create
30
+ @bogusdb = Bogusdb::Record.new(name: 'foo', dob: '04/14/75', profile: { avatar: 'image.jpg' })
31
+ assert_equal @bogusdb.profile.avatar, 'image.jpg'
32
+ end
33
+
34
+ def test_join_array_on_create
35
+ @bogusdb = Bogusdb::Record.create(name: 'foo', dob: '04/14/75', favorite_colors: [ { name: "red" }, { name: "orange" }])
36
+ assert_equal @bogusdb.favorite_colors.first.name, "red"
37
+ end
38
+
39
+ def test_bogusdb_responds_to_join_table
40
+ assert_respond_to @bogusdb, :join_table
41
+ end
42
+
43
+ def test_bogusdb_responds_to_has_one
44
+ assert_respond_to @bogusdb, :has_one
45
+ end
46
+
47
+ def test_bogusdb_responds_to_has_many
48
+ assert_respond_to @bogusdb, :has_many
49
+ end
50
+
51
+ def test_bogusdb_responds_to_name
52
+ assert_respond_to @bogusdb, :name
53
+ end
54
+
55
+ def test_bogusdb_responds_to_dob
56
+ assert_respond_to @bogusdb, :dob
57
+ end
58
+
59
+ def test_record_responds_to_attributes
60
+ assert_respond_to(@bogusdb, :attributes, "Failure message.")
61
+ end
62
+
63
+ def test_attributes_returns_hash
64
+ assert_kind_of(Hash, @bogusdb.attributes)
65
+ end
66
+
67
+ def test_join_table_is_called_with_correct_params
68
+ @bogusdb.expects(:join_table).with(:profile, {avatar: 'image.jpg'})
69
+ @bogusdb.join_table(:profile, {avatar: 'image.jpg'})
70
+ end
71
+
72
+ def test_bogusdb_responds_to_joined_tables
73
+ @bogusdb.join_table(:profile, {avatar: 'image.jpg'})
74
+ assert_respond_to @bogusdb, :profile
75
+ end
76
+
77
+ def test_joined_tabled_responds_to_specific_column
78
+ @bogusdb.join_table(:profile, {avatar: 'image.jpg'})
79
+ assert_respond_to @bogusdb.profile.first, :avatar
80
+ end
81
+
82
+ def test_joined_table_attributes_returns_hash
83
+ @bogusdb.join_table(:profile, [{avatar: 'image.jpg'}, {avatar: 'sunny.jpg'}])
84
+ assert_kind_of(Hash, @bogusdb.profile.first.attributes)
85
+ end
86
+
87
+ def test_has_one_record_association
88
+ @bogusdb.has_one(:profile, {gender: 'm', avatar: 'me.jpg'})
89
+ assert_kind_of Bogusdb::Record, @bogusdb.profile
90
+ end
91
+
92
+ def test_has_many_record_associations
93
+ @bogusdb.has_many(:profiles, [{avatar: 'image.jpg'}, {avatar: 'sunny.jpg'}])
94
+ assert_kind_of Array, @bogusdb.profiles
95
+ end
96
+
97
+ def test_joined_tables_when_multiples_records_are_created
98
+ @bogusdb = Bogusdb::Record.create([{name: 'foo', dob: '04/14/75'},
99
+ {name: 'baz', dob: '01/01/00'}
100
+ ])
101
+
102
+ @bogusdb.map {|i| i.join_table(:profiles, [{avatar: 'me.jpg'},
103
+ {avatar: 'you.png'}])
104
+ }
105
+ assert_equal 'me.jpg', @bogusdb.first.profiles.first.avatar
106
+ end
107
+
108
+ end
@@ -0,0 +1,8 @@
1
+ require "minitest"
2
+ require "minitest/autorun"
3
+ require "minitest/pride"
4
+ require "mocha/setup"
5
+
6
+
7
+ require File.expand_path '../../lib/bogusdb.rb', __FILE__
8
+
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bogusdb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Bradley Smith
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mocha
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: ' A simple fake database object for testing '
70
+ email:
71
+ - bradleydsmith@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - bogusdb.gemspec
82
+ - lib/bogusdb.rb
83
+ - lib/bogusdb/record.rb
84
+ - lib/bogusdb/version.rb
85
+ - test/bogusdb_test.rb
86
+ - test/record_test.rb
87
+ - test/test_helper.rb
88
+ homepage: https://github.com/bradleyd/bogusdb.git
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.1.11
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: A simple fake ORM databsase object used in testing
112
+ test_files:
113
+ - test/bogusdb_test.rb
114
+ - test/record_test.rb
115
+ - test/test_helper.rb