Assert = require "assert"
TypeUtil = require "../lib/typeUtil"

describe "typeUtil", () ->
  describe "packObjectOrArray", () ->
    it "should return a dynamo parse object", () ->
      input =
        a: "plop"
        b: 32
        c: ["tata", "toto", "titi"]
        d: [1, 2, 3]
        e: true

      output = TypeUtil.packObjectOrArray input

      expected =
        "a":
          "S": "plop"
        "b":
          "N": "32"
        "c.0":
          "S": "tata"
        "c.1":
          "S": "toto"
        "c.2":
          "S": "titi"
        "d.0":
          "N": "1"
        "d.1":
          "N": "2"
        "d.2":
          "N": "3"
        "e":
          "B": new Buffer("true").toString "base64"

      Assert.deepEqual output, expected

  describe "unpackObjectOrArray", () ->
    it "should return a plain object", () ->
      input =
        "a":
          "S": "plop"
        "b":
          "N": "32"
        "c.0":
          "S": "tata"
        "c.1":
          "S": "toto"
        "c.2":
          "S": "titi"
        "d.0":
          "N": "1"
        "d.1":
          "N": "2"
        "d.2":
          "N": "3"
        "e":
          "B": new Buffer("true").toString "base64"

      output = TypeUtil.unpackObjectOrArray input

      expected =
        a: "plop"
        b: 32
        c: ["tata", "toto", "titi"]
        d: [1, 2, 3]
        e: true

      Assert.deepEqual output, expected

  describe "formatForUpdate", () ->
    it "should return a dynamo object", () ->
      input =
        a: ""
        b: "plop"

      output = TypeUtil.formatForUpdate input

      expected =
        a:
          "Action": "DELETE"
        b:
          "Action": "PUT"
          "Value":
            "S": "plop"

      Assert.deepEqual output, expected

  describe "objectToQuery", () ->
    it "should return a dynamo object", () ->
      input =
        a: 1
        b: "plop"
        c: ["test", "toto"]

      output = TypeUtil.objectToQuery input

      expected =
        a:
          ComparisonOperator: "EQ"
          AttributeValueList: [
            N: "1"
          ]
        b:
          ComparisonOperator: "EQ"
          AttributeValueList: [
            S: "plop"
          ]
        "c.0":
          ComparisonOperator: "EQ"
          AttributeValueList: [
            S: "test"
          ]
        "c.1":
          ComparisonOperator: "EQ"
          AttributeValueList: [
            S: "toto"
          ]

      Assert.deepEqual output, expected

  describe "sortAndClean", () ->
    it "should return a sorted and clean array", () ->
      input = [
        {
          a: "plop"
          _createTimeStamp: 10
        },
        {
          a: "test"
          _createTimeStamp: 5
        },{
          a: "toto"
          _createTimeStamp: 20
        }
      ]
      
      expected = [
        {
          a: "test"
        },
        {
          a: "plop"
        },{
          a: "toto"
        }
      ]
      
      output = TypeUtil.sortAndClean input
      Assert.deepEqual output, expected

  it "should retur a clean object", () ->
    input =
      a: "plop"
      _createTimeStamp: 10

    expected =
      a: "plop"

    output = TypeUtil.sortAndClean input
    Assert.deepEqual output, expected