<commit_before>require 'coffee-script'
chai = require 'chai'
should = require "should"
Q = require 'q'
Parse = require('parse').Parse
DataStorage = require './controller'
expect = chai.expect

testConfigs =
  parse:
    "applicationId": "hZ9REGxSlyuHw8FQrhWuDtNndHJYYN3ZtncLEpup",
    "javascriptKey": "<KEY>"
    "masterKey": "<KEY>"
  className:
    logs: 'Logs'
    users: 'Users'

Parse.initialize(
  testConfigs.parse.applicationId
  testConfigs.parse.javascriptKey
)

controller = null

describe 'DataStorage', ->

  it 'should have a error when not correctly initialized', ->
    controller = new DataStorage {}
    controller.error.should.equal true

    controller = new DataStorage {parse:{}}
    controller.error.should.equal true

    controller = new DataStorage {parse:applicationId:'foo'}
    controller.error.should.equal true

    controller = new DataStorage {parse:javascriptKey:'foo'}
    controller.error.should.equal true

    controller = new DataStorage
      parse:
        applicationId:'foo'
        javascriptKey:'bar'
    controller.error.should.equal true

    controller = new DataStorage
      className:
        logs: 'Logs'
    controller.error.should.equal true

    controller = new DataStorage
      className:
        users: 'Users'
    controller.error.should.equal true

    controller = new DataStorage
      className:
        logs: 'Logs'
        users: 'Users'
    controller.error.should.equal true

  describe 'should', ->

    beforeEach ->
      controller = new DataStorage testConfigs

    it 'exist', ->
      controller.should.exists

    it 'be initialized correctly', ->
      controller.error.should.equal false
      controller.className.logs.should.equal 'Logs'
      controller.className.users.should.equal 'Users'

    describe 'have', ->

      afterEach (done) ->
        @timeout 10000
        Q.all(
          controller.deleteAll('Test')
          controller.deleteAll('Logs')
          controller.deleteAll('Users')
        ).then done()

      describe 'a generic method', ->

        describe '`insert` which', ->

          it 'exists', ->
            controller.insert.should.exists

          it 'works correctly', (done) ->
            @timeout 10000
            controller.insert('Test', {action: 'insert'})
            .then (result) ->
              Inserted = Parse.Object.extend 'Test'
              query = new Parse.Query Inserted
              query.get result.id,
                success: (result) ->
                  result.get('action').should.equal 'insert'
                  done()

        describe '`find` which', ->

          it 'exists', ->
            controller.find.should.exists

          it 'works correctly', (done) ->
            @timeout 10000

            controller.insert('Test', {action:'find'})
            .then (result) ->
              controller.find 'Test', [ {key:'action',value:'find'} ]
            .then (results) ->
              results[0].get('action').should.equal 'find'
              done()

          it 'works fine when no search results', (done) ->
            @timeout 10000

            controller.find('Test', [ {key:'action',value:'not-existing'} ])
            .then (results) ->
              expect(results).to.deep.equal []
              done()

        describe '`findById` which', ->

          it 'exists', ->
            controller.findById.should.exists

          it 'works fine', (done) ->
            @timeout 10000

            controller.insert 'Test', {action:'findById'}
            .then (result) ->
              controller.findById 'Test', result.id
            .then (result) ->
              result.get('action').should.equal 'findById'
              done()

          it 'works fine when no search results', (done) ->
            @timeout 10000

            controller.findById 'Test', 'not-existing'
            .then (result) ->
              expect(result).to.equal null
              done()

        describe '`findLatest` which', ->

          it 'exists', ->
            controller.findLatest.should.exists

          it 'works fine', (done) ->
            @timeout 10000

            Q.all(
              controller.insert('Test', action:'findLatest-2'),
              controller.insert('Test', action:'findLatest-2'),
              controller.insert('Test', action:'findLatest-3')
            ).then (result) ->
              controller.findLatest('Test') # the latest
            .then (latest) -><commit_msg>data storage controller update<commit_after>require 'coffee-script'
chai = require 'chai'
should = require "should"
Q = require 'q'
Parse = require('parse').Parse
DataStorage = require './controller'
expect = chai.expect

