Exemplo n.º 1
0
    def test_form_data(self):
        # Valid data
        form = self.F(DummyPostData(author=text_type(self.first_author_id)))
        form.author.query = Author.query().order(Author.name)
        assert form.validate()
        ichoices = list(form.author.iter_choices())
        self.assertEqual(len(ichoices), len(self.authors))
        self.assertEqual(list(x[2] for x in ichoices), [True, False, False])

        # Bogus Data
        form = self.F(DummyPostData(author='fooflaf'))
        assert not form.validate()
        print list(form.author.iter_choices())
        assert all(x[2] is False for x in form.author.iter_choices())
Exemplo n.º 2
0
 def test_allow_blank(self):
     F = self.build_form(allow_blank=True, get_label='name')
     form = F(DummyPostData(author='__None'))
     assert form.validate()
     self.assertEqual(form.author.data, None)
     expected = self.author_expected(None)
     expected.add(('__None', '', True))
     self.assertEqual(set(form.author.iter_choices()), expected)
Exemplo n.º 3
0
    def test_bad_value(self):
        data = DummyPostData(authors=[
            'foo',
            RepeatedKeyPropertyField._key_value(self.first_author_key)
        ])

        form = self.get_form(data)

        self.assertFalse(form.validate())
Exemplo n.º 4
0
    def test_empty_form(self):
        form = self.get_form(DummyPostData(authors=[]))

        self.assertTrue(form.validate())
        self.assertEqual(form.authors.data, [])

        inst = Collab()
        form.populate_obj(inst)
        self.assertEqual(inst.authors, [])
Exemplo n.º 5
0
    def test_populate_obj(self):
        author = Author.query().get()
        book = Book(author=author.key)
        book.put()

        form = self.F(DummyPostData(), book)

        book2 = Book()
        form.populate_obj(book2)
        self.assertEqual(book2.author, author.key)
Exemplo n.º 6
0
    def test_basic(self):
        F = self.build_form(get_label='name')
        form = F()
        self.assertEqual(set(form.author.iter_choices()),
                         self.author_expected(None))
        assert not form.validate()

        form = F(DummyPostData(author=str(self.authors[0].key())))
        assert form.validate()
        self.assertEqual(set(form.author.iter_choices()),
                         self.author_expected(0))
Exemplo n.º 7
0
    def test_round_trip(self):
        # Valid data
        test_data = {u'a': {'b': 3, 'c': ['a', 1, False]}}

        form = self.F()
        form.process(data={'field': test_data})
        raw_string = form.field._value()
        assert form.validate()
        form2 = self.F()
        form2.process(formdata=DummyPostData(field=raw_string))
        assert form.validate()
        # Test that we get back the same structure we serialized
        self.assertEqual(test_data, form2.field.data)
Exemplo n.º 8
0
    def test_obj_data(self):
        """
        When creating a form from an object, check that the form will render
        (hint: it didn't before)
        """
        author = Author.query().get()
        book = Book(author=author.key)
        book.put()

        form = self.F(DummyPostData(), book)

        str(form['author'])

        self.assertEqual(form.author.data, author.key)
Exemplo n.º 9
0
    def test_valid_form_data(self):
        # Valid data
        data = DummyPostData(
            author=KeyPropertyField._key_value(self.first_author_key))

        form = self.get_form(data)

        assert form.validate(), "Form validation failed. %r" % form.errors

        # Check that our first author was selected
        ichoices = list(form.author.iter_choices())
        self.assertEqual(len(ichoices), len(self.authors))
        self.assertEqual(list(x[2] for x in ichoices), [True, False, False])

        self.assertEqual(form.author.data, self.first_author_key)
Exemplo n.º 10
0
    def test_values(self):
        data = DummyPostData(authors=[
            RepeatedKeyPropertyField._key_value(self.first_author_key),
            RepeatedKeyPropertyField._key_value(self.second_author_key)
        ])

        form = self.get_form(data)

        assert form.validate(), "Form validation failed. %r" % form.errors
        self.assertEqual(form.authors.data,
                         [self.first_author_key, self.second_author_key])

        inst = Collab()
        form.populate_obj(inst)
        self.assertEqual(inst.authors,
                         [self.first_author_key, self.second_author_key])
Exemplo n.º 11
0
    def test_ancestors(self):
        """
        Test that we support queries that return instances with ancestors.

        Additionally, test that when we have instances with near-identical
        ID's, (i.e. int vs str) we don't mix them up.
        """
        AncestorModel.generate()

        class F(Form):
            empty = KeyPropertyField(reference_class=AncestorModel)

        bound_form = F()

        # Iter through all of the options, and make sure that we
        # haven't returned a similar key.
        for choice_value, choice_label, selected in \
                bound_form['empty'].iter_choices():

            data_form = F(DummyPostData(empty=choice_value))
            assert data_form.validate()

            instance = data_form['empty'].data.get()
            self.assertEqual(unicode(instance), choice_label)
Exemplo n.º 12
0
 def test_basic(self):
     form = self.F(DummyPostData(a='foo\nbar\nbaz'))
     self.assertEqual(form.a.data, ['foo', 'bar', 'baz'])
     self.assertEqual(form.a._value(), 'foo\nbar\nbaz')
Exemplo n.º 13
0
 def test_geopt_property(self):
     form = self.GeoTestForm(DummyPostData(geo='5.0, -7.0'))
     self.assertTrue(form.validate())
     self.assertEqual(form.geo.data, '5.0,-7.0')
     form = self.GeoTestForm(DummyPostData(geo='5.0,-f'))
     self.assertFalse(form.validate())
Exemplo n.º 14
0
 def test_invalid_form_data(self):
     form = self.get_form(DummyPostData(author='fooflaf'))
     assert not form.validate()
     assert all(x[2] is False for x in form.author.iter_choices())