예제 #1
0
    def test_load_lazy_rollback(self):
        Table = tablebase()

        class Tb(Table):
            name = Column()

        grn = Groonga()
        Table.bind(grn)
        self._sendquery('table_create --name Tb --flags TABLE_HASH_KEY '
                        '--key_type ShortText')
        self._sendquery('column_create --table Tb --name name --flags '
                        'COLUMN_SCALAR --type ShortText')

        data1 = [Tb(_key='key1', name='name1'),
                 Tb(_key='key2', name='name2'),
                 Tb(_key='key3', name='name3')]
        result = Tb.load(data1, immediate=False)
        result.rollback()
        with self.assertRaises(RuntimeError):
            result.commit()
        stored = json.loads(self._sendquery('select --table Tb'))
        expected = [[[0],
                     [['_id', 'UInt32'], ['_key', 'ShortText'],
                      ['name', 'ShortText']]]]
        self.assertListEqual(stored[1], expected)
예제 #2
0
    def test_bind(self):
        Table = tablebase()
        self.assertRaises(TypeError, Table.bind, 'dummy')

        grn = Groonga()
        Table.bind(grn)
        self.assertIs(Table.grn, grn)
예제 #3
0
    def test_table(self):
        Table = tablebase()
        namecol = Column(flags=COLUMN_SCALAR, type=ShortText)
        passwordcol = Column(flags=COLUMN_SCALAR, type=ShortText)

        class Tb1(Table):
            name = namecol
            password = passwordcol
            address = 'address'

        self.assertIs(Tb1.__tableflags__, TABLE_HASH_KEY)
        self.assertIs(Tb1.__key_type__, ShortText)
        self.assertEqual(Tb1.__tablename__, 'Tb1')
        self.assertEqual(str(Tb1), 'table_create --name Tb1 --flags ' \
                'TABLE_HASH_KEY --key_type ShortText')
        self.assertListEqual(Tb1.columns, [namecol, passwordcol])
        self.assertListEqual(Table._tables, [Tb1])

        sitecol = Column(flags=COLUMN_SCALAR, type=ShortText)
        addresscol = Column(flags=COLUMN_SCALAR, type=ShortText)

        class Tb2(Table):
            __tableflags__ = TABLE_PAT_KEY
            __key_type__ = UInt32
            site = sitecol
            address = addresscol

        self.assertIs(Tb2.__tableflags__, TABLE_PAT_KEY)
        self.assertIs(Tb2.__key_type__, UInt32)
        self.assertEqual(Tb2.__tablename__, 'Tb2')
        self.assertEqual(str(Tb2), 'table_create --name Tb2 --flags ' \
                'TABLE_PAT_KEY --key_type UInt32')
        self.assertListEqual(Tb2.columns, [sitecol, addresscol])
        self.assertListEqual(Table._tables, [Tb1, Tb2])
예제 #4
0
    def test_select_all(self):
        Table = tablebase()

        class Tb(Table):
            pass

        grn = Groonga()
        Table.bind(grn)
        self._sendquery('table_create --name Tb --flags TABLE_HASH_KEY '
                        '--key_type ShortText')
        self._insert('Tb', [{'_key': 'key1'}, {'_key': 'key2'}])
        result = Tb.select().all()
        expected = [{'_id': 1, '_key': 'key1'},
                    {'_id': 2, '_key': 'key2'}]
        self.assertGroongaResultEqual(result, expected, all_len=2)
