rhino_project_core 0.20.0.beta.76 → 0.20.0.beta.77
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 +4 -4
- data/Rakefile +1 -0
- data/lib/rhino/test_case/model.rb +86 -0
- data/lib/rhino/test_case.rb +1 -0
- data/lib/rhino/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 297bd9f59d6598d9bc3bd82f41865cc1b9de5bbb992d888360d03ae7c80e275d
|
4
|
+
data.tar.gz: 4347d328c0ef0b992b3a70d72dddc35597ba7229bb3ef846051f49e668fb7ac6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 91ebdb1e1c10405669f99fbeeb1cc1231a811567ab9149631cb7739e9fad534db432e223c770dd07cae91b71a43f2da4cc2eda618fb2a79b95bd0bded5f82082
|
7
|
+
data.tar.gz: 9747f25f00a79e67a297e775ff859c68c6b808ec0d9e325a264c6cde4f3205b3fae141084bc91c8e9deb99f069172123f6492fb41313b9db711d5506ee1c8bd7
|
data/Rakefile
CHANGED
@@ -0,0 +1,86 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Rhino
|
4
|
+
module TestCase
|
5
|
+
class ModelTest < ActiveSupport::TestCase
|
6
|
+
protected
|
7
|
+
def assert_required(attribute)
|
8
|
+
instance = @model.new
|
9
|
+
instance[attribute] = nil
|
10
|
+
assert instance.invalid?
|
11
|
+
assert_includes instance.errors.messages[attribute], "can't be blank", ":#{attribute} should be required"
|
12
|
+
end
|
13
|
+
|
14
|
+
def assert_not_required(attribute)
|
15
|
+
instance = build factory
|
16
|
+
instance[attribute] = nil
|
17
|
+
assert instance.valid?, ":#{attribute} should not be required, got errors: #{instance.errors.full_messages}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def assert_association_required(association)
|
21
|
+
instance = @model.new
|
22
|
+
instance.send("#{association}_id=", nil)
|
23
|
+
assert instance.invalid?
|
24
|
+
assert_includes instance.errors.messages[association], "must exist", ":#{association} should be required"
|
25
|
+
end
|
26
|
+
|
27
|
+
def assert_uniqueness(attribute, scope: nil)
|
28
|
+
instance = create factory
|
29
|
+
duplicate = build factory, attribute => instance[attribute]
|
30
|
+
duplicate.send("#{scope}=", instance.send(scope)) if scope.present?
|
31
|
+
assert duplicate.invalid?, "Should be invalid with duplicate #{attribute}"
|
32
|
+
assert_includes duplicate.errors.messages[attribute], "has already been taken", ":#{attribute} should be unique"
|
33
|
+
end
|
34
|
+
|
35
|
+
# association_model is a symbol
|
36
|
+
def assert_accepts_nested_attributes_for(association_model, association_factory: association_model) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
|
37
|
+
association = @model.reflect_on_association(association_model)
|
38
|
+
|
39
|
+
assert association.present?, "association :#{association_model} not found"
|
40
|
+
|
41
|
+
key = if association.macro == :belongs_to || association.macro == :has_one
|
42
|
+
"#{association_model}_attributes"
|
43
|
+
else
|
44
|
+
"#{association_model.to_s.pluralize}_attributes"
|
45
|
+
end
|
46
|
+
|
47
|
+
success_checker = if association.macro == :belongs_to || association.macro == :has_one
|
48
|
+
->(instance) { instance.reload.send(association_model).present? }
|
49
|
+
else
|
50
|
+
->(instance) { instance.reload.send(association_model).any? }
|
51
|
+
end
|
52
|
+
|
53
|
+
assert @model.new.respond_to?("#{key}="),
|
54
|
+
"#{@model} doesn't have a #{key} setter method. Probably there's no `accepts_nested_attributes_for :#{association_model}` in #{@model}" # rubocop:disable Layout/LineLength
|
55
|
+
object = if association_factory.is_a?(Proc)
|
56
|
+
model_instance = build factory
|
57
|
+
association_instance = association_factory.call model_instance
|
58
|
+
model_instance.send("#{key}=", association_instance.attributes)
|
59
|
+
model_instance
|
60
|
+
else
|
61
|
+
build factory, { key => build(association_factory).attributes }
|
62
|
+
end
|
63
|
+
assert object.valid?, "Should be valid after assigning nested model attributes, but got errors: #{object.errors.full_messages}"
|
64
|
+
assert object.save, "Should be able to save after assigning nested model attributes, but got errors: #{object.errors.full_messages}"
|
65
|
+
assert success_checker.call(object), "Should have a #{association_model} after assigning nested model attributes"
|
66
|
+
end
|
67
|
+
|
68
|
+
def assert_destroy_association_cascade(association_model, association_factory: association_model) # rubocop:disable Metrics/AbcSize
|
69
|
+
instance = create factory
|
70
|
+
association_instance = if association_factory.is_a?(Proc)
|
71
|
+
association_factory.call(instance)
|
72
|
+
else
|
73
|
+
create association, { @model.to_s.underscore => instance }
|
74
|
+
end
|
75
|
+
assert instance.reload.send(association_model).any?, "Should have a #{association_model} after creating #{@model}"
|
76
|
+
assert instance.destroy, "Should be able to destroy #{@model}"
|
77
|
+
assert_not association_instance.class.exists?(association_instance.id), "Should destroy #{association_model} when destroying #{@model}"
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
def factory
|
82
|
+
@factory ||= @model.to_s.underscore.to_sym
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
data/lib/rhino/test_case.rb
CHANGED
data/lib/rhino/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rhino_project_core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.20.0.beta.
|
4
|
+
version: 0.20.0.beta.77
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- JP Rosevear
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-06-
|
11
|
+
date: 2024-06-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -227,6 +227,7 @@ files:
|
|
227
227
|
- lib/rhino/sieve/search.rb
|
228
228
|
- lib/rhino/test_case.rb
|
229
229
|
- lib/rhino/test_case/controller.rb
|
230
|
+
- lib/rhino/test_case/model.rb
|
230
231
|
- lib/rhino/test_case/override.rb
|
231
232
|
- lib/rhino/test_case/policy.rb
|
232
233
|
- lib/rhino/version.rb
|