2017-11-02 07:05:11 -07:00

148 lines
4.4 KiB
YAML

desc: Tests manipulation operations on objects
tests:
# Define some objects to work with
- def: obj = r.expr({'a':1, 'b':2,'c':"str",'d':null,'e':{'f':'buzz'}})
## Get attr
- cd: obj['a']
js:
- obj('a')
- obj.getField('a')
ot: 1
- cd: obj['c']
js:
- obj('c')
- obj.getField('c')
ot: 'str'
## Has_Fields
- cd: obj.has_fields('b')
ot: true
- cd: obj.keys().contains('d')
ot: true
- cd: obj.has_fields('d')
ot: false
- cd: obj.has_fields({'e':'f'})
ot: true
- cd: obj.has_fields({'e':'g'})
ot: false
- cd: obj.has_fields('f')
ot: false
# Has_Fields is variadic
- cd: obj.has_fields('a', 'b')
ot: true
- cd: obj.has_fields('a', 'd')
ot: false
- cd: obj.has_fields('a', 'f')
ot: false
- cd: obj.has_fields('a', {'e':'f'})
ot: true
# Has_Fields is polymorphic
- cd: r.expr([obj, obj.pluck('a', 'b')]).has_fields('a', 'b').count()
ot: 2
- cd: r.expr([obj, obj.pluck('a', 'b')]).has_fields('a', 'c').count()
ot: 1
- cd: r.expr([obj, obj.pluck('a', 'e')]).has_fields('a', {'e':'f'}).count()
ot: 2
## Pluck
- cd: obj.pluck('a')
ot: {'a':1}
- cd: obj.pluck('a', 'b')
ot: {'a':1, 'b':2}
## Without
- cd: obj.without('a')
ot: {'b':2, 'c':'str', 'd':null, 'e':{'f':'buzz'}}
- cd: obj.without('a', 'b')
ot: {'c':'str', 'd':null,'e':{'f':'buzz'}}
- cd: obj.without('a', 'b', 'c', 'd')
ot: {'e':{'f':'buzz'}}
- cd: obj.without({'e':'f'})
ot: {'a':1, 'b':2, 'c':'str', 'd':null, 'e':{}}
- cd: obj.without({'e':'buzz'})
ot: {'a':1, 'b':2, 'c':'str', 'd':null, 'e':{'f':'buzz'}}
## Merge
#obj = r.expr({'a':1, 'b':2,'c':"str",'d':null,'e':{'f':'buzz'}})
# complete replacement
- cd: obj.merge(1)
ot: 1
# add attr
- cd: obj.merge({'e':-2})
ot: {'a':1, 'b':2, 'c':'str', 'd':null, 'e':-2}
# delete attr
- cd: obj.merge({'e':r.literal()})
ot: {'a':1, 'b':2, 'c':'str', 'd':null}
# recursive merge
- cd: obj.merge({'e':{'f':'quux'}})
ot: {'a':1, 'b':2, 'c':'str', 'd':null, 'e':{'f':'quux'}}
- cd: obj.merge({'e':{'g':'quux'}})
ot: {'a':1, 'b':2, 'c':'str', 'd':null, 'e':{'f':'buzz', 'g':'quux'}}
- cd: obj.merge({'e':r.literal({'g':'quux'})})
ot: {'a':1, 'b':2, 'c':'str', 'd':null, 'e':{'g':'quux'}}
# overwrite
- cd: obj.merge({'a':-1})
ot: {'a':-1, 'b':2, 'c':'str', 'd':null, 'e':{'f':'buzz'}}
- def: errmsg = 'Stray literal keyword found:'+' literal is only legal inside of the object passed to merge or update and cannot nest inside other literals.'
# errors
- cd: r.literal('foo')
ot: err("ReqlQueryLogicError", errmsg, [])
- cd: obj.merge(r.literal('foo'))
ot: err("ReqlQueryLogicError", errmsg, [])
- cd: obj.merge({'foo':r.literal(r.literal('foo'))})
ot: err("ReqlQueryLogicError", errmsg, [])
- def: o = r.expr({'a':{'b':1, 'c':2}, 'd':3})
- cd: o.merge({'e':4}, {'f':5})
ot: ({'a':{'b':1, 'c':2}, 'd':3, 'e':4, 'f':5})
- rb: r.expr([o, o.merge({'d':4})]).merge{|row| {'e':row['d']}}
py: r.expr([o, o.merge({'d':4})]).merge(lambda row:{'e':row['d']})
js: r.expr([o, o.merge({'d':4})]).merge(function(row){return {'e':row('d')}})
ot: ([{'a':{'b':1, 'c':2}, 'd':3, 'e':3}, {'a':{'b':1, 'c':2}, 'd':4, 'e':4}])
- py: r.expr([o, o.merge({'d':4})]).merge({'e':r.row['d']})
js: r.expr([o, o.merge({'d':4})]).merge({'e':r.row('d')})
ot: ([{'a':{'b':1, 'c':2}, 'd':3, 'e':3}, {'a':{'b':1, 'c':2}, 'd':4, 'e':4}])
- rb: r.expr([o, o.merge({'d':4})]).merge{|row| {'a':{'b':2}}}
py: r.expr([o, o.merge({'d':4})]).merge(lambda row:{'a':{'b':2}})
js: r.expr([o, o.merge({'d':4})]).merge(function(row){return {'a':{'b':2}}})
ot: ([{'a':{'b':2, 'c':2}, 'd':3}, {'a':{'b':2, 'c':2}, 'd':4}])
- rb: r.expr([o, o.merge({'d':4})]).merge{|row| {'a':r.literal({'b':2})}}
py: r.expr([o, o.merge({'d':4})]).merge(lambda row:{'a':r.literal({'b':2})})
js: r.expr([o, o.merge({'d':4})]).merge(function(row){return {'a':r.literal({'b':2})}})
ot: ([{'a':{'b':2}, 'd':3}, {'a':{'b':2}, 'd':4}])
## keys
- cd: obj.keys()
ot: (['a', 'b', 'c', 'd', 'e'])
- cd: obj.values()
ot: ([1, 2, 'str', null, {'f':'buzz'}])
## count
- cd: obj.count()
ot: 5