Exemple #1
0
class WithTest(unittest.TestCase):
    figure = {
        "tables": ["from orders"],
        "outline": {
            "index": {
                "select": ["customer", "subtotal"]
            }
        },
        "arguments": {
            "count": {
                # the `False` will clear out the seleted columns from above
                "select":
                [False, {
                    "column": "*",
                    "agg": "count",
                    "as": "count"
                }],
                "validator": "number",
                "column": "count::int",
                # to compare this value we must make a with query.
                "agg": True,
                # for this example we need to require a groupby value to be set
                "requires": "groupby"
            },
            "groupby": {
                "validator": "string",
                "adapt": False
            }
        }
    }

    @classmethod
    def setUpClass(self):
        self.inquiry = Inquiry(debug=True)
        self.inquiry.add_figure("data", self.figure)

    def setUp(self):
        self.q = self.inquiry.new()

    def test_agg_not_provided(self):
        self.assertEqual(
            self.q('data').pg(), "select customer, subtotal from orders")

    def test_groupby_required(self):
        self.assertRaisesRegexp(valideer.ValidationError,
                                "required property not set: groupby",
                                self.q,
                                'data',
                                count=10)

    def test_agg_provided(self):
        self.assertEqual(
            self.q('data', count=10, groupby="customer").pg(),
            "with _data as (select count(*) as count, customer from orders group by customer) select count, customer from _data where count = 10::int"
        )
Exemple #2
0
class WithTest2(unittest.TestCase):
    figure = {
        "tables": ["from orders"],
        "outline": {
            "index": {
                "select": ["customer", "subtotal"]
            },
            "/bycustomer": {
                "arguments": {
                    "groupby": {
                        "validator": "string",
                        "adapt": False,
                        "default": "customer"
                    },
                    "count": {
                        "select": {
                            "agg": "count",
                            "column": "*",
                            "as": "count"
                        },
                        "validator": "number",
                        "column": "count::int",
                        "agg": True
                    }
                }
            }
        }
    }

    @classmethod
    def setUpClass(self):
        self.inquiry = Inquiry(debug=True)
        self.inquiry.add_figure("data", self.figure)

    def setUp(self):
        self.q = self.inquiry.new()

    def test_agg_not_provided(self):
        self.assertEqual(
            self.q('data').pg(), "select customer, subtotal from orders")

    def test_groupby_required(self):
        self.assertRaisesRegexp(valideer.ValidationError,
                                r"additional properties: \[\'count\'\]",
                                self.q,
                                'data',
                                count=10)

    def test_agg_provided(self):
        self.assertEqual(
            self.q('data', 'bycustomer', count=10).pg(),
            "with _data as (select count(*) as count, customer from orders group by customer) select count, customer from _data where count = 10::int"
        )
Exemple #3
0
class WithTest(unittest.TestCase):
    figure = {
      "tables": ["from orders"],
      "outline": {
        "index": {
          "select": ["customer", "subtotal"]
        }
      },
      "arguments": {
        "count": {
          # the `False` will clear out the seleted columns from above
          "select": [False, {"column": "*", "agg": "count", "as": "count"}],
          "validator": "number",
          "column": "count::int",
          # to compare this value we must make a with query.
          "agg": True,
          # for this example we need to require a groupby value to be set
          "requires": "groupby"
        },
        "groupby" : {
          "validator": "string",
          "adapt": False
        }
      }
    }

    @classmethod
    def setUpClass(self):
        self.inquiry = Inquiry(debug=True)
        self.inquiry.add_figure("data", self.figure)

    def setUp(self):
        self.q = self.inquiry.new()

    def test_agg_not_provided(self):
        self.assertEqual(self.q('data').pg(), "select customer, subtotal from orders")

    def test_groupby_required(self):
        self.assertRaisesRegexp(valideer.ValidationError, "required property not set: groupby", self.q, 'data', count=10)

    def test_agg_provided(self):
        self.assertEqual(self.q('data', count=10, groupby="customer").pg(), "with _data as (select count(*) as count, customer from orders group by customer) select count, customer from _data where count = 10::int")
Exemple #4
0
class WithTest2(unittest.TestCase):
    figure = {
      "tables": ["from orders"],
      "outline": {
        "index": {
          "select": ["customer", "subtotal"]
        },
        "/bycustomer": {
          "arguments": {
            "groupby" : {
              "validator": "string",
              "adapt": False,
              "default": "customer"
            },
            "count": {
              "select": {"agg": "count", "column": "*", "as": "count"},
              "validator": "number",
              "column": "count::int",
              "agg": True
            }
          }
        }
      }
    }
    @classmethod
    def setUpClass(self):
        self.inquiry = Inquiry(debug=True)
        self.inquiry.add_figure("data", self.figure)

    def setUp(self):
        self.q = self.inquiry.new()

    def test_agg_not_provided(self):
        self.assertEqual(self.q('data').pg(), "select customer, subtotal from orders")

    def test_groupby_required(self):
        self.assertRaisesRegexp(valideer.ValidationError, r"additional properties: \[\'count\'\]", self.q, 'data', count=10)

    def test_agg_provided(self):
        self.assertEqual(self.q('data', 'bycustomer', count=10).pg(), "with _data as (select count(*) as count, customer from orders group by customer) select count, customer from _data where count = 10::int")
