Skip to content

Commit

Permalink
refactor: made unit tests use separate real dbs (matter-labs#213)
Browse files Browse the repository at this point in the history
# What ❔
Made unit tests use separate real dbs created in a temporary postgres
container.

## Why ❔
It makes the tests more realistic and greatly simplifies the dal crate.
  • Loading branch information
pompon0 authored Oct 26, 2023
1 parent 3cd24c9 commit c1f2f5e
Show file tree
Hide file tree
Showing 42 changed files with 526 additions and 673 deletions.
14 changes: 2 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ members = [
"core/lib/circuit_breaker",
"core/lib/commitment_utils",
"core/lib/dal",
"core/lib/db_test_macro",
"core/lib/eth_client",
"core/lib/eth_signer",
"core/lib/mempool",
Expand Down
3 changes: 2 additions & 1 deletion core/lib/dal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ zksync_health_check = { path = "../health_check" }
itertools = "0.10.1"
thiserror = "1.0"
anyhow = "1.0"
url = "2"
rand = "0.8"
tokio = { version = "1", features = ["full"] }
sqlx = { version = "0.5.13", default-features = false, features = [
"runtime-tokio-native-tls",
Expand All @@ -44,4 +46,3 @@ tracing = "0.1"

[dev-dependencies]
assert_matches = "1.5.0"
db_test_macro = { path = "../db_test_macro" }
11 changes: 6 additions & 5 deletions core/lib/dal/src/blocks_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1482,15 +1482,15 @@ impl BlocksDal<'_, '_> {

#[cfg(test)]
mod tests {
use db_test_macro::db_test;
use zksync_contracts::BaseSystemContractsHashes;
use zksync_types::{l2_to_l1_log::L2ToL1Log, Address, ProtocolVersion, ProtocolVersionId};

use super::*;
use crate::ConnectionPool;

#[db_test(dal_crate)]
async fn loading_l1_batch_header(pool: ConnectionPool) {
#[tokio::test]
async fn loading_l1_batch_header() {
let pool = ConnectionPool::test_pool().await;
let mut conn = pool.access_storage().await.unwrap();
conn.blocks_dal()
.delete_l1_batches(L1BatchNumber(0))
Expand Down Expand Up @@ -1549,8 +1549,9 @@ mod tests {
.is_none());
}

#[db_test(dal_crate)]
async fn getting_predicted_gas(pool: ConnectionPool) {
#[tokio::test]
async fn getting_predicted_gas() {
let pool = ConnectionPool::test_pool().await;
let mut conn = pool.access_storage().await.unwrap();
conn.blocks_dal()
.delete_l1_batches(L1BatchNumber(0))
Expand Down
36 changes: 20 additions & 16 deletions core/lib/dal/src/blocks_web3_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,6 @@ impl BlocksWeb3Dal<'_, '_> {

#[cfg(test)]
mod tests {
use db_test_macro::db_test;
use zksync_contracts::BaseSystemContractsHashes;
use zksync_types::{
block::{miniblock_hash, MiniblockHeader},
Expand All @@ -592,9 +591,10 @@ mod tests {
use super::*;
use crate::{tests::create_miniblock_header, ConnectionPool};

#[db_test(dal_crate)]
async fn getting_web3_block_and_tx_count(connection_pool: ConnectionPool) {
let mut conn = connection_pool.access_test_storage().await;
#[tokio::test]
async fn getting_web3_block_and_tx_count() {
let connection_pool = ConnectionPool::test_pool().await;
let mut conn = connection_pool.access_storage().await.unwrap();
conn.blocks_dal()
.delete_miniblocks(MiniblockNumber(0))
.await
Expand Down Expand Up @@ -659,9 +659,10 @@ mod tests {
}
}

#[db_test(dal_crate)]
async fn resolving_earliest_block_id(connection_pool: ConnectionPool) {
let mut conn = connection_pool.access_test_storage().await;
#[tokio::test]
async fn resolving_earliest_block_id() {
let connection_pool = ConnectionPool::test_pool().await;
let mut conn = connection_pool.access_storage().await.unwrap();
conn.blocks_dal()
.delete_miniblocks(MiniblockNumber(0))
.await
Expand All @@ -674,9 +675,10 @@ mod tests {
assert_eq!(miniblock_number.unwrap(), Some(MiniblockNumber(0)));
}

#[db_test(dal_crate)]
async fn resolving_latest_block_id(connection_pool: ConnectionPool) {
let mut conn = connection_pool.access_test_storage().await;
#[tokio::test]
async fn resolving_latest_block_id() {
let connection_pool = ConnectionPool::test_pool().await;
let mut conn = connection_pool.access_storage().await.unwrap();
conn.blocks_dal()
.delete_miniblocks(MiniblockNumber(0))
.await
Expand Down Expand Up @@ -729,9 +731,10 @@ mod tests {
assert_eq!(miniblock_number.unwrap(), Some(MiniblockNumber(1)));
}

#[db_test(dal_crate)]
async fn resolving_block_by_hash(connection_pool: ConnectionPool) {
let mut conn = connection_pool.access_test_storage().await;
#[tokio::test]
async fn resolving_block_by_hash() {
let connection_pool = ConnectionPool::test_pool().await;
let mut conn = connection_pool.access_storage().await.unwrap();
conn.blocks_dal()
.delete_miniblocks(MiniblockNumber(0))
.await
Expand Down Expand Up @@ -759,9 +762,10 @@ mod tests {
assert_eq!(miniblock_number.unwrap(), None);
}

#[db_test(dal_crate)]
async fn getting_miniblocks_for_virtual_block(connection_pool: ConnectionPool) {
let mut conn = connection_pool.access_test_storage().await;
#[tokio::test]
async fn getting_miniblocks_for_virtual_block() {
let connection_pool = ConnectionPool::test_pool().await;
let mut conn = connection_pool.access_storage().await.unwrap();

conn.protocol_versions_dal()
.save_protocol_version_with_tx(ProtocolVersion::default())
Expand Down
8 changes: 1 addition & 7 deletions core/lib/dal/src/connection/holder.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
// Built-in deps
use std::fmt;
// External imports
use sqlx::pool::PoolConnection;
use sqlx::{postgres::Postgres, PgConnection, Transaction};
// Workspace imports
// Local imports
use crate::connection::test_pool::TestPoolLock;
use std::fmt;

/// Connection holder unifies the type of underlying connection, which
/// can be either pooled or direct.
pub enum ConnectionHolder<'a> {
Pooled(PoolConnection<Postgres>),
Direct(PgConnection),
Transaction(Transaction<'a, Postgres>),
TestTransaction(TestPoolLock),
}

impl<'a> fmt::Debug for ConnectionHolder<'a> {
Expand All @@ -22,7 +17,6 @@ impl<'a> fmt::Debug for ConnectionHolder<'a> {
Self::Pooled(_) => write!(f, "Pooled connection"),
Self::Direct(_) => write!(f, "Direct connection"),
Self::Transaction(_) => write!(f, "Database Transaction"),
Self::TestTransaction(_) => write!(f, "Test Database Transaction"),
}
}
}
Loading

0 comments on commit c1f2f5e

Please sign in to comment.