Source

fantomongo / test / BsonTest.fan

Full commit
////////////////////////////////////////////////////////////////////////////////
//
//  Copyright 2010 Liam Staskawicz
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//
////////////////////////////////////////////////////////////////////////////////


**
**  BsonTest
**
class BsonTest : Test 
{
  Buf b := Buf() { endian = Endian.little }
  Map map := Str:Obj?[:] { ordered = true }
  
  Void cleanSlate()
  {
    map.clear
    b.clear
  }
  
  Void testBasics()
  {
    cleanSlate()
    map["testStr"] = "number1"
    map["testInt"] = 123
    map["testFloat"] = 12.3f
    map["testBool"] = true
    map["testDateTime"] = DateTime.boot()
    // map["testNull"] = null
    map["testOID"] = ObjectID()
    Bson.write(b.out, map)
    m := Bson.read(b.flip().in)
    verify(m == map, "${m} != ${map}")
  }
  
  **
  **  testBinary
  **  test this separately since Buf.equals actually means ===
  **
  Void testBinary()
  {
    cleanSlate
    map["testBuf"] = Buf.random(56)
    Bson.write(b.out, map)
    m := Bson.read(b.flip().in)
    buf := map["testBuf"] as Buf
    rebuf := m["testBuf"] as Buf
    verify(rebuf.size == buf.size && rebuf.pos == buf.pos)
    while(rebuf.more) {
      if(rebuf.read() != buf.read())
        fail()
    }
  }
  
  Void testOID()
  {
    oid := ObjectID()
    verify(ObjectID.isValid(oid.toStr))
    
    // round trip via Str
    verify(oid == ObjectID.fromStr(oid.toStr))
    
    // round trip via Buf
    b := Buf()
    oid.write(b.out)
    k := ObjectID.fromStream(b.flip.in)
    verify(oid == k, "${oid.toStr} != ${k.toStr}")

    // test Str representation
    oidStr := "001122334455667788990011"
    oid = ObjectID(oidStr)
    verifyEq(oidStr, oid.toStr)

  }
  
  Void testObject()
  {
    cleanSlate
    e := Str:Obj?[:] { ordered = true }
    e["test1"] = 1
    e["test2"] = 2
    ee := Str:Obj?[:] { ordered = true }
    ee["embeddedtest"] = "embedded"
    e["double"] = ee
    map["testMap"] = e
    Bson.write(b.out, map)
    m := Bson.read(b.flip().in)
    verify(m == map, "${m} != ${map}")
  }
  
  Void testArray()
  {
    cleanSlate
    list := [,]
    list.add(1).add(2).add(3).add(4)
    map["testList"] = list
    Bson.write(b.out, map)
    m := Bson.read(b.flip.in)
    verify(m == map, "${m} != ${map}")
  }
  
  Void testMixed()
  {
    cleanSlate
    list := [,]
    list.add(1).add(2).add(3).add(4)
    map["testList"] = list
    e := Str:Obj?[:] { ordered = true }
    e["test1"] = 1
    e["test2"] = 2
    ee := Str:Obj?[:] { ordered = true }
    ee["embeddedtest"] = "embedded"
    e["double"] = ee
    map["testMap"] = e
    Bson.write(b.out, map)
    m := Bson.read(b.flip.in)
    verifyEq(m, map)
  }
  
  Void testCodeWScope()
  {
    cleanSlate
    c1 := Code("function(test, obj) {obj = 0;}", ["test":1])
    map["testCode"] = c1
    Bson.write(b.out, map)
    m := Bson.read(b.flip.in)
    Code c2 := m["testCode"]
    verifyEq(c1.code, c2.code)
    verifyEq(c1.scope["test"], c2.scope["test"])
  }
}