@@ -12,6 +12,33 @@ defmodule Collections.SimpleTest do
12
12
{ :ok , [ pid: pid ] }
13
13
end
14
14
15
+ defmodule Task do
16
+ use Collection
17
+
18
+ collection "tasks" do
19
+ attribute :name , String . t ( ) , default: "Fix errors"
20
+ attribute :status , integer ( ) , derived: true
21
+ after_load & Task . after_load / 1
22
+ end
23
+
24
+ def after_load ( task ) do
25
+ % Task { task | status: :loaded }
26
+ end
27
+
28
+ def insert_one ( task , top ) do
29
+ with map <- dump ( task ) ,
30
+ { :ok , _ } <- Mongo . insert_one ( top , @ collection , map ) do
31
+ :ok
32
+ end
33
+ end
34
+
35
+ def find_one ( id , top ) do
36
+ top
37
+ |> Mongo . find_one ( @ collection , % { @ id => id } )
38
+ |> load ( )
39
+ end
40
+ end
41
+
15
42
defmodule Label do
16
43
use Collection
17
44
@@ -89,6 +116,21 @@ defmodule Collections.SimpleTest do
89
116
assert % Card { intro: "new intro" , label: % Label { color: :red , name: "warning" } } = struct_card
90
117
end
91
118
119
+ test "dump derived attributes" , c do
120
+ alias Collections.SimpleTest.Task
121
+ task = % Task { Task . new ( ) | status: :red }
122
+ assert Map . has_key? ( Task . dump ( task ) , "status" ) == false
123
+
124
+ assert :ok = Task . insert_one ( task , c . pid )
125
+
126
+ task = Task . find_one ( task . _id , c . pid )
127
+
128
+ assert % Task { status: :loaded } = task
129
+
130
+ task = Mongo . find_one ( c . pid , "tasks" , % { _id: task . _id } )
131
+ assert Map . has_key? ( task , "status" ) == false
132
+ end
133
+
92
134
test "save and find" , c do
93
135
alias Collections.SimpleTest.Card
94
136
alias Collections.SimpleTest.Label
0 commit comments