<commit_before>package pl.softwaremill.bootstrap.dao

import pl.softwaremill.bootstrap.domain.Entry
import org.bson.types.ObjectId
import org.joda.time.DateTime

class MongoEntryDAOSpec extends SpecificationWithMongo {
  var entryDAO: EntryDAO = null

  "MongoEntryDAO" should {

    step({
      println("Creating EntryDAO")
      entryDAO = new MongoEntryDAO()
      for (i <- 1 to 3)  {
        entryDAO.add(Entry(_id = new ObjectId(i.toString * 24), text = "Message " + i, authorId = new ObjectId((10-i).toString * 24),
          entered = new DateTime().minusDays(i)))
      }
    })

    "count all entries" in {
      assert(entryDAO.countItems() === 3)
    }

    "add new entry" in {
      // Given
      val numberOfEntries = entryDAO.countItems()

      // When
      entryDAO.add(Entry(_id = new ObjectId("a" * 24), text = "Message 9", authorId = new ObjectId("a" * 24),
        entered = new DateTime()))

      // Then
      assert(entryDAO.countItems() - numberOfEntries === 1)
    }

    "remove entry" in {
      // Given
      val numberOfEntries = entryDAO.countItems()

      // When
      entryDAO.remove("a" * 24)

      // Then
      assert(entryDAO.countItems() - numberOfEntries === -1)
    }

    "load entries sorted from newest" in {

      // When
      val entries: List[Entry] = entryDAO.loadAll

      // Then
      assert(entries.size === 3)
      assert(entries(0).entered.isAfter(entries(1).entered) === true)
      assert(entries(1).entered.isAfter(entries(2).entered) === true)
    }

  }
}<commit_msg>Additional tests for MongoEntryDAO, solves #9<commit_after>package pl.softwaremill.bootstrap.dao

import pl.softwaremill.bootstrap.domain.Entry
import org.bson.types.ObjectId
import org.joda.time.DateTime

class MongoEntryDAOSpec extends SpecificationWithMongo {
  var entryDAO: EntryDAO = null

  "MongoEntryDAO" should {

    step({
      println("Creating EntryDAO")
      entryDAO = new MongoEntryDAO()
      for (i <- 1 to 3)  {
        entryDAO.add(Entry(_id = new ObjectId(i.toString * 24), text = "Message " + i, authorId = new ObjectId((10-i).toString * 24),
          entered = new DateTime().minusDays(i)))
      }
    })

    "count all entries" in {
      assert(entryDAO.countItems() === 3)
    }

    "load all entries" in {
      assert(entryDAO.loadAll.size === 3)
    }

    "load single entry" in {
      val entryOpt: Option[Entry] = entryDAO.load("1" * 24)
      assert(entryOpt.isDefined === true)
    }

    "not find non existing entry" in {
      val entryOpt: Option[Entry] = entryDAO.load("19" * 12)
      assert(entryOpt.isDefined === false)
    }

    "add new entry" in {
      // Given
      val numberOfEntries = entryDAO.countItems()

      // When
      entryDAO.add(Entry(_id = new ObjectId("a" * 24), text = "Message 9", authorId = new ObjectId("a" * 24),
        entered = new DateTime()))

      // Then
      assert(entryDAO.countItems() - numberOfEntries === 1)
    }

    "remove entry" in {
      // Given
      val numberOfEntries = entryDAO.countItems()

      // When
      entryDAO.remove("a" * 24)

      // Then
      assert(entryDAO.countItems() - numberOfEntries === -1)
    }

    "update existing entry" in {
      // Given
      val entryOpt: Option[Entry] = entryDAO.load("1" * 24)
      val message: String = "Updated message"

      // When
      entryOpt.foreach( e => entryDAO.update(e._id.toString, message))

      // Then
      val updatedEntryOpt: Option[Entry] = entryDAO.load("1" * 24)
      updatedEntryOpt match {
        case Some(e) => {
          assert(e.text.equals(message) === true)
        }
        case _ => failure("Entry option should be defined")
      }
    }

    "load entries sorted from newest" in {

      // When
      val entries: List[Entry] = entryDAO.loadAll

      // Then
      assert(entries.size === 3)
      assert(entries(0).entered.isAfter(entries(1).entered) === true)
      assert(entries(1).entered.isAfter(entries(2).entered) === true)
    }

  }
}
{'fraction_non_alphanumeric': 0.1356569709127382, 'fraction_numerical': 0.015295887662988967, 'mean_word_length': 2.4447322970639034, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, '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': '3113392', 'n_tokens_mistral': 1356, 'n_tokens_neox': 1253, 'n_words': 289}