示例#1
0
  public void testForUpdate() {
    ResetBasicData.reset();

    Query<Order> query =
        Ebean.find(Order.class)
            .setAutofetch(false)
            .setForUpdate(false)
            .setMaxRows(1)
            .order()
            .asc("orderDate")
            .order()
            .desc("id");

    int rc = query.findList().size();
    Assert.assertTrue(rc > 0);
    Assert.assertTrue(!query.getGeneratedSql().toLowerCase().contains("for update"));

    query =
        Ebean.find(Order.class)
            .setAutofetch(false)
            .setForUpdate(true)
            .setMaxRows(1)
            .order()
            .asc("orderDate")
            .order()
            .desc("id");

    rc = query.findList().size();
    Assert.assertTrue(rc > 0);
    Assert.assertTrue(query.getGeneratedSql().toLowerCase().contains("for update"));
  }
  @Test
  public void test() {

    ResetBasicData.reset();

    Query<Order> q =
        Ebean.find(Order.class)
            .fetch("shipments")
            .fetch("details")
            .fetch("details.product")
            .fetch("customer")
            .where()
            .gt("id", 0)
            .query();

    List<Order> list = q.findList();
    String sql = q.getGeneratedSql();

    Assert.assertTrue(!list.isEmpty());
    Assert.assertTrue(sql.contains("join o_customer "));

    Assert.assertFalse(sql.contains("left outer join contact "));
    Assert.assertFalse(sql.contains("left outer join o_order_detail "));
    Assert.assertFalse(sql.contains("left outer join o_product "));
  }
  @Test
  public void test_jsonWithPersistenceContext() {

    ResetBasicData.reset();

    List<Order> orders =
        Ebean.find(Order.class)
            .fetch("customer", "id, name")
            .where()
            .eq("customer.id", 1)
            .findList();

    String json = Ebean.json().toJson(orders);

    List<Order> orders1 = Ebean.json().toList(Order.class, json);

    Customer customer = null;
    for (Order order : orders1) {
      Customer tempCustomer = order.getCustomer();
      if (customer == null) {
        customer = tempCustomer;
      } else {
        assertThat(tempCustomer).isSameAs(customer);
      }
    }
  }
  public void test() {

    ResetBasicData.reset();

    try {
      File f = new File("src/test/resources/test1.csv");

      FileReader reader = new FileReader(f);

      CsvReader<Customer> csvReader = Ebean.createCsvReader(Customer.class);

      csvReader.setPersistBatchSize(2);

      csvReader.addIgnore();
      // csvReader.addProperty("id");
      csvReader.addProperty("status");
      csvReader.addProperty("name");
      csvReader.addDateTime("anniversary", "dd-MMM-yyyy", Locale.GERMAN);
      csvReader.addProperty("billingAddress.line1");
      csvReader.addProperty("billingAddress.city");
      csvReader.addReference("billingAddress.country.code");

      csvReader.process(reader);

    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
  public void test() {

    ResetBasicData.reset();

    String sql =
        " select order_id, o.status, c.id, c.name, sum(d.order_qty*d.unit_price) as totalAmount"
            + " from o_order o"
            + " join o_customer c on c.id = o.kcustomer_id "
            + " join o_order_detail d on d.order_id = o.id "
            + " group by order_id, o.status, c.id, c.name ";

    RawSql rawSql =
        RawSqlBuilder.parse(sql)
            .columnMapping("order_id", "order.id")
            .columnMapping("o.status", "order.status")
            .columnMapping("c.id", "order.customer.id")
            .columnMapping("c.name", "order.customer.name")
            // .columnMapping("sum(d.order_qty*d.unit_price)", "totalAmount")
            .create();

    List<OrderAggregate> list2 =
        Ebean.find(OrderAggregate.class)
            .setRawSql(rawSql)
            .fetch("order", new FetchConfig().query())
            .fetch("order.details", new FetchConfig().query())
            .where()
            .gt("order.id", 2)
            .having()
            .gt("totalAmount", 10)
            .filterMany("order.details")
            .gt("unitPrice", 2d)
            .findList();

    output(list2);
  }
  public void test() {

    ResetBasicData.reset();

    Query<Order> query =
        Ebean.find(Order.class)
            .where()
            .eq("status", Order.Status.NEW)
            .ge("id", 1)
            .order()
            .desc("id");

    int rc0 = query.findRowCount();

    List<Object> ids = query.findIds();
    Assert.assertEquals(rc0, ids.size());

    List<Order> list0 = query.findList();
    Assert.assertEquals(rc0, list0.size());

    int rc1 = query.findRowCount();
    Assert.assertEquals(rc0, rc1);

    List<Object> ids1 = query.findIds();
    Assert.assertEquals(rc0, ids1.size());

    List<Order> list1 = query.findList();
    Assert.assertEquals(rc0, list1.size());

    int idGt = 5;
    if (ids1.size() > 0) {
      Object id = ids.get(0);
      idGt = Integer.valueOf("" + id);
    }

    // should still hit query plan cache
    Query<Order> query2 =
        Ebean.find(Order.class)
            .where()
            .eq("status", Order.Status.NEW)
            .ge("id", idGt)
            .order()
            .desc("id");

    int rc2 = query2.findRowCount();

    System.out.println("Expection Not same " + rc0 + " != " + rc2);
    Assert.assertNotSame(rc0, rc2);

    List<Object> ids2 = query2.findIds();
    Assert.assertEquals(rc2, ids2.size());

    List<Order> list2 = query2.findList();
    Assert.assertEquals(rc2, list2.size());
  }
  public void test() {

    ResetBasicData.reset();

    Ebean.find(Customer.class)
        .fetch("orders", new FetchConfig().query())
        .fetch("orders.details", new FetchConfig().query())
        .fetch("orders.shipments", new FetchConfig().query())
        .fetch("shippingAddress", new FetchConfig().query())
        .fetch("billingAddress", new FetchConfig().query())
        .findList();
  }
  @Test
  public void test_countInBackground_withLoadRowCount() throws InterruptedException {

    ResetBasicData.reset();

    // fetch less that total orders (page size 3)
    PagedList<Order> pagedList = Ebean.find(Order.class).findPagedList(0, 3);

    pagedList.loadRowCount();
    List<Order> orders = pagedList.getList();
    int totalRowCount = pagedList.getTotalRowCount();

    assertThat(orders.size()).isLessThan(totalRowCount);
  }
示例#9
0
  @Test
  public void testCountOrderBy() {

    ResetBasicData.reset();

    Query<Order> query =
        Ebean.find(Order.class).setAutofetch(false).order().asc("orderDate").order().desc("id");
    // .orderBy("orderDate");

    int rc = query.findList().size();
    // int rc = query.findRowCount();
    Assert.assertTrue(rc > 0);
    // String generatedSql = query.getGeneratedSql();
    // Assert.assertFalse(generatedSql.contains("order by"));

  }
  @Test
  public void test() {

    ResetBasicData.reset();

    String sql =
        "select order_id, sum(order_qty*unit_price) as total_amount from o_order_detail  where order_qty > :minQty  group by order_id";
    List<SqlRow> sqlRows = Ebean.createSqlQuery(sql).setParameter("minQty", 1).findList();

    for (SqlRow sqlRow : sqlRows) {
      Integer id = sqlRow.getInteger("order_id");
      Double amount = sqlRow.getDouble("total_amount");
      Assert.assertNotNull("sqlRows: " + sqlRows, id);
      Assert.assertNotNull("sqlRows: " + sqlRows, amount);
    }
  }
  @Test
  public void test_noCount() throws ExecutionException, InterruptedException {

    ResetBasicData.reset();

    PagedList<Order> pagedList = Ebean.find(Order.class).findPagedList(0, 4);

    LoggedSqlCollector.start();

    List<Order> orders = pagedList.getList();

    assertTrue(!orders.isEmpty());
    List<String> loggedSql = LoggedSqlCollector.stop();

    assertEquals("Only 1 SQL statement, no count query", 1, loggedSql.size());
  }
示例#12
0
  @Ignore
  @Test
  public void testBulkUpdate() throws IOException {

    ResetBasicData.reset();

    EbeanServer server = Ebean.getServer(null);
    JsonContext jsonContext = server.json();

    StringWriter writer = new StringWriter();
    JsonGenerator generator = jsonContext.createGenerator(writer);

    List<Customer> customers = Ebean.find(Customer.class).findList();

    for (Customer customer : customers) {

      customer.setName(customer.getName() + "esMod");
      PathProperties updateProps = PathProperties.parse("name");

      generator.writeStartObject();
      generator.writeFieldName("update");
      generator.writeStartObject();
      generator.writeStringField("_id", customer.getId().toString());
      generator.writeStringField("_type", "customer");
      generator.writeStringField("_index", "customer");
      generator.writeEndObject();
      generator.writeEndObject();
      generator.writeRaw("\n");

      generator.writeStartObject();
      generator.writeFieldName("doc");
      jsonContext.toJson(customer, generator, updateProps);
      generator.writeEndObject();
      generator.writeRaw("\n");
    }

    generator.close();
    String json = writer.toString();

    post("http://localhost:9200/_bulk", json);

    // curl -s -XPOST localhost:9200/_bulk

    //    { "update" : {"_id" : "1", "_type" : "type1", "_index" : "index1"} }
    //    { "doc" : {"field2" : "value2"} }

  }
示例#13
0
  @Ignore
  @Test
  public void test() throws IOException {

    ResetBasicData.reset();

    EbeanServer server = Ebean.getServer(null);
    JsonContext jsonContext = server.json();

    List<Customer> customers = Ebean.find(Customer.class).findList();

    PathProperties paths = PathProperties.parse("name, status, anniversary");
    for (Customer customer : customers) {

      String json = jsonContext.toJson(customer, paths);
      put("http://localhost:9200/customer/customer/" + customer.getId(), json);
    }
  }
  @Test
  public void test_countInBackground()
      throws ExecutionException, InterruptedException, TimeoutException {

    ResetBasicData.reset();

    PagedList<Order> pagedList = Ebean.find(Order.class).findPagedList(0, 3);

    Future<Integer> rowCount = pagedList.getFutureRowCount();
    List<Order> orders = pagedList.getList();

    // these are each getting the total row count
    int totalRowCount = pagedList.getTotalRowCount();
    Integer totalRowCountWithTimeout = rowCount.get(30, TimeUnit.SECONDS);
    Integer totalRowCountViaFuture = rowCount.get();

    assertTrue(orders.size() < totalRowCount);
    assertEquals(Integer.valueOf(totalRowCount), totalRowCountViaFuture);
    assertEquals(Integer.valueOf(totalRowCount), totalRowCountWithTimeout);
  }
  public void test() {

    ResetBasicData.reset();

    // Due to the use of maxRows... we will convert
    // the fetch join to contacts over to a query join
    // ... otherwise we wouldn't be able to use the
    // limit offset clause

    Query<Customer> query =
        Ebean.find(Customer.class)
            // this will automatically get converted to a
            // query join ... due to the maxRows
            .fetch("contacts")
            .setMaxRows(5);

    List<Customer> list = query.findList();

    System.out.println(list);
  }
  @Test
  public void test_json_loadContext() {

    ResetBasicData.reset();

    List<Order> orders =
        Ebean.find(Order.class).select("status").fetch("customer", "id, name").findList();

    String json = Ebean.json().toJson(orders);

    JsonReadOptions options = new JsonReadOptions().setEnableLazyLoading(true);

    List<Order> orders1 = Ebean.json().toList(Order.class, json, options);

    for (Order order : orders1) {
      Customer customer = order.getCustomer();
      customer.getName();
      customer.getSmallnote();
      List<Contact> contacts = customer.getContacts();
      contacts.size();
    }
  }
  @Test
  public void test_countUsingForegound() throws ExecutionException, InterruptedException {

    ResetBasicData.reset();

    PagedList<Order> pagedList = Ebean.find(Order.class).findPagedList(0, 6);

    LoggedSqlCollector.start();

    // kinda not normal but just wrap in a transaction to assert
    // the background fetch does not occur (which explicitly creates
    // its own transaction) ... so a bit naughty with the test here
    Ebean.beginTransaction();
    try {

      List<Order> orders = pagedList.getList();
      int totalRowCount = pagedList.getTotalRowCount();

      // invoke it again but cached...
      int totalRowCountAgain = pagedList.getTotalRowCount();

      List<String> loggedSql = LoggedSqlCollector.stop();

      assertTrue(orders.size() < totalRowCount);
      assertEquals(2, loggedSql.size());
      assertEquals(totalRowCount, totalRowCountAgain);

      String firstTxn = loggedSql.get(0).substring(0, 10);
      String secTxn = loggedSql.get(1).substring(0, 10);

      assertEquals(firstTxn, secTxn);

    } finally {
      Ebean.endTransaction();
    }
  }