diff options
| author | cedricnjanga | 2017-09-22 15:02:32 +0200 | 
|---|---|---|
| committer | cedricnjanga | 2017-09-22 15:02:32 +0200 | 
| commit | f59f05bbf073f90edd2ae0006f028d802b6ed675 (patch) | |
| tree | 0d44e65ba630a359f11dd38e5bca9c6a2804b62b /spec/lib | |
| parent | c8e5212c9cb03c891db6198d3f5cb58607a805ab (diff) | |
| parent | 82020f683977e7b0373ebfc5c6c2d4549e670990 (diff) | |
| download | chouette-core-f59f05bbf073f90edd2ae0006f028d802b6ed675.tar.bz2 | |
Merge branch 'master' into staging
Diffstat (limited to 'spec/lib')
| -rw-r--r-- | spec/lib/model_attribute_spec.rb | 117 | 
1 files changed, 117 insertions, 0 deletions
| diff --git a/spec/lib/model_attribute_spec.rb b/spec/lib/model_attribute_spec.rb new file mode 100644 index 000000000..427e01490 --- /dev/null +++ b/spec/lib/model_attribute_spec.rb @@ -0,0 +1,117 @@ +RSpec.describe ModelAttribute do +  describe ".define" do +    it "adds a new instance of ModelAttribute to .all" do +      expect do +        ModelAttribute.define(:route, :name, :string) +      end.to change { ModelAttribute.all.length }.by(1) + +      model_attr = ModelAttribute.all.last + +      expect(model_attr).to be_an_instance_of(ModelAttribute) +      expect(model_attr.klass).to eq(:route) +      expect(model_attr.name).to eq(:name) +      expect(model_attr.data_type).to eq(:string) +    end +  end + +  describe ".classes" do +    it "returns the list of classes of ModelAttributes in .all" do +      ModelAttribute.instance_variable_set(:@__all__, [ +        ModelAttribute.new(:route, :name, :string), +        ModelAttribute.new(:journey_pattern, :name, :string), +        ModelAttribute.new(:time_table, :start_date, :date) +      ]) + +      expect(ModelAttribute.classes).to match_array([ +        'Route', +        'JourneyPattern', +        'TimeTable' +      ]) +    end +  end + +  describe ".from_code" do +    it "returns a ModelAttribute from a given code" do +      ModelAttribute.instance_variable_set(:@__all__, [ +        ModelAttribute.new(:journey_pattern, :name, :string) +      ]) + +      expect(ModelAttribute.from_code('journey_pattern#name')).to eq( +        ModelAttribute.new(:journey_pattern, :name, :string) +      ) +    end +  end + +  describe ".group_by_class" do +    it "returns all ModelAttributes grouped by klass" do +      ModelAttribute.instance_variable_set(:@__all__, [ +        ModelAttribute.new(:route, :name, :string), +        ModelAttribute.new(:route, :published_name, :string), +        ModelAttribute.new(:journey_pattern, :name, :string), +        ModelAttribute.new(:vehicle_journey, :number, :integer) +      ]) + +      expect(ModelAttribute.group_by_class).to eq({ +        route: [ +          ModelAttribute.new(:route, :name, :string), +          ModelAttribute.new(:route, :published_name, :string), +        ], +        journey_pattern: [ +          ModelAttribute.new(:journey_pattern, :name, :string), +        ], +        vehicle_journey: [ +          ModelAttribute.new(:vehicle_journey, :number, :integer) +        ] +      }) +    end +  end + +  describe ".methods_by_class" do +    it "returns all ModelAttributes for a given class" do +      ModelAttribute.instance_variable_set(:@__all__, [ +        ModelAttribute.new(:route, :name, :string), +        ModelAttribute.new(:route, :published_name, :string), +        ModelAttribute.new(:route, :direction, :string), +        ModelAttribute.new(:journey_pattern, :name, :string) +      ]) + +      expect(ModelAttribute.methods_by_class(:route)).to match_array([ +        ModelAttribute.new(:route, :name, :string), +        ModelAttribute.new(:route, :published_name, :string), +        ModelAttribute.new(:route, :direction, :string) +      ]) +    end +  end + +  describe ".methods_by_class_and_type" do +    it "returns ModelAttributes of a certain class and type" do +      ModelAttribute.instance_variable_set(:@__all__, [ +        ModelAttribute.new(:route, :name, :string), +        ModelAttribute.new(:route, :checked_at, :date), +        ModelAttribute.new(:journey_pattern, :name, :string), +        ModelAttribute.new(:journey_pattern, :section_status, :integer) +      ]) + +      expect(ModelAttribute.methods_by_class_and_type(:route, :string)).to match_array([ +        ModelAttribute.new(:route, :name, :string) +      ]) +    end +  end + +  describe "#code" do +    it "returns a string representation of the attribute" do +      model_attr = ModelAttribute.new(:route, :name, :string) + +      expect(model_attr.code).to eq('route#name') +    end +  end + +  describe "#==" do +    it "returns true when :klass, :name, and :data_type attributes match" do +      route_name = ModelAttribute.new(:route, :name, :string) +      other_route_name = ModelAttribute.new(:route, :name, :string) + +      expect(route_name == other_route_name).to be true +    end +  end +end | 
