Search in sources :

Example 71 with Decimal128

use of org.bson.types.Decimal128 in project realm-java by realm.

the class RealmJsonTests method createObjectFromJson_streamDecimal128AsLong.

@Test
public void createObjectFromJson_streamDecimal128AsLong() throws IOException {
    assumeThat(Build.VERSION.SDK_INT, greaterThanOrEqualTo(Build.VERSION_CODES.HONEYCOMB));
    InputStream in = TestHelper.loadJsonFromAssets(context, "decimal128_as_long.json");
    realm.beginTransaction();
    realm.createObjectFromJson(AllTypes.class, in);
    realm.commitTransaction();
    in.close();
    AllTypes obj = realm.where(AllTypes.class).findFirst();
    assertEquals(new Decimal128(-32361122672259149L), obj.getColumnDecimal128());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) AllTypes(io.realm.entities.AllTypes) Decimal128(org.bson.types.Decimal128) Test(org.junit.Test)

Example 72 with Decimal128

use of org.bson.types.Decimal128 in project realm-java by realm.

the class RealmJsonTests method assertAllTypesPrimaryKeyUpdated.

// Asserts that the list of AllTypesPrimaryKey objects where inserted and updated properly.
private void assertAllTypesPrimaryKeyUpdated() {
    assertEquals(1, realm.where(AllTypesPrimaryKey.class).count());
    AllTypesPrimaryKey obj = realm.where(AllTypesPrimaryKey.class).findFirst();
    assertEquals("Bar", obj.getColumnString());
    assertEquals(2.23F, obj.getColumnFloat(), 0F);
    assertEquals(2.234D, obj.getColumnDouble(), 0D);
    assertEquals(true, obj.isColumnBoolean());
    assertArrayEquals(new byte[] { 1, 2, 3 }, obj.getColumnBinary());
    assertEquals(new Date(2000), obj.getColumnDate());
    assertEquals(new ObjectId("789ABCDEF0123456789ABCDA"), obj.getColumnObjectId());
    assertEquals(new Decimal128(-43), obj.getColumnDecimal128());
    assertEquals(UUID.fromString("027ba5ca-aa12-4afa-9219-e20cc3018590"), obj.getColumnUUID());
    assertEquals(RealmAny.valueOf("hello world"), obj.getColumnRealmAny());
    assertEquals("Dog4", obj.getColumnRealmObject().getName());
    assertEquals(2, obj.getColumnRealmList().size());
    assertEquals("Dog5", obj.getColumnRealmList().get(0).getName());
}
Also used : PrimaryKeyAsObjectId(io.realm.entities.PrimaryKeyAsObjectId) ObjectId(org.bson.types.ObjectId) Decimal128(org.bson.types.Decimal128) Date(java.util.Date) AllTypesPrimaryKey(io.realm.entities.AllTypesPrimaryKey)

Example 73 with Decimal128

use of org.bson.types.Decimal128 in project realm-java by realm.

the class RealmJsonTests method createObjectFromJson_decimal128.

@Test
public void createObjectFromJson_decimal128() throws JSONException {
    JSONObject json = new JSONObject();
    json.put("columnDecimal128", new Decimal128(BigDecimal.TEN));
    realm.beginTransaction();
    realm.createObjectFromJson(AllTypes.class, json);
    realm.commitTransaction();
    AllTypes obj = realm.where(AllTypes.class).findFirst();
    assertEquals(new Decimal128(BigDecimal.TEN), obj.getColumnDecimal128());
}
Also used : JSONObject(org.json.JSONObject) AllTypes(io.realm.entities.AllTypes) Decimal128(org.bson.types.Decimal128) Test(org.junit.Test)

Example 74 with Decimal128

use of org.bson.types.Decimal128 in project morphia by mongodb.

the class TypeExpressionsTest method testToDecimal.

@Test
public void testToDecimal() {
    checkMinServerVersion(4.0);
    assertAndCheckDocShape("{$toDecimal: true }", toDecimal(value(true)), new Decimal128(1));
}
Also used : Decimal128(org.bson.types.Decimal128) Test(org.testng.annotations.Test)

Example 75 with Decimal128

use of org.bson.types.Decimal128 in project immutables by immutables.

the class NumbersTest method larger_than_decimal128.

/**
 * Serializing big numbers which can't be stored in {@link org.bson.types.Decimal128} format.
 * They should be serialized as strings (or binary) in mongo.
 */
@Test
public void larger_than_decimal128() {
    final AdvancedRepository repo = new AdvancedRepository(context.setup());
    final ObjectId id = ObjectId.get();
    final BigInteger integer = BigInteger.valueOf(Long.MAX_VALUE).pow(4);
    try {
        new Decimal128(new BigDecimal(integer));
        fail("Should fail for " + integer);
    } catch (NumberFormatException ignore) {
    // expected
    }
    final Advanced doc = ImmutableAdvanced.builder().id(id).bigDecimal(// make number big
    new BigDecimal(integer)).bigInteger(// make it also big
    integer).atomicBoolean(new AtomicBoolean(false)).atomicInteger(new AtomicInteger(1)).atomicLong(new AtomicLong(2)).build();
    repo.insert(doc).getUnchecked();
    final Advanced doc2 = repo.findById(id).fetchFirst().getUnchecked().get();
    check(doc2.bigDecimal().unscaledValue()).is(integer);
    check(doc2.bigInteger()).is(integer);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) ObjectId(org.bson.types.ObjectId) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) Decimal128(org.bson.types.Decimal128) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Aggregations

Decimal128 (org.bson.types.Decimal128)80 ObjectId (org.bson.types.ObjectId)50 Date (java.util.Date)46 Test (org.junit.Test)46 AllTypes (io.realm.entities.AllTypes)22 BigDecimal (java.math.BigDecimal)20 UUID (java.util.UUID)17 UiThreadTest (androidx.test.annotation.UiThreadTest)12 PrimaryKeyAsString (io.realm.entities.PrimaryKeyAsString)11 AllJavaTypes (io.realm.entities.AllJavaTypes)10 DictionaryAllTypes (io.realm.entities.DictionaryAllTypes)10 Dog (io.realm.entities.Dog)9 PrimaryKeyAsObjectId (io.realm.entities.PrimaryKeyAsObjectId)8 JSONObject (org.json.JSONObject)8 MappedAllJavaTypes (io.realm.entities.MappedAllJavaTypes)7 NonLatinFieldNames (io.realm.entities.NonLatinFieldNames)6 PrimaryKeyAsLong (io.realm.entities.PrimaryKeyAsLong)6 SimpleDateFormat (java.text.SimpleDateFormat)6 Calendar (java.util.Calendar)6 DefaultValueOfField (io.realm.entities.DefaultValueOfField)5