예제 #5
0
    def test_load_lazy_multiple(self):
        Table = tablebase()

        class Tb(Table):
            name = Column()

        grn = Groonga()
        Table.bind(grn)
        self._sendquery('table_create --name Tb --flags TABLE_HASH_KEY '
                        '--key_type ShortText')
        self._sendquery('column_create --table Tb --name name --flags '
                        'COLUMN_SCALAR --type ShortText')

        data1 = [Tb(_key='key1', name='name1'),
                 Tb(_key='key2', name='name2'),
                 Tb(_key='key3', name='name3')]
        result = Tb.load(data1, immediate=False)
        self.assertIsInstance(result, LoadQuery)
        stored = json.loads(self._sendquery('select --table Tb'))
        expected = [[[0],
                     [['_id', 'UInt32'], ['_key', 'ShortText'],
                      ['name', 'ShortText']]]]
        self.assertListEqual(stored[1], expected)
        data2 = [Tb(_key='key4', name='Madoka Kaname'),
                 Tb(_key='key5', name='Homura Akemi'),
                 Tb(_key='key6', name='Kyoko Sakura'),
                 Tb(_key='key7', name='Sayaka Miki')]
        result.load(data2)
        stored = json.loads(self._sendquery('select --table Tb'))
        expected = [[[0],
                     [['_id', 'UInt32'], ['_key', 'ShortText'],
                      ['name', 'ShortText']]]]
        self.assertListEqual(stored[1], expected)
        result.commit()  # load actually
        stored = json.loads(self._sendquery('select --table Tb'))
        expected = [[[7],
                     [['_id', 'UInt32'], ['_key', 'ShortText'],
                      ['name', 'ShortText']],
                     [1, 'key1', 'name1'],
                     [2, 'key2', 'name2'],
                     [3, 'key3', 'name3'],
                     [4, 'key4', 'Madoka Kaname'],
                     [5, 'key5', 'Homura Akemi'],
                     [6, 'key6', 'Kyoko Sakura'],
                     [7, 'key7', 'Sayaka Miki']]]
        self.assertListEqual(stored[1], expected)
예제 #6
0
    def _maketable2(self):
        Table = tablebase()

        class Tb(Table):
            category = Column()
            name = Column()

        grn = Groonga()
        Table.bind(grn)
        self._sendquery('table_create --name Tb --flags TABLE_HASH_KEY '
                        '--key_type ShortText')
        self._sendquery('column_create --table Tb --name category --flags '
                        'COLUMN_SCALAR --type ShortText')
        self._sendquery('column_create --table Tb --name name --flags '
                        'COLUMN_SCALAR --type ShortText')
        fixture = self.loadfixture(2)
        self._insert('Tb', fixture)
        return Tb, fixture
예제 #7
0
    def test_select_with_column(self):
        Table = tablebase()

        class Tb(Table):
            name = Column()
            address = Column()

        grn = Groonga()
        Table.bind(grn)
        self._sendquery('table_create --name Tb --flags TABLE_HASH_KEY '
                        '--key_type ShortText')
        self._sendquery('column_create --table Tb --name name --flags '
                        'COLUMN_SCALAR --type ShortText')
        self._sendquery('column_create --table Tb --name address --flags '
                        'COLUMN_SCALAR --type ShortText')
        self._insert('Tb', [{'_key': 'key1', 'name': 'Name1',
                             'address': 'Address1'},
                            {'_key': 'key2', 'name': 'name2',
                             'address': 'address2'},
                            {'_key': 'key3', 'name': 'foo',
                             'address': 'bar'}])

        result = Tb.select(_key='1').all()
        expected = [{'_id': 1, '_key': 'key1', 'address': 'Address1',
                     'name': 'Name1'}]
        self.assertGroongaResultEqual(result, expected, all_len=1)

        result = Tb.select(_key='2').all()
        expected = [{'_id': 2, '_key': 'key2', 'address': 'address2',
                     'name': 'name2'}]
        self.assertGroongaResultEqual(result, expected, all_len=1)

        result = Tb.select(name='name').all()
        expected = [{'_id': 1, '_key': 'key1', 'address': 'Address1',
                     'name': 'Name1'},
                    {'_id': 2, '_key': 'key2', 'address': 'address2',
                     'name': 'name2'}]
        self.assertGroongaResultEqual(result, expected, all_len=2)

        result = Tb.select(address='ar').all()
        expected = [{'_id': 3, '_key': 'key3', 'address': 'bar',
                     'name': 'foo'}]
        self.assertGroongaResultEqual(result, expected, all_len=1)
