From 712c1f2d06df8e97766fe1d5672ec8a71436f725 Mon Sep 17 00:00:00 2001 From: "kacaii.dev" Date: Fri, 14 Aug 2026 12:19:18 -0300 Subject: [PATCH] test: add global_value for singletons --- gleam.toml | 1 + manifest.toml | 2 ++ src/server.gleam | 11 +++++++---- test/server_test.gleam | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 4 deletions(-) diff --git a/gleam.toml b/gleam.toml index bf71c36..720ab34 100644 --- a/gleam.toml +++ b/gleam.toml @@ -23,6 +23,7 @@ envoy = ">= 1.2.0 and < 2.0.0" mist = ">= 6.0.3 and < 7.0.0" wisp = ">= 2.2.2 and < 3.0.0" filepath = ">= 1.1.2 and < 2.0.0" +global_value = ">= 1.0.0 and < 2.0.0" [dev_dependencies] gleeunit = ">= 1.0.0 and < 2.0.0" diff --git a/manifest.toml b/manifest.toml index c5a57f8..d3b4d8f 100644 --- a/manifest.toml +++ b/manifest.toml @@ -28,6 +28,7 @@ packages = [ { name = "gleeunit", version = "1.11.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "EC31ABA74256AEA531EDF8169931D775BBB384FED0A8A1BDC4DD9354E3E21826" }, { name = "glexer", version = "2.5.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "splitter"], otp_app = "glexer", source = "hex", outer_checksum = "D423CF63B8D4F9654EF53D0AF4904B249EB0679BD41C63440170AC508C9CC65E" }, { name = "glisten", version = "9.0.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_otp", "gleam_stdlib", "logging"], otp_app = "glisten", source = "hex", outer_checksum = "7795AA50830656F3A0316A6B26595F893C83272DA901B3405E31339CAA31A10B" }, + { name = "global_value", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "global_value", source = "hex", outer_checksum = "23F74C91A7B819C43ABCCBF49DAD5BB8799D81F2A3736BA9A534BD47F309FF4F" }, { name = "gramps", version = "6.0.1", build_tools = ["gleam"], requirements = ["gleam_crypto", "gleam_erlang", "gleam_http", "gleam_stdlib"], otp_app = "gramps", source = "hex", outer_checksum = "D55636072DEE173F6586A5679D3C02EC7A0DE3F8646B78C351B72908FF223DF7" }, { name = "houdini", version = "1.2.1", build_tools = ["gleam"], requirements = [], otp_app = "houdini", source = "hex", outer_checksum = "6F8AC2F12974567FB744BEA66AC93CEB76AAEA19AD28564623F76CDA9BC26A85" }, { name = "hpack_erl", version = "0.3.0", build_tools = ["rebar3"], requirements = [], otp_app = "hpack", source = "hex", outer_checksum = "D6137D7079169D8C485C6962DFE261AF5B9EF60FBC557344511C1E65E3D95FB0" }, @@ -60,6 +61,7 @@ gleam_http = { version = ">= 4.3.0 and < 5.0.0" } gleam_otp = { version = ">= 1.3.0 and < 2.0.0" } gleam_stdlib = { version = ">= 1.0.0 and < 2.0.0" } gleeunit = { version = ">= 1.0.0 and < 2.0.0" } +global_value = { version = ">= 1.0.0 and < 2.0.0" } mist = { version = ">= 6.0.3 and < 7.0.0" } pog = { version = ">= 4.1.0 and < 5.0.0" } squirrel = { version = ">= 4.7.0 and < 5.0.0" } diff --git a/src/server.gleam b/src/server.gleam index 6bc2f0b..a79b524 100644 --- a/src/server.gleam +++ b/src/server.gleam @@ -13,7 +13,8 @@ pub fn main() -> Nil { //  Postgres let db_process_name = process.new_name("db_connection_pool") - let assert Ok(pg_config) = read_database_connection(db_process_name) + let assert Ok(database_url) = envoy.get("DATABASE_URL") + let assert Ok(pog_config) = configure_database(db_process_name, database_url) let database = pog.named_connection(db_process_name) // others @@ -23,12 +24,14 @@ pub fn main() -> Nil { let handler = router.handle_request(_, context) // start supervision tree - let assert Ok(_) = supervision_tree.start(handler, pg_config, secret_key) + let assert Ok(_) = supervision_tree.start(handler, pog_config, secret_key) process.sleep_forever() } -pub fn read_database_connection(name: process.Name(pog.Message)) { - let assert Ok(database_url) = envoy.get("DATABASE_URL") +pub fn configure_database( + name: process.Name(pog.Message), + database_url: String, +) -> Result(pog.Config, Nil) { pog.url_config(name, database_url) } diff --git a/test/server_test.gleam b/test/server_test.gleam index 902c4da..7a462ab 100644 --- a/test/server_test.gleam +++ b/test/server_test.gleam @@ -1,5 +1,40 @@ +import envoy +import gleam/erlang/process import gleeunit +import global_value +import pog +import server +import server/context pub fn main() -> Nil { gleeunit.main() } + +/// Create a persistent `Context` value, available to all processes. +fn global_context() -> context.Context { + global_value.create_with_unique_name("server_test.global.data", fn() { + let db_name = process.new_name("database_connection") + let assert Ok(pog_url) = envoy.get("DATABASE_URL") + let assert Ok(pog_config) = server.configure_database(db_name, pog_url) + let database = pog.named_connection(db_name) + + // --- Building the Context type + let assert Ok(static_directory) = server.static_directory() + let assert Ok(_db_process) = pog.start(pog_config) + + context.Context(static_directory:, database:, secret_key: "test") + }) +} + +pub fn with_context(next: fn(context.Context) -> a) -> Nil { + let context = global_context() + let transaction = fn(database) { + next(context.Context(..context, database:)) + Error(Nil) + } + + let assert Error(pog.TransactionRolledBack(Nil)) = + pog.transaction(context.database, transaction) + + Nil +} -- 2.51.2