moocfi / haskell-mooc

Haskell MOOC University of Helsinki
Other
307 stars 414 forks source link

Set14bTest.hs doesn't close the database connection which leads to incorrect testing #87

Closed Joonas-vonlerber closed 1 year ago

Joonas-vonlerber commented 1 year ago

In Set14bTest.hs when testing ex. 1 & 2 (simple sql), doesn't close the database connection leading to incorrect testing and a database leaking from one test to the other. Running the test multiple times, we see that the length of the getAllQuery increases with every test run. I don't find any issues in the testing code. A bad and very temporary fix might be to always make a new random name when making a sql database in the tests.

===== EXERCISE 1
*** Failed! Falsified (after 1 test):
After running
  db <- openDatabase ""
  deposit db (T.pack "d") 1
  deposit db (T.pack "g") 7
The output of: query_ db getAllQuery :: IO [(String,Int)]
  Expected: [("d",1),("g",7)]
  Was: [("n",6),("k",0),("j",8),("q",7),("u",5),("u",7),("t",0),("m",10),("y",4),("u",0),("u",3),("c",3),("a",9),("p",8),("j",8),("j",1),("j",7),("o",8),("h",9),
("j",4),("j",2),("t",9),("f",9),("e",5),("t",8),("t",5),("j",9),("z",7),("z",10),("w",0),("w",9),("a",5),("a",1),("b",6),("a",2),("a",9),("l",9),("c",4),("e",1),
("e",6),("e",10),("y",10),("p",1),("w",5),("v",6),("v",10),("g",5),("g",0),("rh",10),("rh",10),("v",8),("v",9),("j",1),("i",10),("v",7),("t",9),("t",5),
("v",3),("t",0),("n",7),("h",8),("h",1),("e",8),("n",0),("o",9),("o",10),("o",7),("r",8),("d",1),("g",7)]

----- Fail
===== EXERCISE 2
*** Failed! Falsified (after 2 tests):
After running
  db <- openDatabase ""
  deposit db (T.pack "a") 0
  deposit db (T.pack "a") 7
The output of: balance db "a"
  Expected: 7
  Was: 33

*** Failed! Falsified (after 1 test):
After running
  deposit db "b" 0
The output of: balance db "f"
  Expected: 0
  Was: 9

----- Fail
===== EXERCISE 3
00000 Todo
===== EXERCISE 4
00000 Todo
===== EXERCISE 5
00000 Todo
===== EXERCISE 6
00000 Todo
===== EXERCISE 7
TODO
TOD00000 Todo
===== EXERCISE 8
00000 Todo
===== TOTAL
00______
0 / 8

If there is a possibility that it is a me problem, here is my code for the exercises: ex.1

initQuery :: Query
initQuery = Query (T.pack "CREATE TABLE IF NOT EXISTS events (account TEXT NOT NULL, amount NUMBER NOT NULL);")

depositQuery :: Query
depositQuery = Query (T.pack "INSERT INTO events (account, amount) VALUES (?, ?);")

getAllQuery :: Query
getAllQuery = Query (T.pack "SELECT account, amount FROM events;")

-- openDatabase should open an SQLite database using the given
-- filename, run initQuery on it, and produce a database Connection.
openDatabase :: String -> IO Connection
openDatabase dataBaseName = do
                              db <- open $ dataBaseName ++ ".db"
                              execute_ db initQuery
                              return db

-- given a db connection, an account name, and an amount, deposit
-- should add an (account, amount) row into the database
deposit :: Connection -> T.Text -> Int -> IO ()
deposit db account amount = execute db depositQuery (account, amount)

ex. 2

balanceQuery :: Query
balanceQuery = Query (T.pack "SELECT SUM(amount) FROM events WHERE account = ?;")

balance :: Connection -> T.Text -> IO Int
balance db account = do
                        nums <- query db balanceQuery [account] :: IO [[Int]]
                        return . sum . head $ nums
opqdonut commented 1 year ago

The problem is your ++ ".db". If open is called with an empty string, it creates a temporary in-memory db. That's what the test is trying to use. With your code, a db called .db gets reused on the disk. Sorry for the weird error, I'll try to make the exercise text better!