kadena-io / pact-5

The New Pact
BSD 3-Clause "New" or "Revised" License
8 stars 7 forks source link

Bugfix: Fix sqlite pact db keys implementation, add regression #236

Closed jmcardon closed 1 month ago

jmcardon commented 1 month ago

The following test, prior to this PR, fails on the sqlite pact db (but works fine for in-memory):

(begin-tx)
(module m g (defcap g () true)
(defschema sc a:integer b:integer)
(deftable tbl:{sc})

(defun add-elem(a:integer k:string)
  (write tbl k {"a":a, "b":a})
)

(defun update-elem(a:integer k:string)
  (update tbl k {"a":a})
)

(defun all-elems()
  (fold-db tbl (lambda (x y) true) (lambda (k o) {"key":k, "value":o}))
)
)
(create-table tbl)
(commit-tx)

; Add two entries
(begin-tx)
(m.add-elem 1 "jose")
(m.add-elem 1 "robert")
(commit-tx)

(begin-tx)
(m.update-elem 2 "jose")
(m.update-elem 3 "robert")
(commit-tx)

(begin-tx)
(expect "All-elems return distinct objects"
  [ {"key": "jose","value": {"a": 2,"b": 1}}
  , {"key": "robert","value": {"a": 3,"b": 1}} ]
  (m.all-elems))
(commit-tx)

This PR fixes this behavior. The keys method in the sqlite pact db was not calling DISTINCT on the entries.

Thanks @salamaashoush for the report!! This was found via the pact -s

PR checklist:

Additionally, please justify why you should or should not do the following: