Пример #1
0
    def test_weak_ref(self):
        i = self.get_interface()
        table_name_1 = "".join(
            random.sample(string.ascii_lowercase, random.randint(5, 15)))
        table_name_2 = "".join(
            random.sample(string.ascii_lowercase, random.randint(5, 15)))

        s_1 = Schema(table_name_1,
                     _id=Field(int, pk=True),
                     foo=Field(int, True))
        s_2 = Schema(
            table_name_2,
            _id=Field(int, pk=True),
            s_pk=Field(s_1, False),
        )

        i.set_table(s_1)
        i.set_table(s_2)

        pk1 = i.insert(s_1, {'foo': 1})

        pk2 = i.insert(s_2, {'s_pk': pk1})
        q2 = query.Query()
        q2.is__id(pk2)
        # make sure it exists and is visible
        r = i.get_one(s_2, q2)
        self.assertGreater(len(r), 0)

        q1 = query.Query()
        q1.is__id(pk1)
        i.delete(s_1, q1)

        r = i.get_one(s_2, q2)
        self.assertGreater(len(r), 0)
        self.assertIsNone(r['s_pk'])
Пример #2
0
    def test__normalize_val_SQL_eq(self):
        i = self.get_interface()
        s = Schema("fake_table_name", ts=Field(datetime.datetime, True))
        orm_class = s.create_orm()

        fstr, fargs = orm_class.query.is_ts(day=10).render(placeholder=True)
        self.assertTrue('EXTRACT(DAY FROM "ts") = %s' in fstr)
        self.assertEqual(10, fargs[0])

        fstr, fargs = orm_class.query.is_ts(day=11,
                                            hour=12).render(placeholder=True)
        self.assertTrue(
            'EXTRACT(DAY FROM "ts") = %s AND EXTRACT(HOUR FROM "ts") = %s' in
            fstr)
        self.assertEqual(11, fargs[0])
        self.assertEqual(12, fargs[1])

        fstr, fargs = orm_class.query.is_ts(None).render(placeholder=True)
        self.assertTrue('"ts" IS %s' in fstr)

        fstr, fargs = orm_class.query.not_ts(None).render(placeholder=True)
        self.assertTrue('"ts" IS NOT %s' in fstr)

        with self.assertRaises(KeyError):
            fstr, fargs = orm_class.query.is_ts(bogus=5).render(
                placeholder=True)
Пример #3
0
    def test_transaction_nested_fail_1(self):
        """make sure 2 new tables in a wrapped transaction work as expected"""
        i = self.get_interface()
        table_name_1 = self.get_table_name()
        table_name_2 = self.get_table_name()

        s1 = Schema(table_name_1,
                    _id=Field(int, pk=True),
                    foo=Field(int, True))
        s2 = Schema(
            table_name_2,
            _id=Field(int, pk=True),
            bar=Field(int, True),
            s_pk=Field(s1),
        )

        with i.transaction() as connection:
            pk1 = i.insert(s1, {"foo": 1}, connection=connection)
            pk2 = i.insert(s2, {"bar": 2, "s_pk": pk1}, connection=connection)

        q1 = query.Query()
        q1.is__id(pk1)
        r1 = i.get_one(s1, q1)
        self.assertEqual(pk1, r1['_id'])

        q2 = query.Query()
        q2.is__id(pk2)
        r2 = i.get_one(s2, q2)
        self.assertEqual(pk2, r2['_id'])
        self.assertEqual(pk1, r2['s_pk'])
Пример #4
0
    def test___getattr__(self):
        s = Schema("foo")

        with self.assertRaises(AttributeError):
            s.foo

        s.set_field("foo", Field(int, True))
        self.assertTrue(isinstance(s.foo, Field))
Пример #5
0
    def test__normalize_date_SQL(self):
        """this tests the common date kwargs you can use (in both SQLight and Postgres)
        if we ever add other backends this might need to be moved out of the general
        generator test"""
        i = self.get_interface()
        s = Schema(
            self.get_table_name(),
            foo=Field(datetime.datetime, True),
            _id=Field(int, True, pk=True),
            index_foo=Index('foo'),
        )
        i.set_table(s)

        pk20 = i.insert(s, {'foo': datetime.datetime(2014, 4, 20)})
        pk21 = i.insert(s, {'foo': datetime.datetime(2014, 4, 21)})

        q = query.Query()
        q.is_foo(day=20)
        d = i.get_one(s, q)
        self.assertEqual(d['_id'], pk20)

        q = query.Query()
        q.is_foo(day=21, month=4)
        d = i.get_one(s, q)
        self.assertEqual(d['_id'], pk21)

        q = query.Query()
        q.is_foo(day=21, month=3)
        d = i.get_one(s, q)
        self.assertFalse(d)
