👩‍🚒 Firefighters API written in Gleam!
gleam

:recycle: setup `Context` during unit tests by using a callback function

kacaii.bsky.social f67e16e7 c8a00ce0

verified
+4 -9
dev/app_dev.gleam
··· 20 20 const n_occurences = 200 21 21 22 22 pub fn main() -> Nil { 23 - let ctx = setup_context() 23 + use ctx <- setup_context() 24 24 25 25 case argv.load().arguments { 26 26 ["admin"] -> app.setup_admin(ctx) ··· 94 94 io.println("Total de " <> n_created_occ_str <> " ocorrências criadas") 95 95 } 96 96 97 - fn setup_context() { 97 + fn setup_context(next: fn(Context) -> a) -> a { 98 98 let db_process_name = process.new_name("db_conn") 99 99 let registry_name = process.new_name("registry") 100 100 let env = context.Dev ··· 105 105 let db = pog.named_connection(db_process_name) 106 106 let assert Ok(_) = pog.start(config) 107 107 108 - Context( 109 - static_directory: app.static_directory(), 110 - db:, 111 - registry_name:, 112 - secret_key_base:, 113 - env:, 114 - ) 108 + let static_directory = app.static_directory() 109 + next(Context(static_directory:, db:, registry_name:, secret_key_base:, env:)) 115 110 }
+2 -2
test/admin_test.gleam
··· 14 14 import youid/uuid 15 15 16 16 pub fn admin_update_user_test() { 17 - let ctx = app_test.global_data() 17 + use ctx <- app_test.global_data() 18 18 use _ <- list.each(list.range(1, app_test.n_tests)) 19 19 20 20 // DUMMY USER ---------------------------------------------------------------- ··· 92 92 } 93 93 94 94 pub fn update_user_status_test() { 95 - let ctx = app_test.global_data() 95 + use ctx <- app_test.global_data() 96 96 let base_path = "/admin/users/" 97 97 use _ <- list.each(list.range(1, app_test.n_tests)) 98 98
+3 -3
test/app_test.gleam
··· 18 18 } 19 19 20 20 /// Global context data used in unit tests 21 - pub fn global_data() -> Context { 21 + pub fn global_data(next: fn(Context) -> a) -> a { 22 22 use <- global_value.create_with_unique_name("global_context") 23 23 24 24 let db_process_name = process.new_name("db_conn") ··· 32 32 let assert Ok(_) = pog.start(config) 33 33 34 34 let static_directory = app.static_directory() 35 - Context(static_directory:, db:, registry_name:, secret_key_base:, env:) 35 + next(Context(static_directory:, db:, registry_name:, secret_key_base:, env:)) 36 36 } 37 37 38 38 ///  Create a request with admin privileges 39 39 pub fn with_authorization(next req: wisp.Request) -> wisp.Request { 40 - let ctx = global_data() 40 + use ctx <- global_data() 41 41 42 42 let login_req = 43 43 simulate.browser_request(http.Post, "/user/login")
+3 -3
test/brigade_test.gleam
··· 15 15 16 16 pub fn register_new_brigade_test() { 17 17 let path = "/admin/brigade/new" 18 - let ctx = app_test.global_data() 18 + use ctx <- app_test.global_data() 19 19 use _ <- list.each(list.range(1, app_test.n_tests)) 20 20 21 21 // 󰚩 DUMMY ------------------------------------------------------------------ ··· 88 88 } 89 89 90 90 pub fn get_brigade_members_test() { 91 - let ctx = app_test.global_data() 91 + use ctx <- app_test.global_data() 92 92 use _ <- list.each(list.range(1, app_test.n_tests)) 93 93 94 94 //  DUMMY LEADER ----------------------------------------------------------- ··· 143 143 } 144 144 145 145 pub fn get_all_brigades_test() { 146 - let ctx = app_test.global_data() 146 + use ctx <- app_test.global_data() 147 147 use _ <- list.each(list.range(1, app_test.n_tests)) 148 148 let path = "/admin/brigade" 149 149
+1 -1
test/dashboard_test.gleam
··· 6 6 import wisp/simulate 7 7 8 8 pub fn dashboard_stats_test() { 9 - let ctx = app_test.global_data() 9 + use ctx <- app_test.global_data() 10 10 11 11 let req = simulate.browser_request(http.Get, "/dashboard/stats") 12 12 let resp = http_router.handle_request(req, ctx)
+7 -7
test/occurrence_test.gleam
··· 19 19 import youid/uuid 20 20 21 21 pub fn register_new_occurrence_test() { 22 - let ctx = app_test.global_data() 22 + use ctx <- app_test.global_data() 23 23 use _ <- list.each(list.range(1, app_test.n_tests)) 24 24 25 25 // DUMMY USERS ----------------------------------------------------------- ··· 124 124 } 125 125 126 126 pub fn get_occurrences_by_applicant_test() { 127 - let ctx = app_test.global_data() 127 + use ctx <- app_test.global_data() 128 128 use _ <- list.each(list.range(1, app_test.n_tests)) 129 129 130 130 // DUMMY USERS ----------------------------------------------------------- ··· 215 215 } 216 216 217 217 pub fn delete_occurrence_test() { 218 - let ctx = app_test.global_data() 218 + use ctx <- app_test.global_data() 219 219 use _ <- list.each(list.range(1, app_test.n_tests)) 220 220 221 221 // DUMMY ··· 249 249 } 250 250 251 251 pub fn close_occurrence_test() { 252 - let ctx = app_test.global_data() 252 + use ctx <- app_test.global_data() 253 253 let base_path = "/occurrence/close/" 254 254 use _ <- list.each(list.range(1, app_test.n_tests)) 255 255 ··· 303 303 } 304 304 305 305 pub fn close_missing_occurrence_test() { 306 - let ctx = app_test.global_data() 306 + use ctx <- app_test.global_data() 307 307 let base_path = "/occurrence/close/" 308 308 let path = base_path <> uuid.v7_string() 309 309 ··· 316 316 } 317 317 318 318 pub fn reopen_occurrence_test() { 319 - let ctx = app_test.global_data() 319 + use ctx <- app_test.global_data() 320 320 let base_path = "/occurrence/reopen/" 321 321 use _ <- list.each(list.range(1, app_test.n_tests)) 322 322 ··· 364 364 } 365 365 366 366 pub fn reopen_missing_occurrence_test() { 367 - let ctx = app_test.global_data() 367 + use ctx <- app_test.global_data() 368 368 let base_path = "/occurrence/reopen/" 369 369 let path = base_path <> uuid.v7_string() 370 370
+1 -1
test/role_test.gleam
··· 8 8 import wisp/simulate 9 9 10 10 pub fn get_role_list_test() { 11 - let ctx = app_test.global_data() 11 + use ctx <- app_test.global_data() 12 12 13 13 let req = simulate.browser_request(http.Get, "/user/roles") 14 14 let resp = http_router.handle_request(req, ctx)
+7 -7
test/user_test.gleam
··· 16 16 import youid/uuid 17 17 18 18 pub fn login_test() { 19 - let ctx = app_test.global_data() 19 + use ctx <- app_test.global_data() 20 20 use _ <- list.each(list.range(1, app_test.n_tests)) 21 21 22 22 let req = ··· 50 50 } 51 51 52 52 pub fn signup_test() { 53 - let ctx = app_test.global_data() 53 + use ctx <- app_test.global_data() 54 54 use _ <- list.each(list.range(1, app_test.n_tests)) 55 55 56 56 let dummy_password = wisp.random_string(10) ··· 93 93 } 94 94 95 95 pub fn signup_registration_taken_test() { 96 - let ctx = app_test.global_data() 96 + use ctx <- app_test.global_data() 97 97 98 98 let dummy_pswd = wisp.random_string(10) 99 99 let taken_registration = "000" ··· 119 119 } 120 120 121 121 pub fn signup_email_taken_test() { 122 - let ctx = app_test.global_data() 122 + use ctx <- app_test.global_data() 123 123 124 124 let dummy_pswd = wisp.random_string(10) 125 125 let taken_email = "admin@email.com" ··· 144 144 } 145 145 146 146 pub fn signup_phone_taken_test() { 147 - let ctx = app_test.global_data() 147 + use ctx <- app_test.global_data() 148 148 149 149 let dummy_pswd = wisp.random_string(10) 150 150 let taken_phone = "0000000000" ··· 169 169 } 170 170 171 171 pub fn get_all_users_test() { 172 - let ctx = app_test.global_data() 172 + use ctx <- app_test.global_data() 173 173 174 174 let req = simulate.browser_request(http.Get, "/admin/users") 175 175 let resp = http_router.handle_request(req, ctx) ··· 200 200 } 201 201 202 202 pub fn update_user_profile_test() { 203 - let ctx = app_test.global_data() 203 + use ctx <- app_test.global_data() 204 204 use _ <- list.each(list.range(1, app_test.n_tests)) 205 205 let login_path = "/user/login" 206 206 let signup_path = "/admin/signup"