Ejemplo n.º 1
0
    def testExportOptionsLastOptJSONHierarchy(self):

        assertEqual = self.assertEqual
        assertTrue = self.assertTrue

        db = current.db
        s3db = current.s3db

        # Configure parent-field
        table = db.fotest_lookup_table
        represent = S3Represent(lookup="fotest_lookup_table")
        table.parent.requires = IS_EMPTY_OR(
                                    IS_ONE_OF(db,
                                                "fotest_lookup_table.id",
                                                represent,
                                                ))
        table.parent.requires = represent

        # Configure hierarchy
        s3db.configure("fotest_lookup_table", hierarchy="parent")
        S3Hierarchy.dirty("fotest_lookup_table")

        # Insert a child node
        options = dict(self.records)
        child_node = {"name": "option3",
                      "uuid": "OPTION3",
                      "parent": options.keys()[0],
                      }
        child_id = table.insert(**child_node)
        options[child_id] = child_node

        # Get the last record
        table = db.fotest_lookup_table
        last = db(table.id>0).select(limitby=(0, 1),
                                     orderby=~table.id).first()
        self.assertNotEqual(last, None)

        # Request last option
        resource = s3db.resource("fotest_table")
        result = resource.export_options(fields=["lookup"],
                                         only_last=True,
                                         hierarchy=True,
                                         as_json=True)
        fo = json.loads(result)

        # Inspect result
        assertTrue(isinstance(fo, dict))

        assertTrue("option" in fo)
        assertTrue(isinstance(fo["option"], list))
        assertEqual(len(fo["option"]), 1)

        opt = fo["option"][0]
        value = opt["@value"]
        assertEqual(options[value]["uuid"], last.uuid)
        assertEqual(opt["$"], options[value]["name"])

        assertTrue("@parent" in opt)
        assertEqual(opt["@parent"], str(options[value]["parent"]))
Ejemplo n.º 2
0
    def testExportOptionsAllOptsJSONHierarchy(self):
        """ Test export options, JSON, all options+hierarchy """

        assertEqual = self.assertEqual
        assertTrue = self.assertTrue

        # Configure parent-field
        db = current.db
        table = db.fotest_lookup_table
        represent = S3Represent(lookup="fotest_lookup_table")
        table.parent.requires = IS_EMPTY_OR(
                                    IS_ONE_OF(db,
                                                "fotest_lookup_table.id",
                                                represent,
                                                ))
        table.parent.requires = represent

        # Configure hierarchy
        s3db = current.s3db
        s3db.configure("fotest_lookup_table", hierarchy="parent")
        S3Hierarchy.dirty("fotest_lookup_table")

        # Insert a child node
        options = dict(self.records)
        child_node = {"name": "option3",
                      "uuid": "OPTION3",
                      "parent": options.keys()[0],
                      }
        child_id = table.insert(**child_node)
        options[child_id] = child_node

        # Request options
        resource = s3db.resource("fotest_table")
        result = resource.export_options(fields=["lookup"],
                                         hierarchy=True,
                                         as_json=True)
        fo = json.loads(result)

        # Inspect result
        has_empty = False
        assertTrue(isinstance(fo, dict))
        assertTrue("option" in fo)
        assertTrue(isinstance(fo["option"], list))
        assertEqual(len(fo["option"]), len(options) + 1)
        for opt in fo["option"]:
            value = opt["@value"]
            if value == "":
                has_empty = True
                self.assertFalse("$" in opt)
                continue
            else:
                value = int(value)
            assertTrue(value in options)
            assertEqual(opt["$"], options[value]["name"])
            if "parent" in options[value] and options[value]["parent"]:
                assertTrue("@parent" in opt)
                assertEqual(opt["@parent"], str(options[value]["parent"]))

        assertTrue(has_empty, msg="Empty-option missing")