Пример #6
0
    def test__normalize_val_SQL_with_list(self):
        i = self.get_interface()
        s = Schema("fake_table_name", ts=Field(datetime.datetime, True))

        kwargs = dict(day=[10])
        fstr, fargs = i._normalize_val_SQL(s, {
            'symbol': 'IN',
            'list': True
        }, 'ts', None, kwargs)
        self.assertEqual('EXTRACT(DAY FROM "ts") IN (%s)', fstr)
        self.assertEqual(kwargs['day'], fargs)

        kwargs = dict(day=[11, 13], hour=[12])
        fstr, fargs = i._normalize_val_SQL(s, {
            'symbol': 'IN',
            'list': True
        }, 'ts', None, kwargs)
        self.assertEqual(
            'EXTRACT(DAY FROM "ts") IN (%s, %s) AND EXTRACT(HOUR FROM "ts") IN (%s)',
            fstr)
        self.assertEqual(kwargs['day'], fargs[0:2])
        self.assertEqual(kwargs['hour'], fargs[2:])

        kwargs = dict(bogus=[5])
        with self.assertRaises(KeyError):
            fstr, fargs = i._normalize_val_SQL(s, {
                'symbol': 'IN',
                'list': True
            }, 'ts', None, kwargs)
Пример #7
0
    def test__normalize_val_SQL_in(self):
        i = self.get_interface()
        s = Schema("fake_table_name", ts=Field(datetime.datetime, True))
        orm_class = s.create_orm()

        fstr, fargs = orm_class.query.in_ts(day=10).render(placeholder=True)
        self.assertTrue('EXTRACT(DAY FROM "ts") IN (%s)' in fstr)

        fstr, fargs = orm_class.query.in_ts(day=[11, 13],
                                            hour=12).render(placeholder=True)
        self.assertTrue(
            'EXTRACT(DAY FROM "ts") IN (%s, %s) AND EXTRACT(HOUR FROM "ts") IN (%s)'
            in fstr)
        self.assertEqual([11, 13, 12], fargs)

        with self.assertRaises(KeyError):
            fstr, fargs = orm_class.query.in_ts(bogus=5).render(
                placeholder=True)
Пример #8
0
    def get_schema(self, table_name=None, **fields_or_indexes):
        if not fields_or_indexes:
            fields_or_indexes.setdefault("foo", Field(int, True))
            fields_or_indexes.setdefault("bar", Field(str, True))
            fields_or_indexes.setdefault("ifoobar", Index("foo", "bar"))

        fields_or_indexes.setdefault("_id", Field(long, True, pk=True))

        s = Schema(self.get_table_name(table_name), **fields_or_indexes)
        return s
Пример #9
0
    def test___init__(self):
        """
        I had set the class .fields and .indexes attributes to {} instead of None, so you
        could only ever create one instance of Schema, this test makes sure that's been fixed
        """
        s = Schema("foo")
        self.assertTrue(isinstance(s.fields, dict))
        self.assertTrue(isinstance(s.indexes, dict))

        s2 = Schema("bar")
        self.assertTrue(isinstance(s.fields, dict))
        self.assertTrue(isinstance(s.indexes, dict))

        s = Schema("foo",
                   bar=Field(int),
                   che=Field(str, True),
                   barche=Index("bar", "che"))
        self.assertTrue("bar" in s.fields)
        self.assertTrue("che" in s.fields)
        self.assertTrue("barche" in s.indexes)
Пример #10
0
    def test_handle_error_ref(self):
        i = self.get_interface()
        table_name_1 = self.get_table_name()
        table_name_2 = self.get_table_name()

        s_1 = Schema(table_name_1,
                     _id=Field(int, pk=True),
                     foo=Field(int, True))
        s_2 = Schema(
            table_name_2,
            _id=Field(int, pk=True),
            bar=Field(int, True),
            s_pk=Field(s_1),
        )

        q2 = query.Query()
        q2.is_bar(1)

        r = i.get_one(s_2, q2)
        self.assertTrue(i.has_table(table_name_1))
        self.assertTrue(i.has_table(table_name_2))
