示例#1
0
    def test_habtm_count(self):
        color = yield FavoriteColor(name="red").save()
        colors = [self.favcolor, color]
        yield FavoriteColor(name="green").save()

        args = {'user_id': self.user.id, 'favorite_color_id': colors[0].id}
        yield self.config.insert('favorite_colors_users', args)
        args = {'user_id': self.user.id, 'favorite_color_id': colors[1].id}
        yield self.config.insert('favorite_colors_users', args)

        newcolorsnum = yield self.user.favorite_colors.count()
        self.assertEqual(newcolorsnum, 2)
示例#2
0
    def test_habtm(self):
        color = yield FavoriteColor(name="red").save()
        colors = [self.favcolor, color]
        colorids = [c.id for c in colors]
        yield FavoriteColor(name="green").save()

        args = {'user_id': self.user.id, 'favorite_color_id': colors[0].id}
        yield self.config.insert('favorite_colors_users', args)
        args = {'user_id': self.user.id, 'favorite_color_id': colors[1].id}
        yield self.config.insert('favorite_colors_users', args)

        newcolors = yield self.user.favorite_colors.get()
        newcolorids = [c.id for c in newcolors]
        self.assertEqual(newcolorids, colorids)
示例#3
0
    def test_clear_habtm(self):
        user = yield User().save()
        color = yield FavoriteColor(name="red").save()
        colors = [self.favcolor, color]

        yield user.favorite_colors.set(colors)
        yield user.favorite_colors.clear()
        colors = yield user.favorite_colors.get()
        self.assertEqual(colors, [])
示例#4
0
    def test_set_habtm_blank(self):
        user = yield User().save()
        color = yield FavoriteColor(name="red").save()
        colors = [self.favcolor, color]

        yield user.favorite_colors.set(colors)
        # now blank out
        yield user.favorite_colors.set([])
        newcolors = yield user.favorite_colors.get()
        self.assertEqual(len(newcolors), 0)
示例#5
0
    def test_set_habtm(self):
        user = yield User().save()
        color = yield FavoriteColor(name="red").save()
        colors = [self.favcolor, color]
        colorids = [c.id for c in colors]

        yield user.favorite_colors.set(colors)
        newcolors = yield user.favorite_colors.get()
        newcolorids = [c.id for c in newcolors]
        self.assertEqual(newcolorids, colorids)
示例#6
0
    def test_habtm_count_with_args(self):
        color = yield FavoriteColor(name="red").save()
        colors = [self.favcolor, color]

        args = {'user_id': self.user.id, 'favorite_color_id': colors[0].id}
        yield self.config.insert('favorite_colors_users', args)
        args = {'user_id': self.user.id, 'favorite_color_id': colors[1].id}
        yield self.config.insert('favorite_colors_users', args)

        newcolorsnum = yield self.user.favorite_colors.count(
            where=['name = ?', 'red'])
        self.assertEqual(newcolorsnum, 1)
示例#7
0
    def test_clear_jointable_on_delete_habtm(self):
        user = yield User().save()
        color = yield FavoriteColor(name="red").save()
        colors = [self.favcolor, color]

        yield user.favorite_colors.set(colors)
        old_id = color.id
        yield color.delete()
        result = yield self.config.select(
            'favorite_colors_users',
            where=['favorite_color_id = ?', old_id],
            limit=1)
        self.assertTrue(result is None)
示例#8
0
    def test_habtm_with_joinwhere(self):
        color = yield FavoriteColor(name="red").save()
        colors = [self.favcolor, color]
        yield FavoriteColor(name="green").save()

        args = {
            'user_id': self.user.id,
            'favorite_color_id': colors[0].id,
            'palette_id': 1
        }
        yield self.config.insert('favorite_colors_users', args)
        args = {
            'user_id': self.user.id,
            'favorite_color_id': colors[1].id,
            'palette_id': 2
        }
        yield self.config.insert('favorite_colors_users', args)

        newcolors = yield self.user.favorite_colors.get(
            join_where=['palette_id = ?', 2])
        newcolorids = [c.id for c in newcolors]
        self.assertEqual(newcolorids, [colors[1].id])
示例#9
0
 def setUp(self):
     yield initDB(self)
     self.user = yield User(first_name="First", last_name="Last",
                            age=10).save()
     self.avatar = yield Avatar(name="an avatar name",
                                user_id=self.user.id).save()
     self.picture = yield Picture(name="a pic",
                                  size=10,
                                  user_id=self.user.id).save()
     self.favcolor = yield FavoriteColor(name="blue").save()
     self.boy = yield Boy(name="Robert").save()
     self.girl = yield Girl(name="Susan").save()
     self.config = Registry.getConfig()