MCPcopy Create free account
hub / github.com/erans/pgsqlite / test_pg_attribute_queries

Function test_pg_attribute_queries

tests/catalog_test.rs:127–183  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

125
126#[tokio::test]
127async fn test_pg_attribute_queries() {
128 // Create a test database handler with temporary file
129 let timestamp = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_nanos();
130 let db_path = format!("/tmp/catalog_test_pg_attr_{timestamp}.db");
131 let db = Arc::new(DbHandler::new(&db_path).unwrap());
132
133 // Create a session
134 let session = Arc::new(SessionState::new("test_user".to_string(), "test_db".to_string()));
135
136 // Create a connection for the session
137 db.create_session_connection(session.id).await.unwrap();
138
139 // Create a test table in the session
140 db.execute_with_session("CREATE TABLE test_table (id INTEGER PRIMARY KEY, name TEXT NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)", &session.id).await.unwrap();
141
142 // Test pg_attribute query
143 let query = "SELECT attname, atttypid, attnotnull FROM pg_catalog.pg_attribute";
144 let result = CatalogInterceptor::intercept_query(query, db.clone(), Some(session.clone())).await;
145 assert!(result.is_some());
146
147 let response = result.unwrap().unwrap();
148 // With column projection, we now only get the requested columns
149 assert_eq!(response.columns, vec!["attname", "atttypid", "attnotnull"]);
150
151 // Count columns for test_table
152 let mut column_count = 0;
153 let mut found_id = false;
154 let mut found_name = false;
155
156 for row in &response.rows {
157 if let Some(Some(name_bytes)) = row.first() { // attname is at index 0 (first selected column)
158 let col_name = String::from_utf8_lossy(name_bytes);
159 if col_name == "id" {
160 found_id = true;
161 column_count += 1;
162 } else if col_name == "name" {
163 found_name = true;
164 column_count += 1;
165 // Check NOT NULL constraint
166 if let Some(Some(notnull_bytes)) = row.get(2) { // attnotnull is at index 2 (third selected column)
167 assert_eq!(notnull_bytes, b"t");
168 }
169 } else if col_name == "created_at" {
170 column_count += 1;
171 }
172 }
173 }
174
175 assert!(found_id, "id column should be in pg_attribute");
176 assert!(found_name, "name column should be in pg_attribute");
177 assert!(column_count >= 3, "Should have at least 3 columns for test_table");
178
179 // Cleanup
180 let _ = std::fs::remove_file(&db_path);
181 let _ = std::fs::remove_file(format!("{db_path}-wal"));
182 let _ = std::fs::remove_file(format!("{db_path}-shm"));
183}

Callers

nothing calls this directly

Calls 4

execute_with_sessionMethod · 0.45
cloneMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected