Skip to content

Commit

Permalink
chore: clean up tests, reduce use of fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
ronaldtse committed Jun 14, 2024
1 parent 4f777c5 commit 342cd98
Show file tree
Hide file tree
Showing 16 changed files with 766 additions and 415 deletions.
72 changes: 0 additions & 72 deletions spec/building_spec.rb

This file was deleted.

33 changes: 0 additions & 33 deletions spec/custom_ceramic_spec.rb

This file was deleted.

17 changes: 0 additions & 17 deletions spec/fixtures/building.rb

This file was deleted.

58 changes: 0 additions & 58 deletions spec/fixtures/ceramic.rb

This file was deleted.

29 changes: 0 additions & 29 deletions spec/fixtures/custom_ceramic.rb

This file was deleted.

7 changes: 0 additions & 7 deletions spec/fixtures/glaze.rb

This file was deleted.

33 changes: 0 additions & 33 deletions spec/fixtures/kiln.rb

This file was deleted.

39 changes: 0 additions & 39 deletions spec/fixtures/pottery.rb

This file was deleted.

82 changes: 82 additions & 0 deletions spec/lutaml/model/collection_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# spec/lutaml/model/collection_spec.rb
require "spec_helper"
require "lutaml/model"

module CollectionTests
class Pot < Lutaml::Model::Serializable
attribute :material, Lutaml::Model::Type::String

xml do
root "pot"
map_element "material", to: :material
end
end

class Kiln < Lutaml::Model::Serializable
attribute :brand, Lutaml::Model::Type::String
attribute :pots, Pot, collection: true
attribute :temperatures, Lutaml::Model::Type::Integer, collection: true

xml do
root "kiln"
map_attribute "brand", to: :brand
map_element "pot", to: :pots
map_element "temperature", to: :temperatures
end
end
end

RSpec.describe CollectionTests do
let(:pots) { [CollectionTests::Pot.new(material: "clay"), CollectionTests::Pot.new(material: "ceramic")] }
let(:temperatures) { [1200, 1300, 1400] }
let(:attributes) { { brand: "Skutt", pots: pots, temperatures: temperatures } }
let(:model) { CollectionTests::Kiln.new(attributes) }

let(:model_xml) {
<<~XML
<kiln brand="Skutt">
<pot>
<material>clay</material>
</pot>
<pot>
<material>ceramic</material>
</pot>
<temperature>1200</temperature>
<temperature>1300</temperature>
<temperature>1400</temperature>
</kiln>
XML
}

it "initializes with default values" do
default_model = CollectionTests::Kiln.new
expect(default_model.brand).to eq(nil)
expect(default_model.pots).to eq([])
expect(default_model.temperatures).to eq([])
end

it "serializes to XML" do
expected_xml = model_xml.strip
expect(model.to_xml.strip).to eq(expected_xml)
end

it "deserializes from XML" do
sample = CollectionTests::Kiln.from_xml(model_xml)
expect(sample.brand).to eq("Skutt")
expect(sample.pots.size).to eq(2)
expect(sample.pots[0].material).to eq("clay")
expect(sample.pots[1].material).to eq("ceramic")
expect(sample.temperatures).to eq([1200, 1300, 1400])
end

it "round-trips XML" do
xml = model.to_xml
new_model = CollectionTests::Kiln.from_xml(xml)
expect(new_model.brand).to eq(model.brand)
expect(new_model.pots.size).to eq(model.pots.size)
model.pots.each_with_index do |pot, index|
expect(new_model.pots[index].material).to eq(pot.material)
end
expect(new_model.temperatures).to eq(model.temperatures)
end
end
Loading

0 comments on commit 342cd98

Please sign in to comment.