dump_hook 0.0.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
+ SHA256:
3
+ metadata.gz: 7e3ab2dfea012e21d78286bb192ea9991f56f796877d917c9afc5a5720a58c13
4
+ data.tar.gz: 9088a1c0514ec39ea1c185d5b560b6c309a4840c28481df7a82a553f647d1363
5
+ SHA512:
6
+ metadata.gz: 1e2fcfa0fbfc91aa1203b918f0b654fa2b071cbb8a747a1a0b765263d8615d56cbf574ef60a7777177c0a262a87c152bcc25ed52ccbd0d45f0e522747160ed25
7
+ data.tar.gz: 4b9bae4d2badc64e58d94e4aa16f5c0b8b3a1d4d52c2f602f98860006ab3fb70fc7abb3e3addbfd1a3f80633afdf000b9be72d8d2fe0eb00407b9e98e196548f
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.3
5
+ before_install: gem install bundler -v 1.12.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at shurik.v.r@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dump-hook.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Alexander Rjazantsev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,163 @@
1
+ # Dump Hook
2
+
3
+ A library that helps cache your data received from performing user actions in tests. We use it in our acceptance tests to enhance
4
+ performance of preparing data for the tests. It contains ways to cache Postgres and MySql DBs and
5
+ you need to take care about other sources your own.
6
+
7
+ ## Installation
8
+
9
+ ```ruby
10
+ gem 'dump-hook'
11
+ ```
12
+
13
+ For Rspec/Capybara add to `rails_helper.rb`:
14
+ ```ruby
15
+ require "dump_hook"
16
+
17
+ RSpec.configure do |config|
18
+ config.include DumpHook
19
+ end
20
+ ```
21
+
22
+ For Cucumber/Capybara add to `env.rb`:
23
+ ```ruby
24
+ require "dump_hook"
25
+
26
+ World(DumpHook)
27
+ ```
28
+
29
+ ## Configuration
30
+
31
+ There are several parameters to run and manage it.
32
+
33
+ ```ruby
34
+ DumpHook.setup do |config|
35
+ config.database = ActiveRecord::Base.configurations[Rails.env]["database"]
36
+ config.actual = Date.today.monday.to_s(:number)
37
+ end
38
+ ```
39
+
40
+ ### Db settings:
41
+ * `database` - database name
42
+ * `host` - DB server
43
+ * `port` - DB server port
44
+ * `username` - user to connect
45
+ * `password` - password to connect, works just for MySql
46
+ * `database_type` - `postgres`/`mysql`, by default it's `postgres`
47
+
48
+ It looks like `database` is a single required parameter. Others may be default for you DB connection. In relation to
49
+ `password` for `postgres` you need to use the url way in `database` parameter in order to set `password`. You may use
50
+ the gem `database_url` to get this url.
51
+
52
+ ```ruby
53
+ config.database = postgresql://username:password@localhost/database
54
+ ```
55
+ ### Dump hook settings
56
+ * `actual` - an attribute to manage how long or when you need to create a new dump for your actions. For instance, you
57
+ may set it to `Date.current` to recreate your dumps every day. By default it's empty and your dumps will not recreate.
58
+ * `remove_old_dumps` - when `actual` is pointed your old dumps will be removed. By default it's `true`.
59
+ * `dumps_location` - by default it's `tmp/dump_hook`. You may pass something more exciting, e.g your current git branch
60
+ or some path to store your dumps in your repo and generate them using CI.
61
+
62
+ ## Usage
63
+
64
+ *Attention!* It works for clear DB. In case when you have some previous data you may come across with conflicts or
65
+ incorrect data which may influence your tests.
66
+
67
+ There is just single method what wrap your actions and restore dump again
68
+ ```ruby
69
+ execute_with_dump(name, opts={}, &block)
70
+ ```
71
+ * `name` - identity what explain meaning of this background
72
+ * `opts[:actual]` - the same to the global parameter `actual` what override it
73
+ * `opts[:created_on]` - Date time to use in Time travelling for time dependent tests. In this case `actual` is
74
+ unnecessary and ignored. If you want to recreate such dumps you need to clean them manually.
75
+
76
+ ### Capybara example
77
+ ```ruby
78
+ execute_with_dump("user_data") do
79
+ create_superadmin
80
+ login_as_superadmin
81
+ go_to_users
82
+ add_user("John Doe")
83
+ add_user("Adam Smith")
84
+ add_user("Robert Martin")
85
+ add_user("Kent Beck")
86
+ logout
87
+ end
88
+
89
+ ```
90
+ ### Cucumber example
91
+
92
+ You can use the capybara way or create a wrap step to have all actions in your features
93
+ ```ruby
94
+ Given(/^There is "(.*?)" background with:$/) do |name, steps_order|
95
+ execute_with_dump(name) do
96
+ steps steps_order.to_s
97
+ end
98
+ end
99
+ ```
100
+
101
+ and use it in your scenarios
102
+ ```cucumber
103
+ Feature: User can modify project
104
+ Background:
105
+ Given There is "project_modification" background with:
106
+ """
107
+ There is the user data
108
+ I login as superadmin
109
+ I create a project "Time tracking"
110
+ I logout
111
+ """
112
+
113
+ Scenario: I can rename project
114
+ Given I login as "John Doe"
115
+ And I go to the projects
116
+ And I click "Edit" for "Time tracking" project
117
+ When I fill out "name" with "Time management"
118
+ And I click "Save"
119
+ Then I see the notification "Project is updated"
120
+ ```
121
+
122
+ In this example `There is the user data` is step what wraps the capybara example
123
+
124
+ ## How to add new sources or hooks
125
+ _Disclaimer_: We are working on enhancements
126
+
127
+ If you need to add yet one source, e.g "we have yet one service and need to dump its data" you may use something similar
128
+ ```ruby
129
+ module YourServiceHook
130
+ def store_dump(filename)
131
+ super
132
+ data = YourServiceAPI.import
133
+ File.open(your_service_filename(filename), "w") do |f|
134
+ f.write(data)
135
+ end
136
+ end
137
+
138
+ def restore_dump(filename)
139
+ super
140
+ buffer = File.open(your_service_filename(filename)).read
141
+ YourServiceAPI.export(buffer)
142
+ end
143
+
144
+ def your_service_filename
145
+ filename.gsub(".dump", ".your_service")
146
+ end
147
+ end
148
+ ```
149
+ and add it after `DumpHook` module
150
+ ```ruby
151
+ include DumpHook
152
+ include YourServiceHook
153
+ ```
154
+
155
+ ## TODO
156
+
157
+ * Dump other sources like `cookies`, Elastic Search, etc.
158
+ * Enhance ways of adding hooks to extend current sources without overriding(In progress)
159
+ * Add some sugar to create common dumps for CI or `parallel_tests`
160
+
161
+ ## License
162
+
163
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "dump_hook"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/dump_hook.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dump_hook/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dump_hook"
8
+ spec.version = DumpHook::VERSION
9
+ spec.authors = ["Alexander Ryazantsev"]
10
+ spec.email = ["shurik.v.r@gmail.com"]
11
+
12
+ spec.summary = %q{Dumps to cache you backgrounds}
13
+ spec.description = %q{We use it for our capybara/Cucumber features.}
14
+ spec.homepage = "https://github.com/Anadea/dump_hook"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "bin"
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency 'timecop'
23
+
24
+ spec.add_development_dependency "bundler", "~> 2.0"
25
+ spec.add_development_dependency "rake", ">= 12.3.3"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency "sequel"
28
+ spec.add_development_dependency "pg"
29
+ spec.add_development_dependency "mysql2"
30
+ end
@@ -0,0 +1,3 @@
1
+ module DumpHook
2
+ VERSION = "0.0.2"
3
+ end
data/lib/dump_hook.rb ADDED
@@ -0,0 +1,114 @@
1
+ require "dump_hook/version"
2
+ require "timecop"
3
+
4
+ module DumpHook
5
+ class Settings
6
+ attr_accessor :database,
7
+ :dumps_location,
8
+ :remove_old_dumps,
9
+ :actual,
10
+ :database_type,
11
+ :username,
12
+ :password,
13
+ :host,
14
+ :port
15
+
16
+ def initialize
17
+ @database = 'please set database'
18
+ @database_type = 'postgres'
19
+ @dumps_location = 'tmp/dump_hook'
20
+ @remove_old_dumps = true
21
+ end
22
+ end
23
+
24
+ class << self
25
+ attr_accessor :settings
26
+ end
27
+
28
+ def self.setup
29
+ self.settings = Settings.new
30
+ yield(settings)
31
+ end
32
+
33
+ def execute_with_dump(name, opts={}, &block)
34
+ created_on = opts[:created_on]
35
+ actual = opts[:actual] || settings.actual
36
+ create_dirs_if_not_exists
37
+ filename = full_filename(name, created_on, actual)
38
+ if File.exists?(filename)
39
+ restore_dump(filename)
40
+ else
41
+ if created_on
42
+ Timecop.travel(created_on)
43
+ elsif actual && settings.remove_old_dumps
44
+ FileUtils.rm(Dir.glob(full_filename(name, nil, "*")))
45
+ end
46
+ block.call
47
+ Timecop.return
48
+ store_dump(filename)
49
+ end
50
+ end
51
+
52
+ def settings
53
+ DumpHook.settings
54
+ end
55
+
56
+ def store_dump(filename)
57
+ case settings.database_type
58
+ when 'postgres'
59
+ args = ['-a', '-x', '-O', '-f', filename, '-Fc', '-T', 'schema_migrations']
60
+ args.concat(pg_connection_args)
61
+ Kernel.system("pg_dump", *args)
62
+ when 'mysql'
63
+ args = mysql_connection_args
64
+ args << "--compress"
65
+ args.concat ["--result-file", filename]
66
+ args.concat ["--ignore-table", "#{settings.database}.schema_migrations"]
67
+ Kernel.system("mysqldump", *args)
68
+ end
69
+ end
70
+
71
+ def restore_dump(filename)
72
+ case settings.database_type
73
+ when 'postgres'
74
+ args = pg_connection_args
75
+ args << filename
76
+ Kernel.system("pg_restore", *args)
77
+ when 'mysql'
78
+ args = mysql_connection_args
79
+ args.concat ["-e", "source #{filename}"]
80
+ Kernel.system("mysql", *args)
81
+ end
82
+ end
83
+
84
+ def full_filename(name, created_on, actual)
85
+ name_with_created_on = name
86
+ if created_on
87
+ name_with_created_on = "#{name_with_created_on}_#{created_on.to_s(:number)}"
88
+ elsif actual
89
+ name_with_created_on = "#{name_with_created_on}_actual#{actual}"
90
+ end
91
+ "#{settings.dumps_location}/#{name_with_created_on}.dump"
92
+ end
93
+
94
+ def create_dirs_if_not_exists
95
+ FileUtils.mkdir_p(settings.dumps_location)
96
+ end
97
+
98
+ def mysql_connection_args
99
+ args = [settings.database]
100
+ args.concat ["-u", settings.username] if settings.username
101
+ args.concat ["-p", settings.password] if settings.password
102
+ args.concat ["-h", settings.host] if settings.host
103
+ args.concat ["-P", settings.port] if settings.port
104
+ args
105
+ end
106
+
107
+ def pg_connection_args
108
+ args = ['-d', settings.database]
109
+ args.concat(['-U', settings.username]) if settings.username
110
+ args.concat(['-h', settings.host]) if settings.host
111
+ args.concat(['-p', settings.port]) if settings.port
112
+ args
113
+ end
114
+ end
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dump_hook
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Ryazantsev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-07-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: timecop
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 12.3.3
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 12.3.3
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sequel
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pg
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: mysql2
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: We use it for our capybara/Cucumber features.
112
+ email:
113
+ - shurik.v.r@gmail.com
114
+ executables:
115
+ - console
116
+ - setup
117
+ extensions: []
118
+ extra_rdoc_files: []
119
+ files:
120
+ - ".gitignore"
121
+ - ".rspec"
122
+ - ".travis.yml"
123
+ - CODE_OF_CONDUCT.md
124
+ - Gemfile
125
+ - LICENSE.txt
126
+ - README.md
127
+ - Rakefile
128
+ - bin/console
129
+ - bin/setup
130
+ - dump_hook.gemspec
131
+ - lib/dump_hook.rb
132
+ - lib/dump_hook/version.rb
133
+ homepage: https://github.com/Anadea/dump_hook
134
+ licenses:
135
+ - MIT
136
+ metadata: {}
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubygems_version: 3.1.6
153
+ signing_key:
154
+ specification_version: 4
155
+ summary: Dumps to cache you backgrounds
156
+ test_files: []