Repository: diesel-rs/diesel
File: diesel/src/pg/connection/mod.rs (Rust)

Code before:
    use crate::result::Error::DatabaseError;
    use std::num::NonZeroU32;

    fn connection() -> PgConnection {
        crate::test_helpers::pg_connection_no_transaction()
    }

    #[test]
    fn notifications_arrive() {
        use crate::sql_query;

        let conn = &mut connection();
        sql_query("LISTEN test_notifications")
            .execute(conn)
            .unwrap();
        sql_query("NOTIFY test_notifications, 'first'")
            .execute(conn)
            .unwrap();
        sql_query("NOTIFY test_notifications, 'second'")
            .execute(conn)
            .unwrap();

        let (first, second, third) = {
            let mut iter = conn.notifications_iter();
            (iter.next().unwrap(), iter.next().unwrap(), iter.next())
        };

        assert_eq!(first.channel, "test_notifications");
        assert_eq!(second.channel, "test_notifications");
        assert_eq!(first.payload, "first");
        assert_eq!(second.payload, "second");
        assert!(third.is_none());

        sql_query("NOTIFY test_notifications")
            .execute(conn)
            .unwrap();
        assert_eq!(conn.notifications_iter().next().unwrap().payload, "");
    }

    #[test]
    fn malformed_sql_query() {
        let connection = &mut connection();
        let query =
            crate::sql_query("SELECT not_existent FROM also_not_there;").execute(connection);

        if let Err(DatabaseError(_, string)) = query {
            assert_eq!(Some(26), string.statement_position());
        } else {
            unreachable!();
        }
    }

Review comment (suggestion):
Can we use `let mut notifications = conn.notifications_iter().collect::<Vec<_>>()` here instead and check that this vector contains exactly two elements. We could then try to fetch the next notification via `conn.notifications_iter().next()` to check that there is no notification.

Code after:

        fn get_cursor<'conn, 'query>(
            raw_connection: &'conn mut ConnectionAndTransactionManager,
            result: PgResult,
            source: impl QueryFragment<Pg> + 'query,
        ) -> QueryResult<Self::Cursor<'conn, 'query>> {
            Ok(RowByRowCursor::new(
                result,
                raw_connection,
                Box::new(source),
            ))
        }
    }
}

#[cfg(test)]
// that's a false positive for `panic!`/`assert!` on rust 2018
#[allow(clippy::uninlined_format_args)]
mod tests {
    extern crate dotenvy;

    use super::*;
    use crate::prelude::*;
    use crate::result::Error::DatabaseError;
    use std::num::NonZeroU32;

    fn connection() -> PgConnection {
        crate::test_helpers::pg_connection_no_transaction()
    }

    #[diesel_test_helper::test]
    fn notifications_arrive() {
        use crate::sql_query;

        let conn = &mut connection();
        sql_query("LISTEN test_notifications")
            .execute(conn)
            .unwrap();
        sql_query("NOTIFY test_notifications, 'first'")
            .execute(conn)
            .unwrap();
        sql_query("NOTIFY test_notifications, 'second'")
            .execute(conn)
            .unwrap();

        let notifications = conn
            .notifications_iter()
            .map(Result::unwrap)
            .collect::<Vec<_>>();

        assert_eq!(2, notifications.len());