Ejemplo n.º 3
0
    def testExportOptionsLastOptJSONHierarchy(self):

        assertEqual = self.assertEqual
        assertTrue = self.assertTrue

        db = current.db
        s3db = current.s3db

        # Configure parent-field
        table = db.fotest_lookup_table
        represent = S3Represent(lookup="fotest_lookup_table")
        table.parent.requires = IS_EMPTY_OR(
                                    IS_ONE_OF(db,
                                                "fotest_lookup_table.id",
                                                represent,
                                                ))
        table.parent.requires = represent

        # Configure hierarchy
        s3db.configure("fotest_lookup_table", hierarchy="parent")
        S3Hierarchy.dirty("fotest_lookup_table")

        # Insert a child node
        options = dict(self.records)
        child_node = {"name": "option3",
                      "uuid": "OPTION3",
                      "parent": options.keys()[0],
                      }
        child_id = table.insert(**child_node)
        options[child_id] = child_node

        # Get the last record
        table = db.fotest_lookup_table
        last = db(table.id>0).select(limitby=(0, 1),
                                     orderby=~table.id).first()
        self.assertNotEqual(last, None)

        # Request last option
        resource = s3db.resource("fotest_table")
        result = resource.export_options(fields=["lookup"],
                                         only_last=True,
                                         hierarchy=True,
                                         as_json=True)
        fo = json.loads(result)

        # Inspect result
        assertTrue(isinstance(fo, dict))

        assertTrue("option" in fo)
        assertTrue(isinstance(fo["option"], list))
        assertEqual(len(fo["option"]), 1)

        opt = fo["option"][0]
        value = opt["@value"]
        assertEqual(options[value]["uuid"], last.uuid)
        assertEqual(opt["$"], options[value]["name"])

        assertTrue("@parent" in opt)
        assertEqual(opt["@parent"], str(options[value]["parent"]))
Ejemplo n.º 4
0
    def testExportOptionsAllOptsJSONHierarchy(self):
        """ Test export options, JSON, all options+hierarchy """

        assertEqual = self.assertEqual
        assertTrue = self.assertTrue

        # Configure parent-field
        db = current.db
        table = db.fotest_lookup_table
        represent = S3Represent(lookup="fotest_lookup_table")
        table.parent.requires = IS_EMPTY_OR(
                                    IS_ONE_OF(db,
                                                "fotest_lookup_table.id",
                                                represent,
                                                ))
        table.parent.requires = represent

        # Configure hierarchy
        s3db = current.s3db
        s3db.configure("fotest_lookup_table", hierarchy="parent")
        S3Hierarchy.dirty("fotest_lookup_table")

        # Insert a child node
        options = dict(self.records)
        child_node = {"name": "option3",
                      "uuid": "OPTION3",
                      "parent": options.keys()[0],
                      }
        child_id = table.insert(**child_node)
        options[child_id] = child_node

        # Request options
        resource = s3db.resource("fotest_table")
        result = resource.export_options(fields=["lookup"],
                                         hierarchy=True,
                                         as_json=True)
        fo = json.loads(result)

        # Inspect result
        has_empty = False
        assertTrue(isinstance(fo, dict))
        assertTrue("option" in fo)
        assertTrue(isinstance(fo["option"], list))
        assertEqual(len(fo["option"]), len(options) + 1)
        for opt in fo["option"]:
            value = opt["@value"]
            if value == "":
                has_empty = True
                self.assertFalse("$" in opt)
                continue
            else:
                value = int(value)
            assertTrue(value in options)
            assertEqual(opt["$"], options[value]["name"])
            if "parent" in options[value] and options[value]["parent"]:
                assertTrue("@parent" in opt)
                assertEqual(opt["@parent"], str(options[value]["parent"]))

        assertTrue(has_empty, msg="Empty-option missing")
