Commits

Mark Roddy committed 9d3d393

Added new assert method assertSchema() for making assertions about the schema of a relation.

Comments (0)

Files changed (2)

squealer/pigtest.py

         """
         self._proxy.override(alias, query)
 
+    def assertSchema(self, alias, schema_string):
+        """
+        Assert that the specified relation has the supplied schema.
+        schema_string should be a valid Pig schema notiation.
+        """
+        remove_whitespace = lambda s: ''.join(s.split())
+        # Remove all white space
+        schema_string = remove_whitespace(schema_string)
+        if not schema_string.startswith('('):
+            schema_string = '(' + schema_string
+        if not schema_string.endswith(')'):
+            schema_string += ')'
+        actual_schema = self._proxy.schemaFor(alias)
+        actual_schema = remove_whitespace(actual_schema)
+        self.assertEqual(actual_schema, schema_string)
+    
     def assertRelationEquals(self, alias, expected, ignore_ordering = True):
         """Assert that the specified alias has the expected set of records"""
         actual = self.relation(alias)

tests/test_pigtest.py

         test = some_test('testOverride')
         test.testOverride()
 
+    def testAssertSchema_AreEqual(self):
+        class some_test(PigTest):
+            Parameters = {
+                "input" : self.INPUT_FILE,
+                "output" : "top_3_queries",
+                }
+            PigScript = self.PIG_SCRIPT
+            def testAssertSchema_AreEqual(self):
+                self.assertSchema('queries_limit', 'query:chararray,count:long')
+        test = some_test('testAssertSchema_AreEqual')
+        test.testAssertSchema_AreEqual()
+
+    def testAssertSchema_NotEqual(self):
+        class some_test(PigTest):
+            Parameters = {
+                "input" : self.INPUT_FILE,
+                "output" : "top_3_queries",
+                }
+            PigScript = self.PIG_SCRIPT
+            def testAssertSchema_NotEqual(self):
+                self.assertSchema('queries_limit', 'query:chararray,count:long,notes:chararray')
+        test = some_test('testAssertSchema_NotEqual')
+        self.assertRaises(self.failureException, test.testAssertSchema_NotEqual)
+
+    def testAssertSchema_AreEqual_Whitespace_Ignored(self):
+        class some_test(PigTest):
+            Parameters = {
+                "input" : self.INPUT_FILE,
+                "output" : "top_3_queries",
+                }
+            PigScript = self.PIG_SCRIPT
+            def testAssertSchema_AreEqual_Whitespace_Ignored(self):
+                self.assertSchema('queries_limit', ' query:  chararray,  count:long ')
+        test = some_test('testAssertSchema_AreEqual_Whitespace_Ignored')
+        test.testAssertSchema_AreEqual_Whitespace_Ignored()
+
+
 class TestPigFloatingPointTest(unittest.TestCase):
 
     PIG_SCRIPT = "tests/pig-scripts/top_queries_floating_point.pig"