htsql / test / regress.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#
# Copyright (c) 2006-2013, Prometheus Research, LLC
#


from pbbt import test, test_type, test_field, TestCaseMixin, RunAndCompareMixin
from pbbt.check import choiceof, maybe, oneof, listof, tupleof, dictof
from pbbt.std import is_filename, to_identifier
import re
import StringIO
import traceback
import tempfile
import shutil
import atexit
import subprocess
import os
import time


class TermStringIO(StringIO.StringIO):
    """
    A readable file-like object with an "echo".  Whenever some content is read
    from it, the same content is echoed to the specified `output` stream.

    Use :class:`TermStringIO` to preserve the content of interactive
    sessions with pre-recorded input.  Assign::

        sys.stdout = StringIO.StringIO()
        sys.stdin = TermStringIO(input, sys.stdout)

    where `input` contains the pre-recorded input data.  After the
    session is done, the content of `sys.stdout` will be the same as
    if the session was performed on a real terminal with echo enabled.

    `buf` (a string)
        The content of the stream.

    `output` (a writable file-like object)
        A stream that records data being read.
    """

    def __init__(self, buf, output):
        StringIO.StringIO.__init__(self, buf)
        self.output = output

    def read(self, n=-1):
        data = StringIO.StringIO.read(self, n)
        self.output.write(data)
        return data

    def readline(self, length=None):
        data = StringIO.StringIO.readline(self, length)
        self.output.write(data)
        return data


@test_type
class SQLCase(TestCaseMixin):

    class Input:
        connect = test_field(maybe(oneof(str, dictof(str, object))))
        sql = test_field(str)
        autocommit = test_field(bool, default=False)
        ignore = test_field(bool, default=False)

        @property
        def sql_key(self):
            if is_filename(self.sql):
                return self.sql
            else:
                return to_identifier(self.sql)

        @property
        def sql_as_filename(self):
            if is_filename(self.sql):
                return self.sql

        @property
        def sql_as_source(self):
            if not is_filename(self.sql):
                return self.sql

        def __str__(self):
            return "SQL: %s" % self.sql_key

    def check(self):
        filename = self.input.sql_as_filename
        if filename is not None:
            stream = open(self.input.sql)
            source = stream.read()
            stream.close()
        else:
            source = self.input.sql
        from htsql import HTSQL
        from htsql.core.error import Error
        from htsql.core.connect import connect
        from htsql.core.split_sql import split_sql
        try:
            app = HTSQL(self.input.connect)
        except Exception:
            self.ui.literal(traceback.format_exc())
            self.ctl.failed("exception occured while"
                            " initializing an HTSQL application")
            return

        with app:
            try:
                statements = list(split_sql(source))
            except ValueError, exc:
                self.ctl.failed("cannot parse SQL: %s" % exc)
                return

            try:
                connection = connect(with_autocommit=self.input.autocommit)
                cursor = connection.cursor()
            except Error, exc:
                self.ui.literal(str(exc))
                self.ctl.failed("failed to connect to the database")
                return

            for statement in statements:
                try:
                    cursor.execute(statement)
                except Error, exc:
                    self.ui.literal(statement)
                    self.ui.literal(str(exc))
                    if not self.input.ignore:
                        self.ctl.failed("failed to execute SQL")
                        return
                    break

            else:
                if not self.input.autocommit:
                    try:
                        connection.commit()
                    except Error, exc:
                        self.ui.literal(str(exc))
                        if not self.input.ignore:
                            self.ctl.failed("failed to commit the transaction")
                            return

            try:
                connection.close()
            except Error, exc:
                self.ui.literal(str(exc))
                self.ctl.failed("failed to close the connection")
                return

        return self.ctl.passed()


class SavedDB(object):

    def __init__(self, db, *extensions):
        self.db = db
        self.extensions = extensions


@test_type
class DBCase(TestCaseMixin):

    class Input:
        db = test_field(maybe(oneof(str, dictof(str, object))))
        extensions = test_field(dictof(str, dictof(str, object)), default={})
        save = test_field(str, default=None)

        def __str__(self):
            if not self.db:
                return "DB: -"
            try:
                from htsql.core.util import DB
            except ImportError:
                return "DB: -"
            try:
                db = DB.parse(self.db)
            except ValueError:
                return "DB: -"
            db = db.clone(password=None)
            return "DB: %s" % db

    def check(self):
        from htsql import HTSQL
        self.ctl.state['htsql'] = None
        try:
            self.ctl.state['htsql'] = HTSQL(self.input.db,
                                            self.input.extensions)
        except Exception:
            self.ui.literal(traceback.format_exc())
            self.ctl.failed("exception occured while"
                            " initializing the HTSQL application")
            return

        # Record the configuration.
        if self.input.save is not None:
            self.ctl.state[self.input.save] = SavedDB(self.input.db,
                                                      self.input.extensions)

        self.ctl.passed()
        return


