fixpoints 0.1.3 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 64fc801ca2cedc0a4abc0de5eda59a90ade61b99b462bff2dcad91c7f11909f7
4
- data.tar.gz: 60267a9065b27e86f9180a3da0243267a0fcdc7aa45a8b9f35390ad3e5ac5fdc
3
+ metadata.gz: a94f748e8f266bfd2d83d42166715d17ad166e383d1e009fd40fb154e4642ecb
4
+ data.tar.gz: a6136f20da4c80d9a64edcf9e4fe5602e7182da81140cc277728d6dcdaae3ea6
5
5
  SHA512:
6
- metadata.gz: 3e6d1e3ae14981d469135f62ca1b5f66f81e8239cbe27b7965694791ac22a79652cea2532cfc8c35f625aa870131d73cb56a9c245ed8d7e4a044d28956702b09
7
- data.tar.gz: 94b15a4d1b186466eed68c7db66c32f1f73c8bb168a818a46a137181adacddb67756ccdb1f911c1bd6f12a7547033235682616a065829c5719fb8196ba71a379
6
+ metadata.gz: '0708fcb7a177d855efdecd36f023dace60a1ae18e88dec97aafbf4c9ae929c5d30549cf07035de9d1e20b6416198c659e491ded5e7b4a329ddef41ebc1c59a5b'
7
+ data.tar.gz: edebfda332b465d557454b248f4b917b7f8c1801da6f937ec7e6efb01baa2904fe237bdadb29d94241ebc916301eeb70e59bbb4f7291335e918664724806336e
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fixpoints (0.1.2)
4
+ fixpoints (0.2.0)
5
5
  activerecord (>= 5.0.0)
6
6
  rspec
7
7
 
data/README.md CHANGED
@@ -54,7 +54,7 @@ RSpec.describe 'User Flow', order: :defined do # !!! mind the order here !!!
54
54
  fill_in 'Name', with: 'Tom'
55
55
  click_on 'Save'
56
56
 
57
- store_fixpoint_unless_present :registred_user
57
+ store_fixpoint_unless_present :registered_user
58
58
  # creates a YAML file containing all records (/spec/fixpoints/registred_user.yml)
59
59
  end
60
60
 
@@ -113,6 +113,15 @@ In order to achieve this, we must make sure that we let the store function know
113
113
  end
