LocalFileCacher.TestHelpers (Local File Cacher v0.1.2)

View Source

Assertions and setup functions that can be used in your application's tests to ensure that LocalFileCacher is working correctly.

Setting up your tests

For modules that test a single endpoint

  defmodule YourProject.SomeApiTest do
    use ExUnit.Case

    @file_cache_directory_path LocalFileCacher.get_file_cache_directory_path(
                                 YourProject.SomeApi,
                                 "some_endpoint"
                               )
    setup_all do
      # Delete the local file cache directory after the tests have completed
      on_exit(fn -> File.rm_rf!(@file_cache_directory_path) end)

      %{file_cache_directory_path: @file_cache_directory_path}
    end

    setup_all [{LocalFileCacher.TestHelpers, :setup_file_cache_directory}]
  end

For modules that test multiple endopints

  defmodule YourProject.SomeApiTest do
    use ExUnit.Case

    @base_file_cache_directory_path LocalFileCacher.get_file_cache_directory_path(
                                      YourProject.SomeApi
                                    )

    @some_endpoint_file_cache_directory_path LocalFileCacher.get_file_cache_directory_path(
                                               YourProject.SomeApi,
                                               "some_endpoint"
                                             )

    @some_other_endpoint_file_cache_directory_path LocalFileCacher.get_file_cache_directory_path(
                                                     YourProject.SomeApi,
                                                     "some_other_endpoint"
                                                   )

    setup_all do
      # Create file cache directories for all endpoints in this test module
      File.mkdir_p!(@some_endpoint_file_cache_directory_path)
      File.mkdir_p!(@some_other_endpoint_file_cache_directory_path)

      # Delete all cached files for this category after the tests have completed
      on_exit(fn -> File.rm_rf!(@base_file_cache_directory_path) end)
    end
  end

Using the assertions in your tests

After following the setup instructions above, these assertions can be used in your tests:

test "saves data to the local file cache" do
  LocalFileCacher.TestHelpers.assert_files_have_been_saved_to_local_cache(
    @file_cache_directory_path,
    &YourProject.SomeApi.get_some_endpoint/0
  )
end

test "prunes old data from the local file cache" do
  LocalFileCacher.TestHelpers.assert_old_files_are_pruned_from_local_cache(
    @file_cache_directory_path,
    &YourProject.SomeApi.get_some_endpoint/0
  )
end

Summary

Functions

Execute a zero-arity callback, then assert that one or more files have been saved to the given file_cache_directory_path.

Execute a zero-arity callback, then assert that old files are pruned from the given file_cache_directory_path.

Ensure the file cache directory exists in the file system.

Functions

assert_files_have_been_saved_to_local_cache(file_cache_directory_path, callback)

@spec assert_files_have_been_saved_to_local_cache(String.t(), function()) :: any()

Execute a zero-arity callback, then assert that one or more files have been saved to the given file_cache_directory_path.

Examples

iex> LocalFileCacher.TestAssertions.assert_files_have_been_saved_to_local_cache(
...>   "/tmp/path/to/your/cached/files",
...>   &YourProject.SomeApi.get_some_endpoint/0
...> )
:ok

assert_old_files_are_pruned_from_local_cache(file_cache_directory_path, callback)

@spec assert_old_files_are_pruned_from_local_cache(String.t(), (-> any())) :: any()

Execute a zero-arity callback, then assert that old files are pruned from the given file_cache_directory_path.

Examples

iex> LocalFileCacher.TestAssertions.assert_old_files_are_pruned_from_local_cache(
...>   "/tmp/path/to/your/cached/files",
...>   &YourProject.SomeApi.get_some_endpoint/0
...> )
:ok

setup_file_cache_directory(tags)

@spec setup_file_cache_directory(map() | keyword()) :: :ok

Ensure the file cache directory exists in the file system.