@@ -2,6 +2,7 @@ defmodule TdCache.StructureCacheTest do
22 use ExUnit.Case
33
44 import Assertions
5+ import TdCache.Factory
56
67 alias TdCache.LinkCache
78 alias TdCache.Redix
@@ -11,21 +12,8 @@ defmodule TdCache.StructureCacheTest do
1112 doctest TdCache.StructureCache
1213
1314 setup do
14- system = % { id: System . unique_integer ( [ :positive ] ) , external_id: "foo" , name: "bar" }
15-
16- structure = % {
17- id: System . unique_integer ( [ :positive ] ) ,
18- name: "name" ,
19- external_id: "ext_id" ,
20- group: "group" ,
21- type: "type" ,
22- path: [ "foo" , "bar" ] ,
23- updated_at: DateTime . utc_now ( ) ,
24- metadata: % { "alias" => "source_alias" } ,
25- system_id: system . id ,
26- domain_ids: [ 1 , 2 ] ,
27- deleted_at: DateTime . utc_now ( )
28- }
15+ system = build ( :system )
16+ structure = build ( :structure , system_id: system . id )
2917
3018 { :ok , _ } = SystemCache . put ( system )
3119
@@ -68,6 +56,38 @@ defmodule TdCache.StructureCacheTest do
6856 assert s . system == system
6957 end
7058
59+ test "reads many structures" , % { system: system } do
60+ % { id: domain_id1 } = build ( :domain )
61+ % { id: domain_id2 } = build ( :domain )
62+
63+ [ % { id: id1 } , % { id: id2 } , % { id: id3 } ] =
64+ inserted_structures =
65+ Enum . map ( 1 .. 3 , fn _ ->
66+ structure =
67+ build ( :structure , domain_ids: [ domain_id1 , domain_id2 ] , system_id: system . id )
68+
69+ { :ok , _ } = StructureCache . put ( structure )
70+ structure
71+ end )
72+
73+ ids = [ id1 , id2 , id3 , id1 ]
74+
75+ not_valid_id = Enum . max ( ids ) + 1
76+
77+ { :ok , cache_structures } = StructureCache . get_many ( Enum . shuffle ( ids ++ [ not_valid_id ] ) )
78+
79+ assert Enum . count ( cache_structures ) == 3
80+
81+ assert Enum . all? ( cache_structures , fn % { id: structure_id , name: structure_name } ->
82+ Enum . find ( inserted_structures , fn % { id: inserted_id , name: inserted_name } ->
83+ inserted_id == structure_id and inserted_name == structure_name
84+ end )
85+ end )
86+
87+ Redix . command! ( [ "DEL" ] ++ Redix . command! ( [ "KEYS" , "data_structure:*" ] ) )
88+ Redix . command! ( [ "DEL" , "domain:deleted_ids" ] )
89+ end
90+
7191 test "returns an empty map for metadata if not present" , % { structure: structure } do
7292 structure = Map . delete ( structure , :metadata )
7393 assert { :ok , _ } = StructureCache . put ( structure )
@@ -93,15 +113,15 @@ defmodule TdCache.StructureCacheTest do
93113 end
94114
95115 test "does not update a structure already cached in redis having same update_at value" , % {
96- structure: structure
116+ structure: % { external_id: external_id } = structure
97117 } do
98118 { :ok , _ } = StructureCache . put ( structure )
99119 { :ok , s } = StructureCache . get ( structure . id )
100120 assert s
101121 updated_structure = Map . put ( structure , :external_id , "new_ext_id" )
102122 { :ok , _ } = StructureCache . put ( updated_structure )
103123 { :ok , s } = StructureCache . get ( structure . id )
104- assert s . external_id == "ext_id"
124+ assert s . external_id == external_id
105125 end
106126
107127 test "updates a structure already cached in redis when deleted_at has changed" , % {
0 commit comments