114
114
  ```
115
115
 
116
+ **Multiple Databases** If an application uses multiple databases, you can use the optional `connection` parameter
117
+ to specify the database connection to use.
118
+
119
+ ```ruby
120
+ it 'posts an item' do
121
+ restore_fixpoint :registered_user, connection: ActiveRecord::Base.connection
122
+ # ...
123
+ end
124
+ ```
116
125
 
117
126
  ## Limitations & Known issues
118
127
 
@@ -45,8 +45,8 @@ class Fixpoint
45
45
  end
46
46
 
47
47
  # Creates a Fixpoint from the database contents. Empty tables are skipped.
48
- def from_database
49
- new(read_database_records)
48
+ def from_database(conn)
49
+ new(read_database_records(conn))
50
50
  end
51
51
 
52
52
  def remove(fixname)
@@ -56,7 +56,7 @@ class Fixpoint
56
56
  # reset primary key sequences for all tables
57
57
  # useful when tests sometimes run before the storing the first fixpoint.
58
58
  # these test might have incremented the id sequence already, so the ids in the fixpoints chance (which leads to differences).
59
- def reset_pk_sequences!
59
+ def reset_pk_sequences!(conn)
60
60
  return unless conn.respond_to?(:reset_pk_sequence!)
61
61
  conn.tables.each { |table_name| conn.reset_pk_sequence!(table_name) }
62
62
  end
@@ -69,10 +69,6 @@ class Fixpoint
69
69
  File.join(fspath, "#{fixname}.yml")
70
70
  end
71
71
 
72
- def conn
73
- ActiveRecord::Base.connection
74
- end
75
-
76
72
  protected
77
73
 
78
74
  def fixpoints_path
@@ -85,13 +81,13 @@ class Fixpoint
85
81
  File.join(spec_path, FIXPOINT_FOLDER)
86
82
  end
87
83
 
88
- def read_database_records
84
+ def read_database_records(conn)
89
85
  # adapted from: https://yizeng.me/2017/07/16/generate-rails-test-fixtures-yaml-from-database-dump/
90
86
  tables = conn.tables
91
87
  tables.reject! { |table_name| TABLES_TO_SKIP.include?(table_name) }
92
88
 
93
89
  tables.each_with_object({}) do |table_name, acc|
94
- result = conn.select_all("SELECT * FROM #{table_name}")
90
+ result = conn.select_all("SELECT * FROM #{conn.quote_table_name(table_name)}")
95
91
  next if result.count.zero?
96
92
 
97
93
  rows = result.to_a
@@ -107,7 +103,7 @@ class Fixpoint
107
103
  @records_in_tables = records_in_tables
108
104
  end
109
105
 
110
- def load_into_database
106
+ def load_into_database(conn)
111
107
  # Here some more pointers on implementation details of fixtures:
112
108
  # - https://github.com/rails/rails/blob/2998672fc22f0d5e1a79a29ccb60d0d0e627a430/activerecord/lib/active_record/fixtures.rb#L612
113
109
  # - http://api.rubyonrails.org/v5.2.4/classes/ActiveRecord/FixtureSet.html#method-c-create_fixtures
@@ -123,7 +119,7 @@ class Fixpoint
123
119
 
124
120
  # actually insert
125
121
  conn.insert_fixtures_set(@records_in_tables)
126
- self.class.reset_pk_sequences!
122
+ self.class.reset_pk_sequences!(conn)
127
123
  end
128
124
 
129
125
  def save_to_file(fixname)
@@ -146,8 +142,6 @@ class Fixpoint
146
142
 
147
143
  protected
148
144
 
149
- delegate :conn, to: :class
150
-
151
145
  def contents_for_file
152
146
  YAML.dump(@records_in_tables)
153
147
  end
@@ -1,8 +1,8 @@
1
1
  # Helper methods to be included into RSpec
2
2
  module FixpointTestHelpers
3
- def restore_fixpoint(fixname)
3
+ def restore_fixpoint(fixname, connection: default_connection)
4
4
  @last_restored = fixname
5
- IncrementalFixpoint.from_file(fixname).load_into_database
5
+ IncrementalFixpoint.from_file(fixname).load_into_database(connection)
6
6
  end
7
7
 
8
8
  # Compares the fixpoint with the records in the database.
@@ -16,10 +16,10 @@ module FixpointTestHelpers
16
16
  # ---
17
17
  # If we refactor this to a gem, we should rely on rspec (e.g. use minitest or move comparison logic to Fixpoint class).
18
18
  # Anyhow, we keep it like this for now, because the expectations give much nicer output than the minitest assertions.
19
- def compare_fixpoint(fixname, ignored_columns=[:updated_at, :created_at], tables_to_compare: :all, store_fixpoint_and_fail: false, parent_fixname: nil)
19
+ def compare_fixpoint(fixname, ignored_columns=[:updated_at, :created_at], tables_to_compare: :all, store_fixpoint_and_fail: false, parent_fixname: nil, connection: default_connection)
20
20
  if !IncrementalFixpoint.exists?(fixname)
21
21
  if store_fixpoint_and_fail
22
- store_fixpoint(fixname, parent_fixname)
22
+ store_fixpoint(fixname, parent_fixname, connection: connection)
23
23
  pending("Fixpoint \"#{fixname}\" did not exist yet. Skipping comparison, but created fixpoint from database. Try re-running the test.")
24
24
  fail
25
25
  else
@@ -27,7 +27,7 @@ module FixpointTestHelpers
27
27
  end
28
28
  end
29
29
 
30
- database_fp = IncrementalFixpoint.from_database
30
+ database_fp = IncrementalFixpoint.from_database(nil, connection)
31
31
  fixpoint_fp = IncrementalFixpoint.from_file(fixname)
32
32
 
33
33
  tables_to_compare = (database_fp.table_names + fixpoint_fp.table_names).uniq if tables_to_compare == :all
@@ -45,14 +45,18 @@ module FixpointTestHelpers
45
45
 
46
46
  # it is not a good idea to overwrite the fixpoint each time because timestamps may change (which then shows up in version control).
47
47
  # Hence we only provide a method to write to it if it does not exist.
48
- def store_fixpoint_unless_present(fixname, parent_fixname = nil)
49
- store_fixpoint(fixname, parent_fixname) unless IncrementalFixpoint.exists?(fixname)
48
+ def store_fixpoint_unless_present(fixname, parent_fixname = nil, connection: default_connection)
49
+ store_fixpoint(fixname, parent_fixname, connection: connection) unless IncrementalFixpoint.exists?(fixname)
50
50
  end
51
51
 
52
52
  # +parent_fixname+ when given, only the (incremental) changes to the parent are saved
53
53
  # please see store_fixpoint_unless_present for note on why not to use this method
54
- def store_fixpoint(fixname, parent_fixname = nil)
54
+ def store_fixpoint(fixname, parent_fixname = nil, connection: default_connection)
55
55
  parent_fixname = @last_restored if parent_fixname == :last_restored
56
- IncrementalFixpoint.from_database(parent_fixname).save_to_file(fixname)
56
+ IncrementalFixpoint.from_database(parent_fixname, connection).save_to_file(fixname)
57
+ end
58
+
59
+ private def default_connection
60
+ ActiveRecord::Base.connection
57
61
  end
58
62
  end
@@ -1,3 +1,3 @@
1
1
  module Fixpoints
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -33,11 +33,11 @@ class IncrementalFixpoint < Fixpoint
33
33
  end
34
34
 
35
35
  # Creates a Fixpoint from the database contents. Empty tables are skipped.
36
- def self.from_database(parent_fixname=nil)
37
- return super() if parent_fixname.nil?
36
+ def self.from_database(parent_fixname=nil, conn)
37
+ return super(conn) if parent_fixname.nil?
38
38
 
39
39
  parent = from_file(parent_fixname)
40
- changes_in_tables = FixpointDiff.extract_changes(parent.records_in_tables, read_database_records)
40
+ changes_in_tables = FixpointDiff.extract_changes(parent.records_in_tables, read_database_records(conn))
41
41
  new(changes_in_tables, parent_fixname)
42
42
  end
43
43
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fixpoints
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Rothe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-15 00:00:00.000000000 Z
11
+ date: 2020-09-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord