Skip to content

Instantly share code, notes, and snippets.

@matejuh
Created February 23, 2012 12:40
Show Gist options
  • Save matejuh/1892658 to your computer and use it in GitHub Desktop.
Save matejuh/1892658 to your computer and use it in GitHub Desktop.
play-siena relationship
Thing(thing1):
name: thing1
Thing(thing2):
name: thing2
Parent(p1):
name: parent1
things: thing1, thing2
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import models.Flight;
import models.Parent;
import models.Thing;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import play.modules.siena.SienaFixtures;
import play.test.UnitTest;
import siena.Model;
public class ModelTest extends UnitTest {
@Before
public void setUp() throws Exception {
SienaFixtures.delete(Parent.class);
SienaFixtures.delete(Thing.class);
SienaFixtures.loadModels("model.yml");
}
@Test
public void fixtureTest(){
Parent p=Model.all(Parent.class).filter("name", "parent1").get();
assertNotNull(p);
}
@Test
public void relationTest(){
Parent p=Model.all(Parent.class).filter("name", "parent1").get();
assertEquals(2, p.things.asList().size());
}
@Test
public void thingTest(){
Thing t=Model.all(Thing.class).filter("name", "thing1").get();
assertNotNull(t);
assertNotNull(t.parent);
assertEquals("parent1", t.parent.name);
}
}
ModelTest
test Failure, expected:<10> but was:<0>
In /test/ModelTest.java, line 53 :
assertEquals(10, r.things.asList().size());
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import models.Flight;
import models.Parent;
import models.Thing;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import play.test.UnitTest;
import siena.Model;
public class ModelTest extends UnitTest {
// @Before
// public void setUp() throws Exception {
// }
//
// @After
// public void tearDown() throws Exception {
// }
@Test
public void test(){
//init parent
Parent p=new Parent("parent1");
//give him 10 things
for(int i=0; i<10; i++){
Thing thing = new Thing("thing"+i);
p.things.asList().add(thing);
}
//save parent + things
p.save();
//retrieve parent back
Parent r=Model.all(Parent.class).get();
//assert that I got something back
assertNotNull(r);
//assert parent name
assertEquals("parent1", r.name);
//get parents things
//this test fails
assertEquals(10, r.things.asList().size());
}
}
package models;
import siena.Generator;
import siena.Id;
import siena.Model;
import siena.Table;
import siena.core.Many;
import siena.core.Owned;
@Table("parents")
public class Parent extends Model {
@Id(Generator.AUTO_INCREMENT)
public Long id;
public String name;
@Owned(mappedBy="parent")
public Many<Thing> things;
public Parent(){
super();
}
public Parent(String name){
super();
this.name=name;
}
}
package models;
import siena.Generator;
import siena.Id;
import siena.Model;
import siena.Table;
import play.modules.siena.EnhancedModel;
@Table("things")
public class Thing extends Model {
@Id(Generator.AUTO_INCREMENT)
public Long id;
public String name;
public Parent parent;
public Thing(){
super();
}
public Thing(String name){
super();
this.name=name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment