<commit_before>library sembast.store_test;

// basically same as the io runner but with extra output
import 'package:sembast/src/common_import.dart';

import 'test_common.dart';

void main() {
  defineTests(memoryDatabaseContext);
}

void defineTests(DatabaseTestContext ctx) {
  group('record', () {
    Database db;

    setUp(() async {
      db = await setupForTest(ctx, 'record.db');
    });

    tearDown(() {
      return db.close();
    });

    test('equals', () {
      var record1 = StoreRef.main().record(1);
      var record2 = StoreRef.main().record(1);
      expect(record1, record2);
      expect(record1.hashCode, record2.hashCode);
      expect(record1, isNot(StoreRef('test').record(1)));
      expect(record1, isNot(StoreRef.main().record(2)));
      expect(record1, StoreRef.main().record(1));
      expect(record1, isNot(StoreRef.main().record('test')));
      expect(
          StoreRef.main().record('test'),
          StoreRef.main()
              .record((StringBuffer()..write('te')..write('st')).toString()));
    });

    test('put/get timing', () async {
      var record = StoreRef<int, String>.main().record(1);
      var future1 = record.get(db);
      await record.put(db, 'test');
      var future2 = record.get(db);
      await record.put(db, 'test2');
      var future3 = record.get(db);
      await record.delete(db);
      var future4 = record.get(db);
      expect(await future1, isNull);
      expect(await future2, 'test');
      expect(await future3, 'test2');
      expect(await future4, isNull);
    });

    test('put/on timing', () async {
      var record = StoreRef<int, String>.main().record(1);
      var future1 = record.onSnapshot(db).first;
      await record.put(db, 'test');
      var future2 = record.onSnapshot(db).first;
      await record.put(db, 'test2');
      var future3 = record.onSnapshot(db).first;
      await record.delete(db);
      var future4 = record.onSnapshot(db).first;
      expect(await future1, isNull);
      expect((await future2).value, 'test');
      expect((await future3).value, 'test2');
      expect(await future4, isNull);
    });

    test('add', () async {
      var record = StoreRef<int, String>.main().record(1);
      expect(await record.add(db, 'test'), 1);
      expect(await record.add(db, 'test'), isNull);
      await record.delete(db);
      expect(await record.add(db, 'test'), 1);
      expect(await record.add(db, 'test'), isNull);
    });

    test('update', () async {
      var record = StoreRef<int, String>.main().record(1);
      expect(await record.update(db, 'test'), 1);
      expect(await record.add(db, 'test'), isNull);
      await record.delete(db);
      expect(await record.add(db, 'test'), 1);
      expect(await record.add(db, 'test'), isNull);
    });

    test('get closed', () async {
      await db.close();
      var record = StoreRef<int, String>.main().record(1);
      try {
        expect(await record.get(db), isNull);
        fail('should fail');
      } on DatabaseException catch (e) {
        expect(e.code, DatabaseException.errDatabaseClosed);
      }
    });
    test('onSnapshot', () async {
      var store = StoreRef<int, String>.main();
      var record = store.record(1);
      int index = 0;
      var completer = Completer();

      // When starting listening the record does not exists yet
      var sub = record.onSnapshot(db).listen((snapshot) {
        //return;
        int key = snapshot?.key;
        String value = snapshot?.value;
<commit_msg>fix: unit test on Record.update<commit_after>library sembast.store_test;

// basically same as the io runner but with extra output
import 'package:sembast/src/common_import.dart';

import 'test_common.dart';

void main() {
  defineTests(memoryDatabaseContext);
}

void defineTests(DatabaseTestContext ctx) {
  group('record', () {
    Database db;

    setUp(() async {
      db = await setupForTest(ctx, 'record.db');
    });

    tearDown(() {
      return db.close();
    });

    test('equals', () {
      var record1 = StoreRef.main().record(1);
      var record2 = StoreRef.main().record(1);
      expect(record1, record2);
      expect(record1.hashCode, record2.hashCode);
      expect(record1, isNot(StoreRef('test').record(1)));
      expect(record1, isNot(StoreRef.main().record(2)));
      expect(record1, StoreRef.main().record(1));
      expect(record1, isNot(StoreRef.main().record('test')));
      expect(
          StoreRef.main().record('test'),
          StoreRef.main()
              .record((StringBuffer()..write('te')..write('st')).toString()));
    });

    test('put/get timing', () async {
      var record = StoreRef<int, String>.main().record(1);
      var future1 = record.get(db);
      await record.put(db, 'test');
      var future2 = record.get(db);
      await record.put(db, 'test2');
      var future3 = record.get(db);
      await record.delete(db);
      var future4 = record.get(db);
      expect(await future1, isNull);
      expect(await future2, 'test');
      expect(await future3, 'test2');
      expect(await future4, isNull);
    });

    test('put/on timing', () async {
      var record = StoreRef<int, String>.main().record(1);
      var future1 = record.onSnapshot(db).first;
      await record.put(db, 'test');
      var future2 = record.onSnapshot(db).first;
      await record.put(db, 'test2');
      var future3 = record.onSnapshot(db).first;
      await record.delete(db);
      var future4 = record.onSnapshot(db).first;
      expect(await future1, isNull);
      expect((await future2).value, 'test');
      expect((await future3).value, 'test2');
      expect(await future4, isNull);
    });

    test('add', () async {
      var record = StoreRef<int, String>.main().record(1);
      expect(await record.add(db, 'test'), 1);
      expect(await record.add(db, 'test'), isNull);
      await record.delete(db);
      expect(await record.add(db, 'test'), 1);
      expect(await record.add(db, 'test'), isNull);
    });

    test('update', () async {
      var record = StoreRef<int, String>.main().record(1);
      expect(await record.update(db, 'test1'), isNull);
      expect(await record.add(db, 'test'), 1);
      expect(await record.update(db, 'test1'), 'test1');
    });

    test('get closed', () async {
      await db.close();
      var record = StoreRef<int, String>.main().record(1);
      try {
        expect(await record.get(db), isNull);
        fail('should fail');
      } on DatabaseException catch (e) {
        expect(e.code, DatabaseException.errDatabaseClosed);
      }
    });
    test('onSnapshot', () async {
      var store = StoreRef<int, String>.main();
      var record = store.record(1);
      int index = 0;
      var completer = Completer();

      // When starting listening the record does not exists yet
      var sub = record.onSnapshot(db).listen((snapshot) {
        //return;
        int key = snapshot?.key;
        String value = snapshot?.value;

{'fraction_non_alphanumeric': 0.1660420870567887, 'fraction_numerical': 0.013548573075814356, 'mean_word_length': 2.8722098214285716, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '791314', 'n_tokens_mistral': 2151, 'n_tokens_neox': 2177, 'n_words': 474}