def testWriteUnicodeCharacters(self):
        del self.table[:]
        for i in xrange(100):
            self.table.add(FieldSpec("col_{}".format(i), 'NVARCHAR', 8))

        fts = FastTableStream(self.table, use_names=False)
        with fts:
            for j in xrange(10000):
                fts.write([100 * j + i for i in xrange(100)])
        self.LOGGER.info("Table name {}".format(self.table))
 def testValidateFailNewEmptyTable(self):
     drop_table_if_exists(self.table)
     del self.table[:]
     table_stream = FastTableStream(self.table)
     try:
         table_stream.validate_write_inputs()
     except ValueError:
         # Expected error
         return
     self.fail(
         "Expected a ValueError if called on to create a new table with no fields"
     )
 def testValidateFailColumnTooShort(self):
     drop_table_if_exists(self.table)
     self.db_context.executeNoResults(self.table.definition)
     self.table['col1'].data_length = 100
     table_stream = FastTableStream(self.table)
     try:
         table_stream.validate_write_inputs()
     except ValueError:
         # Expected error
         return
     self.fail(
         "Expected a ValueError if db column is shorter than TableSpec column"
     )
 def testValidateFailWrongColumnType(self):
     drop_table_if_exists(self.table)
     self.db_context.executeNoResults(self.table.definition)
     self.table['col1'].basic_type = 'VARCHAR'
     table_stream = FastTableStream(self.table)
     try:
         table_stream.validate_write_inputs()
     except ValueError:
         # Expected error
         return
     self.fail(
         "Expected a ValueError if db column type is different from TableSpec column type"
     )
 def testValidateFailWrongColumnName(self):
     drop_table_if_exists(self.table)
     self.db_context.executeNoResults(self.table.definition)
     col1 = self.table.pop('col1')
     col1.field_name = 'col_new'
     self.table.add(col1)
     table_stream = FastTableStream(self.table)
     try:
         table_stream.validate_write_inputs()
     except ValueError:
         # Expected error
         return
     self.fail("Expected a ValueError if column names do not match")
 def testValidateFailWrongColumnCount(self):
     drop_table_if_exists(self.table)
     self.db_context.executeNoResults(self.table.definition)
     self.table.add(FieldSpec('col7', 'NVARCHAR', 15))
     table_stream = FastTableStream(self.table)
     try:
         table_stream.validate_write_inputs()
     except ValueError:
         # Expected error
         return
     self.fail(
         "Expected a ValueError if TableSpec has different column count from db"
     )
    def testWriteUnicodeNoNull(self):
        del self.table[:]
        for i in xrange(100):
            self.table.add(
                FieldSpec("col_{}".format(i), 'NVARCHAR', 8, nullable=False))

        fts = FastTableStream(self.table, use_names=False, raw=True)
        data = [[unicode(100 * j + i) for i in xrange(100)]
                for j in xrange(1000)]

        def do_write():
            with fts:
                for j in xrange(20):
                    fts.write_many(data)

        pr = cProfile.Profile()
        pr.enable()
        pr.runcall(do_write)
        pr.disable()

        filename = os.path.join(self.run_context.logs_dir,
                                'fast_table_write_unicode_nonull_profile.txt')
        with open(filename, 'w') as f:
            stats = pstats.Stats(pr, stream=f)
            stats.print_stats()
        self.LOGGER.info("Table name {}".format(self.table))
    def test1(self):
        run_context = SuiteContext('unittest')
        db_context = run_context.getDBContext('remote')
        self.LOGGER = run_context.get_logger()

        # Set up a test table definition
        with get_temp_table(db_context) as table:
            for i in xrange(100):
                table.add(FieldSpec("col_{}".format(i), 'NVARCHAR', 8))

            fts = FastTableStream(table, use_names=False, raw=True)
            data = [[unicode(100 * j + i) for i in xrange(100)]
                    for j in xrange(1000)]

            def do_write():
                with fts:
                    for j in xrange(5):
                        fts.write_many(data)

            pr = cProfile.Profile()
            pr.enable()
            pr.runcall(do_write)
            pr.disable()

            filename = os.path.join(
                run_context.logs_dir,
                'fast_table_write_remote_unicode_raw_profile.txt')
            with open(filename, 'w') as f:
                stats = pstats.Stats(pr, stream=f)
                stats.print_stats()
            self.LOGGER.info("Table name {}".format(table))
    def testWriteMixed(self):
        del self.table[:]
        for i in xrange(33):
            self.table.add(FieldSpec("float_{}".format(i), 'FLOAT'))
            self.table.add(FieldSpec("int_{}".format(i), 'INT'))
            self.table.add(FieldSpec("str_{}".format(i), 'VARCHAR', 6))

        fts = FastTableStream(self.table, use_names=False)
        data = []
        for j in xrange(1000):
            row = []
            for i in xrange(33):
                k = 100 * j + i
                row.extend((int(k), float(k), str(k)))

        def do_write():
            with fts:
                for j in xrange(20):
                    fts.write_many(data)

        pr = cProfile.Profile()
        pr.enable()
        pr.runcall(do_write)
        pr.disable()

        filename = os.path.join(self.run_context.logs_dir,
                                'fast_table_write_mixed_profile.txt')
        with open(filename, 'w') as f:
            stats = pstats.Stats(pr, stream=f)
            stats.print_stats()
        self.LOGGER.info("Table name {}".format(self.table))
    def testWriteFloatMany(self):
        del self.table[:]
        for i in xrange(100):
            self.table.add(FieldSpec("col_{}".format(i), 'FLOAT'))

        fts = FastTableStream(self.table,
                              use_names=False,
                              dumpfile="C:\\Scratch\\float.dat")
        data = [[100.0 * j + i for i in xrange(100)] for j in xrange(1000)]

        def do_write():
            with fts:
                for j in xrange(20):
                    fts.write_many(data)

        pr = cProfile.Profile()
        pr.enable()
        pr.runcall(do_write)
        pr.disable()

        filename = os.path.join(self.run_context.logs_dir,
                                'fast_table_write_float_nonull_profile.txt')
        with open(filename, 'w') as f:
            stats = pstats.Stats(pr, stream=f)
            stats.print_stats()
        self.LOGGER.info("Table name {}".format(self.table))
    def testWriteIntegerMany(self):
        del self.table[:]
        for i in xrange(100):
            self.table.add(FieldSpec("col_{}".format(i), 'INT'))

        fts = FastTableStream(self.table, use_names=False)
        data = [[100 * j + i for i in xrange(100)] for j in xrange(1000)]

        def do_write():
            with fts:
                for j in xrange(20):
                    fts.write_many(data)

        pr = cProfile.Profile()
        pr.enable()
        pr.runcall(do_write)
        pr.disable()

        filename = os.path.join(
            self.run_context.logs_dir,
            'fast_table_write_int_with_checks_profile.txt')
        with open(filename, 'w') as f:
            stats = pstats.Stats(pr, stream=f)
            stats.print_stats()
        self.LOGGER.info("Table name {}".format(self.table))
 def testValidateColumnLonger(self):
     drop_table_if_exists(self.table)
     self.db_context.executeNoResults(self.table.definition)
     self.table['col1'].data_length = 1
     table_stream = FastTableStream(self.table)
     table_stream.validate_write_inputs()
 def testValidateExistingTableAgainstEmptySpec(self):
     drop_table_if_exists(self.table)
     self.db_context.executeNoResults(self.table.definition)
     del self.table[:]
     table_stream = FastTableStream(self.table)
     table_stream.validate_write_inputs()
 def testValidateNewTable(self):
     drop_table_if_exists(self.table)
     table_stream = FastTableStream(self.table)
     table_stream.validate_write_inputs()