# fetch tests
# tests to do
#  normal
#  bad field name
#  null addr values
# new scope fetch("addr").id  should get Address id
# double fetch allowed  fetch('addr')...fetch('cust') -- will be messy!

FEATURE: fetch-field
background:
 configure loadFKs = true
thenType: struct

# --- Single ---
---
title: single
given:
 type Customer struct {id int unique, relation addr Address optional parent one } end
 type Address struct {id int unique, relation cust Customer optional one } end

  insert Customer {id: 55 }
when:
  insert Address {id: 100, cust: 55 }
  let x = Customer[55].fetch('addr').id
then:
value:a:int:100

---
title: single missing
given:
 type Customer struct {id int unique, relation addr Address optional parent one } end
 type Address struct {id int unique, relation cust Customer optional one } end

  insert Customer {id: 55 }
when:
  insert Address {id: 100, cust: null }
  let x = Customer[55].fetch('addr').id
then:
null

---
title: single bad ref
given:
 type Customer struct {id int unique, relation addr Address optional parent one } end
 type Address struct {id int unique, relation cust Customer optional one } end

  insert Customer {id: 55 }
when:
  insert Address {id: 100, cust: 55 }
  let x = Customer[55].fetch('addr').zzz
then:
ERROR: unknown-field

# --- Multiple ---
---
title: multiple
given:
 type Customer struct {id int unique, relation addr Address optional many } end
 type Address struct {id int unique, relation cust Customer optional one } end

  insert Customer {id: 55 }
when:
  insert Address {id: 100, cust: 55 }
  insert Address {id: 101, cust: 55 }
  let x = Customer[55].fetch('addr').id
then:
value:a:int:100
value:a:int:101

---
title: multiple partial missing
given:
 type Customer struct {id int unique, relation addr Address optional parent one } end
 type Address struct {id int unique, relation cust Customer optional one } end

  insert Customer {id: 55 }
when:
  insert Address {id: 100, cust: null }
  insert Address {id: 101, cust: 55 }
  let x = Customer[55].fetch('addr').id
then:
value:a:int:101

---
title: multiple all missing
given:
 type Customer struct {id int unique, relation addr Address optional parent one } end
 type Address struct {id int unique, relation cust Customer optional one } end

  insert Customer {id: 55 }
when:
  insert Address {id: 100, cust: null }
  insert Address {id: 101, cust: null }
  let x = Customer[55].fetch('addr').id
then:
null

