@@ -1606,20 +1606,87 @@ def self.dynamoid_field_type
16061606 end
16071607
16081608 describe 'Binary field' do
1609- let ( :klass ) do
1610- new_class do
1611- field :image , :binary
1609+ let ( :unfrozen_string ) { +"\x00 \x88 \xFF " }
1610+ let ( :binary_value ) { unfrozen_string . force_encoding ( 'ASCII-8BIT' ) }
1611+
1612+ context 'default non-native binary' do
1613+ let ( :klass ) do
1614+ new_class do
1615+ field :image , :binary
1616+ end
1617+ end
1618+
1619+ it 'encodes a string in base64-encoded format' do
1620+ obj = klass . create ( image : binary_value )
1621+
1622+ expect ( reload ( obj ) . image ) . to eql ( binary_value )
1623+ expect ( raw_attributes ( obj ) [ :image ] ) . to eql ( Base64 . strict_encode64 ( binary_value ) )
16121624 end
16131625 end
16141626
1615- let ( :unfrozen_string ) { +"\x00 \x88 \xFF " }
1616- let ( :binary_value ) { unfrozen_string . force_encoding ( 'ASCII-8BIT' ) }
1627+ context 'native binary' do
1628+ let ( :klass ) do
1629+ new_class do
1630+ field :image , :binary , store_as_native_binary : true
1631+ end
1632+ end
1633+
1634+ it 'converts string to StringIO object' do
1635+ obj = klass . create ( image : binary_value )
1636+
1637+ expect ( reload ( obj ) . image ) . to eql ( binary_value )
1638+ expect ( raw_attributes ( obj ) [ :image ] . class ) . to eql ( StringIO )
1639+ expect ( raw_attributes ( obj ) [ :image ] . string ) . to eql ( binary_value )
1640+ end
16171641
1618- it 'encodes a string in base64-encoded format' do
1619- obj = klass . create ( image : binary_value )
1642+ it 'accepts StringIO object' do
1643+ image = StringIO . new ( binary_value )
1644+ obj = klass . create ( image : image )
16201645
1621- expect ( reload ( obj ) . image ) . to eql ( binary_value )
1622- expect ( raw_attributes ( obj ) [ :image ] ) . to eql ( Base64 . strict_encode64 ( binary_value ) )
1646+ expect ( reload ( obj ) . image ) . to eql ( binary_value )
1647+ expect ( raw_attributes ( obj ) [ :image ] . class ) . to eql ( StringIO )
1648+ expect ( raw_attributes ( obj ) [ :image ] . string ) . to eql ( binary_value )
1649+ end
1650+
1651+ it 'accepts IO object' do
1652+ Tempfile . create ( 'image' ) do |image |
1653+ image . write ( binary_value )
1654+ image . rewind
1655+
1656+ obj = klass . create ( image : image )
1657+
1658+ expect ( reload ( obj ) . image ) . to eql ( binary_value )
1659+ expect ( raw_attributes ( obj ) [ :image ] . class ) . to eql ( StringIO )
1660+ expect ( raw_attributes ( obj ) [ :image ] . string ) . to eql ( binary_value )
1661+ end
1662+ end
1663+ end
1664+
1665+ context 'store_binary_as_native config option' do
1666+ it 'is stored as binary if store_binary_as_native config option is true' ,
1667+ config : { store_binary_as_native : true } do
1668+ klass = new_class do
1669+ field :image , :binary
1670+ end
1671+
1672+ obj = klass . create ( image : binary_value )
1673+
1674+ expect ( reload ( obj ) . image ) . to eql ( binary_value )
1675+ expect ( raw_attributes ( obj ) [ :image ] . class ) . to eql ( StringIO )
1676+ expect ( raw_attributes ( obj ) [ :image ] . string ) . to eql ( binary_value )
1677+ end
1678+
1679+ it 'is not stored as binary if store_binary_as_native config option is false' ,
1680+ config : { store_binary_as_native : false } do
1681+ klass = new_class do
1682+ field :image , :binary
1683+ end
1684+
1685+ obj = klass . create ( image : binary_value )
1686+
1687+ expect ( reload ( obj ) . image ) . to eql ( binary_value )
1688+ expect ( raw_attributes ( obj ) [ :image ] ) . to eql ( Base64 . strict_encode64 ( binary_value ) )
1689+ end
16231690 end
16241691 end
16251692end
0 commit comments