Ejemplo n.º 5
0
    def testExportOptionsXMLHierarchy(self):
        """ Test Export Options (all options, XML+hierarchy) """

        assertEqual = self.assertEqual
        assertTrue = self.assertTrue

        # Configure parent-field
        db = current.db
        table = db.fotest_lookup_table
        represent = S3Represent(lookup="fotest_lookup_table")
        table.parent.requires = IS_EMPTY_OR(
            IS_ONE_OF(
                db,
                "fotest_lookup_table.id",
                represent,
            ))
        table.parent.requires = represent

        # Configure hierarchy
        s3db = current.s3db
        s3db.configure("fotest_lookup_table", hierarchy="parent")
        S3Hierarchy.dirty("fotest_lookup_table")

        # Insert a child node
        options = dict(self.records)
        child_node = {
            "name": "option3",
            "uuid": "OPTION3",
            "parent": list(options.keys())[0],
        }
        child_id = table.insert(**child_node)
        options[child_id] = child_node

        # Request options
        resource = s3db.resource("fotest_table")
        result = resource.export_options(fields=["lookup"], hierarchy=True)
        fo = etree.XML(result)

        # Inspect result
        xml = current.xml
        ATTRIBUTE = xml.ATTRIBUTE
        VALUE = ATTRIBUTE.value
        PARENT = ATTRIBUTE.parent
        UID = xml.UID

        has_empty = False
        self.assertEqual(len(fo), len(options) + 1)
        for opt in fo:
            assertEqual(opt.tag, "option")

            attr = opt.attrib
            assertTrue(VALUE in attr)

            value = attr[VALUE]
            if value == "":
                has_empty = True
                self.assertFalse(UID in attr)
                assertEqual(opt.text, None)
                continue
            else:
                value = int(value)

            assertTrue(value in options)
            assertEqual(opt.text, options[value]["name"])

            if "parent" in options[value] and options[value]["parent"]:
                assertTrue(PARENT in attr)
                assertEqual(attr[PARENT], str(options[value]["parent"]))

        assertTrue(has_empty, msg="Empty-option missing")
Ejemplo n.º 6
0
    def testWithHierarchyInfo(self):
        """ Test options lookup with foreign key constraint with hierarchy info """

        assertEqual = self.assertEqual
        assertTrue = self.assertTrue

        db = current.db
        s3db = current.s3db
        table = db.fotest_lookup_table

        # Configure parent-field
        represent = S3Represent(lookup="fotest_lookup_table")
        table.parent.requires = IS_EMPTY_OR(
            IS_ONE_OF(
                db,
                "fotest_lookup_table.id",
                represent,
            ))
        table.parent.requires = represent

        # Configure hierarchy
        s3db.configure("fotest_lookup_table", hierarchy="parent")
        S3Hierarchy.dirty("fotest_lookup_table")

        # Insert a child node
        options = dict(self.records)
        child_node = {
            "name": "option3",
            "uuid": "OPTION3",
            "parent": list(options.keys())[0],
        }
        child_id = table.insert(**child_node)
        options[child_id] = child_node

        xml = current.xml
        table = db.fotest_table
        fo = xml.get_field_options(table,
                                   "lookup",
                                   show_uids=True,
                                   hierarchy=True)

        assertTrue(isinstance(fo, etree._Element))
        assertEqual(fo.tag, "select")

        ATTRIBUTE = xml.ATTRIBUTE
        VALUE = ATTRIBUTE.value
        PARENT = ATTRIBUTE.parent
        UID = xml.UID

        has_empty = False
        self.assertEqual(len(fo), len(options) + 1)
        for opt in fo:
            assertEqual(opt.tag, "option")

            attr = opt.attrib
            assertTrue(VALUE in attr)

            value = attr[VALUE]
            if value == "":
                has_empty = True
                self.assertFalse(UID in attr)
                assertEqual(opt.text, "")
                continue
            else:
                value = int(value)

            assertTrue(UID in attr)
            assertEqual(attr[UID], options[value]["uuid"])
            assertTrue(value in options)
            assertEqual(opt.text, options[value]["name"])

            if "parent" in options[value] and options[value]["parent"]:
                assertTrue(PARENT in attr)
                assertEqual(attr[PARENT], str(options[value]["parent"]))

        assertTrue(has_empty, msg="Empty-option missing")
