desc: Tests insertion into tables
tests:

    # Set up our test tables
    - cd: r.db('test').table_create('test1')
      ot: ({'created':1})

    - def: tbl = r.db('test').table('test1')

    - cd: r.db('test').table_create('test2')
      ot: ({'created':1})

    - def: tbl2 = r.db('test').table('test2')

    # Single doc insert
    - cd: tbl.insert({'id':0,'a':0})
      ot: ({'deleted':0.0,'replaced':0.0,'unchanged':0.0,'errors':0.0,'skipped':0.0,'inserted':1})
    - cd: tbl.count()
      ot: 1

    # Hard durability insert
    - py: tbl.insert({'id':1, 'a':1}, durability='hard')
      js: tbl.insert({id:1, a:1}, {durability:'hard'})
      rb: tbl.insert({ :id => 1, :a => 1 }, { :durability => 'hard' })
      ot: ({'deleted':0.0,'replaced':0.0,'unchanged':0.0,'errors':0.0,'skipped':0.0,'inserted':1})
    - cd: tbl.count()
      ot: 2

    # Soft durability insert
    - py: tbl.insert({'id':2, 'a':2}, durability='soft')
      js: tbl.insert({id:2, a:2}, {durability:'soft'})
      rb: tbl.insert({ :id => 2, :a => 2 }, { :durability => 'soft' })
      ot: ({'deleted':0.0,'replaced':0.0,'unchanged':0.0,'errors':0.0,'skipped':0.0,'inserted':1})
    - cd: tbl.count()
      ot: 3

    # Wrong durability insert
    - py: tbl.insert({'id':3, 'a':3}, durability='wrong')
      js: tbl.insert({id:3, a:3}, {durability:'wrong'})
      rb: tbl.insert({ :id => 3, :a => 3 }, { :durability => 'wrong' })
      ot: err('RqlRuntimeError', 'Durability option `wrong` unrecognized (options are "hard" and "soft").', [0])
    - cd: tbl.count()
      ot: 3

    # Cleanup.
    - cd: tbl.get(2).delete()
      ot: ({'deleted':1,'replaced':0.0,'unchanged':0.0,'errors':0.0,'skipped':0.0,'inserted':0.0})

    # Multi doc insert
    - cd: tbl.insert([{'id':2,'a':2}, {'id':3,'a':3}])
      ot: ({'deleted':0.0,'replaced':0.0,'unchanged':0.0,'errors':0.0,'skipped':0.0,'inserted':2})

    # Stream insert
    - cd: tbl2.insert(tbl)
      ot: ({'deleted':0.0,'replaced':0.0,'unchanged':0.0,'errors':0.0,'skipped':0.0,'inserted':4})

    # test pkey clash error
    - cd: tbl.insert({'id':2,'b':20})
      ot: ({'first_error':"Duplicate primary key `id`:\n{\n\t\"a\":\t2,\n\t\"id\":\t2\n}\n{\n\t\"b\":\t20,\n\t\"id\":\t2\n}",'deleted':0.0,'replaced':0.0,'unchanged':0.0,'errors':1,'skipped':0.0,'inserted':0.0})

    # test upsert option (object exists)
    - py: tbl.insert({'id':2,'b':20}, upsert=True)
      js: tbl.insert({'id':2,'b':20}, {upsert:true})
      rb: tbl.insert({:id => 2, :b => 20}, { :upsert => true })
      ot: ({'deleted':0.0,'replaced':1,'unchanged':0.0,'errors':0.0,'skipped':0.0,'inserted':0.0})

    - cd: tbl.get(2)
      ot: ({'id':2,'b':20})

    # test upsert option (object doesn't exit)
    - py: tbl.insert({'id':20,'b':20}, upsert=True)
      js: tbl.insert({'id':20,'b':20}, {upsert:true})
      rb: tbl.insert({:id => 20, :b => 20}, { :upsert => true })
      ot: ({'deleted':0.0,'replaced':0.0,'unchanged':0.0,'errors':0.0,'skipped':0.0,'inserted':1})

    - cd: tbl.get(20)
      ot: ({'id':20,'b':20})

    # test auto pkey generation
    - py: r.db('test').table_create('testpkey', primary_key='foo')
      js: r.db('test').tableCreate('testpkey', {primaryKey:'foo'})
      rb: r.db('test').table_create('testpkey', { :primary_key => 'foo' })
      ot: ({'created':1})

      def: tblpkey = r.db('test').table('testpkey')

    - cd: tblpkey.insert({})
      ot: ({'deleted':0.0,'replaced':0.0,'generated_keys':arrlen(1,uuid()),'unchanged':0.0,'errors':0.0,'skipped':0.0,'inserted':1})

    - cd: tblpkey
      ot: ([{'foo':uuid()}])

    # test upsert pkey generation
    - py: tblpkey.insert({'b':20}, upsert=True)
      js: tblpkey.insert({'b':20}, {upsert:true})
      rb: tblpkey.insert({:b => 20}, { :upsert => true })
      ot: ({'deleted':0.0,'replaced':0.0,'generated_keys':arrlen(1,uuid()),'unchanged':0.0,'errors':0.0,'skipped':0.0,'inserted':1})

    - cd: r.db('test').table_drop('testpkey')
      ot: "({'dropped':1})"

    # Insert within for each
    - py: "tbl.for_each(lambda  row:          tbl2.insert(row.merge({'id':row['id']  +  100 }))   )"
      js: "tbl.forEach(function(row) { return tbl2.insert(row.merge({'id':row('id').add(100)})); })"
      rb: tbl.for_each(proc {  |row|          tbl2.insert(row.merge({'id'=>row['id']  +  100 }))  })
      ot: ({'deleted':0.0,'replaced':0.0,'unchanged':0.0,'errors':0.0,'skipped':0.0,'inserted':5})

    # clean up
    - cd: r.db('test').table_drop('test1')
      ot: "({'dropped':1})"
    - cd: r.db('test').table_drop('test2')
      ot: "({'dropped':1})"

