chris613
2020-1-25 15:08:32

is there a simple way of comparing two structs by value in tests ?


chris613
2020-1-25 15:08:54

e.g. (check-equal? (mystruct 4) (mystruct 4))


soegaard2
2020-1-25 15:09:01

If the struct is transparent, you can use equal?


chris613
2020-1-25 15:09:11

mmmmm i dont think it is ….


chris613
2020-1-25 15:09:23

its the schema scrtuct from the deta lib


soegaard2
2020-1-25 15:09:56

Ooh. That’s more tricky then.


chris613
2020-1-25 15:09:58

(equal? (make-meals #:Name “SpagBol” #:Time (today)) (make-meals #:Name “SpagBol” #:Time (today)))


chris613
2020-1-25 15:10:34

hmmm if i do (make-meals #:Name "SpagBol" #:Time (today)) in the console i get (meals #<meta> "SpagBol" #<date 2020-01-25>)


chris613
2020-1-25 15:10:48

so that is transparent isnt it ?


chris613
2020-1-25 15:11:17

ahhhh possibly its the #<meta> thats a nested struct right, thats non transparent …


soegaard2
2020-1-25 15:12:22

Yeah.


chris613
2020-1-25 15:12:46

so thats prob whats tripping my equal?s up


chris613
2020-1-25 15:13:03

okies back to drawing board me thinks …


soegaard2
2020-1-25 15:13:13

The good news is that deta could generate an meals-equal? automatically.


chris613
2020-1-25 15:13:22

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)) )))


chris613
2020-1-25 15:13:36

i thought that, but i dont think it does …


soegaard2
2020-1-25 15:14:24

Yes - but it means that @popa.bogdanp could add one.


chris613
2020-1-25 15:15:10

oh im sure they could, and i would be mega grateful if they did, i wouldnt expect that though :wink:


soegaard2
2020-1-25 15:15:36

In the mean time, the simplest is to write your own meals-equal?.


chris613
2020-1-25 15:15:49

yeah probs what ill end up with.


soegaard2
2020-1-25 15:16:02

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.”


soegaard2
2020-1-25 15:16:30

And I think there is a struct option that can alter how equal? works.


chris613
2020-1-25 15:16:39

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:


chris613
2020-1-25 15:19:50

'#(struct:meals #<meta> "SpagBol" #<date 2020-01-25>) does '#( represent a vector ?


chris613
2020-1-25 15:20:08

as opposed to a list repr '(


soegaard2
2020-1-25 15:20:40

Is it input or output?


chris613
2020-1-25 15:23:49

output on the console


chris613
2020-1-25 15:24:20

for future refence i made a bit of generic ish helper func (define (as-value e) (drop (vector->list (struct->vector e)) 2))


chris613
2020-1-25 15:24:51

probs should be entity->values


chris613
2020-1-25 15:25:54

and im relying on ordering behaviour of (vector->list but it will do for now :slightly_smiling_face:


soegaard2
2020-1-25 15:27:14

In that case it’s a struct.


soegaard2
2020-1-25 15:27:59

The as-value solutions is great. That’ll work for all your database values, not just meals.


soegaard2
2020-1-25 15:28:16

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)


soegaard2
2020-1-25 15:29:07

But I get an error at the #:methods so maybe the struct-options aren’t passed along correctly?


chris613
2020-1-25 15:29:28

like i mentioned though, it replies on behaviour that im not sure is part of the “contract” of vector-list.


chris613
2020-1-25 15:29:53

i could probably make it more robust with some sort of struct->map or something


chris613
2020-1-25 15:30:19

but it will do for now, and liek you said. should work for all my deta schema things :smile:


soegaard2
2020-1-25 15:30:23

Both vector->list and struct->vector preserves order, so it as-value looks fine to me.


chris613
2020-1-25 15:30:32

sweet


chris613
2020-1-25 15:31:08

ahhhhh i think i may have just had a bit of a breakthrough …


chris613
2020-1-25 15:31:52

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) )))


chris613
2020-1-25 15:32:34

but could it be because insert-one! will be (essentially) async, and may have not completed before the test runs ?


soegaard2
2020-1-25 15:33:17

What does (all-meals test-conn) return?


soegaard2
2020-1-25 15:33:42

wait - are conn and test-conn the same?


chris613
2020-1-25 15:33:46

Never mind! as soon as i read what i just wrote i soppted the bug!


chris613
2020-1-25 15:33:47

lol


chris613
2020-1-25 15:33:55

yeah just spotted that!!! :smile:


soegaard2
2020-1-25 15:34:05

:slightly_smiling_face:


chris613
2020-1-25 15:34:18

i suppose that teaches me that conn should probably live outside this module


soegaard2
2020-1-25 16:19:20

Sigh. I just attempted to compare to floating points as (<= (- x y) 0.0001) and couldn’t spot the mistake…


popa.bogdanp
2020-1-25 19:35:52

@popa.bogdanp has joined the channel


popa.bogdanp
2020-1-25 19:37:32

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:


soegaard2
2020-1-25 19:38:09

That’s it then. I haven’t upgraded in a while.


soegaard2
2020-1-25 19:39:17

Thanks for recommending Postmark btw.


popa.bogdanp
2020-1-25 19:39:50

:+1: my pleasure. They provide a good service


sydney.lambda
2020-1-25 21:07:01

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?


sydney.lambda
2020-1-25 21:23:57

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.