var schema = require('./schema'),
  db = require('./db'),
  expect = require('chai').expect,
  sinon = require('sinon'),
  bluebird = require('bluebird');

describe('schema', function () {
  var sandbox;

  beforeEach(function () {
    sandbox = sinon.sandbox.create();
  });

  afterEach(function () {
    sandbox.restore();
  });

  it('read basic schema', function () {
    var textSchema = schema.getSchema('test/fixtures/text');

    expect(textSchema).to.deep.equal({
      name: {
        _type: 'text',
        _required: true
      },
      areas: {
        body: {
          _type: 'component-list'
        }
      }
    });
  });

  it('getSchemaComponents gets all components', function () {
    var result = schema.getSchemaComponents('test/fixtures/text');

    expect(result).to.deep.equal([
      {
        _type:'text',
        _required: true
      },
      {
        _type:'component-list'
      }
    ]);
  });

  it('listDeepObjects gets all deep objects', function () {
    var result = schema.listDeepObjects({a:{b:{c:{d:"e"}}, f:{g:{h:"e"}}}});

    expect(result).to.have.length(5);
  });

  it('listDeepObjects can filter by existence of properties', function () {
    var result = schema.listDeepObjects({a:{b:{c:{d:"e"}}, f:{d:{g:"e"}}}}, "d");

    expect(result).to.have.length(2);
  });

  it('listDeepObjects can filter by component', function () {
    var result = schema.listDeepObjects({a: {_type:'yarn'}, b: {c: {_type:'sweater'}}}, schema.isComponent);

    expect(result).to.deep.equal([
      {_type:'yarn'},
      {_type:'sweater'}
    ]);
  });

  it('resolveDataReferences looks up references', function (done) {
    var mock,
      data = {
        a: {_ref:'b'},
        c: {d: {_ref:'e'}}
      };

    mock = sandbox.mock(db);
    mock.expects('get').withArgs('b').once().returns(bluebird.resolve({g: 'h'}));
    mock.expects('get').withArgs('e').once().returns(bluebird.resolve({i: 'j'}));

    schema.resolveDataReferences(data).then(function (result) {
      sandbox.verify();
      expect(result).to.deep.equal({
        a: { _ref: 'b', g: 'h' },
        c: { d: { _ref: 'e', i: 'j' } }
      });
      done()
    });
  });
});
@@ -1974,12 +1974,711 @@
 ta).
-then
+done(function (result) %7B%0A      sandbox.verify();%0A      expect(result).to.deep.equal(%7B%0A        a: %7B _ref: 'b', g: 'h' %7D,%0A        c: %7B d: %7B _ref: 'e', i: 'j' %7D %7D%0A      %7D);%0A      done()%0A    %7D);%0A  %7D);%0A%0A  it('resolveDataReferences looks up references recursively', function (done) %7B%0A    var mock,%0A      data = %7B%0A        a: %7B_ref:'b'%7D,%0A        c: %7Bd: %7B_ref:'e'%7D%7D%0A      %7D;%0A%0A    mock = sandbox.mock(db);%0A    mock.expects('get').withArgs('b').once().returns(bluebird.resolve(%7Bg: 'h'%7D));%0A    mock.expects('get').withArgs('e').once().returns(bluebird.resolve(%7Bi: 'j', 'k': %7B_ref:'m'%7D%7D));%0A    mock.expects('get').withArgs('m').once().returns(bluebird.resolve(%7Bn: 'o'%7D));%0A%0A    schema.resolveDataReferences(data).done
 (fun
@@ -2798,32 +2798,42 @@
        c: %7B d: %7B
+%0A         
  _ref: 'e', i: '
@@ -2819,39 +2819,185 @@
       _ref: 'e',
- i: 'j'
+%0A          i: 'j' ,%0A          k: %7B%0A            _ref: 'm',%0A            n: 'o' //we just recursively looked this up from another lookup%0A          %7D%0A       
  %7D %7D%0A      %7D);%0A 

add a test for recursive lookup