chai      = require 'chai'
expect    = chai.expect
sinon     = require 'sinon'
sandbox   = sinon.sandbox.create()
sinonChai = require 'sinon-chai'
chai.use sinonChai


describe 'TingoDB Store Adapter', ->
  tingoDbStore = null
  options = null
  domainEvent =
    name: 'SomethingHappened'
    aggregate:
      id: 23
      name: 'Example'

  before (done) ->
    TingoDbStore = require './tingodb_store'
    tingoDbStore = new TingoDbStore

    tingoDbStore.initialize 'exampleContext', (err) ->
      done()


  beforeEach (done) ->
    tingoDbStore.db.dropDatabase ->
      done()


  afterEach ->
    sandbox.restore()


  after ->
    tingoDbStore.db.close()


  describe 'domain events', ->
    describe '#saveDomainEvent', ->
      it 'should save the given doc', (done) ->
        tingoDbStore.saveDomainEvent domainEvent, (err, domainEvents) ->
          expect(err).to.be.null
          expect(domainEvents[0]._id).to.be.ok
          done()


    describe 'find', ->
      beforeEach (done) ->
        tingoDbStore.saveDomainEvent domainEvent, ->
          done()


      describe '#findAllDomainEvents', ->
        it 'should find the previously saved domainevent', (done) ->
          tingoDbStore.findAllDomainEvents (err, domainEvents) ->
            expect(domainEvents).to.deep.equal [
              domainEvent
            ]
            done()


      describe '#findDomainEventsByName', ->
        it 'should find the previously saved domainevent', (done) ->
          tingoDbStore.findDomainEventsByName 'SomethingHappened', (err, domainEvents) ->
            expect(domainEvents).to.deep.equal [
              domainEvent
            ]
            done()


        it 'should find the previously saved domainevent', (done) ->
          tingoDbStore.findDomainEventsByName ['SomethingHappened'], (err, domainEvents) ->
            expect(domainEvents).to.deep.equal [
              domainEvent
            ]
            done()


      describe '#findDomainEventsByAggregateId', ->
        it 'should find the previously saved domainevent', (done) ->
          tingoDbStore.findDomainEventsByAggregateId 23, (err, domainEvents) ->
            expect(domainEvents).to.deep.equal [
              domainEvent
            ]
            done()


        it 'should find the previously saved domainevent', (done) ->
          tingoDbStore.findDomainEventsByAggregateId [23], (err, domainEvents) ->
            expect(domainEvents).to.deep.equal [
              domainEvent
            ]
            done()


      describe '#findDomainEventsByAggregateName', ->
        it 'should find the previously saved domainevent', (done) ->
          tingoDbStore.findDomainEventsByAggregateName 'Example', (err, domainEvents) ->
            expect(domainEvents).to.deep.equal [
              domainEvent
            ]
            done()


        it 'should find the previously saved domainevent', (done) ->
          tingoDbStore.findDomainEventsByAggregateName ['Example'], (err, domainEvents) ->
            expect(domainEvents).to.deep.equal [
              domainEvent
            ]
            done()


    describe 'projection stores', ->

      describe '#getProjectionStore', ->
        it 'should callback with the collection', (done) ->
          tingoDbStore.getProjectionStore 'exampleProjection', (err, collection) ->
            expect(collection).to.be.ok
            done()


      describe '#clearProjectionStore', ->
        beforeEach (done) ->
          tingoDbStore.getProjectionStore 'exampleProjection', (err, collection) ->
            collection.insert some: 'thing'
            done()


        it 'should callback after removing', (done) ->
          tingoDbStore.clearProjectionStore 'exampleProjection', ->
            tingoDbStore.db.collection('system.namespaces').find().toArray (err, items) ->
              expect(items.length).to.equal 0
              done()
