# struct tests

FEATURE: relation-one-to-one-oneway
background:
 configure loadFKs = true
thenType: struct


# --- Child-only  ---
---
title: normal
given:
 type Customer struct {id int unique } 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]
then:
value:a:Customer {
 vid:55
}

---
title: normal2
given:
 type Customer struct {id int unique } 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 = Address[100]
then:
value:a:Address {
 vid:100
 vcust:{55}
}

---
title: optional-missing
given:
 //note forward-ref
 type Customer struct {id int unique } end
 type Address struct {id int unique, relation cust Customer optional one } end

  insert Customer {id: 55 }
when:
  insert Address {id: 100 }
  let x = Customer[55]
then:
value:a:Customer {
 vid:55
}

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

  insert Customer {id: 55 }
when:
  insert Address {id: 100, cust: 44 }
  let x = Customer[55];
then:
ERROR: rule-relationOne
}

# --- Parent-only (not supported) ---

# --- Mandatory Child-only ---
---
title: mandatory-normal
given:
 type Customer struct {id int unique } end
 type Address struct {id int unique, relation cust Customer one } end

  insert Customer {id: 55 }
when:
  insert Address {id: 100, cust: 55 }
  let x = Customer[55]
then:
value:a:Customer {
 vid:55
}

---
title: mandatory-optional-missing
given:
 //note forward-ref
 type Customer struct {id int unique } end
 type Address struct {id int unique, relation cust Customer one } end

  insert Customer {id: 55 }
when:
  insert Address {id: 100 }
  let x = Customer[55]
then:
ERROR: rule-relationOne

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

  insert Customer {id: 55 }
when:
  insert Address {id: 100, cust: 44 }
  let x = Customer[55];
then:
ERROR: rule-relationOne
}

---
title: mandatory-normal-other-way
given:
 type Customer struct {id int unique, relation addr Address one } end
 type Address struct {id int unique } end

  insert Address {id: 100 }
when:
  insert Customer {id: 55, addr: 100 }
  let x = Customer[55]
then:
value:a:Customer {
 vid:55
 vaddr:{100}
}
