@@ -2,6 +2,8 @@ defmodule MeadowWeb.AuthorityRecordsControllerTest do
2
2
use Meadow.DataCase
3
3
use MeadowWeb.ConnCase , async: true
4
4
5
+ alias Meadow.Repo
6
+ alias NUL.AuthorityRecords
5
7
alias NUL.Schemas.AuthorityRecord
6
8
7
9
describe "POST /api/authority_records/:filename (failure)" do
@@ -45,6 +47,7 @@ defmodule MeadowWeb.AuthorityRecordsControllerTest do
45
47
authority_record_fixture ( % { label: "Ver Steeg, Dorothy A." } ) ,
46
48
authority_record_fixture ( % { label: "Netsch, Walter A." } )
47
49
]
50
+
48
51
:ok
49
52
end
50
53
@@ -62,4 +65,61 @@ defmodule MeadowWeb.AuthorityRecordsControllerTest do
62
65
assert response ( conn , 200 )
63
66
end
64
67
end
68
+
69
+ describe "POST /api/authority_records/bulk_create" do
70
+ setup % { fixture: fixture } do
71
+ upload = % Plug.Upload {
72
+ content_type: "text/csv" ,
73
+ filename: "authority_import.csv" ,
74
+ path: fixture
75
+ }
76
+
77
+ { :ok , % { upload: upload } }
78
+ end
79
+
80
+ @ tag fixture: "test/fixtures/authority_records/bad_authority_import.csv"
81
+ test "bad data" , % { conn: conn , upload: upload } do
82
+ conn =
83
+ conn
84
+ |> auth_user ( user_fixture ( "TestAdmins" ) )
85
+ |> post ( "/api/authority_records/bulk_create" , % { records: upload } )
86
+
87
+ assert response ( conn , 400 )
88
+ end
89
+
90
+ @ tag fixture: "test/fixtures/authority_records/good_authority_import.csv"
91
+ test "good data" , % { conn: conn , upload: upload } do
92
+ precount = Meadow.Repo . aggregate ( AuthorityRecord , :count )
93
+
94
+ conn =
95
+ conn
96
+ |> auth_user ( user_fixture ( "TestAdmins" ) )
97
+ |> post ( "/api/authority_records/bulk_create" , % { records: upload } )
98
+
99
+ assert Meadow.Repo . aggregate ( AuthorityRecord , :count ) == precount + 3
100
+ assert conn . state == :chunked
101
+ assert response_content_type ( conn , :csv )
102
+ assert response ( conn , 200 )
103
+ end
104
+
105
+ @ tag fixture: "test/fixtures/authority_records/good_authority_import.csv"
106
+ test "one duplicate entry" , % { conn: conn , upload: upload } do
107
+ AuthorityRecords . create_authority_record ( % {
108
+ label: "Second Imported Thing" ,
109
+ hint: "preexisting entry"
110
+ } )
111
+
112
+ precount = Meadow.Repo . aggregate ( AuthorityRecord , :count )
113
+
114
+ conn =
115
+ conn
116
+ |> auth_user ( user_fixture ( "TestAdmins" ) )
117
+ |> post ( "/api/authority_records/bulk_create" , % { records: upload } )
118
+
119
+ assert Meadow.Repo . aggregate ( AuthorityRecord , :count ) == precount + 2
120
+ assert conn . state == :chunked
121
+ assert response_content_type ( conn , :csv )
122
+ assert response ( conn , 200 )
123
+ end
124
+ end
65
125
end
0 commit comments