aboutsummaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authorAlban Peignier2018-03-05 23:08:45 +0100
committerGitHub2018-03-05 23:08:45 +0100
commit75f50a80a458b1de6d211ee6a98587f1262bc1bf (patch)
tree298111da5f2f3dfccb46e9e142c9ec9d82a8c887 /spec
parent4a9b3e5868e379fa41b214b35d3ded60d8464a64 (diff)
parentcfdd12aa6b46331435bc62209c51cc14f470bd38 (diff)
downloadchouette-core-75f50a80a458b1de6d211ee6a98587f1262bc1bf.tar.bz2
Merge pull request #359 from af83/6068-simple-exporter
Simple exporter. Refs #6068
Diffstat (limited to 'spec')
-rw-r--r--spec/models/chouette/journey_pattern_spec.rb17
-rw-r--r--spec/models/simple_exporter_spec.rb118
-rw-r--r--spec/models/simple_importer_spec.rb6
-rw-r--r--spec/models/simple_json_exporter_spec.rb95
4 files changed, 234 insertions, 2 deletions
diff --git a/spec/models/chouette/journey_pattern_spec.rb b/spec/models/chouette/journey_pattern_spec.rb
index 7c767e4d1..dac45d6b5 100644
--- a/spec/models/chouette/journey_pattern_spec.rb
+++ b/spec/models/chouette/journey_pattern_spec.rb
@@ -71,6 +71,23 @@ describe Chouette::JourneyPattern, :type => :model do
end
end
+ describe "distance_to" do
+ let(:journey_pattern) { create :journey_pattern }
+ before do
+ journey_pattern.costs = generate_journey_pattern_costs(10, 10)
+ end
+ subject{ journey_pattern.distance_to stop}
+ context "for the first stop" do
+ let(:stop){ journey_pattern.stop_points.first }
+ it { should eq 0 }
+ end
+
+ context "for the last stop" do
+ let(:stop){ journey_pattern.stop_points.last }
+ it { should eq 40 }
+ end
+ end
+
describe "set_distances" do
let(:journey_pattern) { create :journey_pattern }
let(:distances){ [] }
diff --git a/spec/models/simple_exporter_spec.rb b/spec/models/simple_exporter_spec.rb
new file mode 100644
index 000000000..75051aeb9
--- /dev/null
+++ b/spec/models/simple_exporter_spec.rb
@@ -0,0 +1,118 @@
+RSpec.describe SimpleExporter do
+ describe "#define" do
+ context "with an incomplete configuration" do
+ it "should raise an error" do
+ SimpleExporter.define :foo
+ expect do
+ SimpleExporter.new(configuration_name: :test).export
+ end.to raise_error
+ end
+ end
+ context "with a complete configuration" do
+ before do
+ SimpleExporter.define :foo do |config|
+ config.collection = Chouette::StopArea.all
+ end
+ end
+
+ it "should define an exporter" do
+ expect{SimpleExporter.find_configuration(:foo)}.to_not raise_error
+ expect{SimpleExporter.new(configuration_name: :foo, filepath: "").export}.to_not raise_error
+ expect{SimpleExporter.find_configuration(:bar)}.to raise_error
+ expect{SimpleExporter.new(configuration_name: :bar, filepath: "")}.to raise_error
+ expect{SimpleExporter.new(configuration_name: :bar, filepath: "").export}.to raise_error
+ expect{SimpleExporter.create(configuration_name: :foo, filepath: "")}.to change{SimpleExporter.count}.by 1
+ end
+ end
+
+ context "when defining the same col twice" do
+ it "should raise an error" do
+ expect do
+ SimpleExporter.define :foo do |config|
+ config.collection = Chouette::StopArea.all
+ config.add_column :name
+ config.add_column :name
+ end
+ end.to raise_error
+ end
+ end
+ end
+
+ describe "#export" do
+ let(:exporter){ importer = SimpleExporter.new(configuration_name: :test, filepath: filepath) }
+ let(:filepath){ Rails.root + "tmp/" + filename }
+ let(:filename){ "stop_area.csv" }
+ # let(:stop_area_referential){ create(:stop_area_referential, objectid_format: :stif_netex) }
+
+ context "with one row per item" do
+ before(:each) do
+ @stop_area = create :stop_area
+ SimpleExporter.define :test do |config|
+ config.collection = ->{ Chouette::StopArea.all }
+ config.separator = ";"
+ config.add_column :name
+ config.add_column :lat, attribute: :latitude
+ config.add_column :lng, attribute: [:latitude, :to_i, :next]
+ config.add_column :type, attribute: :area_type
+ config.add_column :street_name, value: "Lil Exporter"
+ config.on_relation :stop_area_referential do
+ config.add_column :stop_area_referential_id, attribute: :id
+ config.add_column :stop_area_referential_id_other, value: ->(item){ item.id }
+ end
+ config.add_column :forty_two, value: 42
+ end
+ end
+
+ it "should export the given file" do
+ expect{exporter.export verbose: false}.to_not raise_error
+ expect(exporter.status).to eq "success"
+ expect(File.exists?(filepath)).to be_truthy
+ csv = CSV.read(filepath, headers: true, col_sep: ";")
+ row = csv.by_row.values_at(0).last
+ expect(row["name"]).to eq @stop_area.name
+ expect(row["lat"]).to eq @stop_area.latitude.to_s
+ expect(row["lng"]).to eq (@stop_area.latitude.to_i + 1).to_s
+ expect(row["street_name"]).to eq "Lil Exporter"
+ expect(row["stop_area_referential_id"]).to eq @stop_area.stop_area_referential_id.to_s
+ expect(row["stop_area_referential_id_other"]).to eq @stop_area.stop_area_referential_id.to_s
+ expect(row["forty_two"]).to eq "42"
+ end
+ end
+
+ context "with several rows for the same object" do
+ before(:each) do
+ @stop_area = create :stop_area
+ @stop_1 = create :stop_point, stop_area: @stop_area
+ @stop_2 = create :stop_point, stop_area: @stop_area
+ SimpleExporter.define :test do |config|
+ config.collection = ->{ Chouette::StopArea.all }
+ config.map_item_to_rows do |stop_area|
+ stop_area.stop_points.map do |sp|
+ {
+ id: sp.id,
+ stop_area: stop_area
+ }
+ end
+ end
+ config.add_column :id
+ config.on_relation :stop_area do
+ config.add_column :stop_area_name, attribute: :name
+ end
+ end
+ end
+
+ it "should export the given file" do
+ expect{exporter.export verbose: false}.to_not raise_error
+ expect(exporter.status).to eq "success"
+ expect(File.exists?(filepath)).to be_truthy
+ csv = CSV.read(filepath, headers: true)
+ row = csv.by_row.values_at(0).last
+ expect(row["id"]).to eq @stop_1.id.to_s
+ expect(row["stop_area_name"]).to eq @stop_area.name
+ row = csv.by_row.values_at(1).last
+ expect(row["id"]).to eq @stop_2.id.to_s
+ expect(row["stop_area_name"]).to eq @stop_area.name
+ end
+ end
+ end
+end
diff --git a/spec/models/simple_importer_spec.rb b/spec/models/simple_importer_spec.rb
index 60d7b7882..5f9eb0651 100644
--- a/spec/models/simple_importer_spec.rb
+++ b/spec/models/simple_importer_spec.rb
@@ -3,8 +3,9 @@ RSpec.describe SimpleImporter do
context "with an incomplete configuration" do
it "should raise an error" do
+ SimpleImporter.define :foo
expect do
- SimpleImporter.define :foo
+ SimpleImporter.new(configuration_name: :foo, filepath: "").import
end.to raise_error
end
end
@@ -18,6 +19,7 @@ RSpec.describe SimpleImporter do
it "should define an importer" do
expect{SimpleImporter.find_configuration(:foo)}.to_not raise_error
expect{SimpleImporter.new(configuration_name: :foo, filepath: "")}.to_not raise_error
+ expect{SimpleImporter.new(configuration_name: :foo, filepath: "").import}.to_not raise_error
expect{SimpleImporter.find_configuration(:bar)}.to raise_error
expect{SimpleImporter.new(configuration_name: :bar, filepath: "")}.to raise_error
expect{SimpleImporter.create(configuration_name: :foo, filepath: "")}.to change{SimpleImporter.count}.by 1
@@ -47,7 +49,7 @@ RSpec.describe SimpleImporter do
end
it "should import the given file" do
- expect{importer.import verbose: false}.to change{Chouette::StopArea.count}.by 1
+ expect{importer.import verbose: true}.to change{Chouette::StopArea.count}.by 1
expect(importer.status).to eq "success"
stop = Chouette::StopArea.last
expect(stop.name).to eq "Nom du Stop"
diff --git a/spec/models/simple_json_exporter_spec.rb b/spec/models/simple_json_exporter_spec.rb
new file mode 100644
index 000000000..4b48dc732
--- /dev/null
+++ b/spec/models/simple_json_exporter_spec.rb
@@ -0,0 +1,95 @@
+RSpec.describe SimpleJsonExporter do
+ describe "#define" do
+ context "with an incomplete configuration" do
+ it "should raise an error" do
+ SimpleJsonExporter.define :foo
+ expect do
+ SimpleJsonExporter.new(configuration_name: :test).export
+ end.to raise_error
+ end
+ end
+ context "with a complete configuration" do
+ before do
+ SimpleJsonExporter.define :foo do |config|
+ config.collection = Chouette::StopArea.all
+ end
+ end
+
+ it "should define an exporter" do
+ expect{SimpleJsonExporter.find_configuration(:foo)}.to_not raise_error
+ expect{SimpleJsonExporter.new(configuration_name: :foo, filepath: "").export}.to_not raise_error
+ expect{SimpleJsonExporter.find_configuration(:bar)}.to raise_error
+ expect{SimpleJsonExporter.new(configuration_name: :bar, filepath: "")}.to raise_error
+ expect{SimpleJsonExporter.new(configuration_name: :bar, filepath: "").export}.to raise_error
+ expect{SimpleJsonExporter.create(configuration_name: :foo, filepath: "")}.to change{SimpleJsonExporter.count}.by 1
+ end
+ end
+
+ context "when defining the same col twice" do
+ it "should raise an error" do
+ expect do
+ SimpleJsonExporter.define :foo do |config|
+ config.collection = Chouette::StopArea.all
+ config.add_field :name
+ config.add_field :name
+ end
+ end.to raise_error
+ end
+ end
+ end
+
+ describe "#export" do
+ let(:exporter){ importer = SimpleJsonExporter.new(configuration_name: :test, filepath: filepath) }
+ let(:filepath){ Rails.root + "tmp/" + filename }
+ let(:filename){ "stop_area.json" }
+ # let(:stop_area_referential){ create(:stop_area_referential, objectid_format: :stif_netex) }
+
+ context "with one row per item" do
+ before(:each) do
+ @stop_area = create :stop_area
+ @stop_1 = create :stop_point, stop_area: @stop_area
+ @stop_2 = create :stop_point, stop_area: @stop_area
+
+ SimpleJsonExporter.define :test do |config|
+ config.collection = ->{ Chouette::StopArea.all }
+ config.root = "stops"
+ config.add_field :name
+ config.add_field :lat, attribute: :latitude
+ config.add_field :lng, attribute: [:latitude, :to_i, :next]
+ config.add_field :type, attribute: :area_type
+ config.add_field :street_name, value: "Lil Exporter"
+ config.add_node :stop_area_referential do |config|
+ config.add_field :id, attribute: :id
+ config.add_field :id_other, value: ->(item){ item.id }
+ end
+
+ config.add_nodes :stop_points do |config|
+ config.add_field :id
+ config.add_node :stop_area do |config|
+ config.add_field :id
+ end
+ end
+ config.add_field :forty_two, value: 42
+ end
+ end
+
+ it "should export the given file" do
+ expect{exporter.export verbose: false}.to_not raise_error
+ expect(exporter.status).to eq "success"
+ expect(File.exists?(filepath)).to be_truthy
+ json = JSON.parse File.read(filepath)
+ row = json["stops"].first
+ expect(row["name"]).to eq @stop_area.name
+ expect(row["lat"]).to eq @stop_area.latitude.to_s
+ expect(row["lng"]).to eq (@stop_area.latitude.to_i + 1)
+ expect(row["street_name"]).to eq "Lil Exporter"
+ expect(row["stop_area_referential"]["id"]).to eq @stop_area.stop_area_referential_id
+ expect(row["stop_area_referential"]["id_other"]).to eq @stop_area.stop_area_referential_id
+ expect(row["stop_points"][0]["id"]).to eq @stop_1.id
+ expect(row["stop_points"][0]["stop_area"]["id"]).to eq @stop_area.id
+ expect(row["stop_points"][1]["id"]).to eq @stop_2.id
+ expect(row["forty_two"]).to eq 42
+ end
+ end
+ end
+end