@test_type
class DBLoadCase(TestCaseMixin):

    class Input:
        load = test_field(str)
        extensions = test_field(dictof(str, dictof(str, object)), default={})
        save = test_field(str, default=None)

    def check(self):
        if not isinstance(self.ctl.state.get(self.input.load), SavedDB):
            self.ctl.failed("unknown configuration %r" % self.input.load)
            return
        configuration = self.ctl.state[self.input.load]
        db = configuration.db
        extensions = configuration.extensions+(self.input.extensions,)
        from htsql import HTSQL
        self.ctl.state['htsql'] = None
        try:
            self.ctl.state['htsql'] = HTSQL(db, *extensions)
        except Exception:
            self.ui.literal(traceback.format_exc())
            self.ctl.failed("exception occured while"
                            " initializing the HTSQL application")
            return

        # Record the configuration.
        if self.input.save is not None:
            self.ctl.state[self.input.save] = SavedDB(db, *extensions)

        self.ctl.passed()


@test_type
class QueryCase(RunAndCompareMixin):

    class Input:
        uri = test_field(str)
        method = test_field(choiceof(['GET', 'POST']), default='GET')
        remote_user = test_field(str, default=None)
        headers = test_field(dictof(str, str), default=None)
        content_type = test_field(str, default=None)
        content_body = test_field(str, default=None)
        expect = test_field(int, default=200)

    class Output:
        uri = test_field(str)
        status = test_field(str)
        headers = test_field(listof(tupleof(str, str)))
        body = test_field(str)

        @classmethod
        def __load__(cls, mapping):
            if 'headers' in mapping and \
                    isinstance(mapping['headers'], listof(listof(str, length=2))):
                mapping['headers'] = [tuple(header)
                                      for header in mapping['headers']]
            return super(QueryCase.Output, cls).__load__(mapping)

        def __dump__(self):
            mapping = {}
            mapping['uri'] = self.uri
            mapping['status'] = self.status
            mapping['headers'] = [list(header) for header in self.headers]
            mapping['body'] = self.body
            return mapping

    def run(self):
        app = self.ctl.state.get('htsql')
        if app is None:
            self.ui.warning("HTSQL application is not defined")
            return

        from htsql.ctl.request import Request

        request = Request.prepare(method=self.input.method,
                                  query=self.input.uri,
                                  remote_user=self.input.remote_user,
                                  content_type=self.input.content_type,
                                  content_body=self.input.content_body,
                                  extra_headers=self.input.headers)
        response = request.execute(app)

        if response.exc_info is not None:
            lines = traceback.format_exception(*response.exc_info)
            self.ui.literal("".join(lines))
            self.ui.warning("exception occured while executing the query")
            return
        if not response.complete():
            self.ui.warning("incomplete response")
            return

        new_output = self.Output(uri=self.input.uri,
                                 status=response.status,
                                 headers=response.headers,
                                 body=response.body)

        if not response.status.startswith(str(self.input.expect)):
            text = self.render(self.output)
            new_text = self.render(new_output)
            self.display(text, new_text)
            self.ui.warning("unexpected status code: %s" % response.status)
            return

        return new_output

    def render(self, output):
        if output is None:
            return None
        lines = []
        lines.append(output.status)
        for header, value in output.headers:
            lines.append("%s: %s" % (header, value))
        lines.append("")
        lines.extend(output.body.splitlines())
        return "\n".join(lines)+"\n"


