Skip to content

Commit

Permalink
Répare GTFSDiff.dump_diff (#3668)
Browse files Browse the repository at this point in the history
* Add tests for GTFSDiff.dump_diff

* Run Stream
  • Loading branch information
AntoineAugusti authored Dec 19, 2023
1 parent 729cd77 commit fcc46f0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
9 changes: 1 addition & 8 deletions apps/transport/lib/transport/gtfs_diff.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
45 changes: 39 additions & 6 deletions apps/transport/test/transport/gtfs_diff_test.exs
Original file line number Diff line number Diff line change
@@ -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")
Expand Down Expand Up @@ -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

0 comments on commit fcc46f0

Please sign in to comment.