def test_dynamic_with_null_first_row(value): df = pd.DataFrame({'a': [None, value]}) model = PandasToRecordsTransformer(df, "MyRecord", optional_fields=['a']) records = model.from_pandas() assert pd.isna(records[0].a) # pd.isna(None) == true assert records[1].a == value
def test_one_column_all_nulls(): df = pd.DataFrame({ 'non_empty_col': [1, 2], 'empty_col': [None, None] }) with pytest.raises(ValueError) as exception_info: model = PandasToRecordsTransformer(df, "MyRecord", optional_fields=['a']) model.from_pandas() assert "Invalid type for field 'empty_col'" in str(exception_info.value)
def test_dynamic_defines_key_fields(pandas_data, method_name): model = PandasToRecordsTransformer(pandas_data, "MyRecord", key_fields={'key_field'}) from_pandas_method = getattr(model, method_name) record = list(from_pandas_method(pandas_data))[0] assert record.key_fields == {'key_field'}
def test_dynamic_creates_from_original_by_default(pandas_data, method_name): model = PandasToRecordsTransformer(pandas_data, "MyRecord", key_fields={'key_field'}) from_pandas_method = getattr(model, method_name) record = list(from_pandas_method())[0] assert not record.include_key
def test_dynamic_defined_correct_iter(pandas_data, method_name, expected_type): model = PandasToRecordsTransformer(pandas_data, "MyRecord", key_fields={'key_field'}) from_pandas_method = getattr(model, method_name) result = from_pandas_method(pandas_data) assert isinstance(result, expected_type)
def test_dynamic_key_included_false_as_default(pandas_data, method_name): model = PandasToRecordsTransformer(pandas_data, "MyRecord", key_fields={'key_field'}) from_pandas_method = getattr(model, method_name) record = list(from_pandas_method(pandas_data))[0] assert not record.include_key
def test_dynamic_model_creates_creator_from_dataframe(model_creator_class): df = pd.DataFrame({'a': [1, 2], 'b': ["bla", "alb"]}) params = df, ANY, ANY, ANY, ANY PandasToRecordsTransformer(*params) params_to_call = *params, KafkaRecord called_df, *_ = called_args(model_creator_class, len(params_to_call)) assert_frame_equal(df, called_df)
def test_dynamic_model_creates_pandas_model_creator(model_creator_class): model_name = 'TestModel' fields_defaults = {'field1': 10, 'field2': "aaa"} types_defaults = {int: 12} optional_fields = ['field4'] params = ANY, model_name, fields_defaults, types_defaults, optional_fields PandasToRecordsTransformer(*params) params_to_call = *params, ANY model_creator_class.assert_called_with(*params_to_call)