def test_author_sort(self):
        """Attempts to sort a dataset by the author attribute. This
        data must be collected beforehand, but that specific
        functionality is not being tested here.

        The sorting occurs in both lexicographical and reverse
        lexicographical order. The sorting should function should no
        author attribute exist.
        """

        # Get the JSON metadata
        with open(os.path.join(test_path, "sample_files", "sampleMeta.json"), "r") as f:
            data = json.loads(f.read())

        # Sort by author in lexicographical order.
        # Verification is still by title, but that is for simplicity.
        util.sort_metadata(2, data, False)
        self.assertTrue(
            data[0]["title"] == "Subtle Knife",
            "Sorted data did not have Subtle Knife in position 4",
        )
        self.assertTrue(
            data[1]["title"] == "GOLDFINGER (Golden Ratio)",
            "Sorted data did not have GOLDFINGER in position 0",
        )
        self.assertTrue(
            data[2]["title"] == "Upward",
            "Sorted data did not have Upward in position 1",
        )
        self.assertTrue(
            data[3]["title"] == "LOVELESS",
            "Sorted data did not have LOVELESS in position 2",
        )
        self.assertTrue(
            data[4]["title"] == "Dream Bender",
            "Sorted data did not have Dream Bender in position 3",
        )

        # Sort by author in reverse lexicographical order.
        util.sort_metadata(2, data, True)
        self.assertTrue(
            data[0]["title"] == "Dream Bender",
            "Sorted data did not have Dream Bender in position 1",
        )
        self.assertTrue(
            data[1]["title"] == "LOVELESS",
            "Sorted data did not have LOVELESS in position 2",
        )
        self.assertTrue(
            data[2]["title"] == "Upward",
            "Sorted data did not have Upward in position 3",
        )
        self.assertTrue(
            data[3]["title"] == "GOLDFINGER (Golden Ratio)",
            "Sorted data did not have GOLDFINGER in position 4",
        )
        self.assertTrue(
            data[4]["title"] == "Subtle Knife",
            "Sorted data did not have Subtle Knife in position 0",
        )
Exemple #2
0
    def test_sort_edge_cases(self):
        """ Test the edge cases of the sorting method should it
        encounter unexpected input.
        """

        # Try to break the method.
        exc = errors.SortingError

        self.assertTrue(
            util.sort_metadata(None, None, None) is None,
            "Expected None but got data in return.")
        self.assertRaises(exc, util.sort_metadata, -1, [{"Hi"}], False)
        self.assertTrue(
            util.sort_metadata(3, None, True) is None,
            "Expected None but got data in return.")
        self.assertTrue(
            util.sort_metadata(4, [{"Hello"}], None) is None,
            "Expected None but got data in return.")
        self.assertRaises(exc, util.sort_metadata, 5, "Nope", True)