spec/functional/render/attribute_spec.rb in render-0.0.8 vs spec/functional/render/attribute_spec.rb in render-0.0.9

- old
+ new

@@ -8,19 +8,77 @@ after(:each) do Generator.instances = @original_generators end - it "uses matching generator for #faux_value" do - name = "Canada Dry" - Generator.create!(String, %r{.*name.*}, proc { name }) + describe "#default_value" do + it "returns default value defined by schema" do + schema_default = "foo" + attribute = HashAttribute.new({ name: { type: String, default: schema_default } }) - HashAttribute.new({ name: { type: String } }).default_value.should == name - end + Render.stub({ live: false }) + attribute.default_value.should == schema_default + Render.stub({ live: true }) + attribute.default_value.should == schema_default + end - it "uses bare-boned type if no generator is found" do - bare_boned_string = "the_attribute_name (generated)" - HashAttribute.new({ the_attribute_name: { type: String } }).default_value.should == bare_boned_string + it "returns fake data from matching generator" do + name = "Canada Dry" + Generator.create!(String, %r{.*name.*}, proc { name }) + + HashAttribute.new({ name: { type: String } }).default_value.should == name + end + + it "generates fake data for all standard JSON types" do + # Objects' and Arrays' fake data comes from their attributes. + + string_attribute = HashAttribute.new({ name: { type: "string" } }) + string_attribute.default_value.should be_a(String) + + number_attribute = HashAttribute.new({ name: { type: "number" } }) + number_attribute.default_value.should be_a(Float) + + boolean_attribute = HashAttribute.new({ name: { type: "boolean" } }) + [true, false].should include(boolean_attribute.default_value) + + Render.logger.should_not_receive(:warn) + null_attribute = HashAttribute.new({ name: { type: "null" } }) + null_attribute.default_value.should == nil + end + + it "generates fake data for all standard JSON formats" do + hostname_attribute = HashAttribute.new({ name: { format: "hostname" } }) + hostname_attribute.default_value.should eq("localhost") + + ipv4_attribute = HashAttribute.new({ name: { format: "ipv4" } }) + ipv4_attribute.default_value.should eq("127.0.0.1") + + ipv6_attribute = HashAttribute.new({ name: { format: "ipv6" } }) + ipv6_attribute.default_value.should eq("::1") + + date_time = DateTime.now + DateTime.stub({ now: date_time }) + date_time_attribute = HashAttribute.new({ name: { format: "date-time" } }) + date_time_attribute.default_value.should eq(date_time.to_s) + + email_attribute = HashAttribute.new({ name: { format: "email" } }) + email_attribute.default_value.should eq("you@localhost") + + email_attribute = HashAttribute.new({ name: { format: "uri" } }) + email_attribute.default_value.should eq("http://localhost") + end + + it "generates fake data for enums" do + enum_values = ["foo", "bar"] + enum_attribute = HashAttribute.new({ name: { enum: enum_values } }) + enum_values.should include(enum_attribute.default_value) + end + + it "biases format's generator to type's generator" do + ipv6_attribute = HashAttribute.new({ name: { type: String, format: "ipv6" } }) + ipv6_attribute.default_value.should eq("::1") + end + end end end end