def test_to_numpy_dtype_integer(self): mock_name = Mock(name='name') mock_statistics = Mock(name='statistics') mock_representations = Mock(name='representations') mock_field_type = 'integer' expectations = { (-100, 100): np.int8, (0, 240): np.uint8, (-10000, 10000): np.int16, (500, 40000): np.uint16, (-200000000, 200000000): np.int32, (25, 4000000000): np.uint32, (-9000000000000000000, 2000000000): np.int64, (25, 10000000000000000000): np.uint64, (25, 1000000000000000000000000000): np.float128, (None, None): np.int64 } for (mock_min, mock_max), expected_pandas_type in expectations.items(): mock_constraints = Mock(name='constraints') mock_constraints.min_ = mock_min mock_constraints.max_ = mock_max field = RecordsSchemaField(name=mock_name, field_type=mock_field_type, constraints=mock_constraints, statistics=mock_statistics, representations=mock_representations) out = field.to_numpy_dtype() self.assertEqual(out, expected_pandas_type, f"min={mock_min}, max={mock_max}")
def test_to_numpy_dtype_decimal_float(self): mock_name = Mock(name='name') mock_statistics = Mock(name='statistics') mock_representations = Mock(name='representations') mock_field_type = 'decimal' expectations = { (8, 4): np.float16, (20, 10): np.float32, (40, 20): np.float64, (80, 64): np.float128, (500, 250): np.float128, (None, None): np.float64, } for (fp_total_bits, fp_significand_bits), expected_pandas_type in expectations.items(): mock_constraints = Mock(name='constraints') mock_constraints.fixed_precision = None mock_constraints.fixed_scale = None mock_constraints.fp_total_bits = fp_total_bits mock_constraints.fp_significand_bits = fp_significand_bits field = RecordsSchemaField(name=mock_name, field_type=mock_field_type, constraints=mock_constraints, statistics=mock_statistics, representations=mock_representations) out = field.to_numpy_dtype() self.assertEqual(out, expected_pandas_type)
def test_to_numpy_dtype_decimal_no_constraints(self): mock_name = Mock(name='name') mock_statistics = Mock(name='statistics') mock_representations = Mock(name='representations') mock_field_type = 'decimal' field = RecordsSchemaField(name=mock_name, field_type=mock_field_type, constraints=None, statistics=mock_statistics, representations=mock_representations) out = field.to_numpy_dtype() self.assertEqual(out, np.float64)
def test_to_numpy_dtype_fixed_precision_(self): mock_name = Mock(name='name') mock_statistics = Mock(name='statistics') mock_representations = Mock(name='representations') mock_constraints = Mock(name='constraints') mock_constraints.fixed_precision = 1 mock_constraints.fixed_scale = 1 mock_field_type = 'decimal' field = RecordsSchemaField(name=mock_name, field_type=mock_field_type, constraints=mock_constraints, statistics=mock_statistics, representations=mock_representations) out = field.to_numpy_dtype() self.assertEqual(out, np.float64)
def test_to_numpy_dtype_misc(self): mock_name = Mock(name='name') mock_constraints = Mock(name='constraints') mock_statistics = Mock(name='statistics') mock_representations = Mock(name='representations') expectations = { 'boolean': np.bool_, 'string': np.object_, 'date': np.object_, 'datetime': 'datetime64[ns]', 'datetimetz': 'datetime64[ns, UTC]', 'time': np.object_, } for field_type, expected_pandas_type in expectations.items(): field = RecordsSchemaField(name=mock_name, field_type=field_type, constraints=mock_constraints, statistics=mock_statistics, representations=mock_representations) out = field.to_numpy_dtype() self.assertEqual(out, expected_pandas_type)