Writing a unit test for a specific elixir function task (parsing then sorting)

142 Views Asked by At

I am new to elixir and trying to learn some of it's features including the testing library ExUnit, but I am having some trouble understanding the concept of how to write and setup a test for a particular function and if I am going in the correct direction.

For example, to test if a list is ordered after getting data fr om a CSV file, would I need to actually call the function I wrote that does this or just provide a mock list of unordered data?

//Module function that parses the CSV file
  def parse_csv do
    @csv_path
    |> Path.expand(__DIR__)
    |> File.stream!
    |> CSV.decode
    |> CSV_MODULE.prioritize_claims
  end

Do I need to actually import that module function into my test file and then actually invoke it or do I just provide a sample test list that is unordered then pass the list to the function that is suppose to sort it.

3

There are 3 best solutions below

0
Ismael Abreu On

If you write a helper function to do just that and call it with the output you get from your parse_csv function you can and should just test it directly with the possible outputs you expect to get from that function.

If you want to test everything together it’s also a good option to have a sample CSV file, or even multiple to test that everything works as expected.

0
Adam Millerchip On

do I just provide a sample test list that is unordered then pass the list to the function that is suppose to sort it

It depends what you want to test. If you just want to test the sorting behaviour, that works. I think you would import CSV_MODULE in your test file, then call CSV_MODULE.prioritize_claims with your test list, and assert the result is what you expect.

If you wanted to test parse_csv, you could write a known CSV to @csv_path in the test, then call your function. However, I think it would be better pass the path as an argument, rather than store it as a module attribute. You could still have a default value, but it allows you to provide a different one when necessary, including when testing.

4
Aleksei Matiushkin On

You should not test the core/library code, because using the library means you trust library authors.

That said, in your case, you test neither File.stream!/1 nor CSV.parse/1. All you need to test is your CSV_MODULE.prioritize_claims/1.

def parse_csv(csv_path \\ @csv_path) do
  csv_path
  |> Path.expand(__DIR__)
  |> File.stream!()
  |> CSV.decode()
  # Until here we call functions proven to work.
  # The result of the above would be the input
  # to the function below.
  # In tests, just pass alike input to the function  
  |> CSV_MODULE.prioritize_claims()
end