コード例 #1
0
 def test_it_throws_for_unconvertable_matches(self):
     with pytest.raises(ValueError):
         cast_to_type(
             "mystr",
             "test_col",
             {
                 "StorageDescriptor": {
                     "Columns": [{
                         "Name": "test_col",
                         "Type": "int"
                     }]
                 }
             },
         )
コード例 #2
0
 def test_it_throws_for_unknown_col(self):
     with pytest.raises(ValueError):
         cast_to_type(
             "mystr",
             "doesnt_exist",
             {
                 "StorageDescriptor": {
                     "Columns": [{
                         "Name": "test_col",
                         "Type": "string"
                     }]
                 }
             },
         )
コード例 #3
0
 def test_it_throws_for_unsupported_col_types(self):
     with pytest.raises(ValueError) as e:
         cast_to_type(
             "2.56",
             "test_col",
             {
                 "StorageDescriptor": {
                     "Columns": [{
                         "Name": "test_col",
                         "Type": "decimal"
                     }]
                 }
             },
         )
     assert (e.value.args[0] ==
             "Column test_col is not a supported column type for querying")
コード例 #4
0
 def test_it_converts_supported_types_when_nested_in_struct(self):
     column_type = "struct<type:int,x:map<string,struct<a:int>>,info:struct<user_id:int,name:string>>"
     table = {
         "StorageDescriptor": {
             "Columns": [{
                 "Name": "user",
                 "Type": column_type
             }]
         }
     }
     for scenario in [
         {
             "value": "john_doe",
             "id": "user.info.name",
             "expected": "john_doe"
         },
         {
             "value": "1234567890",
             "id": "user.info.user_id",
             "expected": 1234567890
         },
         {
             "value": "1",
             "id": "user.type",
             "expected": 1
         },
     ]:
         res = cast_to_type(scenario["value"], scenario["id"], table)
         assert res == scenario["expected"]
コード例 #5
0
 def test_it_throws_for_unsupported_complex_nested_types(self):
     for scenario in [
             "array<x:int>",
             "array<struct<x:int>>",
             "struct<a:array<struct<a:int,x:int>>>",
             "array<struct<a:int,b:struct<x:int>>>",
             "struct<a:map<string,struct<x:int>>>",
             "map<string,struct<x:int>>",
     ]:
         with pytest.raises(ValueError):
             cast_to_type(
                 123,
                 "user.x",
                 {
                     "StorageDescriptor": {
                         "Columns": [{
                             "Name": "user",
                             "Type": scenario
                         }]
                     }
                 },
             )
コード例 #6
0
    def test_it_converts_supported_types(self):
        for scenario in [
            {
                "value": "m",
                "type": "char",
                "expected": "m"
            },
            {
                "value": "mystr",
                "type": "string",
                "expected": "mystr"
            },
            {
                "value": "mystr",
                "type": "varchar",
                "expected": "mystr"
            },
            {
                "value": "2",
                "type": "bigint",
                "expected": 2
            },
            {
                "value": "2",
                "type": "int",
                "expected": 2
            },
            {
                "value": "2",
                "type": "smallint",
                "expected": 2
            },
            {
                "value": "2",
                "type": "tinyint",
                "expected": 2
            },
            {
                "value": "2.23",
                "type": "double",
                "expected": 2.23
            },
            {
                "value": "2.23",
                "type": "float",
                "expected": 2.23
            },
        ]:
            res = cast_to_type(
                scenario["value"],
                "test_col",
                {
                    "StorageDescriptor": {
                        "Columns": [{
                            "Name": "test_col",
                            "Type": scenario["type"]
                        }]
                    }
                },
            )

            assert res == scenario["expected"]