<commit_before>#!/usr/bin/env python
#
#-----------------------------------------------------------------------
# A test suite for the table interface built on bsddb.db
#-----------------------------------------------------------------------
#
# Copyright (C) 2000, 2001 by Autonomous Zone Industries
# Copyright (C) 2002 <NAME>
#
# March 20, 2000
#
# License:      This is free software.  You may use this software for any
#               purpose including modification/redistribution, so long as
#               this header remains intact and that you do not claim any
#               rights of ownership or authorship of this software.  This
#               software has been tested, but no warranty is expressed or
#               implied.
#
#   --  <NAME> <<EMAIL>>
#
# $Id$

import sys, os, re
import pickle
import tempfile

import unittest
from bsddb.test.test_all import verbose

try:
    # For Pythons w/distutils pybsddb
    from bsddb3 import db, dbtables
except ImportError:
    # For Python 2.3
    from bsddb import db, dbtables

try:
    from bsddb3 import test_support
except ImportError:
    from test import support as test_support


#----------------------------------------------------------------------

class TableDBTestCase(unittest.TestCase):
    db_name = 'test-table.db'

    def setUp(self):
        homeDir = tempfile.mkdtemp()
        self.testHomeDir = homeDir
        try: os.mkdir(homeDir)
        except os.error: pass

        self.tdb = dbtables.bsdTableDB(
            filename='tabletest.db', dbhome=homeDir, create=1)

    def tearDown(self):
        self.tdb.close()
        test_support.rmtree(self.testHomeDir)

    def test01(self):
        tabname = "test01"
        colname = 'cool numbers'
        try:
            self.tdb.Drop(tabname)
        except dbtables.TableDBError:
            pass
        self.tdb.CreateTable(tabname, [colname])
        try:
            self.tdb.Insert(tabname, {colname: pickle.dumps(3.14159, 1)})
        except Exception:
            import traceback
            traceback.print_exc()

        if verbose:
            self.tdb._db_print()

        values = self.tdb.Select(
            tabname, [colname], conditions={colname: None})
        values = list(values)

        colval = pickle.loads(values[0][colname])
        self.assertTrue(colval > 3.141 and colval < 3.142)


    def test02(self):
        tabname = "test02"
        col0 = 'coolness factor'
        col1 = 'but can it fly?'
        col2 = 'Species'
        testinfo = [
            {col0: pickle.dumps(8, 1), col1: b'no', col2: b'Penguin'},
            {col0: pickle.dumps(-1, 1), col1: b'no', col2: b'Turkey'},
            {col0: pickle.dumps(9, 1), col1: b'yes', col2: b'SR-71A Blackbird'}
        ]

        try:
            self.tdb.Drop(tabname)
        except dbtables.TableDBError:
            pass
        self.tdb.CreateTable(tabname, [col0, col1, col2])
        for row in testinfo :
            self.tdb.Insert(tabname, row)

        values = self.tdb.Select(tabname, [col2],
            conditions={col0: lambda x: pickle.loads(x) >= 8})
        values = list(values)

        self.assertEquals(len(values), 2)
        if values[0]['Species'] == b'Penguin' :
            self.assertEquals(values[1]['Species'], b'SR-71A Blackbird')
        elif values[0]['Species'] == b'SR-71A Blackbird' :
            self.assertEquals(values[1]['Species'], b'Penguin')
        else :
            if verbose:
                print("values= %r" % (values,))
            self.fail("Wrong values returned!")

    def test03(self):
        tabname = "test03"
        try:
            self.tdb.Drop(tabname)
        except dbtables.TableDBError:
            pass
        if verbose:
            print('...before CreateTable...')
            self.tdb._db_print()
        self.tdb.CreateTable(tabname, ['a', 'b', 'c', 'd', 'e'])
        if verbose:
            print('...after CreateTable...')
            self.tdb._db_print()
        self.tdb.Drop(tabname)
        if verbose:
            print('...after Drop...')
            self.tdb._db_print()
        self.tdb.CreateTable(tabname, ['a', 'b', 'c', 'd', 'e'])

        try:
            self.tdb.Insert(tabname,
                            {'a': "",
                             'e': pickle.dumps([{4:5, 6:7}, 'foo'], 1),
                             'f': "Zero"})
            self.fail("exception not raised")
        except dbtables.TableDBError:
            pass

        try:
            self.tdb.Select(tabname, [], conditions={'foo': '123'})
            self.fail("exception not raised")
        except dbtables.TableDBError:
            pass

        self.tdb.Insert(tabname,
                        {'a': b'42',
                         'b': b'bad',
                         'c': b'meep',
                         'e': b'Fuzzy wuzzy was a bear'})
        self.tdb.Insert(tabname,
                        {'a': b'581750',
                         'b': b'good',
                         'd': b'bla',
                         'c': b'black',
                         'e': b'fuzzy was here'})
        self.tdb.Insert(tabname,
                        {'a': b'800000',
                         'b': b'good',
                         'd': b'bla',
                         'c': b'black',
                         'e': b'Fuzzy wuzzy is a bear'})

        if verbose:
            self.tdb._db_print()

        # this should return two rows
        values = self.tdb.Select(tabname, ['b', 'a', 'd'],
            conditions={'e': re.compile('wuzzy').search,
                        'a': re.compile('^[0-9]+$').match})
        self.assertEquals(len(values), 2)

        # now lets delete one of them and try again
        self.tdb.Delete(tabname, conditions={'b': dbtables.ExactCond('good')})
        values = self.tdb.Select(
            tabname, ['a', 'd', 'b'],
            conditions={'e': dbtables.PrefixCond('Fuzzy')})
        values = list(values)
        self.assertEquals(len(values), 1)
        self.assertEquals(values[0]['d'], None)

        values = self.tdb.Select(tabname, ['b'],<commit_msg>Fix a failure in bsddb tests, following the changes in regular expression semantics<commit_after>#!/usr/bin/env python
#
#-----------------------------------------------------------------------
# A test suite for the table interface built on bsddb.db
#-----------------------------------------------------------------------
#
# Copyright (C) 2000, 2001 by Autonomous Zone Industries
# Copyright (C) 2002 <NAME>
#
# March 20, 2000
#
# License:      This is free software.  You may use this software for any
#               purpose including modification/redistribution, so long as
#               this header remains intact and that you do not claim any
#               rights of ownership or authorship of this software.  This
#               software has been tested, but no warranty is expressed or
#               implied.
#
#   --  <NAME> <<EMAIL>>
#
# $Id$

import sys, os, re
import pickle
import tempfile

import unittest
from bsddb.test.test_all import verbose

try:
    # For Pythons w/distutils pybsddb
    from bsddb3 import db, dbtables
except ImportError:
    # For Python 2.3
    from bsddb import db, dbtables

try:
    from bsddb3 import test_support
except ImportError:
    from test import support as test_support


#----------------------------------------------------------------------

class TableDBTestCase(unittest.TestCase):
    db_name = 'test-table.db'

    def setUp(self):
        homeDir = tempfile.mkdtemp()
        self.testHomeDir = homeDir
        try: os.mkdir(homeDir)
        except os.error: pass

        self.tdb = dbtables.bsdTableDB(
            filename='tabletest.db', dbhome=homeDir, create=1)

    def tearDown(self):
        self.tdb.close()
        test_support.rmtree(self.testHomeDir)

    def test01(self):
        tabname = "test01"
        colname = 'cool numbers'
        try:
            self.tdb.Drop(tabname)
        except dbtables.TableDBError:
            pass
        self.tdb.CreateTable(tabname, [colname])
        try:
            self.tdb.Insert(tabname, {colname: pickle.dumps(3.14159, 1)})
        except Exception:
            import traceback
            traceback.print_exc()

        if verbose:
            self.tdb._db_print()

        values = self.tdb.Select(
            tabname, [colname], conditions={colname: None})
        values = list(values)

        colval = pickle.loads(values[0][colname])
        self.assertTrue(colval > 3.141 and colval < 3.142)


    def test02(self):
        tabname = "test02"
        col0 = 'coolness factor'
        col1 = 'but can it fly?'
        col2 = 'Species'
        testinfo = [
            {col0: pickle.dumps(8, 1), col1: b'no', col2: b'Penguin'},
            {col0: pickle.dumps(-1, 1), col1: b'no', col2: b'Turkey'},
            {col0: pickle.dumps(9, 1), col1: b'yes', col2: b'SR-71A Blackbird'}
        ]

        try:
            self.tdb.Drop(tabname)
        except dbtables.TableDBError:
            pass
        self.tdb.CreateTable(tabname, [col0, col1, col2])
        for row in testinfo :
            self.tdb.Insert(tabname, row)

        values = self.tdb.Select(tabname, [col2],
            conditions={col0: lambda x: pickle.loads(x) >= 8})
        values = list(values)

        self.assertEquals(len(values), 2)
        if values[0]['Species'] == b'Penguin' :
            self.assertEquals(values[1]['Species'], b'SR-71A Blackbird')
        elif values[0]['Species'] == b'SR-71A Blackbird' :
            self.assertEquals(values[1]['Species'], b'Penguin')
        else :
            if verbose:
                print("values= %r" % (values,))
            self.fail("Wrong values returned!")

    def test03(self):
        tabname = "test03"
        try:
            self.tdb.Drop(tabname)
        except dbtables.TableDBError:
            pass
        if verbose:
            print('...before CreateTable...')
            self.tdb._db_print()
        self.tdb.CreateTable(tabname, ['a', 'b', 'c', 'd', 'e'])
        if verbose:
            print('...after CreateTable...')
            self.tdb._db_print()
        self.tdb.Drop(tabname)
        if verbose:
            print('...after Drop...')
            self.tdb._db_print()
        self.tdb.CreateTable(tabname, ['a', 'b', 'c', 'd', 'e'])

        try:
            self.tdb.Insert(tabname,
                            {'a': "",
                             'e': pickle.dumps([{4:5, 6:7}, 'foo'], 1),
                             'f': "Zero"})
            self.fail("exception not raised")
        except dbtables.TableDBError:
            pass

        try:
            self.tdb.Select(tabname, [], conditions={'foo': '123'})
            self.fail("exception not raised")
        except dbtables.TableDBError:
            pass

        self.tdb.Insert(tabname,
                        {'a': b'42',
                         'b': b'bad',
                         'c': b'meep',
                         'e': b'Fuzzy wuzzy was a bear'})
        self.tdb.Insert(tabname,
                        {'a': b'581750',
                         'b': b'good',
                         'd': b'bla',
                         'c': b'black',
                         'e': b'fuzzy was here'})
        self.tdb.Insert(tabname,
                        {'a': b'800000',
                         'b': b'good',
                         'd': b'bla',
                         'c': b'black',
                         'e': b'Fuzzy wuzzy is a bear'})

        if verbose:
            self.tdb._db_print()

        # this should return two rows
        values = self.tdb.Select(tabname, ['b', 'a', 'd'],
            conditions={'e': re.compile(b'wuzzy').search,
                        'a': re.compile(b'^[0-9]+$').match})
        self.assertEquals(len(values), 2)

        # now lets delete one of them and try again
        self.tdb.Delete(tabname, conditions={'b': dbtables.ExactCond('good')})
        values = self.tdb.Select(
            tabname, ['a', 'd', 'b'],
            conditions={'e': dbtables.PrefixCond('Fuzzy')})
        values = list(values)
        self.assertEquals(len(values), 1)
        self.assertEquals(values[0]['d'], None)

        values = self.tdb.Select(tabname, ['b'],
{'fraction_non_alphanumeric': 0.16255715218811234, 'fraction_numerical': 0.01845199216198563, 'mean_word_length': 1.5407591785936527, 'pattern_counts': {'":': 0, '<': 13, '<?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': '903226', 'n_tokens_mistral': 3847, 'n_tokens_neox': 3611, 'n_words': 869}