|
47 | 47 | load_birth_names_dashboard_with_slices,
|
48 | 48 | load_birth_names_data,
|
49 | 49 | )
|
| 50 | +from tests.integration_tests.test_app import app |
50 | 51 |
|
51 | 52 | from .base_tests import SupersetTestCase
|
52 | 53 |
|
@@ -475,80 +476,133 @@ def test_labels_expected_on_mutated_query(self):
|
475 | 476 | db.session.delete(database)
|
476 | 477 | db.session.commit()
|
477 | 478 |
|
478 |
| - def test_values_for_column(self): |
| 479 | + |
| 480 | +@pytest.fixture |
| 481 | +def text_column_table(): |
| 482 | + with app.app_context(): |
479 | 483 | table = SqlaTable(
|
480 |
| - table_name="test_null_in_column", |
| 484 | + table_name="text_column_table", |
481 | 485 | sql=(
|
482 | 486 | "SELECT 'foo' as foo "
|
483 | 487 | "UNION SELECT '' "
|
484 | 488 | "UNION SELECT NULL "
|
485 |
| - "UNION SELECT 'null'" |
| 489 | + "UNION SELECT 'null' " |
| 490 | + "UNION SELECT '\"text in double quotes\"' " |
| 491 | + "UNION SELECT '''text in single quotes''' " |
| 492 | + "UNION SELECT 'double quotes \" in text' " |
| 493 | + "UNION SELECT 'single quotes '' in text' " |
486 | 494 | ),
|
487 | 495 | database=get_example_database(),
|
488 | 496 | )
|
489 | 497 | TableColumn(column_name="foo", type="VARCHAR(255)", table=table)
|
490 | 498 | SqlMetric(metric_name="count", expression="count(*)", table=table)
|
| 499 | + yield table |
491 | 500 |
|
492 |
| - # null value, empty string and text should be retrieved |
493 |
| - with_null = table.values_for_column(column_name="foo", limit=10000) |
494 |
| - assert None in with_null |
495 |
| - assert len(with_null) == 4 |
496 | 501 |
|
497 |
| - # null value should be replaced |
498 |
| - result_object = table.query( |
499 |
| - { |
500 |
| - "metrics": ["count"], |
501 |
| - "filter": [{"col": "foo", "val": [NULL_STRING], "op": "IN"}], |
502 |
| - "is_timeseries": False, |
503 |
| - } |
504 |
| - ) |
505 |
| - assert result_object.df["count"][0] == 1 |
| 502 | +def test_values_for_column_on_text_column(text_column_table): |
| 503 | + # null value, empty string and text should be retrieved |
| 504 | + with_null = text_column_table.values_for_column(column_name="foo", limit=10000) |
| 505 | + assert None in with_null |
| 506 | + assert len(with_null) == 8 |
506 | 507 |
|
507 |
| - # also accept None value |
508 |
| - result_object = table.query( |
509 |
| - { |
510 |
| - "metrics": ["count"], |
511 |
| - "filter": [{"col": "foo", "val": [None], "op": "IN"}], |
512 |
| - "is_timeseries": False, |
513 |
| - } |
514 |
| - ) |
515 |
| - assert result_object.df["count"][0] == 1 |
516 | 508 |
|
517 |
| - # empty string should be replaced |
518 |
| - result_object = table.query( |
519 |
| - { |
520 |
| - "metrics": ["count"], |
521 |
| - "filter": [{"col": "foo", "val": [EMPTY_STRING], "op": "IN"}], |
522 |
| - "is_timeseries": False, |
523 |
| - } |
524 |
| - ) |
525 |
| - assert result_object.df["count"][0] == 1 |
| 509 | +def test_filter_on_text_column(text_column_table): |
| 510 | + table = text_column_table |
| 511 | + # null value should be replaced |
| 512 | + result_object = table.query( |
| 513 | + { |
| 514 | + "metrics": ["count"], |
| 515 | + "filter": [{"col": "foo", "val": [NULL_STRING], "op": "IN"}], |
| 516 | + "is_timeseries": False, |
| 517 | + } |
| 518 | + ) |
| 519 | + assert result_object.df["count"][0] == 1 |
526 | 520 |
|
527 |
| - # also accept "" string |
528 |
| - result_object = table.query( |
529 |
| - { |
530 |
| - "metrics": ["count"], |
531 |
| - "filter": [{"col": "foo", "val": [""], "op": "IN"}], |
532 |
| - "is_timeseries": False, |
533 |
| - } |
534 |
| - ) |
535 |
| - assert result_object.df["count"][0] == 1 |
| 521 | + # also accept None value |
| 522 | + result_object = table.query( |
| 523 | + { |
| 524 | + "metrics": ["count"], |
| 525 | + "filter": [{"col": "foo", "val": [None], "op": "IN"}], |
| 526 | + "is_timeseries": False, |
| 527 | + } |
| 528 | + ) |
| 529 | + assert result_object.df["count"][0] == 1 |
536 | 530 |
|
537 |
| - # both replaced |
538 |
| - result_object = table.query( |
539 |
| - { |
540 |
| - "metrics": ["count"], |
541 |
| - "filter": [ |
542 |
| - { |
543 |
| - "col": "foo", |
544 |
| - "val": [EMPTY_STRING, NULL_STRING, "null", "foo"], |
545 |
| - "op": "IN", |
546 |
| - } |
547 |
| - ], |
548 |
| - "is_timeseries": False, |
549 |
| - } |
550 |
| - ) |
551 |
| - assert result_object.df["count"][0] == 4 |
| 531 | + # empty string should be replaced |
| 532 | + result_object = table.query( |
| 533 | + { |
| 534 | + "metrics": ["count"], |
| 535 | + "filter": [{"col": "foo", "val": [EMPTY_STRING], "op": "IN"}], |
| 536 | + "is_timeseries": False, |
| 537 | + } |
| 538 | + ) |
| 539 | + assert result_object.df["count"][0] == 1 |
| 540 | + |
| 541 | + # also accept "" string |
| 542 | + result_object = table.query( |
| 543 | + { |
| 544 | + "metrics": ["count"], |
| 545 | + "filter": [{"col": "foo", "val": [""], "op": "IN"}], |
| 546 | + "is_timeseries": False, |
| 547 | + } |
| 548 | + ) |
| 549 | + assert result_object.df["count"][0] == 1 |
| 550 | + |
| 551 | + # both replaced |
| 552 | + result_object = table.query( |
| 553 | + { |
| 554 | + "metrics": ["count"], |
| 555 | + "filter": [ |
| 556 | + { |
| 557 | + "col": "foo", |
| 558 | + "val": [EMPTY_STRING, NULL_STRING, "null", "foo"], |
| 559 | + "op": "IN", |
| 560 | + } |
| 561 | + ], |
| 562 | + "is_timeseries": False, |
| 563 | + } |
| 564 | + ) |
| 565 | + assert result_object.df["count"][0] == 4 |
| 566 | + |
| 567 | + # should filter text in double quotes |
| 568 | + result_object = table.query( |
| 569 | + { |
| 570 | + "metrics": ["count"], |
| 571 | + "filter": [{"col": "foo", "val": ['"text in double quotes"'], "op": "IN",}], |
| 572 | + "is_timeseries": False, |
| 573 | + } |
| 574 | + ) |
| 575 | + assert result_object.df["count"][0] == 1 |
| 576 | + |
| 577 | + # should filter text in single quotes |
| 578 | + result_object = table.query( |
| 579 | + { |
| 580 | + "metrics": ["count"], |
| 581 | + "filter": [{"col": "foo", "val": ["'text in single quotes'"], "op": "IN",}], |
| 582 | + "is_timeseries": False, |
| 583 | + } |
| 584 | + ) |
| 585 | + assert result_object.df["count"][0] == 1 |
| 586 | + |
| 587 | + # should filter text with double quote |
| 588 | + result_object = table.query( |
| 589 | + { |
| 590 | + "metrics": ["count"], |
| 591 | + "filter": [{"col": "foo", "val": ['double quotes " in text'], "op": "IN",}], |
| 592 | + "is_timeseries": False, |
| 593 | + } |
| 594 | + ) |
| 595 | + assert result_object.df["count"][0] == 1 |
| 596 | + |
| 597 | + # should filter text with single quote |
| 598 | + result_object = table.query( |
| 599 | + { |
| 600 | + "metrics": ["count"], |
| 601 | + "filter": [{"col": "foo", "val": ["single quotes ' in text"], "op": "IN",}], |
| 602 | + "is_timeseries": False, |
| 603 | + } |
| 604 | + ) |
| 605 | + assert result_object.df["count"][0] == 1 |
552 | 606 |
|
553 | 607 |
|
554 | 608 | @pytest.mark.parametrize(
|
|
0 commit comments