-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathnested_save_operation_spec.cr
115 lines (94 loc) · 3.06 KB
/
nested_save_operation_spec.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
require "../spec_helper"
private class SaveBusiness < Business::SaveOperation
permit_columns name
has_one save_email_address : SaveEmailAddress
has_one save_tax_id : SaveTaxId
class SaveEmailAddress < EmailAddress::SaveOperation
permit_columns address
end
class SaveTaxId < TaxId::SaveOperation
permit_columns number
end
end
private class NestedParams
include Avram::Paramable
@business : Hash(String, String) = {} of String => String
@email_address : Hash(String, String) = {} of String => String
@tax_id : Hash(String, String) = {} of String => String
def initialize(@business, @email_address, @tax_id)
end
def nested?(key : String) : Hash(String, String)
if key == "email_address"
@email_address
elsif key == "tax_id"
@tax_id
elsif key == "business"
@business
else
raise "What is this key!? #{key}"
end
end
def nested(key : String) : Hash(String, String)
nested?(key)
end
def get?(key)
raise "Not implemented"
end
def get(key)
raise "Not implemented"
end
def nested_file?(key)
raise "Not implemented"
end
def nested_file(key)
raise "Not implemented"
end
end
describe "Avram::SaveOperation with nested operation" do
context "when not all forms are valid" do
it "does not save either" do
params = NestedParams.new business: {"name" => "Fubar"},
email_address: {"address" => ""},
tax_id: {"number" => ""}
SaveBusiness.create(params) do |operation, business|
business.should be_nil
operations_saved?(operation, false)
end
params = NestedParams.new business: {"name" => "Fubar"},
email_address: {"address" => "123 Main St."},
tax_id: {"number" => ""}
SaveBusiness.create(params) do |operation, business|
business.should be_nil
operations_saved?(operation, false)
end
params = NestedParams.new business: {"name" => "Fubar"},
email_address: {"address" => ""},
tax_id: {"number" => "123"}
SaveBusiness.create(params) do |operation, business|
business.should be_nil
operations_saved?(operation, false)
end
end
end
context "all forms are valid" do
it "sets the relationship and saves both" do
params = NestedParams.new business: {"name" => "Fubar"},
email_address: {"address" => "[email protected]"},
tax_id: {"number" => "123"}
SaveBusiness.create(params) do |operation, business|
operations_saved?(operation, saved?: true)
operation.errors.keys.size.should eq 0
operation.save_tax_id.errors.keys.size.should eq 0
operation.save_email_address.errors.keys.size.should eq 0
business.not_nil!.name.should eq "Fubar"
business.not_nil!.email_address!.address.should eq "[email protected]"
business.not_nil!.tax_id!.number.should eq 123
end
end
end
end
private def operations_saved?(operation, saved? : Bool)
operation.saved?.should eq(saved?)
operation.save_email_address.saved?.should eq(saved?)
operation.save_tax_id.saved?.should eq(saved?)
end