'use strict';

/**
 * super-orm tests
 *
 * @author Zongmin Lei <leizongmin@gmail.com>
 */

const expect = require('chai').expect;
const { createSchema } = require('../');


describe('Schema', function () {

  const query = createSchema({
    fields: {
      name: true,
      info: { type: 'json' },
      data: {
        encode(v) {
          return Buffer.from(v).toString('base64');
        },
        decode(v) {
          return Buffer.from(v, 'base64').toString();
        },
      },
    },
  });

  it('serialize && unserialize', function () {
    const data = query.serialize({
      name: 'ABC',
      info: {
        url: 'http://baidu.com/',
        email: 'me@ucdok.com',
      },
      data: 'Hello, world',
    });
    console.log(data);
    expect(data).to.equal('{"name":"ABC","info":{"url":"http://baidu.com/","email":"me@ucdok.com"},"data":"Hello, world"}');

    const ret = query.unserialize(data);
    console.log(ret);
    expect(ret).to.deep.equal({
      name: 'ABC',
      info: {
        url: 'http://baidu.com/',
        email: 'me@ucdok.com',
      },
      data: 'Hello, world',
    });
  });

  it('formatInput & formatInputList', function () {
    expect(query.formatInput({
      name: 'ABC',
      OK: true,
    })).to.deep.equal({
      name: 'ABC',
    });
    expect(query.formatInput({
      name: 'ABC',
      OK: true,
      info: 'aaa',
    })).to.deep.equal({
      name: 'ABC',
      info: '"aaa"',
    });
    expect(query.formatInput({
      name: 'ABC',
      info: 'aaa',
      data: 'hahaha',
    })).to.deep.equal({
      name: 'ABC',
      info: '"aaa"',
      data: 'aGFoYWhh',
    });
    expect(query.formatInputList([{
      name: 'ABC',
      info: 'aaa',
      data: 'hahaha',
    }, {
      name: 'DDD',
      info: 'aaa',
      data: 'hahaha',
    }])).to.deep.equal([{
      name: 'ABC',
      info: '"aaa"',
      data: 'aGFoYWhh',
    }, {
      name: 'DDD',
      info: '"aaa"',
      data: 'aGFoYWhh',
    }]);
  });

  it('formatOutput & formatOutputList', function () {
    expect(query.formatOutput({
      name: 'hello',
      data: 'aGFoYWhh',
    })).to.deep.equal({
      name: 'hello',
      data: 'hahaha',
    });
    expect(query.formatOutput({
      name: 'hello',
      data: 'aGFoYWhh',
      message: 'yes',
    })).to.deep.equal({
      name: 'hello',
      data: 'hahaha',
      message: 'yes',
    });
    expect(query.formatOutput({
      name: 'hello',
      data: 'aGFoYWhh',
      message: 'yes',
      info: '"aaa"',
    })).to.deep.equal({
      name: 'hello',
      data: 'hahaha',
      message: 'yes',
      info: 'aaa',
    });
    expect(query.formatOutputList([{
      name: 'hello',
      data: 'aGFoYWhh',
      message: 'yes',
    }, {
      name: 'hello',
      data: 'aGFoYWhh',
      message: 'yes',
      info: '"aaa"',
    }])).to.deep.equal([{
      name: 'hello',
      data: 'hahaha',
      message: 'yes',
    }, {
      name: 'hello',
      data: 'hahaha',
      message: 'yes',
      info: 'aaa',
    }]);
  });

});

@@ -3003,23 +3003,827 @@
 ',%0A    %7D%5D);%0A
+    // %E7%95%B8%E5%BD%A2json%E5%AD%97%E6%AE%B5%0A    expect(query.formatOutput(%7B%0A      info: undefined,%0A    %7D)).to.deep.equal(%7B%0A      info: undefined,%0A    %7D);%0A    expect(query.formatOutput(%7B%0A      info: null,%0A    %7D)).to.deep.equal(%7B%0A      info: %7B%7D,%0A    %7D);%0A    expect(query.formatOutput(%7B%0A      info: '',%0A    %7D)).to.deep.equal(%7B%0A      info: %7B%7D,%0A    %7D);%0A    expect(function () %7B%0A      query.formatOutput(%7B%0A        info: 123,%0A      %7D);%0A    %7D).to.throw('jsonDecoder: invalid input type: 123');%0A    expect(function () %7B%0A      query.formatOutput(%7B%0A        info: '%7B%22a%22:',%0A      %7D);%0A    %7D).to.throw('jsonDecoder: fail to parse JSON');%0A  %7D);%0A%0A  it('not support type', function () %7B%0A    expect(function () %7B%0A      createSchema(%7B%0A        fields: %7B%0A          info: %7B type: 'xxxx' %7D,%0A        %7D,%0A      %7D);%0A    %7D).to.throw('not support type %22xxxx%22');%0A
   %7D);%0A%0A%7D);%0A

test schema