is there a simple way of comparing two structs by value in tests ?
e.g. (check-equal? (mystruct 4) (mystruct 4))
If the struct is transparent, you can use equal?
mmmmm i dont think it is ….
its the schema scrtuct from the deta lib
Ooh. That’s more tricky then.
(equal? (make-meals #:Name “SpagBol” #:Time (today)) (make-meals #:Name “SpagBol” #:Time (today)))
hmmm if i do (make-meals #:Name "SpagBol" #:Time (today))
in the console i get (meals #<meta> "SpagBol" #<date 2020-01-25>)
so that is transparent isnt it ?
ahhhh possibly its the #<meta>
thats a nested struct right, thats non transparent …
Yeah.
so thats prob whats tripping my equal?s up
okies back to drawing board me thinks …
The good news is that deta
could generate an meals-equal?
automatically.
for context (run-tests
(test-suite
"basic CRUD"
(check-equal? (length (recipe-list-data test-conn)) 0)
(check-equal? (length (all-meals test-conn)) 1)
(check-eqv?
(insert-one! conn (make-meals #:Name "SpagBol" #:Time (today)))
(make-meals #:Name "SpagBol" #:Time (today)))
;; (check-not-false (insert-one! test-conn fake-recipe))
)))
i thought that, but i dont think it does …
Yes - but it means that @popa.bogdanp could add one.
oh im sure they could, and i would be mega grateful if they did, i wouldnt expect that though :wink:
In the mean time, the simplest is to write your own meals-equal?
.
yeah probs what ill end up with.
But there might be another solution. The documentaiton of define-schema
says: “All provided _struct-option_s are passed directly to the underlying struct definition.”
And I think there is a struct option that can alter how equal?
works.
im just trying to get my head round the whole lib etc, b/c just now im getting some weird behaviour with update-one! and want to narrow it down to a small test before calling it a bug :slightly_smiling_face:
'#(struct:meals #<meta> "SpagBol" #<date 2020-01-25>)
does '#(
represent a vector ?
as opposed to a list repr '(
Is it input or output?
output on the console
for future refence i made a bit of generic ish helper func (define (as-value e) (drop (vector->list (struct->vector e)) 2))
probs should be entity->values
and im relying on ordering behaviour of (vector->list
but it will do for now :slightly_smiling_face:
In that case it’s a struct.
The as-value
solutions is great. That’ll work for all your database values, not just meals.
I attempted this: #lang racket
(require deta)
(define-schema meal
([id id/f #:primary-key #:auto-increment]
[name string/f #:contract non-empty-string? ]
[price integer/f])
#:methods gen:equal+hash
[(define equal-proc
(λ (m1 m2) (and (equal? (meal-name m1) (meal-name m2))
(equal? (meal-price m1) (meal-price m2)))))])
(define rice (make-meal #:name "Rice" #:price 4))
(define rice2 (make-meal #:name "Rice" #:price 4))
(equal? rice rice2)
But I get an error at the #:methods
so maybe the struct-options aren’t passed along correctly?
like i mentioned though, it replies on behaviour that im not sure is part of the “contract” of vector-list.
i could probably make it more robust with some sort of struct->map or something
but it will do for now, and liek you said. should work for all my deta schema things :smile:
Both vector->list and struct->vector preserves order, so it as-value
looks fine to me.
sweet
ahhhhh i think i may have just had a bit of a breakthrough …
i didnt know why this wasnt working (insert-one! conn (make-meals #:Name "SpagBol" #:Time (today)))
(run-tests
(test-suite
"basic CRUD"
(check-equal? (length (all-meals test-conn)) 1)
)))
but could it be because insert-one! will be (essentially) async, and may have not completed before the test runs ?
What does (all-meals test-conn)
return?
wait - are conn and test-conn the same?
Never mind! as soon as i read what i just wrote i soppted the bug!
lol
yeah just spotted that!!! :smile:
:slightly_smiling_face:
i suppose that teaches me that conn
should probably live outside this module
Sigh. I just attempted to compare to floating points as (<= (- x y) 0.0001)
and couldn’t spot the mistake…
@popa.bogdanp has joined the channel
This should work. You might need to upgrade your local version of deta because I only added support for extra struct options a couple weeks ago :smile:
That’s it then. I haven’t upgraded in a while.
Thanks for recommending Postmark btw.
:+1: my pleasure. They provide a good service
The SDL2 library for Racket provides an events example: https://github.com/lockie/racket-sdl2/blob/master/examples/events.rkt However, it doesn’t actually appear to work. Pressing one of the arrow keys which are being tested for yields the following error: enum:int->_SDL_Keycode: expected a known #<ctype:int32>, got: 1073741906
any ideas?
using scancodes instead seems to work: [(SDL_KEYDOWN)
(case (SDL_Keysym-scancode
(SDL_KeyboardEvent-keysym
(union-ref event 4)))
[(SDL_SCANCODE_UP) #t]
[else #f])]
but if anyone can shed any light, I’d still be interested to know.