fixtury 0.3.0 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c8026933c72e6773550464325733bf9c31c7fc601c00aabcc6fa18dd384439d6
4
- data.tar.gz: a96c566602765f01a949814362022d687e5b06e794d1c09ecbef8dd5fd254a34
3
+ metadata.gz: d422110eda024f73368d6d4e244f909fcbc0b288a223df00e7b527c18f551b13
4
+ data.tar.gz: 440dd84ef120db60bd9cad4bcdac952e43c61c915199801c27023c95775725c5
5
5
  SHA512:
6
- metadata.gz: 7b95b7af4242c7e90f29f913be405a4a50163c66b8d363b2187ca04e4f71ff59b9cfdf88acbf6c4754c226321f8663c6d54b02472fe9c49342a49b9f98a1e2b2
7
- data.tar.gz: 7b78291b5980f5dc3a854e31f15668ee6765a7173869d68b09a19d8e58cc06e60240c293fa7984afd7271c3d31e8977d5d1e0d96cf71bfd018ada973fe3cf75f
6
+ metadata.gz: 8b7c93a1dc5781514337168f86407a0db1c05b0697df2cfb2a595b7766f02f7fcf762b5a1102984e1b18bfcaaaa02d756128c6af4bbb2072a99c13f99809d068
7
+ data.tar.gz: 2fad96dd5f9aa75f8af8f0bfc62e15864d3fd70289dc353b8431fee1362bcd53842261b37b5fcba58d583177f9ef7aa0bea4426d81ddd8ab13b097ab76ff03d0
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fixtury (0.3.0)
4
+ fixtury (0.3.4)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,11 +1,14 @@
1
1
  # Fixtury
2
2
 
3
- The goal of this library is to provide an interface for accessing fixture data on-demand rather than having to manage all resources up front. By centralizing and wrapping the definitions of data generation, we can preload and optimize how we load data, yet allow deferred behaviors when desired.
3
+ Fixtury aims to provide an interface for creating, managing, and accessing test data in a simple and efficient way. It has no opinion on how you generate the data, it simply provides efficient ways to access it.
4
4
 
5
- For example, if a developer is running a test locally, there's no reason to build all fixtures for your suite.
5
+ Often, fixture frameworks require you to either heavily maintain static fixtures or generate all your fixtures at runtime. Fixtury attempts to find a middle ground that enables a faster and more effecient development process.
6
6
 
7
- ```
8
- class MyTest < ::Test
7
+ For example, if a developer is running a test locally in their development environment there's no reason to build all fixtures for your suite of 30k tests. Instead, if we're able to track the fixture dependencies of the tests that are running we can build (and cache) the data relevant for the specific tests that are run.
8
+
9
+ ```ruby
10
+ class MyTest < ::ActiveSupport::TestCase
11
+ include ::Fixtury::TestHooks
9
12
 
10
13
  fixtury "users.fresh"
11
14
  let(:user) { fixtury("users.fresh") }
@@ -15,7 +18,6 @@ class MyTest < ::Test
15
18
  end
16
19
 
17
20
  end
18
-
19
21
  ```
20
22
 
21
23
  Loading this file would ensure `users.fresh` is loaded into the fixture set before the suite is run. In the context of ActiveSupport::TestCase, the Fixtury::Hooks file will ensure the database records are present prior to your suite running. Setting `use_transactional_fixtures` ensures all records are rolled back prior to running another test.
@@ -8,15 +8,19 @@ require "fixtury/schema"
8
8
  require "fixtury/locator"
9
9
  require "fixtury/store"
10
10
 
11
+ # Top level namespace of the gem
11
12
  module Fixtury
12
13
 
14
+ # Shortcut for opening the top level schema.
13
15
  def self.define(&block)
14
16
  schema.define(&block)
15
17
  schema
16
18
  end
17
19
 
20
+ # The default top level schema. Fixtury::Schema instances can be completely self-contained but most
21
+ # usage would be through this shared definition.
18
22
  def self.schema
19
- @top_level_schema ||= ::Fixtury::Schema.new(parent: nil, name: "")
23
+ @schema ||= ::Fixtury::Schema.new(parent: nil, name: "")
20
24
  end
21
25
 
22
26
  end
@@ -7,14 +7,17 @@ module Fixtury
7
7
 
8
8
  attr_reader :name
9
9
  attr_reader :schema
10
+ alias parent schema
11
+ attr_reader :options
10
12
 
11
13
  attr_reader :callable
12
14
  attr_reader :enhancements
13
15
 
14
- def initialize(schema: nil, name:, &block)
16
+ def initialize(schema: nil, name:, options: {}, &block)
15
17
  @name = name
16
18
  @schema = schema
17
19
  @callable = block
20
+ @options = options
18
21
  @enhancements = []
19
22
  end
20
23
 
@@ -9,12 +9,17 @@ module Fixtury
9
9
  new(name, HOLDER_VALUE)
10
10
  end
11
11
 
12
- attr_reader :name, :value, :created_at
12
+ def self.create(name, value)
13
+ new(name, value)
14
+ end
15
+
16
+ attr_reader :name, :value, :created_at, :options
13
17
 
14
- def initialize(name, value)
18
+ def initialize(name, value, options = {})
15
19
  @name = name
16
20
  @value = value
17
21
  @created_at = Time.now.to_i
22
+ @options = options
18
23
  end
19
24
 
20
25
  def holder?
@@ -9,13 +9,14 @@ require "fixtury/errors/schema_frozen_error"
9
9
  module Fixtury
10
10
  class Schema
11
11
 
12
- attr_reader :definitions, :children, :name, :parent, :relative_name, :around_fixture_definition
12
+ attr_reader :definitions, :children, :name, :parent, :relative_name, :around_fixture_definition, :options
13
13
 
14
- def initialize(parent:, name:)
14
+ def initialize(parent:, name:, options: {})
15
15
  @name = name
16
16
  @parent = parent
17
17
  @relative_name = @name.split("/").last
18
18
  @around_fixture_definition = nil
19
+ @options = options
19
20
  @frozen = false
20
21
  reset!
21
22
  end
@@ -83,19 +84,19 @@ module Fixtury
83
84
  out.join("\n")
84
85
  end
85
86
 
86
- def namespace(name, &block)
87
+ def namespace(name, options = {}, &block)
87
88
  ensure_not_frozen!
88
89
  ensure_no_conflict!(name: name, definitions: true, namespaces: false)
89
90
 
90
- child = find_or_create_child_schema(name: name)
91
+ child = find_or_create_child_schema(name: name, options: options)
91
92
  child.instance_eval(&block) if block_given?
92
93
  child
93
94
  end
94
95
 
95
- def fixture(name, &block)
96
+ def fixture(name, options = {}, &block)
96
97
  ensure_not_frozen!
97
98
  ensure_no_conflict!(name: name, definitions: true, namespaces: true)
98
- create_child_definition(name: name, &block)
99
+ create_child_definition(name: name, options: options, &block)
99
100
  end
100
101
 
101
102
  def enhance(name, &block)
@@ -186,13 +187,13 @@ module Fixtury
186
187
  children[name.to_s]
187
188
  end
188
189
 
189
- def find_or_create_child_schema(name:)
190
+ def find_or_create_child_schema(name:, options:)
190
191
  name = name.to_s
191
192
  child = find_child_schema(name: name)
192
193
  child ||= begin
193
194
  children[name] = begin
194
195
  child_name = build_child_name(name: name)
195
- self.class.new(name: child_name, parent: self)
196
+ self.class.new(name: child_name, parent: self, options: options)
196
197
  end
197
198
  end
198
199
  child
@@ -202,9 +203,9 @@ module Fixtury
202
203
  definitions[name.to_s]
203
204
  end
204
205
 
205
- def create_child_definition(name:, &block)
206
+ def create_child_definition(name:, options:, &block)
206
207
  child_name = build_child_name(name: name)
207
- definition = ::Fixtury::Definition.new(name: child_name, schema: self, &block)
208
+ definition = ::Fixtury::Definition.new(name: child_name, schema: self, options: options, &block)
208
209
  definitions[name.to_s] = definition
209
210
  end
210
211
 
@@ -11,6 +11,9 @@ module Fixtury
11
11
  included do
12
12
  class_attribute :fixtury_dependencies
13
13
  self.fixtury_dependencies = Set.new
14
+
15
+ class_attribute :local_fixtury_dependencies
16
+ self.local_fixtury_dependencies = Set.new
14
17
  end
15
18
 
16
19
  module ClassMethods
@@ -22,7 +25,7 @@ module Fixtury
22
25
  if block_given?
23
26
  raise ArgumentError, "A fixture cannot be defined in an anonymous class" if name.nil?
24
27
 
25
- namespace = name.underscore
28
+ namespace = fixtury_namespace
26
29
 
27
30
  ns = ::Fixtury.schema
28
31
 
@@ -30,9 +33,11 @@ module Fixtury
30
33
  ns = ns.namespace(ns_name){}
31
34
  end
32
35
 
33
- names.each do |fixture_name|
36
+ names.map! do |fixture_name|
34
37
  ns.fixture(fixture_name, &definition)
35
- self.fixtury_dependencies += ["#{namespace}/#{fixture_name}"]
38
+ new_name = "/#{namespace}/#{fixture_name}"
39
+ self.local_fixtury_dependencies += [new_name]
40
+ new_name
36
41
  end
37
42
 
38
43
  # otherwise, just record the dependency
@@ -40,12 +45,14 @@ module Fixtury
40
45
  self.fixtury_dependencies += names.flatten.compact.map(&:to_s)
41
46
  end
42
47
 
43
- accessor_option = opts.key?(:accessor) ? opts[:accessor] : true
48
+ accessor_option = opts[:as]
49
+ accessor_option = opts[:accessor] if accessor_option.nil? # old version, backwards compatability
50
+ accessor_option = accessor_option.nil? ? true : accessor_option
44
51
 
45
52
  if accessor_option
46
53
 
47
54
  if accessor_option != true && names.length > 1
48
- raise ArgumentError, "A named :accessor option is only available when providing one fixture"
55
+ raise ArgumentError, "A named :as option is only available when providing one fixture"
49
56
  end
50
57
 
51
58
  names.each do |fixture_name|
@@ -64,6 +71,10 @@ module Fixtury
64
71
  end
65
72
  end
66
73
 
74
+ def fixtury_namespace
75
+ name.underscore
76
+ end
77
+
67
78
  end
68
79
 
69
80
  def fixtury(name)
@@ -71,14 +82,15 @@ module Fixtury
71
82
 
72
83
  name = name.to_s
73
84
 
85
+ # in the case that we're looking for a relative fixture, see if it's registered relative to the test's namespace.
74
86
  unless name.include?("/")
75
- local_name = "#{self.class.name.underscore}/#{name}"
76
- if self.fixtury_dependencies.include?(local_name)
87
+ local_name = "/#{self.class.fixtury_namespace}/#{name}"
88
+ if local_fixtury_dependencies.include?(local_name)
77
89
  return fixtury_store.get(local_name, execution_context: self)
78
90
  end
79
91
  end
80
92
 
81
- unless self.fixtury_dependencies.include?(name)
93
+ unless fixtury_dependencies.include?(name) || local_fixtury_dependencies.include?(name)
82
94
  raise ArgumentError, "Unrecognized fixtury dependency `#{name}` for #{self.class}"
