Last active
May 3, 2019 23:42
-
-
Save marciol/82918efcefc2ce8a5aa3b8485c440592 to your computer and use it in GitHub Desktop.
How to purge rows from tables to s3 from a Elixir application using Ports - audit_log_purge_test.exs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule Mix.Tasks.AuditLogPurgeTest do | |
| use RkOneApi.DataCase | |
| import Ecto.Query | |
| import Ecto.Changeset | |
| alias MyApp.AuditLog | |
| alias Mix.Tasks.AuditLogPurge | |
| defp build_fake_backup_cmd(_, _, _), do: "cat" | |
| defp create_records(_) do | |
| do_create_records(%{ | |
| count: 2, | |
| logged_at: ~N[2019-01-01 14:59:00] | |
| }) | |
| do_create_records(%{ | |
| count: 2, | |
| logged_at: ~N[2019-02-01 14:59:00] | |
| }) | |
| :ok | |
| end | |
| defp do_create_records(%{count: count, logged_at: logged_at}) do | |
| for n <- count..0 do | |
| AuditLog | |
| |> change( | |
| ... | |
| logged_at: NaiveDateTime.add(logged_at, n) | |
| ) | |
| |> Repo.insert() | |
| end | |
| end | |
| defp audit_logs_records_to_be_purged do | |
| from(i in AuditLog) | |
| |> where( | |
| [i], | |
| fragment("date_part('year', ?)", i.logged_at) == 2019 | |
| ) | |
| |> where( | |
| [i], | |
| fragment("date_part('month', ?)", i.logged_at) == 1 | |
| ) | |
| end | |
| defp audit_logs_records_to_be_maintained do | |
| from(i in AuditLog) | |
| |> where( | |
| [i], | |
| fragment("date_part('year', ?)", i.logged_at) == 2019 | |
| ) | |
| |> where( | |
| [i], | |
| fragment("date_part('month', ?)", i.logged_at) == 1 | |
| ) | |
| end | |
| describe("run/1") do | |
| setup do | |
| # Get Mix output sent to the current | |
| # process to avoid polluting tests. | |
| Mix.shell(Mix.Shell.Process) | |
| on_exit(fn -> | |
| Mix.shell(Mix.Shell.IO) | |
| end) | |
| end | |
| setup [:create_records] | |
| test "calls the command only with the selected chunk" do | |
| AuditLogPurge.run( | |
| ["--year", "2019", "--month", "01"], | |
| &build_fake_backup_cmd/3 | |
| ) | |
| assert_received { | |
| :mix_shell, | |
| :info, | |
| ["Audit Logs purged successfully!"] | |
| } | |
| for record <- | |
| audit_logs_records_to_be_purged() | |
| |> Repo.all() do | |
| encoded_record = | |
| record | |
| |> Jason.encode!(pretty: true) | |
| |> Kernel.<>("\n") | |
| assert_received {_, {:data, ^encoded_record}} | |
| end | |
| for record <- | |
| audit_logs_records_to_be_maintained() | |
| |> Repo.all() do | |
| encoded_record = | |
| record | |
| |> Jason.encode!(pretty: true) | |
| |> Kernel.<>("\n") | |
| refute_received {_, {:data, ^encoded_record}} | |
| end | |
| refute audit_logs_records_to_be_purged() |> Repo.exists?() | |
| assert audit_logs_records_to_be_maintained() |> Repo.exists?() | |
| end | |
| test "raises a message when required params are not present" do | |
| message = """ | |
| Lacking required year and month arguments. | |
| Type `mix help audit_log_purge` to know more details. | |
| """ | |
| assert_raise Mix.Error, message, fn -> | |
| AuditLogPurge.run([], &build_fake_backup_cmd/3) | |
| end | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment