An easy-to-host PDS on the ATProtocol, MacOS. Grandma-approved.

feat(relay): add db: SqlitePool to AppState and update test fixture

authored by malpercio.dev and committed by

Tangled 5798ac51 36ca3005

+26 -7
+26 -7
crates/relay/src/app.rs
··· 10 10 // Read by handlers once XRPC endpoints are implemented; suppressed until then. 11 11 #[allow(dead_code)] 12 12 pub config: Arc<Config>, 13 + // Read by handlers once XRPC endpoints are implemented; suppressed until then. 14 + #[allow(dead_code)] 15 + pub db: sqlx::SqlitePool, 13 16 } 14 17 15 18 /// Build the Axum router with middleware and routes. ··· 46 49 use std::path::PathBuf; 47 50 use tower::ServiceExt; 48 51 49 - fn test_state() -> AppState { 52 + async fn test_state() -> AppState { 53 + let pool = crate::db::open_pool("sqlite::memory:") 54 + .await 55 + .expect("failed to open test pool"); 56 + crate::db::run_migrations(&pool) 57 + .await 58 + .expect("failed to run test migrations"); 50 59 AppState { 51 60 config: Arc::new(Config { 52 61 bind_address: "127.0.0.1".to_string(), 53 62 port: 8080, 54 63 data_dir: PathBuf::from("/tmp"), 55 - database_url: "/tmp/test.db".to_string(), 64 + database_url: "sqlite::memory:".to_string(), 56 65 public_url: "https://test.example.com".to_string(), 57 66 blobs: BlobsConfig::default(), 58 67 oauth: OAuthConfig::default(), 59 68 iroh: IrohConfig::default(), 60 69 }), 70 + db: pool, 61 71 } 62 72 } 63 73 64 74 #[tokio::test] 65 75 async fn xrpc_get_unknown_method_returns_501() { 66 - let response = app(test_state()) 76 + let response = app(test_state().await) 67 77 .oneshot( 68 78 Request::builder() 69 79 .uri("/xrpc/com.example.unknownMethod") ··· 78 88 79 89 #[tokio::test] 80 90 async fn xrpc_post_unknown_method_returns_501() { 81 - let response = app(test_state()) 91 + let response = app(test_state().await) 82 92 .oneshot( 83 93 Request::builder() 84 94 .method("POST") ··· 96 106 // the protocol and correctly return 405. 97 107 #[tokio::test] 98 108 async fn xrpc_delete_returns_405() { 99 - let response = app(test_state()) 109 + let response = app(test_state().await) 100 110 .oneshot( 101 111 Request::builder() 102 112 .method("DELETE") ··· 112 122 113 123 #[tokio::test] 114 124 async fn xrpc_response_has_json_content_type() { 115 - let response = app(test_state()) 125 + let response = app(test_state().await) 116 126 .oneshot( 117 127 Request::builder() 118 128 .uri("/xrpc/com.example.unknownMethod") ··· 130 140 131 141 #[tokio::test] 132 142 async fn xrpc_response_body_is_method_not_implemented() { 133 - let response = app(test_state()) 143 + let response = app(test_state().await) 134 144 .oneshot( 135 145 Request::builder() 136 146 .uri("/xrpc/com.atproto.server.createSession") ··· 148 158 149 159 assert_eq!(status, StatusCode::NOT_IMPLEMENTED); 150 160 assert_eq!(json["error"]["code"], "MethodNotImplemented"); 161 + } 162 + 163 + #[tokio::test] 164 + async fn appstate_db_pool_is_queryable() { 165 + let state = test_state().await; 166 + sqlx::query("SELECT 1") 167 + .execute(&state.db) 168 + .await 169 + .expect("db pool in AppState must be queryable"); 151 170 } 152 171 }