예제 #8
0
    def test_load_immediately(self):
        Table = tablebase()

        class Tb(Table):
            name = Column()

        grn = Groonga()
        Table.bind(grn)
        self._sendquery('table_create --name Tb --flags TABLE_HASH_KEY '
                        '--key_type ShortText')
        self._sendquery('column_create --table Tb --name name --flags '
                        'COLUMN_SCALAR --type ShortText')

        data = [Tb(_key='key1', name='name1'),
                Tb(_key='key2', name='name2'),
                Tb(_key='key3', name='name3')]
        result = Tb.load(data)
        self.assertEqual(result, 3)
        stored = json.loads(self._sendquery('select --table Tb'))
        expected = [[[3],
                     [['_id', 'UInt32'], ['_key', 'ShortText'],
                      ['name', 'ShortText']],
                     [1, 'key1', 'name1'],
                     [2, 'key2', 'name2'],
                     [3, 'key3', 'name3']]]
        self.assertListEqual(stored[1], expected)

        # data override test
        data = [Tb(_key='key1', name='foo'),
                Tb(_key='key2', name='bar'),
                Tb(_key='key3', name='baz')]
        result = Tb.load(data)
        stored = json.loads(self._sendquery('select --table Tb'))
        expected = [[[3],
                     [['_id', 'UInt32'], ['_key', 'ShortText'],
                      ['name', 'ShortText']],
                     [1, 'key1', 'foo'],
                     [2, 'key2', 'bar'],
                     [3, 'key3', 'baz']]]
        self.assertListEqual(stored[1], expected)
예제 #9
0
    def test_create_all(self):
        Table = tablebase()

        class Tb1(Table):
            name = Column(flags=COLUMN_SCALAR, type=ShortText)

        class Tb2(Table):
            word = Column(flags=COLUMN_SCALAR, type=ShortText)

        grn = Groonga()
        Table.bind(grn)
        Table.create_all()
        result = json.loads(self._sendquery('table_list'))
        expected = [[['id', 'UInt32'],
                     ['name', 'ShortText'],
                     ['path', 'ShortText'],
                     ['flags', 'ShortText'],
                     ['domain', 'ShortText'],
                     ['range', 'ShortText']],
                    [256, 'Tb1', GroongaTestBase.DB_PATH + '.0000100',
                     'TABLE_HASH_KEY|PERSISTENT',
                     'ShortText',
                     'null'],
                    [258, 'Tb2', GroongaTestBase.DB_PATH + '.0000102',
                     'TABLE_HASH_KEY|PERSISTENT',
                     'ShortText',
                     'null']]
        self.assertListEqual(result[1], expected)

        result = json.loads(self._sendquery('column_list Tb1'))
        expected = [[['id', 'UInt32'],
                     ['name', 'ShortText'],
                     ['path', 'ShortText'],
                     ['type', 'ShortText'],
                     ['flags', 'ShortText'],
                     ['domain', 'ShortText'],
                     ['range', 'ShortText'],
                     ['source', 'ShortText']],
                    [256, '_key', '', '', 'COLUMN_SCALAR', 'Tb1', 'ShortText', []],
                    [257,
                     'name',
                     GroongaTestBase.DB_PATH + '.0000101',
                     'var',
                     'COLUMN_SCALAR|PERSISTENT',
                     'Tb1',
                     'ShortText',
                     []]]
        self.assertListEqual(result[1], expected)

        result = json.loads(self._sendquery('column_list Tb2'))
        expected = [[['id', 'UInt32'],
                     ['name', 'ShortText'],
                     ['path', 'ShortText'],
                     ['type', 'ShortText'],
                     ['flags', 'ShortText'],
                     ['domain', 'ShortText'],
                     ['range', 'ShortText'],
                     ['source', 'ShortText']],
                    [258, '_key', '', '', 'COLUMN_SCALAR', 'Tb2', 'ShortText', []],
                    [259,
                     'word',
                     GroongaTestBase.DB_PATH + '.0000103',
                     'var',
                     'COLUMN_SCALAR|PERSISTENT',
                     'Tb2',
                     'ShortText',
                     []]]
        self.assertListEqual(result[1], expected)
예제 #10
0
 def test_default_value(self):
     Table = tablebase()
     self.assertIs(Table.__tableflags__, TABLE_HASH_KEY)
     self.assertIs(Table.__key_type__, ShortText)
     self.assertEqual(Table.__tablename__, 'Table')