<commit_before>// Copyright (c) 2015, <your name>. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.

library mongo_fixtures.test;

import 'package:unittest/unittest.dart';
import 'package:mongo_fixtures/mongo_fixtures.dart';

main() {
  group('A group of tests', () {
    Awesome awesome;

    setUp(() {
      awesome = new Awesome();
    });

    test('First Test', () {
      expect(awesome.isAwesome, isTrue);
    });
  });
}<commit_msg>write tests<commit_after>library mongo_fixtures.test;

import 'dart:async';
import 'package:unittest/unittest.dart';
import 'package:mongo_fixtures/mongo_fixtures.dart';
import 'package:mongo_dart/mongo_dart.dart';

List<MongoFixtureEntity> getFixtures() {
  return [

      new MongoFixtureCollection('some_collection')
        ..insert({
          'field_one': 'value1-1',
          'field_two': 'value1-2',
      })
        ..insert({
          'field_one': 'value2-1',
          'field_two': 'value2-2',
      }),

      new MongoFixtureCollection('some_another_collection')
        ..insert({
          'another_field_one': 'value3-1',
          'another_field_two': 'value3-2',
      })

  ];
}

main() {
  group('inserting fixtures to database', () {

    String connectionString = 'mongodb://127.0.0.1/mongo_fixtures_test';

    Db db = new Db(connectionString);

    setUp(() => db.open()
      .then((_) => db.listCollections())
      .then((List<String> collections) =>
        Future.forEach(collections, (String name) =>
          db.dropCollection(name))
      )
    );
    tearDown(() => db.close());

    test('clean all & insert', () {

      MongoFixtureLoader loader = new MongoFixtureLoader(connectionString);

      /// inserting
      return loader.insert([
          new MongoFixtureCollection('colection_to_delete')
            ..insert({'bla_bla': 'bla_bla'})
      ])
      /// checking collection
      .then((_) => db.listCollections().then((List<String> list) {
        expect(list, hasLength(1));
        expect(list, contains('colection_to_delete'));
      }))

      /// clean & insert
      .then((_) => loader.cleanAllAndInsert(getFixtures()))

      /// checking existing collections
      .then((_) => db.listCollections().then((List<String> list) {
        expect(list, hasLength(2));
        expect(list, contains('some_collection'));
        expect(list, contains('some_another_collection'));
      }))

      /// checking collection 'some_collection'
      .then((_) => db.collection('some_collection').find().toList().then((List<Map> list) {
        expect(list, hasLength(2));
        expect(list[0], containsPair('field_one', 'value1-1'));
        expect(list[0], containsPair('field_two', 'value1-2'));
        expect(list[1], containsPair('field_one', 'value2-1'));
        expect(list[1], containsPair('field_two', 'value2-2'));
      }))

      /// checking collection 'some_another_collection'
      .then((_) => db.collection('some_another_collection').find().toList().then((List<Map> list) {
        expect(list, hasLength(1));
        expect(list[0], containsPair('another_field_one', 'value3-1'));
        expect(list[0], containsPair('another_field_two', 'value3-2'));
      }));

    });

    test('insert and then clean', () {

      MongoFixtureLoader loader = new MongoFixtureLoader(connectionString);

      /// inserting
      return loader.insert(getFixtures())

      /// checking existing collections
      .then((_) => db.listCollections().then((List<String> list) {
        expect(list, hasLength(2));
        expect(list, contains('some_collection'));
        expect(list, contains('some_another_collection'));
      }))

      /// checking collection 'some_collection'
      .then((_) => db.collection('some_collection').find().toList().then((List<Map> list) {
        expect(list, hasLength(2));
        expect(list[0], containsPair('field_one', 'value1-1'));
        expect(list[0], containsPair('field_two', 'value1-2'));
        expect(list[1], containsPair('field_one', 'value2-1'));
        expect(list[1], containsPair('field_two', 'value2-2'));
      }))

      /// checking collection 'some_another_collection'
      .then((_) => db.collection('some_another_collection').find().toList().then((List<Map> list) {
        expect(list, hasLength(1));
        expect(list[0], containsPair('another_field_one', 'value3-1'));
        expect(list[0], containsPair('another_field_two', 'value3-2'));
      }))

      .then((_) => loader.cleanAll())

      /// checking existing collections
      .then((_) => db.listCollections().then((List<String> list) {
        expect(list, hasLength(0));
      }));

    });

  });

}
{'fraction_non_alphanumeric': 0.17333049766056996, 'fraction_numerical': 0.014036580178647383, 'mean_word_length': 3.1038394415357766, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, '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': '5130631', 'n_tokens_mistral': 1471, 'n_tokens_neox': 1407, 'n_words': 246}