I have a CSV file. I would like to use the data in the CSV file as inputs for the fields in my test factories. Any suggestions on how we could do this?
This is a sample csv file
The post test factory looks like this
def post_factory(attrs \\ %{}) do
%Post{
body: body from posts csv,
title: title from posts csv,
comment: build(:comment) #comment has its own factory and csv
}
end
What have you tried so far?
So far I have been using the Faker library for generating test data for the factories. This is my first time trying to extract data from csv in Elixir.
I came across a method from a blog that helps in fetching data from csv -
Using File.read!
to load the csv,
String.split
to fetch all the rows
then Enum.map
to convert the each row to a column and then filter
I am not certain where this parsing should belong and how to use these as inputs for the fields in factory.
You should use nimble_csv to parse your data.
2 Likes
Thank you for this suggestion. This is something that fits my requirements.