<commit_before>should = require 'should'
{Server, Db} = require 'mongodb'
{isEqual} = require 'lodash'
{inspect} = require 'util'
logger = (args...) -> console.log args.map((a) -> if (typeof a) is 'string' then a else inspect a, null, 4)...

MongoWatch = require '../'

describe 'Mongo Watch', ->

  before (done) ->
    client = new Db 'test', new Server('localhost', 27017), {w: 1}

    client.open (err) =>
      return done err if err

      client.collection 'users', (err, @users) =>
        done err

  afterEach (done) ->
    @watcher.stopAll() if @watcher
    delete @watcher
    @users.remove {}, done

  it 'insert should emit an event', (done) ->
    @watcher = new MongoWatch
    @watcher.watch 'test.users', (event) ->

      if event.op is 'i'
        should.exist event.o?.email
        event.o.email.should.eql '<EMAIL>'
        done()

    @users.insert {email: '<EMAIL>'}, (err, status) ->
      should.not.exist err

  it 'pretty format should work', (done) ->
    @watcher = new MongoWatch {format: 'pretty'}
    @watcher.watch 'test.users', (event) ->

      if event.operation is 'insert'
        should.exist event.data?.email
        event.data.email.should.eql '<EMAIL>'
        done()

    @users.insert {email: '<EMAIL>'}, (err, status) ->
      should.not.exist err

  it 'update multiple should emit an event', (done) ->

    # Given I have two users
    @users.insert [{email: '<EMAIL>'}, {email: '<EMAIL>'}], (err, status) =>
      should.not.exist err

      # And I'm watching the users collection
      counter = 0
      @watcher = new MongoWatch {format: 'pretty'}
      @watcher.watch 'test.users', (event) ->

        # I should get three update events
        if event.operation is 'update'
          counter++
          if counter is 3
            done()

      data =
        '$set':
          firstName: 'Trickster'
        '$pushAll':
          'stuff': [
              text: 'trick you'
            ,
              text: 'try you'
          ]
        '$unset':
          lastName: true

      data2 =
        '$pull':
          'stuff':
            timestamp: '$lte': new Date()
        '$set':
          'foo.bar.baz': 'herro'

      # When I update 2 existing documents
      @users.update {}, data, {multi: true}, (err, status) =>
        should.not.exist err

        # And I update 1 existing document
        @users.update {}, data2, (err, status) ->
          should.not.exist err

  describe 'normal format', ->

    it 'should process insert', (done) ->
      @watcher = new MongoWatch {format: 'normal'}
      @watcher.watch 'test.users', (event) ->
        should.exist event.timestamp
        should.exist event.targetId
        for op in event.oplist
          expected = {
            operation: 'set'
            path: '.'
            data: {email: '<EMAIL>'}
          }
          if isEqual op, expected
            done()

      @users.insert {email: '<EMAIL>'}, (err, status) ->
        should.not.exist err

    it 'should process simple update', (done) ->
      @users.insert {email: '<EMAIL>'}, (err, status) =>
        should.not.exist err

        @watcher = new MongoWatch {format: 'normal'}
        @watcher.watch 'test.users', (event) ->
          should.exist event.timestamp, 'expected timestamp'
          should.exist event.targetId, 'expected targetId'
          should.exist event.oplist, 'expected oplist'

          for op in event.oplist
            expected = {
              operation: 'set'
              path: '.'
              data: {firstName: 'Graham'}
            }
            if isEqual op, expected
              done()

        @users.update {email: '<EMAIL>'}, {firstName: 'Graham'}, (err, status) -><commit_msg>minor test refactor<commit_after>should = require 'should'
{Server, Db} = require 'mongodb'
{isEqual} = require 'lodash'
{inspect} = require 'util'
logger = (args...) -> console.log args.map((a) -> if (typeof a) is 'string' then a else inspect a, null, 4)...

MongoWatch = require '../'

describe 'Mongo Watch', ->

  before (done) ->
    client = new Db 'test', new Server('localhost', 27017), {w: 1}

    client.open (err) =>
      return done err if err

      client.collection 'users', (err, @users) =>
        done err

  afterEach (done) ->
    @watcher.stopAll() if @watcher
    delete @watcher
    @users.remove {}, done

  it 'insert should emit an event', (done) ->
    @watcher = new MongoWatch
    @watcher.watch 'test.users', (event) ->

      if event.op is 'i'
        should.exist event.o?.email
        event.o.email.should.eql '<EMAIL>'
        done()

    @users.insert {email: '<EMAIL>'}, (err, status) ->
      should.not.exist err

  it 'pretty format should work', (done) ->
    @watcher = new MongoWatch {format: 'pretty'}
    @watcher.watch 'test.users', (event) ->

      if event.operation is 'insert'
        should.exist event.data?.email
        event.data.email.should.eql '<EMAIL>'
        done()

    @users.insert {email: '<EMAIL>'}, (err, status) ->
      should.not.exist err

  it 'update multiple should emit an event', (done) ->

    # Given I have two users
    @users.insert [{email: '<EMAIL>'}, {email: '<EMAIL>'}], (err, status) =>
      should.not.exist err

      # And I'm watching the users collection
      counter = 0
      @watcher = new MongoWatch {format: 'pretty'}
      @watcher.watch 'test.users', (event) ->

        # I should get three update events
        if event.operation is 'update'
          counter++
          if counter is 3
            done()

      data =
        '$set':
          firstName: 'Trickster'
        '$pushAll':
          'stuff': [
              text: 'trick you'
            ,
              text: 'try you'
          ]
        '$unset':
          lastName: true

      data2 =
        '$pull':
          'stuff':
            timestamp: '$lte': new Date()
        '$set':
          'foo.bar.baz': 'herro'

      # When I update 2 existing documents
      @users.update {}, data, {multi: true}, (err, status) =>
        should.not.exist err

        # And I update 1 existing document
        @users.update {}, data2, (err, status) ->
          should.not.exist err

  describe 'normal format', ->

    it 'should process insert', (done) ->
      @watcher = new MongoWatch {format: 'normal'}
      @watcher.watch 'test.users', (event) ->
        should.exist event.timestamp
        should.exist event.targetId
        expected = {
          operation: 'set'
          path: '.'
          data: {email: '<EMAIL>'}
        }
        for op in event.oplist
          done() if isEqual op, expected

      @users.insert {email: '<EMAIL>'}, (err, status) ->
        should.not.exist err

    it 'should process simple update', (done) ->
      @users.insert {email: '<EMAIL>'}, (err, status) =>
        should.not.exist err

        @watcher = new MongoWatch {format: 'normal'}
        @watcher.watch 'test.users', (event) ->
          should.exist event.timestamp, 'expected timestamp'
          should.exist event.targetId, 'expected targetId'
          should.exist event.oplist, 'expected oplist'

          for op in event.oplist
            expected = {
              operation: 'set'
              path: '.'
              data: {firstName: 'Graham'}
            }
            if isEqual op, expected
              done()

        @users.update {email: '<EMAIL>'}, {firstName: 'Graham'}, (err, status) ->
{'fraction_non_alphanumeric': 0.14471192859074924, 'fraction_numerical': 0.0035163646199621317, 'mean_word_length': 1.9380214541120382, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1277530', 'n_tokens_mistral': 2279, 'n_tokens_neox': 2175, 'n_words': 662}