Exemple #5
0
 def __init__(self, *a, **k):
     Inquiry.__init__(self, *a, **k)
     self.db = tornpsql.Connection(os.getenv("PSQL", "postgres://peak:@localhost:5432/inquiry"))
Exemple #6
0
 def setUpClass(self):
     self.inquiry = Inquiry(debug=True)
     self.inquiry.add_figure("a", EXAMPLES[0])
     self.inquiry.add_figure("b", EXAMPLES[1])
Exemple #7
0
class Tests(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        self.inquiry = Inquiry(debug=True)
        self.inquiry.add_figure("a", EXAMPLES[0])
        self.inquiry.add_figure("b", EXAMPLES[1])

    def setUp(self):
        self.q = self.inquiry.new()

    def test_inquiry_nav(self):
        "nav - new"
        self.assertIsInstance(self.q, Navigator)

    def test_nav_via_getattr(self):
        "nav - __getattr__"
        self.assertIsInstance(self.q.a, Navigator)

    def test_nav_via_getitem(self):
        "nav - __getitem__"
        self.assertIsInstance(self.q['a'], Navigator)

    def test_outline_nav_regexp(self):
        "outline - nav with regexp"
        self.assertEqual(
            self.q('a', 'count').pg(),
            "select count(o.*) as count from table where b > 10 and col_a = 'Hello'::text"
        )

    def test_outline_nav_regexp_2(self):
        self.assertEqual(
            self.q('a', 'total').pg(),
            "select count(o.*) as count from table where b > 10 and col_a = 'Hello'::text"
        )

    def test_outline_inheritance(self):
        self.assertRaisesRegexp(valideer.ValidationError,
                                "missing required property: this", self.q, 'a',
                                'inherit')

    def test_outline_inheritance_2(self):
        self.assertEqual(
            self.q('a', 'inherit', this="apples", r="something").pg(),
            "select that, this from table inner join other using (this) where r = 'something'::text"
        )

    def test_default_arguments(self):
        "argument - `default`"
        self.assertEqual(
            self.q('a').pg(),
            "select value from table where b > 10 and col_a = 'Hello'::text")

    def test_argument_list(self):
        "argument - accepts lists with `[]` at end of key"
        self.assertEqual(
            self.q('a', a=["this", "that"]).pg(),
            "select value from table where ARRAY['this', 'that']::text[] @> array[col_a] and b > 10"
        )

    def test_arguments_option_regexp(self):
        "argument:options - keys are regexp"
        self.assertEqual(
            self.q('a', groupby="day").pg(),
            "select column_day as day, value from table where b > 10 and col_a = 'Hello'::text group by day"
        )

    def test_arguments_option_regexp_2(self):
        self.assertEqual(
            self.q('a', groupby="days").pg(),
            "select column_day as day, value from table where b > 10 and col_a = 'Hello'::text group by day"
        )

    def test_arguments_seeds_ignore_str(self):
        "argument:ignore - <str>"
        self.assertEqual(
            self.q('a', ignore1="true").pg(),
            "select value from table where b > 10")

    def test_arguments_seeds_ignore_list(self):
        "argument:ignore - <list>"
        self.assertRaisesRegexp(valideer.ValidationError,
                                "additional properties: groupby",
                                self.q,
                                'a',
                                groupby="day",
                                ignore2="true")

    def test_argument_seed_requires(self):
        "argument:required - requres a value"
        self.assertRaisesRegexp(valideer.ValidationError,
                                "missing required property: r", self.q, 'b')

    def test_argument_merge(self):
        "arguments - can merge"
        self.assertEqual(
            self.q('a', 'merge').pg(),
            "select c from table where b > 10 and col_a = 'Whats up!'::text")

    def test_create(self):
        self.assertEqual(
            self.q('b', 'create', a=1, r='world').pg(),
            "insert into _table (a, r) values (1, 'world') returning _id")

    def test_update(self):
        self.assertEqual(
            self.q('b', 'update', id=10, r='something').pg(),
            "update _table set r='something'::text where id=10::int")

    def test_update_many(self):
        self.skipTest("wip")
        self.assertEqual(
            self.q('b', 'update', id=[4, 50], r='something').pg(),
            "update _table set r='something' where ARRAY[id] @> ARRAY[4, 50]")

    def test_update_many_more(self):
        self.skipTest("wip")
        self.assertEqual(
            self.q('b', 'update', a=5, r='something', id=[5, 6]).pg(),
            "update _table set a=5, r='something' where array[id] @> ARRAY[5, 6]"
        )
Exemple #8
0
 def setUpClass(self):
     self.inquiry = Inquiry(debug=True)
     self.inquiry.add_figure("data", self.figure)
Exemple #9
0
 def setUpClass(self):
     self.inquiry = Inquiry(debug=True)
     self.inquiry.add_figure("a", EXAMPLES[0])
     self.inquiry.add_figure("b", EXAMPLES[1])
Exemple #10
0
class Tests(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        self.inquiry = Inquiry(debug=True)
        self.inquiry.add_figure("a", EXAMPLES[0])
        self.inquiry.add_figure("b", EXAMPLES[1])

    def setUp(self):
        self.q = self.inquiry.new()

    def test_inquiry_nav(self):
        "nav - new"
        self.assertIsInstance(self.q, Navigator)

    def test_nav_via_getattr(self):
        "nav - __getattr__"
        self.assertIsInstance(self.q.a, Navigator)

    def test_nav_via_getitem(self):
        "nav - __getitem__"
        self.assertIsInstance(self.q['a'], Navigator)

    def test_outline_nav_regexp(self):
        "outline - nav with regexp"
        self.assertEqual(self.q('a', 'count').pg(), "select count(o.*) as count from table where b > 10 and col_a = 'Hello'::text")

    def test_outline_nav_regexp_2(self):
        self.assertEqual(self.q('a', 'total').pg(), "select count(o.*) as count from table where b > 10 and col_a = 'Hello'::text")

    def test_outline_inheritance(self):
        self.assertRaisesRegexp(valideer.ValidationError, "missing required property: this", self.q, 'a', 'inherit')

    def test_outline_inheritance_2(self):
        self.assertEqual(self.q('a', 'inherit', this="apples", r="something").pg(), "select that, this from table inner join other using (this) where r = 'something'::text")

    def test_default_arguments(self):
        "argument - `default`"
        self.assertEqual(self.q('a').pg(), "select value from table where b > 10 and col_a = 'Hello'::text")

    def test_argument_list(self):
        "argument - accepts lists with `[]` at end of key"
        self.assertEqual(self.q('a', a=["this", "that"]).pg(), "select value from table where ARRAY['this', 'that']::text[] @> array[col_a] and b > 10")

    def test_arguments_option_regexp(self):
        "argument:options - keys are regexp"
        self.assertEqual(self.q('a', groupby="day").pg(),  "select column_day as day, value from table where b > 10 and col_a = 'Hello'::text group by day")

    def test_arguments_option_regexp_2(self):
        self.assertEqual(self.q('a', groupby="days").pg(), "select column_day as day, value from table where b > 10 and col_a = 'Hello'::text group by day")

    def test_arguments_seeds_ignore_str(self):
        "argument:ignore - <str>"
        self.assertEqual(self.q('a', ignore1="true").pg(), "select value from table where b > 10")

    def test_arguments_seeds_ignore_list(self):
        "argument:ignore - <list>"
        self.assertRaisesRegexp(valideer.ValidationError, "additional properties: groupby", self.q, 'a', groupby="day", ignore2="true")

    def test_argument_seed_requires(self):
        "argument:required - requres a value"
        self.assertRaisesRegexp(valideer.ValidationError, "missing required property: r", self.q, 'b')

    def test_argument_merge(self):
        "arguments - can merge"
        self.assertEqual(self.q('a', 'merge').pg(), "select c from table where b > 10 and col_a = 'Whats up!'::text")

    def test_create(self):
        self.assertEqual(self.q('b', 'create', a=1, r='world').pg(), "insert into _table (a, r) values (1, 'world') returning _id")

    def test_update(self):
        self.assertEqual(self.q('b', 'update', id=10, r='something').pg(), "update _table set r='something'::text where id=10::int")

    def test_update_many(self):
        self.skipTest("wip")
        self.assertEqual(self.q('b', 'update', id=[4,50], r='something').pg(), "update _table set r='something' where ARRAY[id] @> ARRAY[4, 50]")

    def test_update_many_more(self):
        self.skipTest("wip")
        self.assertEqual(self.q('b', 'update', a=5, r='something', id=[5, 6]).pg(), "update _table set a=5, r='something' where array[id] @> ARRAY[5, 6]")
Exemple #11
0
 def setUpClass(self):
     self.inquiry = Inquiry(debug=True)
     self.inquiry.add_figure("data", self.figure)
Exemple #12
0
 def __init__(self, *a, **k):
     Inquiry.__init__(self, *a, **k)
     self.db = tornpsql.Connection(
         os.getenv("PSQL", "postgres://peak:@localhost:5432/inquiry"))