83
95
  end
84
96
 
@@ -101,7 +113,7 @@ module Fixtury
101
113
 
102
114
  # piggybacking activerecord fixture setup for now.
103
115
  def setup_fixtures(*args)
104
- if fixtury_dependencies.any?
116
+ if fixtury_dependencies.any? || local_fixtury_dependencies.any?
105
117
  setup_fixtury_fixtures
106
118
  else
107
119
  super
@@ -110,7 +122,7 @@ module Fixtury
110
122
 
111
123
  # piggybacking activerecord fixture setup for now.
112
124
  def teardown_fixtures(*args)
113
- if fixtury_dependencies.any?
125
+ if fixtury_dependencies.any? || local_fixtury_dependencies.any?
114
126
  teardown_fixtury_fixtures
115
127
  else
116
128
  super
@@ -118,7 +130,7 @@ module Fixtury
118
130
  end
119
131
 
120
132
  def setup_fixtury_fixtures
121
- return unless use_transactional_fixtures
133
+ return unless fixtury_use_transactions?
122
134
 
123
135
  clear_expired_fixtury_fixtures!
124
136
  load_all_fixtury_fixtures!
@@ -129,7 +141,7 @@ module Fixtury
129
141
  end
130
142
 
131
143
  def teardown_fixtury_fixtures
132
- return unless use_transactional_fixtures
144
+ return unless fixtury_use_transactions?
133
145
 
134
146
  fixtury_database_connections.each(&:rollback_transaction)
135
147
  end
@@ -141,10 +153,17 @@ module Fixtury
141
153
  end
142
154
 
143
155
  def load_all_fixtury_fixtures!
144
- fixtury_dependencies.each do |name|
156
+ (fixtury_dependencies | local_fixtury_dependencies).each do |name|
145
157
  fixtury(name) unless fixtury_loaded?(name)
146
158
  end
147
159
  end
148
160
 
161
+ def fixtury_use_transactions?
162
+ return use_transactional_tests if respond_to?(:use_transactional_tests)
163
+ return use_transactional_fixtures if respond_to?(:use_transactional_fixtures)
164
+
165
+ true
166
+ end
167
+
149
168
  end
150
169
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Fixtury
4
4
 
5
- VERSION = "0.3.0"
5
+ VERSION = "0.3.5"
6
6
 
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fixtury
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Nelson
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-10-20 00:00:00.000000000 Z
11
+ date: 2020-10-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: autotest