@@ -3,18 +3,6 @@ defmodule Transport.NeTEx.ToGeoJSONTest do
33
44 alias Transport.NeTEx.ToGeoJSON
55
6- defmodule ZipCreator do
7- @ spec create! ( String . t ( ) , [ { String . t ( ) , binary ( ) } ] ) :: no_return ( )
8- def create! ( zip_filename , file_data ) do
9- { :ok , ^ zip_filename } =
10- :zip . create (
11- zip_filename ,
12- file_data
13- |> Enum . map ( fn { name , content } -> { name |> to_charlist ( ) , content } end )
14- )
15- end
16- end
17-
186 describe "convert_xml/1" do
197 test "converts StopPlaces to Point features" do
208 xml = """
@@ -305,19 +293,16 @@ defmodule Transport.NeTEx.ToGeoJSONTest do
305293 </root>
306294 """
307295
308- tmp_file = System . tmp_dir! ( ) |> Path . join ( "netex-geojson- #{ Ecto.UUID . generate ( ) } .zip" )
309- ZipCreator . create! ( tmp_file , [ { "stops.xml" , xml1 } , { "quays.xml" , xml2 } ] )
296+ ZipCreator . with_tmp_zip ( [ { "stops.xml" , xml1 } , { "quays.xml" , xml2 } ] , fn tmp_file ->
297+ assert { :ok , geojson } = ToGeoJSON . convert_archive ( tmp_file )
310298
311- assert { :ok , geojson } = ToGeoJSON . convert_archive ( tmp_file )
299+ assert geojson [ "type" ] == "FeatureCollection"
300+ assert length ( geojson [ "features" ] ) == 2
312301
313- assert geojson [ "type" ] == "FeatureCollection"
314- assert length ( geojson [ "features" ] ) == 2
315-
316- ids = Enum . map ( geojson [ "features" ] , & & 1 [ "id" ] )
317- assert "stop_1" in ids
318- assert "quay_1" in ids
319-
320- File . rm! ( tmp_file )
302+ ids = Enum . map ( geojson [ "features" ] , & & 1 [ "id" ] )
303+ assert "stop_1" in ids
304+ assert "quay_1" in ids
305+ end )
321306 end
322307
323308 test "filters by types option" do
@@ -344,15 +329,12 @@ defmodule Transport.NeTEx.ToGeoJSONTest do
344329 </root>
345330 """
346331
347- tmp_file = System . tmp_dir! ( ) |> Path . join ( "netex-geojson-#{ Ecto.UUID . generate ( ) } .zip" )
348- ZipCreator . create! ( tmp_file , [ { "data.xml" , xml } ] )
349-
350- assert { :ok , geojson } = ToGeoJSON . convert_archive ( tmp_file , types: [ :quays ] )
351-
352- assert length ( geojson [ "features" ] ) == 1
353- assert hd ( geojson [ "features" ] ) [ "id" ] == "quay_1"
332+ ZipCreator . with_tmp_zip ( [ { "data.xml" , xml } ] , fn tmp_file ->
333+ assert { :ok , geojson } = ToGeoJSON . convert_archive ( tmp_file , types: [ :quays ] )
354334
355- File . rm! ( tmp_file )
335+ assert length ( geojson [ "features" ] ) == 1
336+ assert hd ( geojson [ "features" ] ) [ "id" ] == "quay_1"
337+ end )
356338 end
357339
358340 test "skips non-XML files" do
@@ -370,14 +352,11 @@ defmodule Transport.NeTEx.ToGeoJSONTest do
370352 </root>
371353 """
372354
373- tmp_file = System . tmp_dir! ( ) |> Path . join ( "netex-geojson-#{ Ecto.UUID . generate ( ) } .zip" )
374- ZipCreator . create! ( tmp_file , [ { "data.xml" , xml } , { "readme.txt" , "Some text" } ] )
375-
376- assert { :ok , geojson } = ToGeoJSON . convert_archive ( tmp_file )
377-
378- assert length ( geojson [ "features" ] ) == 1
355+ ZipCreator . with_tmp_zip ( [ { "data.xml" , xml } , { "readme.txt" , "Some text" } ] , fn tmp_file ->
356+ assert { :ok , geojson } = ToGeoJSON . convert_archive ( tmp_file )
379357
380- File . rm! ( tmp_file )
358+ assert length ( geojson [ "features" ] ) == 1
359+ end )
381360 end
382361
383362 test "skips directories" do
@@ -395,15 +374,12 @@ defmodule Transport.NeTEx.ToGeoJSONTest do
395374 </root>
396375 """
397376
398- tmp_file = System . tmp_dir! ( ) |> Path . join ( "netex-geojson- #{ Ecto.UUID . generate ( ) } .zip" )
399- ZipCreator . create! ( tmp_file , [ { "subdir/data.xml" , xml } ] )
377+ ZipCreator . with_tmp_zip ( [ { "subdir/data.xml" , xml } ] , fn tmp_file ->
378+ assert { :ok , geojson } = ToGeoJSON . convert_archive ( tmp_file )
400379
401- assert { :ok , geojson } = ToGeoJSON . convert_archive ( tmp_file )
402-
403- # Should still find the XML in the subdirectory
404- assert length ( geojson [ "features" ] ) == 1
405-
406- File . rm! ( tmp_file )
380+ # Should still find the XML in the subdirectory
381+ assert length ( geojson [ "features" ] ) == 1
382+ end )
407383 end
408384 end
409385
@@ -423,15 +399,12 @@ defmodule Transport.NeTEx.ToGeoJSONTest do
423399 </root>
424400 """
425401
426- tmp_file = System . tmp_dir! ( ) |> Path . join ( "netex-geojson-#{ Ecto.UUID . generate ( ) } .zip" )
427- ZipCreator . create! ( tmp_file , [ { "data.xml" , xml } ] )
428-
429- assert { :ok , geojson } = Transport.NeTEx.ArchiveParser . to_geojson ( tmp_file )
402+ ZipCreator . with_tmp_zip ( [ { "data.xml" , xml } ] , fn tmp_file ->
403+ assert { :ok , geojson } = Transport.NeTEx.ArchiveParser . to_geojson ( tmp_file )
430404
431- assert geojson [ "type" ] == "FeatureCollection"
432- assert length ( geojson [ "features" ] ) == 1
433-
434- File . rm! ( tmp_file )
405+ assert geojson [ "type" ] == "FeatureCollection"
406+ assert length ( geojson [ "features" ] ) == 1
407+ end )
435408 end
436409
437410 test "accepts types option" do
@@ -458,15 +431,32 @@ defmodule Transport.NeTEx.ToGeoJSONTest do
458431 </root>
459432 """
460433
461- tmp_file = System . tmp_dir! ( ) |> Path . join ( "netex-geojson-#{ Ecto.UUID . generate ( ) } .zip" )
462- ZipCreator . create! ( tmp_file , [ { "data.xml" , xml } ] )
434+ ZipCreator . with_tmp_zip ( [ { "data.xml" , xml } ] , fn tmp_file ->
435+ assert { :ok , geojson } = Transport.NeTEx.ArchiveParser . to_geojson ( tmp_file , types: [ :stop_places ] )
436+
437+ assert length ( geojson [ "features" ] ) == 1
438+ assert hd ( geojson [ "features" ] ) [ "id" ] == "stop_1"
439+ end )
440+ end
463441
464- assert { :ok , geojson } = Transport.NeTEx.ArchiveParser . to_geojson ( tmp_file , types: [ :stop_places ] )
442+ test "empty archive" do
443+ ZipCreator . with_tmp_zip ( [ ] , fn tmp_file ->
444+ assert { :ok , geojson } = Transport.NeTEx.ArchiveParser . to_geojson ( tmp_file , types: [ :stop_places ] )
465445
466- assert length ( geojson [ "features" ] ) == 1
467- assert hd ( geojson [ "features" ] ) [ "id" ] == "stop_1"
446+ assert geojson == % { "features" => [ ] , "type" => "FeatureCollection" }
447+ end )
448+ end
449+
450+ test "bad NeTEx" do
451+ xml = """
452+ not an XML
453+ """
454+
455+ ZipCreator . with_tmp_zip ( [ { "data.xml" , xml } ] , fn tmp_file ->
456+ assert { :ok , geojson } = Transport.NeTEx.ArchiveParser . to_geojson ( tmp_file , types: [ :stop_places ] )
468457
469- File . rm! ( tmp_file )
458+ assert geojson == % { "features" => [ ] , "type" => "FeatureCollection" }
459+ end )
470460 end
471461 end
472462end
0 commit comments