diff --git a/apps/transport/lib/transport/gtfs_diff.ex b/apps/transport/lib/transport/gtfs_diff.ex index 4f4f4b6e8c..98c28e0d15 100644 --- a/apps/transport/lib/transport/gtfs_diff.ex +++ b/apps/transport/lib/transport/gtfs_diff.ex @@ -334,6 +334,7 @@ defmodule Transport.GTFSDiff do [headers | body] |> CSV.dump_to_stream() |> Stream.into(File.stream!(filepath)) + |> Stream.run() end def apply_delete(file, diff, primary_key) do @@ -410,11 +411,3 @@ defmodule Transport.GTFSDiff do |> IO.iodata_to_binary() end end - -# usage - -# unzip_1 = Transport.GTFSDiff.unzip("path/to/gtfs_1.zip") -# unzip_2 = Transport.GTFSDiff.unzip("path/to/gtfs_2.zip") - -# diff = Transport.GTFSDiff.diff(unzip_1, unzip_2) -# File.write!("diff_output.txt", diff |> Transport.GTFSDiff.dump_diff()) diff --git a/apps/transport/test/transport/gtfs_diff_test.exs b/apps/transport/test/transport/gtfs_diff_test.exs index 7a76b9c101..09661aeabb 100644 --- a/apps/transport/test/transport/gtfs_diff_test.exs +++ b/apps/transport/test/transport/gtfs_diff_test.exs @@ -1,12 +1,6 @@ defmodule Transport.GTFSDiffTest do use ExUnit.Case, async: true - defp unzip(path) do - zip_file = Unzip.LocalFile.open(path) - {:ok, unzip} = Unzip.new(zip_file) - unzip - end - describe "GTFS Diff" do test "2 identical files" do unzip_1 = unzip("test/fixture/files/gtfs_diff/gtfs.zip") @@ -128,4 +122,43 @@ defmodule Transport.GTFSDiffTest do ] end end + + describe "dump_diff" do + test "empty diff, file is created" do + tmp_path = System.tmp_dir!() |> Path.join(Ecto.UUID.generate()) + refute File.exists?(tmp_path) + Transport.GTFSDiff.dump_diff([], tmp_path) + assert File.exists?(tmp_path) + + assert [["id", "file", "action", "target", "identifier", "initial_value", "new_value", "note"]] == + read_csv(tmp_path) + + File.rm(tmp_path) + end + + test "simple diff" do + diff = [%{action: "delete", file: "stops.txt", id: 1, identifier: %{"stop_id" => "near1"}, target: "row"}] + tmp_path = System.tmp_dir!() |> Path.join(Ecto.UUID.generate()) + refute File.exists?(tmp_path) + Transport.GTFSDiff.dump_diff(diff, tmp_path) + assert File.exists?(tmp_path) + + assert [ + ["id", "file", "action", "target", "identifier", "initial_value", "new_value", "note"], + ["1", "stops.txt", "delete", "row", ~s({"stop_id":"near1"}), "", "", ""] + ] == read_csv(tmp_path) + + File.rm(tmp_path) + end + end + + defp unzip(path) do + zip_file = Unzip.LocalFile.open(path) + {:ok, unzip} = Unzip.new(zip_file) + unzip + end + + defp read_csv(filepath) do + filepath |> File.read!() |> NimbleCSV.RFC4180.parse_string(skip_headers: false) + end end