I am writing test cases for my application . I am trying to write generic test cases . I am using faker to insert the fake data. I want to insert the data inside the map depending upon the data type
I am converting schema struct to map first like this:
Map.from_struct(struct(Model))
I can access the data types with schema functions:
Model.___schema__(:type, field)
It return the data type of the fields.
I can use comprehension to return the data types of all the fields:
num = [schema_fields]
for x <- num, do: @model.__schema__(:type, x)
I cannot use if statement because it wont allow invoke functions inside of it.
So any suggestions how can insert data inside the map depending upon the data type?
# A comprehension with a generator and a filter
iex> for n <- [1, 2, 3, 4, 5, 6], rem(n, 2) == 0, do: n
[2, 4, 6]
# with a guard clause
iex> for {type, name} when type != :guest <- users, do: String.upcase(name)
some thing that will check the type of the field and then insert the fake data. like if its s timestamp it insert faker.Date.backwards(3) and if its string then faker.Name.namelike this. Build a map dynamically for every model.
But do you actually test the changeset with this if you supply the data it already expects and works with? Why doesn’t static data doesn’t work for you? Changesets are supposed to be pure anyway.
it will duplicate the code a lot. I f i write this for every model. So I am thinking about writing a macro that will be included in every model test. thats why I want to write it generically
Macros are better they will be added automatically at the compile time and and they can inherit all the functionality of your current module unlike functions. I already wrote a marcro its working fine. Now i only need to make it generic