package org.bricolages.streaming.filter;

import org.junit.Test;
import static org.junit.Assert.*;
import lombok.*;

public class FloatOpTest {
    OpBuilder builder = new OpBuilder();

    @Test
    public void apply() throws Exception {
        val def = new OperatorDefinition("float", "schema.table", "b", "{}");
        val op = (FloatOp) builder.build(def);
        {
            val rec = Record.parse("{\"a\":1,\"b\":1.5,\"c\":3}");
            val out = op.apply(rec);
            assertEquals("{\"a\":1,\"b\":1.5,\"c\":3}", out.serialize());
        }
        {
            val rec = Record.parse("{\"a\":1,\"b\":\"1.5\",\"c\":3}");
            val out = op.apply(rec);
            assertEquals("{\"a\":1,\"b\":1.5,\"c\":3}", out.serialize());
        }
        {
            val rec = Record.parse("{\"a\":1,\"b\":\"1.234e2\",\"c\":3}");
            val out = op.apply(rec);
            assertEquals("{\"a\":1,\"b\":123.4,\"c\":3}", out.serialize());
        }
    }

    @Test
    public void apply_too_large_value() throws Exception {
        val f = new FloatOp(null);
        assertEquals(null, f.applyValue(Double.MAX_VALUE, null));
    }

    @Test
    public void apply_too_small_value() throws Exception {
        val f = new FloatOp(null);
        assertEquals(null, f.applyValue(Double.MIN_VALUE, null));
    }

    @Test(expected = FilterException.class)
    public void apply_invalid() throws Exception {
        val f = new FloatOp(null);
        f.applyValue("junk value", null);
    }

    @Test(expected = FilterException.class)
    public void apply_unsupported() throws Exception {
        val f = new FloatOp(null);
        f.applyValue(new Object(), null);
    }
}

@@ -299,17 +299,17 @@
 able%22, %22
-b
+n
 %22, %22%7B%7D%22)
@@ -406,39 +406,23 @@
 rse(%22%7B%5C%22
-a%5C%22:1,%5C%22b%5C%22:1.5,%5C%22c%5C%22:3
+n%5C%22:1.5
 %7D%22);%0A   
@@ -484,39 +484,23 @@
 als(%22%7B%5C%22
-a%5C%22:1,%5C%22b%5C%22:1.5,%5C%22c%5C%22:3
+n%5C%22:1.5
 %7D%22, out.
@@ -568,33 +568,25 @@
 d.parse(%22%7B%5C%22
-a%5C%22:1,%5C%22b
+n
 %5C%22:%5C%221.5%5C%22,%5C
@@ -583,24 +583,16 @@
 :%5C%221.5%5C%22
-,%5C%22c%5C%22:3
 %7D%22);%0A   
@@ -658,31 +658,15 @@
 %22%7B%5C%22
-a%5C%22:1,%5C%22b%5C%22:1.5,%5C%22c%5C%22:3
+n%5C%22:1.5
 %7D%22, 
@@ -742,25 +742,17 @@
 rse(%22%7B%5C%22
-a%5C%22:1,%5C%22b
+n
 %5C%22:%5C%221.2
@@ -757,24 +757,16 @@
 .234e2%5C%22
-,%5C%22c%5C%22:3
 %7D%22);%0A   
@@ -832,33 +832,189 @@
 %22%7B%5C%22
-a
+n
 %5C%22:1
-,%5C%22b
+23.4%7D%22, out.serialize());%0A        %7D%0A        %7B%0A            val rec = Record.parse(%22%7B%5C%22n
 %5C%22:1
+.
 23
-.4,%5C%22c%5C%22:3
+4e2%7D%22);%0A            val out = op.apply(rec);%0A            assertEquals(%22%7B%5C%22n%5C%22:123.4
 %7D%22, 

Tweak FloatOp test