@test_type
class CtlCase(RunAndCompareMixin):

    class Input:
        ctl = test_field(listof(str))
        stdin = test_field(str, default='')
        expect = test_field(int, default=0)

        def __str__(self):
            return "CTL: %s" % " ".join(self.ctl)

    class Output:
        ctl = test_field(listof(str))
        stdout = test_field(str)

    def run(self):
        stdout = StringIO.StringIO()
        stderr = stdout
        stdin = TermStringIO(self.input.stdin, stdout)
        command_line = ['htsql-ctl']+self.input.ctl

        # The script class.
        from htsql.ctl import HTSQL_CTL

        # Initialize and execute the script; check for exceptions.
        try:
            ctl = HTSQL_CTL(stdin, stdout, stderr)
            exit = ctl.main(command_line)
        except:
            self.ui.literal(stdout.getvalue())
            self.ui.literal(traceback.format_exc())
            self.ui.warning("exception occured"
                            " while running the application")
            return

        # Normalize the exit code.
        if exit is None:
            exit = 0
        elif not isinstance(exit, int):
            stderr.write(str(exit))
            exit = 1

        if exit != self.input.expect and self.input.ignore is not True:
            self.ui.literal(stdout.getvalue())
            self.ui.warning("unexpected exit code: %s" % exit)
            return

        new_output = self.Output(ctl=self.input.ctl,
                                 stdout=stdout.getvalue())
        return new_output

    def render(self, output):
        return output.stdout


class Fork(object):

    active_forks = []
    active_fork_map = {}
    is_atexit_registered = False

    @classmethod
    def push(cls, key, fork):
        cls.active_fork_map[key] = fork

    @classmethod
    def pop(cls, key):
        return cls.active_fork_map.pop(key)

    @classmethod
    def start(cls, executable, arguments, stdin):
        # Create a temporary directory with the files 'input' and 'output'.
        temp_path = tempfile.mkdtemp()
        stream = open("%s/input" % temp_path, 'wb')
        stream.write(stdin)
        stream.close()
        # Prepare the standard input and the standard output streams.
        stdin = open("%s/input" % temp_path, 'rb')
        stdout = open("%s/output" % temp_path, 'wb')
        # Start the process.
        try:
            process = subprocess.Popen([executable]+arguments,
                                       stdin=stdin,
                                       stdout=stdout,
                                       stderr=subprocess.STDOUT)
        except:
            shutil.rmtree(temp_path)
            raise
        # Return a new `Fork` instance.
        return cls(process, temp_path)

    @classmethod
    def atexit(cls):
        # Finalize any remaining active processes.
        for fork in cls.active_forks:
            fork.end()

    @classmethod
    def atexit_register(cls):
        # Register the `atexit` callable if not done already.
        if not cls.is_atexit_registered:
            atexit.register(cls.atexit)
            cls.is_atexit_registered = True

    def __init__(self, process, temp_path):
        # Sanity check on the arguments.
        assert isinstance(process, subprocess.Popen)
        assert isinstance(temp_path, str) and os.path.isdir(temp_path)

        self.process = process
        self.temp_path = temp_path

        # Save themselves in the global list of active processes.
        self.active_forks.append(self)
        # Register the `atexit` callback.
        self.atexit_register()

    def end(self):
        # Terminate the process if it is still alive.
        if self.process.poll() is None:
            self.process.terminate()
            time.sleep(1.0)
        # Read the standard output.
        stream = open("%s/output" % self.temp_path, 'rb')
        output = stream.read()
        stream.close()
        # Remove the temporary directory.
        shutil.rmtree(self.temp_path)
        # Remove it from the list of active processes.
        self.active_forks.remove(self)
        return output


@test_type
class StartCtlCase(TestCaseMixin):

    class Input:
        start_ctl = test_field(listof(str))
        stdin = test_field(str, default='')
        sleep = test_field(oneof(int, float), default=0)

        def __str__(self):
            return "START-CTL: %s" % " ".join(self.start_ctl)

    def check(self):
        key = tuple(self.input.start_ctl)
        ctl_path = self.state.get('HTSQL_CTL', 'htsql-ctl')
        fork = Fork.start(ctl_path,
                          self.input.start_ctl,
                          self.input.stdin)
        Fork.push(key, fork)


@test_type
class EndCtlCase(RunAndCompareMixin):

    class Input:
        end_ctl = test_field(listof(str))

        def __str__(self):
            return "END-CTL: %s" % " ".join(self.end_ctl)

    class Output:
        end_ctl = test_field(listof(str))
        stdout = test_field(str)

    def run(self):
        key = tuple(self.input.end_ctl)
        fork = Fork.pop(key)
        stdout = fork.end()

        new_output = self.Output(end_ctl=self.input.end_ctl,
                                 stdout=stdout)
        return new_output

    def render(self, output):
        return output.stdout
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.