karsthammer-dataset 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,49 @@
1
+ *1.1.0 [STI, belongs_to] (February 14, 2009)
2
+
3
+ * STI is better supported for inserting, naming and finding records [aiwilliams]
4
+
5
+ class Place < ActiveRecord::Base; end
6
+ class State < Place; end
7
+ class NorthCarolina < State; end
8
+
9
+ create_record(NorthCarolina, :state) # no need to define the 'type' column value
10
+ states(:state) == places(:state) == north_carolinas(:state) # read with the class names pluralized
11
+
12
+ * Moved to jeweler for much cleaner, github embracing gem building [aiwilliams]
13
+ * Support for simple belongs to associations [aiwilliams]
14
+
15
+ class Person < ActiveRecord::Base; end
16
+ class Note < ActiveRecord::Base
17
+ belongs_to :person
18
+ end
19
+
20
+ person_id = create_record Person, :myguy
21
+ create_record Note, :person => :myguy
22
+ Note.last.person_id == person_id
23
+
24
+ * Models inside modules are supported a little more conveniently [aiwilliams]
25
+
26
+ module MList
27
+ class Message < ActiveRecord::Base
28
+ end
29
+ end
30
+
31
+ # We'll get rid of the underscore in 'm_list_messages'
32
+ create_record MList::Message, :test
33
+ mlist_messages(:test)
34
+
35
+ * Helper method for converting strings to useful symbols for finding records [siannopollo]
36
+
37
+ This is useful if you write creator methods of your own.
38
+
39
+ def create_person(attributes)
40
+ create_record Person, name_to_sym(attributes[:name]), attributes
41
+ end
42
+
43
+ create_person(:name => 'Little John')
44
+ people(:little_john)
45
+
46
+
47
+ *1.0.0 [Scenarios Replacement] (December 15, 2008)
48
+
49
+ * Drop-in replacement for Scenarios plugin of old [aiwilliams]
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2008-2009, Adam Williams
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
data/README ADDED
@@ -0,0 +1,111 @@
1
+ = Dataset
2
+
3
+ Dataset provides a simple API for creating and finding sets of data in your database. Check out Dataset::RecordMethods and Dataset::ModelFinders.
4
+
5
+ Dataset loads data intelligently if you use 'nested contexts' in your tests (RSpec, anything that uses Test::Unit::TestCase subclassing for creating nested contexts):
6
+
7
+ describe Something do
8
+ dataset :a => Dataset :a is loaded (at the right time)
9
+
10
+ it 'should whatever'
11
+ end
12
+
13
+ describe More do
14
+ dataset :b => Dataset :b is loaded. :a data is still there
15
+
16
+ it 'should'
17
+ end
18
+ end
19
+
20
+ describe Another do => Database is restored to :a, without re-running :a logic
21
+ it 'should'
22
+ end
23
+ end
24
+ end
25
+
26
+ The goal is to see a marked improvement in overall test run speed, basing this on the assumption that it is faster to have the OS copy a file or mySQL dump and load. Of course, we may find this to be a false assumption, but there were plenty of bugs in the former 'Scenarios' - addressing that afforded the opportunity to test the assumption.
27
+
28
+
29
+ Dataset does not prevent you from using other libraries like Machinist or factory_girl. If you were to used either of those, you could have a dataset like this:
30
+
31
+ require 'faker'
32
+
33
+ class OrganizationsDataset < Dataset::Base
34
+ Sham.name { Faker::Name.name }
35
+
36
+ Organization.blueprint do
37
+ name { Sham.name }
38
+ end
39
+
40
+ def load
41
+ name_model Organization.make, :org_one
42
+ end
43
+ end
44
+
45
+ The benefit is that you can reuse interesting sets of data, without sacrificing the utility of those other libraries.
46
+
47
+ describe Organization, 'stuff' do
48
+ dataset :organizations
49
+ end
50
+
51
+ describe Organization, 'other stuff' do
52
+ dataset :organizations
53
+ end
54
+
55
+
56
+ Get things installed, then read more in the Dataset documentation at http://aiwilliams.github.com/dataset
57
+
58
+
59
+ == Installation
60
+
61
+ Install the plugin:
62
+
63
+ ./script/plugin install git://github.com/aiwilliams/dataset.git
64
+
65
+ In your test_helper.rb/spec_helper.rb:
66
+
67
+ require 'dataset'
68
+ class Test::Unit::TestCase
69
+ include Dataset
70
+ datasets_directory "#{RAILS_ROOT}/spec/datasets"
71
+ end
72
+
73
+ If you don't use rspec_on_rails, or you have specs that aren't of the RailsExampleGroup type, you should do this in spec_helper.rb:
74
+
75
+ require 'dataset'
76
+ class Spec::Example::ExampleGroup
77
+ include Dataset
78
+ datasets_directory "#{RAILS_ROOT}/spec/datasets"
79
+ end
80
+
81
+ If you were a user of the Scenarios plugin, and want to do as little as possible to get going (assumes you are using rspec_on_rails):
82
+
83
+ require 'dataset'
84
+ Scenario = Scenarios = Dataset
85
+ class Test::Unit::TestCase
86
+ include Dataset
87
+ class << self
88
+ alias_method :scenario, :dataset
89
+ end
90
+ end
91
+ class ScenariosResolver < Dataset::DirectoryResolver
92
+ def suffix
93
+ @suffix ||= 'Scenario'
94
+ end
95
+ end
96
+ Dataset::Resolver.default = ScenariosResolver.new("#{RAILS_ROOT}/spec/scenarios")
97
+
98
+
99
+ == Credits
100
+
101
+ Written by [Adam Williams](http://github.com/aiwilliams).
102
+
103
+ Contributors:
104
+
105
+ - [Saturn Flyer](http://www.saturnflyer.com) [github](http://github.com/saturnflyer)
106
+ - [Steve Iannopollo](http://github.com/siannopollo)
107
+ - [John Long](http://github.com/jlong)
108
+
109
+ ---
110
+
111
+ Dataset is released under the MIT-License and is Copyright (c)2008 Adam Williams.
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), 'lib'))
2
+
3
+ require 'rubygems'
4
+ require 'spec/rake/spectask'
5
+
6
+ task :default => :spec
7
+
8
+ desc "Run all specs"
9
+ Spec::Rake::SpecTask.new do |t|
10
+ t.spec_files = FileList['spec/**/*_spec.rb']
11
+ t.spec_opts = ['--options', 'spec/spec.opts']
12
+ end
13
+
14
+ begin
15
+ require 'jeweler'
16
+ Jeweler::Tasks.new do |s|
17
+ s.name = 'dataset'
18
+ s.summary = 'A simple API for creating and finding sets of data in your database, built on ActiveRecord.'
19
+ s.email = 'adam@thewilliams.ws'
20
+ s.files = FileList["[A-Z]*", "{lib,tasks}/**/*", "plugit/descriptor.rb"].exclude("tmp")
21
+ s.homepage = "http://github.com/aiwilliams/dataset"
22
+ s.description = s.summary
23
+ s.authors = ['Adam Williams']
24
+ end
25
+ rescue LoadError
26
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
27
+ end
data/TODO ADDED
@@ -0,0 +1,15 @@
1
+ take any instance variables already in context and make them available to dataset blocks - this is for nested describes
2
+ I'm not sure about this one. It can be very frustrating to lose context of when the state of an iv is modified.
3
+
4
+ add ability to clear the database (some tests wanted to guarantee a clear db)
5
+ This is acheived with "dataset {}"
6
+
7
+ clear database completely at beginning of session, only tables where data was created within a session??
8
+
9
+ clear all dumps on new run of tests
10
+ be sure we are capturing a dataset if it has already be captured before during a run
11
+ describe what happens when someone has a fixtures file - they get loaded after our datasets, thereby causing all the data in the table of the fixture file (like things.yml) to be deleted - the fixtures are then loaded
12
+ look into truncating database instead individual table deletes
13
+ allow configuration of dataset
14
+ * permatable / global scope
15
+ re-evaluation location of some tests that depend on TestCase in non-test/unit tests
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :minor: 1
3
+ :patch: 0
4
+ :major: 1
data/lib/dataset.rb ADDED
@@ -0,0 +1,123 @@
1
+ require 'dataset/version'
2
+ require 'dataset/instance_methods'
3
+ require 'dataset/base'
4
+ require 'dataset/database/base'
5
+ require 'dataset/database/mysql'
6
+ require 'dataset/database/sqlite3'
7
+ require 'dataset/database/postgresql'
8
+ require 'dataset/collection'
9
+ require 'dataset/load'
10
+ require 'dataset/resolver'
11
+ require 'dataset/session'
12
+ require 'dataset/session_binding'
13
+ require 'dataset/record/meta'
14
+ require 'dataset/record/fixture'
15
+ require 'dataset/record/model'
16
+
17
+ # == Quick Start
18
+ #
19
+ # Write a test. If you want some data in your database, create a dataset.
20
+ # Start simple.
21
+ #
22
+ # describe States do
23
+ # dataset do
24
+ # [%w(Colorado CO), %w(North\ Carolina NC), %w(South\ Carolina SC)].each do |name,abbrev|
25
+ # create_record :state, abbrev.downcase, :name => name, :abbrev => abbrev
26
+ # end
27
+ # end
28
+ #
29
+ # it 'should have an abbreviated name'
30
+ # states(:nc).abbrev.should be('NC')
31
+ # end
32
+ #
33
+ # it 'should have a name'
34
+ # states(:nc).name.should be('North Carolin')
35
+ # end
36
+ # end
37
+ #
38
+ # Notice that you won't be using _find_id_ or _find_model_ in your tests. You
39
+ # use methods like _states_ and _state_id_, as in the example above.
40
+ #
41
+ # When you find that you're seeing patterns in the data you are creating, pull it into a class.
42
+ #
43
+ # spec/datasets/states.rb
44
+ # class StatesDataset < Dataset::Base
45
+ # def load
46
+ # # create useful data
47
+ # end
48
+ # end
49
+ #
50
+ # spec/models/state.rb
51
+ # describe State do
52
+ # dataset :states
53
+ # end
54
+ #
55
+ # == Installation
56
+ #
57
+ # Dataset is installed into your testing environment by requiring the library,
58
+ # then including it into the class that will be the context of your test
59
+ # methods.
60
+ #
61
+ # require 'dataset'
62
+ # class Test::Unit::TestCase
63
+ # include Dataset
64
+ # datasets_directory "#{RAILS_ROOT}/test/datasets"
65
+ # end
66
+ #
67
+ # Note that should you desire your Dataset::Base subclasses be
68
+ # auto-discovered, you can set the _datasets_directory_.
69
+ #
70
+ module Dataset
71
+ def self.included(test_context) # :nodoc:
72
+ if test_context.name =~ /TestCase\Z/
73
+ require 'dataset/extensions/test_unit'
74
+ elsif test_context.name =~ /ExampleGroup\Z/
75
+ require 'dataset/extensions/rspec'
76
+ else
77
+ raise "I don't understand your test framework"
78
+ end
79
+
80
+ test_context.extend ContextClassMethods
81
+ end
82
+
83
+ # Methods that are added to the class that Dataset is included in (the test
84
+ # context class).
85
+ #
86
+ module ContextClassMethods
87
+ def self.extended(context_class) # :nodoc:
88
+ context_class.module_eval do
89
+ include InstanceMethods
90
+ superclass_delegating_accessor :dataset_session
91
+ end
92
+ end
93
+
94
+ mattr_accessor :datasets_database_dump_path
95
+ self.datasets_database_dump_path = File.expand_path(RAILS_ROOT + '/tmp/dataset') if defined?(RAILS_ROOT)
96
+
97
+ # Replaces the default Dataset::Resolver with one that will look for
98
+ # dataset class definitions in the specified directory. Captures of the
99
+ # database will be stored in a subdirectory 'tmp' (see
100
+ # Dataset::Database::Base).
101
+ def datasets_directory(path)
102
+ Dataset::Resolver.default = Dataset::DirectoryResolver.new(path)
103
+ Dataset::ContextClassMethods.datasets_database_dump_path = File.join(path, '/tmp/dataset')
104
+ end
105
+
106
+ def add_dataset(*datasets, &block) # :nodoc:
107
+ dataset_session = dataset_session_in_hierarchy
108
+ datasets.each { |dataset| dataset_session.add_dataset(self, dataset) }
109
+ dataset_session.add_dataset(self, Class.new(Dataset::Block) {
110
+ define_method :doload, block
111
+ }) unless block.nil?
112
+ end
113
+
114
+ def dataset_session_in_hierarchy # :nodoc:
115
+ self.dataset_session ||= begin
116
+ database_spec = ActiveRecord::Base.configurations['test'].with_indifferent_access
117
+ database_class = Dataset::Database.const_get(database_spec[:adapter].classify)
118
+ database = database_class.new(database_spec, Dataset::ContextClassMethods.datasets_database_dump_path)
119
+ Dataset::Session.new(database)
120
+ end
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,157 @@
1
+ module Dataset
2
+
3
+ # The superclass of your Dataset classes.
4
+ #
5
+ # It is recommended that you create a dataset using the Dataset::Block
6
+ # method first, then grow into using classes as you recognize patterns in
7
+ # your test data creation. This will help you to keep simple things simple.
8
+ #
9
+ class Base
10
+ class << self
11
+ # Allows a subclass to define helper methods that should be made
12
+ # available to instances of this dataset, to datasets that use this
13
+ # dataset, and to tests that use this dataset.
14
+ #
15
+ # This feature is great for providing any kind of method that would help
16
+ # test the code around the data your dataset creates. Be careful,
17
+ # though, to keep from adding business logic to these methods! That
18
+ # belongs in your production code.
19
+ #
20
+ def helpers(&method_definitions)
21
+ @helper_methods ||= begin
22
+ mod = Module.new
23
+ include mod
24
+ mod
25
+ end
26
+ @helper_methods.module_eval &method_definitions
27
+ end
28
+
29
+ def helper_methods # :nodoc:
30
+ @helper_methods
31
+ end
32
+
33
+ # Allows a subsclass to declare which datasets it uses.
34
+ #
35
+ # Dataset is designed to promote 'design by composition', rather than
36
+ # 'design by inheritance'. You should not use class hiearchies to share
37
+ # data and code in your datasets. Instead, you can write something like
38
+ # this:
39
+ #
40
+ # class PeopleDataset < Dataset::Base; end
41
+ # class DepartmentsDataset < Dataset::Base; end
42
+ # class OrganizationsDataset < Dataset::Base
43
+ # uses :people, :departments
44
+ # end
45
+ #
46
+ # When the OrganizationsDataset is loaded, it will have all the data
47
+ # from the datasets is uses, as well as all of the helper methods
48
+ # defined by those datasets.
49
+ #
50
+ # When a dataset uses other datasets, and those datasets themselves use
51
+ # datasets, things will be loaded in the order of dependency you would
52
+ # expect:
53
+ #
54
+ # C uses B
55
+ # A uses C
56
+ # B, C, A is the load order
57
+ #
58
+ def uses(*datasets)
59
+ @used_datasets = datasets
60
+ end
61
+
62
+ def used_datasets # :nodoc:
63
+ @used_datasets
64
+ end
65
+ end
66
+
67
+ # Invoked once before a collection of tests is run. If you use a dataset
68
+ # in multiple test classes, it will be called once for each of them -
69
+ # remember that the database will be cleared at the beginning of running a
70
+ # 'suite' or 'group' of tests, unless you are using nested contexts (as in
71
+ # nested describe blocks in RSpec).
72
+ #
73
+ # Override this method in your subclasses.
74
+ #
75
+ def load; end
76
+ end
77
+
78
+ # The easiest way to create some data before a suite of tests is run is by
79
+ # using a Dataset::Block. An example works wonders:
80
+ #
81
+ # class PeopleTest < Test::Unit::TestCase
82
+ # dataset do
83
+ # create_record :person, :billy, :name => 'Billy'
84
+ # end
85
+ #
86
+ # def test_name
87
+ # assert_equal 'Billy', people(:billy).name
88
+ # end
89
+ # end
90
+ #
91
+ # The database will be cleared and billy will be inserted once before
92
+ # running each of the tests within a transaction. All the normal transaction
93
+ # fixtures stuff will still work.
94
+ #
95
+ # One of the great features of Dataset, at least when things get really
96
+ # interesting in your data needs, is that nested contexts will be additive.
97
+ # Consider this:
98
+ #
99
+ # describe Something do
100
+ # dataset :a => Dataset :a is loaded (at the right time)
101
+ #
102
+ # it 'should whatever'
103
+ # end
104
+ #
105
+ # describe More do
106
+ # dataset :b => Dataset :b is loaded. :a data is still there
107
+ #
108
+ # it 'should'
109
+ # end
110
+ # end
111
+ #
112
+ # describe Another do => Database is restored to :a, without re-running :a logic
113
+ # it 'should'
114
+ # end
115
+ # end
116
+ # end
117
+ #
118
+ # == Instance Variables
119
+ #
120
+ # You may also assign instance variables in a dataset block, and they will
121
+ # be available to your test methods. You have to be careful with this in a
122
+ # similar way that you must with an RSpec before :all block. Since the
123
+ # instance variables are pointing to the same instances accross all tests,
124
+ # things can get weird if you intend to change their state. It's best use is
125
+ # for loading objects that you want to read a lot without loading over and
126
+ # over again for each test.
127
+ #
128
+ # == Building on Other Datasets
129
+ #
130
+ # You may pass any number of Dataset::Base subclasses - or better yet, their
131
+ # names - to the dataset method. When you use a block, this adds a lot of
132
+ # clarity:
133
+ #
134
+ # class PersonTest < Test::Unit::TestCase
135
+ # dataset :organization, :people do
136
+ # id = create_record :person, :second_admin, :name => 'Admin Three'
137
+ # create_record :organization_administratorship, :organization_id => organization_id(:first_bank), :person_id => id
138
+ # end
139
+ #
140
+ # def test_admins
141
+ # assert organizations(:first_bank).admins.include?(people(:second_admin))
142
+ # end
143
+ # end
144
+ #
145
+ # == Reusing a Dataset
146
+ #
147
+ # When you need to go beyond the block, create a Dataset::Base subclass!
148
+ class Block < Base
149
+ include Dataset::InstanceMethods
150
+
151
+ def load # :nodoc:
152
+ dataset_session_binding.install_block_variables(self)
153
+ doload
154
+ dataset_session_binding.copy_block_variables(self)
155
+ end
156
+ end
157
+ end