Mysql seed set for rand

Is it possible to set the seed for the RAND () function in MySQL? I need this for unit testing to make sure I know what the expected result is.

In PHP, it is simple:

<?php srand(12345); echo rand(); ?> 

In my model, I currently have a query like:

 SELECT * FROM table ORDER BY RAND() LIMIT 1; 

Now in my unit test, I want to make sure that I know what the seed is for RAND (), so I know which record is returned.

Perhaps by completing an additional request before the request in my model?

I know that I can just add the RAND () argument, but that is not what I want; I do not want to modify the request.

PS. I am using Linux; will this help set the seed for / dev / random, maybe?

+4
source share
2 answers

You can reference this: MySQL RAND with parameter

The RAND function supports the RAND(N) parameter, which will be used as a seed.

+1
source

As a rule, we are not a unit test SQL function, we unit test our code, which calls the SQL function.

So maybe instead of testing a specific return value, just make sure you get some value. If the query returns a value, you know that it "works" and you remain isolated from MySQL's internal hardware. You also do not need to change your request.

0
source

Source: https://habr.com/ru/post/1446183/


All Articles