# Server Encoding: UTF8
# VERSION: PostgreSQL 9.3.5 on x86_64-unknown-linux-gnu, compiled by gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2, 64-bit
# Ubuntu 14.04.1 LTS

USER=postgres
DATABASE=postgres

echo 'Creating the table and adding the Hstore extension.'

psql -U $USER -d $DATABASE -c 'CREATE EXTENSION IF NOT EXISTS hstore; DROP TABLE IF EXISTS hstore_to_json_test; CREATE TABLE hstore_to_json_test (properties hstore);'


### TEST 1 ###

echo 'Adding the data with only one key/value pair'.

printf 'content=>"Ausstellung 16.11.2014 : Prinzessinstaße  9.11.2014: Sommerudstillingen \x14\x1426.6. &#8211; 10.8.2014\x03\x10\x15\x05\x16\x05\x0F\x17\x03\x10\x15\x05\x05\x0F\x10\x14\x18\x14\x14\x03\x10\x15\x05\x16\x05\x0F\x17\x03\x10\x15\x05\x05\x0F\x10\x14\x18 : GEDOKmünchen \x14\x1427.6. &#8211; 13.7.2014\x03\x10\x15\x05\x16\x05\x0F\x17\x03\x10\x15\x05\x05\x0F\x10\x14\x18\x14\x14\x03\x10\x15\x05\x16\x05\x0F\x17\x03\x10\x15\x05\x05\x0F\x10\x14\x18 : Paradies \x14\x1428.3. &#8211; 13.4.2014\x03\x10\x15\x05\x16\x05\x0F\x17\x03\x10\x15\x05\x05\x0F\x10\x14\x18\x14\x14\x03\x10\x15\x05\x16\x05\x0F\x17\x03\x10\x15\x05\x05\x0F\x10\x14\x18 : GRÆNSELANDSUDSTILLINGEN 2013 \x14\x1411.8. &#8211; 23.8.2013\x03\x10\x15\x05\x16\x05\x0F\x17\x03\x10\x15\x05\x05\x0F\x10\x14\x18\x14\x14\x03\x10\x15\x05\x16\x05\x0F\x17\x03\x10\x15\x05\x05\x0F\x10\x14\x18 &hellip;"' | psql -U $USER -d $DATABASE -c 'COPY hstore_to_json_test (properties) FROM STDIN'

echo 'Attempting to cast the properties (hstore) column to JSON (twice in the same session). This works!'

psql -U $USER -d $DATABASE -c 'SELECT hstore_to_json(properties) FROM hstore_to_json_test; SELECT hstore_to_json(properties) FROM hstore_to_json_test;'


### TEST 2 ###

echo 'Now truncate the table and load the same key/value pair, as well as another key/value pair ("a"=>"b").'

psql -U $USER -d $DATABASE -c 'TRUNCATE hstore_to_json_test;'

printf 'a=>b, content=>"Ausstellung 16.11.2014 : Prinzessinstaße  9.11.2014: Sommerudstillingen \x14\x1426.6. &#8211; 10.8.2014\x03\x10\x15\x05\x16\x05\x0F\x17\x03\x10\x15\x05\x05\x0F\x10\x14\x18\x14\x14\x03\x10\x15\x05\x16\x05\x0F\x17\x03\x10\x15\x05\x05\x0F\x10\x14\x18 : GEDOKmünchen \x14\x1427.6. &#8211; 13.7.2014\x03\x10\x15\x05\x16\x05\x0F\x17\x03\x10\x15\x05\x05\x0F\x10\x14\x18\x14\x14\x03\x10\x15\x05\x16\x05\x0F\x17\x03\x10\x15\x05\x05\x0F\x10\x14\x18 : Paradies \x14\x1428.3. &#8211; 13.4.2014\x03\x10\x15\x05\x16\x05\x0F\x17\x03\x10\x15\x05\x05\x0F\x10\x14\x18\x14\x14\x03\x10\x15\x05\x16\x05\x0F\x17\x03\x10\x15\x05\x05\x0F\x10\x14\x18 : GRÆNSELANDSUDSTILLINGEN 2013 \x14\x1411.8. &#8211; 23.8.2013\x03\x10\x15\x05\x16\x05\x0F\x17\x03\x10\x15\x05\x05\x0F\x10\x14\x18\x14\x14\x03\x10\x15\x05\x16\x05\x0F\x17\x03\x10\x15\x05\x05\x0F\x10\x14\x18 &hellip;"' | psql -U $USER -d $DATABASE -c 'COPY hstore_to_json_test (properties) FROM STDIN'

echo 'Attempting to cast the properties (hstore) column to JSON with a short key (twice in the same session). This works!'

psql -U $USER -d $DATABASE -c 'SELECT hstore_to_json(properties) FROM hstore_to_json_test; SELECT hstore_to_json(properties) FROM hstore_to_json_test;'


### TEST 3 ###

echo 'Now truncate the table and load the first key/value pair, as well as a key/value pair with a longer key ("aaaaaaaa"=>"b").'

psql -U $USER -d $DATABASE -c 'TRUNCATE hstore_to_json_test;'

printf 'aaaaaaaa=>b, content=>"Ausstellung 16.11.2014 : Prinzessinstaße  9.11.2014: Sommerudstillingen \x14\x1426.6. &#8211; 10.8.2014\x03\x10\x15\x05\x16\x05\x0F\x17\x03\x10\x15\x05\x05\x0F\x10\x14\x18\x14\x14\x03\x10\x15\x05\x16\x05\x0F\x17\x03\x10\x15\x05\x05\x0F\x10\x14\x18 : GEDOKmünchen \x14\x1427.6. &#8211; 13.7.2014\x03\x10\x15\x05\x16\x05\x0F\x17\x03\x10\x15\x05\x05\x0F\x10\x14\x18\x14\x14\x03\x10\x15\x05\x16\x05\x0F\x17\x03\x10\x15\x05\x05\x0F\x10\x14\x18 : Paradies \x14\x1428.3. &#8211; 13.4.2014\x03\x10\x15\x05\x16\x05\x0F\x17\x03\x10\x15\x05\x05\x0F\x10\x14\x18\x14\x14\x03\x10\x15\x05\x16\x05\x0F\x17\x03\x10\x15\x05\x05\x0F\x10\x14\x18 : GRÆNSELANDSUDSTILLINGEN 2013 \x14\x1411.8. &#8211; 23.8.2013\x03\x10\x15\x05\x16\x05\x0F\x17\x03\x10\x15\x05\x05\x0F\x10\x14\x18\x14\x14\x03\x10\x15\x05\x16\x05\x0F\x17\x03\x10\x15\x05\x05\x0F\x10\x14\x18 &hellip;"' | psql -U $USER -d $DATABASE -c 'COPY hstore_to_json_test (properties) FROM STDIN'

echo 'Attempting to cast the properties (hstore) column to JSON with a longer key (once in this session). This works!'

psql -U $USER -d $DATABASE -c 'SELECT hstore_to_json(properties) FROM hstore_to_json_test;'


### TEST 4 ###

echo 'Attempting to cast the properties (hstore) column to JSON with a longer key (twice in the same session). This breaks!'

psql -U $USER -d $DATABASE -c 'SELECT hstore_to_json(properties) FROM hstore_to_json_test; SELECT hstore_to_json(properties) FROM hstore_to_json_test;'
