示例#1
0
  @Test
  public void shouldIncludeOnlyProvidedAttributes() {
    deleteAndPopulateTables("users", "addresses");

    User u = User.findById(1);
    String json = u.toJson(true, "email", "last_name");
    JsonHelper.readTree(json); // validate
    the(json)
        .shouldBeEqual(
            "{\n" + "  \"email\":\"[email protected]\",\n" + "  \"last_name\":\"Monroe\"\n" + "}");
  }
示例#2
0
  @Test
  public void testUniquenessValidator() {
    deleteAndPopulateTables("users", "addresses");
    // create a new user
    new User().set("email", "*****@*****.**").saveIt();

    // attempt creating another user with the same email
    User u = new User();
    u.set("email", "*****@*****.**").saveIt();
    a(u).shouldNotBe("valid");
    a(u.errors().get("email")).shouldBeEqual("This email is already taken.");
  }
示例#3
0
  @Test
  public void testSaveItMethod() {
    deleteAndPopulateTables("users", "addresses");
    final User u = new User();

    // cause exception
    u.set("email", "this is not email value");
    expect(
        new ExceptionExpectation(ValidationException.class) {
          public void exec() {
            u.saveIt();
          }
        });
  }
示例#4
0
  @Test
  public void shouldGenerateFromList() {
    deleteAndPopulateTables("users", "addresses");
    LazyList<User> personList = User.findAll().orderBy("id").include(Address.class);

    String json = personList.toJson(false);
    JsonHelper.readTree(json); // validate
  }
示例#5
0
  @Test
  public void shouldIncludeUglyChildren() {
    deleteAndPopulateTables("users", "addresses");
    List<User> personList = User.findAll().orderBy("id").include(Address.class);
    User u = personList.get(0);
    String json = u.toJson(false);
    Map m = JsonHelper.toMap(json);

    a(m.get("first_name")).shouldBeEqual("Marilyn");
    a(m.get("last_name")).shouldBeEqual("Monroe");

    Map children = (Map) m.get("children");
    List<Map> addresses = (List<Map>) children.get("addresses");

    the(addresses.size()).shouldBeEqual(3);
    // at this point, no need to verify, since the order of addresses is not guaranteed.. or is it??
  }
示例#6
0
  @Test
  public void testRegexpValidator() {
    deleteAndPopulateTables("users", "addresses");
    User u = new User();

    // test good value
    u.set("email", "*****@*****.**");
    u.validate();
    a(u.errors().size()).shouldBeEqual(0);

    // test bad value
    u.set("email", "this is not email value");
    u.validate();
    a(u.errors().size()).shouldBeEqual(1);
  }