Пример #11
0
    def test_index_ignore_case(self):
        i = self.get_interface()
        s = Schema(
            self.get_table_name(),
            _id=Field(int, pk=True),
            foo=Field(str, True, ignore_case=True),
            bar=Field(str, True),
            index_foo=Index('foo', 'bar'),
        )
        i.set_table(s)

        v = '*****@*****.**'
        d = i.insert(s, {'foo': v, 'bar': 'bar'})
        q = query.Query()
        q.is_foo(v)
        r = i.get_one(s, q)
        self.assertGreater(len(r), 0)

        lv = list(v)
        for x in range(len(v)):
            lv[x] = lv[x].upper()
            qv = "".join(lv)
            q = query.Query()
            q.is_foo(qv)
            r = i.get_one(s, q)
            self.assertGreater(len(r), 0)
            lv[x] = lv[x].lower()

        d = i.insert(s, {'foo': 'FoO', 'bar': 'bar'})
        q = query.Query()
        q.is_foo('foo')
        r = i.get_one(s, q)
        self.assertGreater(len(r), 0)
        self.assertEqual(r['foo'], 'FoO')

        q = query.Query()
        q.is_foo('Foo').is_bar('BAR')
        r = i.get_one(s, q)
        self.assertEqual(len(r), 0)

        q = query.Query()
        q.is_foo('FoO').is_bar('bar')
        r = i.get_one(s, q)
        self.assertGreater(len(r), 0)
        self.assertEqual(r['foo'], 'FoO')

        d = i.insert(s, {'foo': 'foo2', 'bar': 'bar'})
        q = query.Query()
        q.is_foo('foo2')
        r = i.get_one(s, q)
        self.assertGreater(len(r), 0)
        self.assertEqual(r['foo'], 'foo2')
Пример #12
0
    def test_transaction_context(self):
        i = self.get_interface()
        table_name_1 = "{}_1".format(self.get_table_name())
        table_name_2 = "{}_2".format(self.get_table_name())

        # these 2 tables exist before the transaction starts
        s1 = Schema(table_name_1,
                    _id=Field(int, pk=True),
                    foo=Field(int, True))
        i.set_table(s1)

        s2 = Schema(
            table_name_2,
            _id=Field(int, pk=True),
            bar=Field(int, True),
            s_pk=Field(s1, True),
        )
        i.set_table(s2)

        pk1 = 0
        pk2 = 0

        try:
            with i.transaction() as connection:
                pk1 = i.insert(s1, {"foo": 1}, connection=connection)

                with i.transaction(connection):
                    pk2 = i.set(s2, {
                        "bar": 2,
                        "s_pk": pk1
                    },
                                connection=connection)
                    raise RuntimeError("testing")

        except Exception as e:
            pass

        self.assertEqual(0, i.count(s1, query.Query().is__id(pk1)))
        self.assertEqual(0, i.count(s2, query.Query().is__id(pk2)))
Пример #13
0
    def get_schema(self, table_name=None, **fields_or_indexes):
        if not fields_or_indexes:
            fields_or_indexes.setdefault("foo", Field(int, True))
            fields_or_indexes.setdefault("bar", Field(str, True))
            fields_or_indexes.setdefault("ifoobar", Index("foo", "bar"))

        fields_or_indexes.setdefault("_id", Field(long, True, pk=True))

        # remove any None values
        for k in list(fields_or_indexes.keys()):
            if not fields_or_indexes[k]:
                fields_or_indexes.pop(k)

        s = Schema(self.get_table_name(table_name), **fields_or_indexes)
        return s
Пример #14
0
    def test_group_field_name(self):
        i = self.get_interface()
        s = Schema(
            self.get_table_name(),
            _id=Field(int, True, pk=True),
            group=Field(str, True),
        )
        i.set_table(s)

        text = testdata.get_words()
        pk = i.insert(s, {'group': text})

        q = query.Query().is__id(pk)
        d = dict(i.get_one(s, q))
        self.assertEqual(text, d["group"])
        self.assertEqual(pk, d["_id"])
Пример #15
0
    def get_schema_all(self, inter=None):
        """return a schema that has a field for all supported standard field types"""

        # this is for foreign key fields
        orm_class = self.get_orm_class()
        if inter:
            orm_class.interface = inter
            i = inter
            orm_class.install()

        s = Schema(
            self.get_table_name(),
            _id=Field(long, pk=True),
            a_bool_y=Field(bool, True),
            a_bool_n=Field(bool, False),
            a_sint_y=Field(int, True, size=50),
            a_sint_n=Field(int, False, size=50),
            a_dec_y=Field(decimal.Decimal, True),
            a_dec_n=Field(decimal.Decimal, False),
            a_float_y=Field(float, True, size=10),
            a_float_n=Field(float, False, size=10),
            a_long_y=Field(long, True),
            a_long_n=Field(long, False),
            a_fk_y=Field(orm_class, True),
            a_fk_n=Field(orm_class, False),
            a_dt_y=Field(datetime.datetime, True),
            a_dt_n=Field(datetime.datetime, False),
            a_d_y=Field(datetime.date, True),
            a_d_n=Field(datetime.date, False),
            a_int_y=Field(int, True),
            a_int_n=Field(int, False),
            a_str_y=Field(str, True),
            a_str_n=Field(str, False),
            a_vchar_y=Field(str, True, max_size=512),
            a_vchar_n=Field(str, False, max_size=512),
            a_char_y=Field(str, True, size=32),
            a_char_n=Field(str, False, size=32),
        )

        if inter:
            #i = self.get_interface()
            inter.set_table(s)

        return s
Пример #16
0
    def test_null_values(self):
        i = self.get_interface()
        s = Schema(
            self.get_table_name(),
            _id=Field(int, pk=True),
            foo=Field(int, False),
            bar=Field(int, False),
        )

        # add one with non NULL foo
        pk1 = i.insert(s, {"bar": 1, "foo": 2})

        # and one with NULL foo
        pk2 = i.insert(s, {"bar": 1})

        r = i.get_one(s, query.Query().is_bar(1).is_foo(None))
        self.assertEqual(pk2, r['_id'])

        r = i.get_one(s, query.Query().is_bar(1).not_foo(None))
        self.assertEqual(pk1, r['_id'])
Пример #17
0
    def test__normalize_val_SQL(self):
        i = self.get_interface()
        s = Schema("fake_table_name", ts=Field(datetime.datetime, True))

        #kwargs = dict(day=int(datetime.datetime.utcnow().strftime('%d')))
        kwargs = dict(day=10)
        fstr, fargs = i._normalize_val_SQL(s, {'symbol': '='}, 'ts', None,
                                           kwargs)
        self.assertEqual('EXTRACT(DAY FROM "ts") = %s', fstr)
        self.assertEqual(10, fargs[0])

        kwargs = dict(day=11, hour=12)
        fstr, fargs = i._normalize_val_SQL(s, {'symbol': '='}, 'ts', None,
                                           kwargs)
        self.assertEqual(
            'EXTRACT(DAY FROM "ts") = %s AND EXTRACT(HOUR FROM "ts") = %s',
            fstr)
        self.assertEqual(11, fargs[0])
        self.assertEqual(12, fargs[1])

        fstr, fargs = i._normalize_val_SQL(s, {
            'symbol': '=',
            'none_symbol': 'IS'
        }, 'ts', None)
        self.assertEqual('"ts" IS %s', fstr)

        fstr, fargs = i._normalize_val_SQL(s, {
            'symbol': '!=',
            'none_symbol': 'IS NOT'
        }, 'ts', None)
        self.assertEqual('"ts" IS NOT %s', fstr)

        kwargs = dict(bogus=5)
        with self.assertRaises(KeyError):
            fstr, fargs = i._normalize_val_SQL(s, {'symbol': '='}, 'ts', None,
                                               kwargs)
Пример #18
0
    def test_set_field(self):
        s = Schema("foo")

        with self.assertRaises(ValueError):
            s.set_field("", int)

        with self.assertRaises(ValueError):
            s.set_field("foo", "bogus")

        s.set_field("foo", Field(int))
        with self.assertRaises(ValueError):
            s.set_field("foo", int)

        s = Schema("foo")
        s.set_field("foo", Field(int, unique=True))
        self.assertTrue("foo" in s.fields)
        self.assertTrue("foo" in s.indexes)

        s = Schema("foo")
        s.set_field("foo", Field(int, ignore_case=True))
        self.assertTrue(s.foo.options["ignore_case"])
Пример #19
0
    def test_set_index(self):
        s = Schema("foo")
        s.set_field("bar", Field(int, True))
        s.set_field("che", Field(str))

        with self.assertRaises(ValueError):
            s.set_index("foo", Index())

        with self.assertRaises(ValueError):
            s.set_index("", Index("bar", "che"))

        s.set_index("bar_che", Index("che", "bar"))
        with self.assertRaises(ValueError):
            s.set_index("bar_che", Index("che", "bar"))

        s.set_index("testing", Index("che", unique=True))
        self.assertTrue(s.indexes["testing"].unique)