testConfigs =
  parse:
    "applicationId": "hZ9REGxSlyuHw8FQrhWuDtNndHJYYN3ZtncLEpup",
    "javascriptKey": "<KEY>"
    "masterKey": "<KEY>"
  className:
    logs: 'Logs'
    users: 'Users'

Parse.initialize(
  testConfigs.parse.applicationId
  testConfigs.parse.javascriptKey
)

controller = null

describe 'DataStorage', ->

  it 'should have a error when not correctly initialized', ->
    controller = new DataStorage {}
    controller.error.should.equal true

    controller = new DataStorage {parse:{}}
    controller.error.should.equal true

    controller = new DataStorage {parse:applicationId:'foo'}
    controller.error.should.equal true

    controller = new DataStorage {parse:javascriptKey:'foo'}
    controller.error.should.equal true

    controller = new DataStorage
      parse:
        applicationId:'foo'
        javascriptKey:'bar'
    controller.error.should.equal true

    controller = new DataStorage
      className:
        logs: 'Logs'
    controller.error.should.equal true

    controller = new DataStorage
      className:
        users: 'Users'
    controller.error.should.equal true

    controller = new DataStorage
      className:
        logs: 'Logs'
        users: 'Users'
    controller.error.should.equal true

  describe 'should', ->

    beforeEach (done) ->
      @timeout 10000
      controller = new DataStorage testConfigs
      setTimeout done, 500

    it 'exist', ->
      controller.should.exists

    it 'be initialized correctly', ->
      controller.error.should.equal false
      controller.className.logs.should.equal 'Logs'
      controller.className.users.should.equal 'Users'

    describe 'have', ->

      afterEach (done) ->
        @timeout 10000
        Q.all(
          controller.deleteAll('Test')
          controller.deleteAll('Logs')
          controller.deleteAll('Users')
        ).then(setTimeout done, 1000)

      describe 'a generic method', ->

        describe '`insert` which', ->

          it 'exists', ->
            controller.insert.should.exists

          it 'works correctly', (done) ->
            @timeout 10000
            controller.insert('Test', {action: 'insert'})
            .then (result) ->
              Inserted = Parse.Object.extend 'Test'
              query = new Parse.Query Inserted
              query.get result.id,
                success: (result) ->
                  result.get('action').should.equal 'insert'
                  done()

        describe '`find` which', ->

          it 'exists', ->
            controller.find.should.exists

          it 'works fine', (done) ->
            @timeout 10000

            controller.insert('Test', {action:'find'})
            .then (result) ->
              controller.find 'Test', [ {key:'action',value:'find'} ]
            .then (results) ->
              results[0].get('action').should.equal 'find'
              done()

          it 'works fine when no search results', (done) ->
            @timeout 10000

            controller.find('Test', [ {key:'action',value:'not-existing'} ])
            .then (results) ->
              expect(results).to.deep.equal []
              done()

        describe '`findById` which', ->

          it 'exists', ->
            controller.findById.should.exists

          it 'works fine', (done) ->
            @timeout 10000

            controller.insert 'Test', {action:'findById'}
            .then (result) ->
              controller.findById 'Test', result.id
            .then (result) ->
              result.get('action').should.equal 'findById'
              done()

          it 'works fine when no search results', (done) ->
            @timeout 10000

            controller.findById 'Test', 'not-existing'
            .then (result) ->
              expect(result).to.equal null
              done()

        describe '`findLatest` which', ->

          it 'exists', ->
            controller.findLatest.should.exists

          it 'works fine', (done) ->
            @timeout 10000

            Q.all(
              controller.insert('Test', action:'findLatest-2'),
              controller.insert('Test', action:'findLatest-2'),
              controller.insert('Test', action:'findLatest-3')
            ).then (result) ->
              controller.findLatest('Test') # the latest
            .then (latest) ->
              latest.should.have.length 3
{'fraction_non_alphanumeric': 0.12321960914210003, 'fraction_numerical': 0.01070994810643701, 'mean_word_length': 1.9351911860012962, 'pattern_counts': {'":': 6, '<': 7, '<?xml version=': 0, '>': 73, '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': '6943327', 'n_tokens_mistral': 2522, 'n_tokens_neox': 2355, 'n_words': 572}