Ejemplo n.º 7
0
    def testExportOptionsXMLHierarchy(self):
        """ Test Export Options (all options, XML+hierarchy) """

        assertEqual = self.assertEqual
        assertTrue = self.assertTrue

        # Configure parent-field
        db = current.db
        table = db.fotest_lookup_table
        represent = S3Represent(lookup="fotest_lookup_table")
        table.parent.requires = IS_EMPTY_OR(
                                    IS_ONE_OF(db,
                                                "fotest_lookup_table.id",
                                                represent,
                                                ))
        table.parent.requires = represent

        # Configure hierarchy
        s3db = current.s3db
        s3db.configure("fotest_lookup_table", hierarchy="parent")
        S3Hierarchy.dirty("fotest_lookup_table")

        # Insert a child node
        options = dict(self.records)
        child_node = {"name": "option3",
                      "uuid": "OPTION3",
                      "parent": options.keys()[0],
                      }
        child_id = table.insert(**child_node)
        options[child_id] = child_node

        # Request options
        resource = s3db.resource("fotest_table")
        result = resource.export_options(fields=["lookup"], hierarchy=True)
        fo = etree.XML(result)

        # Inspect result
        xml = current.xml
        ATTRIBUTE = xml.ATTRIBUTE
        VALUE = ATTRIBUTE.value
        PARENT = ATTRIBUTE.parent
        UID = xml.UID

        has_empty = False
        self.assertEqual(len(fo), len(options) + 1)
        for opt in fo:
            assertEqual(opt.tag, "option")

            attr = opt.attrib
            assertTrue(VALUE in attr)

            value = attr[VALUE]
            if value == "":
                has_empty = True
                self.assertFalse(UID in attr)
                assertEqual(opt.text, None)
                continue
            else:
                value = int(value)

            assertTrue(value in options)
            assertEqual(opt.text, options[value]["name"])

            if "parent" in options[value] and options[value]["parent"]:
                assertTrue(PARENT in attr)
                assertEqual(attr[PARENT], str(options[value]["parent"]))

        assertTrue(has_empty, msg="Empty-option missing")
Ejemplo n.º 8
0
    def testWithHierarchyInfo(self):
        """ Test options lookup with foreign key constraint with hierarchy info """

        assertEqual = self.assertEqual
        assertTrue = self.assertTrue

        db = current.db
        s3db = current.s3db
        table = db.fotest_lookup_table

        # Configure parent-field
        represent = S3Represent(lookup="fotest_lookup_table")
        table.parent.requires = IS_EMPTY_OR(
                                    IS_ONE_OF(db,
                                                "fotest_lookup_table.id",
                                                represent,
                                                ))
        table.parent.requires = represent

        # Configure hierarchy
        s3db.configure("fotest_lookup_table", hierarchy="parent")
        S3Hierarchy.dirty("fotest_lookup_table")

        # Insert a child node
        options = dict(self.records)
        child_node = {"name": "option3",
                      "uuid": "OPTION3",
                      "parent": options.keys()[0],
                      }
        child_id = table.insert(**child_node)
        options[child_id] = child_node

        xml = current.xml
        table = db.fotest_table
        fo = xml.get_field_options(table, "lookup", show_uids=True, hierarchy=True)

        assertTrue(isinstance(fo, etree._Element))
        assertEqual(fo.tag, "select")

        ATTRIBUTE = xml.ATTRIBUTE
        VALUE = ATTRIBUTE.value
        PARENT = ATTRIBUTE.parent
        UID = xml.UID

        has_empty = False
        self.assertEqual(len(fo), len(options) + 1)
        for opt in fo:
            assertEqual(opt.tag, "option")

            attr = opt.attrib
            assertTrue(VALUE in attr)

            value = attr[VALUE]
            if value == "":
                has_empty = True
                self.assertFalse(UID in attr)
                assertEqual(opt.text, "")
                continue
            else:
                value = int(value)

            assertTrue(UID in attr)
            assertEqual(attr[UID], options[value]["uuid"])
            assertTrue(value in options)
            assertEqual(opt.text, options[value]["name"])

            if "parent" in options[value] and options[value]["parent"]:
                assertTrue(PARENT in attr)
                assertEqual(attr[PARENT], str(options[value]["parent"]))

        assertTrue(has_empty, msg="Empty-option missing")