#How can I make tests run against the database?

1 messages · Page 1 of 1 (latest)

normal jay
#

I'm trying to make sure I start with the actual problem. I have started writing a classic crud application with a database and I want to run isolated tests. looking at how rails does this tests run in a transaction make sense.

I've got to the following setup. I have this test:

pub fn create_and_show_note_test() {
  use context <- test_context()

  let request = create_note_request("unit tests", "unit tests are better")
  let response = router.route(request, context)

  assert response.status == 303
  let assert Ok(location) = response.get_header(response, "location")

  let request =
    simulate.request(http.Get, location)
    |> as_web_user("peter")
  let response = router.route(request, context)

  assert response.status == 200
  let body = simulate.read_body(response)
  assert string.contains(body, "unit tests")
}

and test_context helper:

fn test_context(then) {
  dot_env.new()
  |> dot_env.set_path(".env")
  |> dot_env.set_debug(True)
  |> dot_env.load

  let assert Ok(config) = db.from_env()
  let assert Ok(conn) = db.start(config)

  let assert Error(pog.TransactionRolledBack(Nil)) =
    pog.transaction(conn.data, fn(db) {
      let context =
        config.Config(db:)
      then(context)
      Error(Nil)
    })
}

I like this approach but I am not getting the error.
sorry, too many clients already
How should I solve this? I've tried a few things but open to any suggestion really.

hollow sphinx
#

You are starting a new pool every time you create a new context.
Maybe use global_value to start a single pool for all tests?

light basin
#

Yup that’s what I’d do

#

If you create a pool per test you need to make sure it is shut down afterward

#

I also create a transaction around each test and roll it back so the database and seeds are pristine at the start of every test

sharp viper
#

I would love to test my squirrel generated functions

#

this came right on time, I wonder if I can make it work

sacred anvil
#

Ooo! I was just wondering if there was a way of doing this for an expensive operation. TY!

normal jay
normal jay
normal jay
light basin
light basin
light basin