feat(auth): Pluggable AuthService with abstract base class by ogabrielluiz · Pull Request #10702 · langflow-ai/langflow (original) (raw)

import math

imports

import pytest from langflow.agentic.mcp.support import replace_none_and_null_with_empty_str

unit tests

-------------------- Basic Test Cases --------------------

def test_none_and_null_string_replacement(): # Test that None and 'null' (case-insensitive) are replaced data = [ {"a": None, "b": "null", "c": "NuLl", "d": "something"}, {"a": "NULL", "b": "not null", "c": None, "d": 123} ] expected = [ {"a": "Not available", "b": "Not available", "c": "Not available", "d": "something"}, {"a": "Not available", "b": "not null", "c": "Not available", "d": 123} ] codeflash_output = replace_none_and_null_with_empty_str(data)

def test_nan_and_infinity_string_replacement(): # Test that 'NaN', 'Infinity', '-Infinity' strings are replaced data = [ {"x": "NaN", "y": "Infinity", "z": "-Infinity"}, {"x": "nan", "y": " infinity ", "z": " -infinity "} ] expected = [ {"x": "Not available", "y": "Not available", "z": "Not available"}, {"x": "Not available", "y": "Not available", "z": "Not available"} ] codeflash_output = replace_none_and_null_with_empty_str(data)

def test_float_nan_and_regular_numbers(): # Test that float('nan') is replaced, but regular floats are not data = [ {"x": float('nan'), "y": 3.14, "z": 0.0}, {"x": -1.23, "y": 42.0} ] expected = [ {"x": "Not available", "y": 3.14, "z": 0.0}, {"x": -1.23, "y": 42.0} ] codeflash_output = replace_none_and_null_with_empty_str(data)

def test_no_replacement_needed(): # Test that normal values are untouched data = [{"a": 1, "b": "hello", "c": False}] expected = [{"a": 1, "b": "hello", "c": False}] codeflash_output = replace_none_and_null_with_empty_str(data)

-------------------- Edge Test Cases --------------------

def test_required_fields_missing(): # Test that missing required fields are added with "Not available" data = [{"a": 1}, {"b": 2}] required_fields = ["a", "b", "c"] expected = [ {"a": 1, "b": "Not available", "c": "Not available"}, {"a": "Not available", "b": 2, "c": "Not available"} ] codeflash_output = replace_none_and_null_with_empty_str(data, required_fields)

def test_required_fields_present(): # Test that required fields are not overwritten if present data = [{"a": None, "b": 2, "c": 3}] required_fields = ["a", "b", "c"] expected = [{"a": "Not available", "b": 2, "c": 3}] codeflash_output = replace_none_and_null_with_empty_str(data, required_fields)

def test_empty_data_list(): # Test empty input list codeflash_output = replace_none_and_null_with_empty_str([])

def test_non_dict_elements(): # Test that non-dict elements are returned as-is data = [{"a": None}, 123, "hello", None] expected = [{"a": "Not available"}, 123, "hello", None] codeflash_output = replace_none_and_null_with_empty_str(data)

def test_dict_with_non_string_keys(): # Test dicts with non-string keys data = [{1: None, 2.5: "null", (1,2): "no"}] expected = [{1: "Not available", 2.5: "Not available", (1,2): "no"}] codeflash_output = replace_none_and_null_with_empty_str(data)

def test_dict_with_nested_dict(): # Test that nested dicts are not recursively processed nested = {"x": None} data = [{"a": nested}] expected = [{"a": nested}] codeflash_output = replace_none_and_null_with_empty_str(data)

def test_custom_object_with_isnat(): # Test object with isnat attribute True/False class Dummy: def init(self, isnat): self.isnat = isnat data = [{"x": Dummy(True)}, {"x": Dummy(False)}] codeflash_output = replace_none_and_null_with_empty_str(data); result = codeflash_output

def test_required_fields_empty_list(): # Test required_fields as empty list (should not add any fields) data = [{"a": None}] expected = [{"a": "Not available"}] codeflash_output = replace_none_and_null_with_empty_str(data, required_fields=[])

def test_required_fields_none(): # Test required_fields as None (should not add any fields) data = [{"a": None}] expected = [{"a": "Not available"}] codeflash_output = replace_none_and_null_with_empty_str(data, required_fields=None)

def test_string_with_spaces(): # Test that strings with spaces are stripped before checking for null/nan/infinity data = [{"a": " null ", "b": " NaN ", "c": " Infinity ", "d": " -Infinity "}] expected = [{"a": "Not available", "b": "Not available", "c": "Not available", "d": "Not available"}] codeflash_output = replace_none_and_null_with_empty_str(data)

def test_case_insensitive_matching(): # Test that matching is case-insensitive data = [{"a": "NULL", "b": "nAn", "c": "INFINITY", "d": "-INFINITY"}] expected = [{"a": "Not available", "b": "Not available", "c": "Not available", "d": "Not available"}] codeflash_output = replace_none_and_null_with_empty_str(data)

-------------------- Large Scale Test Cases --------------------

def test_large_list_of_dicts(): # Test with a large list of dicts n = 500 data = [{"a": None if i % 2 == 0 else i, "b": "null" if i % 3 == 0 else i, "c": float('nan') if i % 5 == 0 else i} for i in range(n)] codeflash_output = replace_none_and_null_with_empty_str(data); result = codeflash_output for i, d in enumerate(result): # "a" should be "Not available" if i even, else i if i % 2 == 0: pass else: pass # "b" should be "Not available" if i divisible by 3, else i if i % 3 == 0: pass else: pass # "c" should be "Not available" if i divisible by 5, else i if i % 5 == 0: pass else: pass

def test_large_required_fields(): # Test with many required fields n = 100 data = [{} for _ in range(10)] required_fields = [f"f{i}" for i in range(n)] codeflash_output = replace_none_and_null_with_empty_str(data, required_fields); result = codeflash_output for d in result: for f in required_fields: pass

def test_large_dict(): # Test with a single dict with many keys n = 500 data = [{str(i): None if i % 2 == 0 else "ok" for i in range(n)}] codeflash_output = replace_none_and_null_with_empty_str(data); result = codeflash_output for i in range(n): key = str(i) if i % 2 == 0: pass else: pass

codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

#------------------------------------------------ import math

imports

import pytest from langflow.agentic.mcp.support import replace_none_and_null_with_empty_str

unit tests

1. BASIC TEST CASES

def test_basic_none_and_null_replacement(): # Test replacing None and 'null' in various cases data = [ {"a": None, "b": "null", "c": "NULL", "d": "Null", "e": "NuLl"}, {"a": 1, "b": "hello", "c": 0, "d": False, "e": ""}, ] expected = [ {"a": "Not available", "b": "Not available", "c": "Not available", "d": "Not available", "e": "Not available"}, {"a": 1, "b": "hello", "c": 0, "d": False, "e": ""}, ] codeflash_output = replace_none_and_null_with_empty_str(data)

def test_basic_nan_and_infinity_strings(): # Test replacing 'NaN', 'Infinity', '-Infinity' strings data = [ {"a": "NaN", "b": "Infinity", "c": "-Infinity", "d": "nan", "e": " infinity "}, {"a": "foo", "b": "bar", "c": "baz", "d": "qux", "e": "quux"}, ] expected = [ {"a": "Not available", "b": "Not available", "c": "Not available", "d": "Not available", "e": "Not available"}, {"a": "foo", "b": "bar", "c": "baz", "d": "qux", "e": "quux"}, ] codeflash_output = replace_none_and_null_with_empty_str(data)

def test_basic_float_nan(): # Test replacing float('nan') data = [ {"a": float('nan'), "b": 1.0, "c": 0.0, "d": -1.0}, ] expected = [ {"a": "Not available", "b": 1.0, "c": 0.0, "d": -1.0}, ] codeflash_output = replace_none_and_null_with_empty_str(data)

def test_basic_no_replacements(): # Test when nothing should be replaced data = [ {"a": 1, "b": "test", "c": 2.5}, ] expected = [ {"a": 1, "b": "test", "c": 2.5}, ] codeflash_output = replace_none_and_null_with_empty_str(data)

def test_basic_required_fields(): # Test required_fields adds missing keys with "Not available" data = [ {"a": 1, "b": 2}, {"b": 3}, ] required_fields = ["a", "b", "c"] expected = [ {"a": 1, "b": 2, "c": "Not available"}, {"b": 3, "a": "Not available", "c": "Not available"}, ] codeflash_output = replace_none_and_null_with_empty_str(data, required_fields)

2. EDGE TEST CASES

def test_edge_empty_list(): # Test empty input list data = [] expected = [] codeflash_output = replace_none_and_null_with_empty_str(data)

def test_edge_empty_dict(): # Test list with empty dict data = [{}] expected = [{}] codeflash_output = replace_none_and_null_with_empty_str(data)

def test_edge_non_dict_input(): # Test list containing non-dict elements data = [{"a": None}, 42, "string", None, [1,2,3]] expected = [{"a": "Not available"}, 42, "string", None, [1,2,3]] codeflash_output = replace_none_and_null_with_empty_str(data)

def test_edge_strip_spaces(): # Test that strings with leading/trailing whitespace are handled data = [ {"a": " null ", "b": " NaN", "c": "Infinity ", "d": " -Infinity "}, ] expected = [ {"a": "Not available", "b": "Not available", "c": "Not available", "d": "Not available"}, ] codeflash_output = replace_none_and_null_with_empty_str(data)

def test_edge_nested_dicts(): # Test that nested dicts are not recursively processed data = [ {"a": {"b": None, "c": "null"}, "d": None} ] expected = [ {"a": {"b": None, "c": "null"}, "d": "Not available"} ] codeflash_output = replace_none_and_null_with_empty_str(data)

def test_edge_isnat_object(): # Test object with isnat attribute set to True class FakeNat: isnat = True data = [{"a": FakeNat()}] expected = [{"a": "Not available"}] codeflash_output = replace_none_and_null_with_empty_str(data)

def test_edge_isnat_false(): # Test object with isnat attribute set to False class FakeNotNat: isnat = False data = [{"a": FakeNotNat()}] expected = [{"a": FakeNotNat()}] codeflash_output = replace_none_and_null_with_empty_str(data); result = codeflash_output

def test_edge_required_fields_none(): # Test required_fields is None data = [{"a": None}] expected = [{"a": "Not available"}] codeflash_output = replace_none_and_null_with_empty_str(data, None)

def test_edge_required_fields_empty(): # Test required_fields is empty list data = [{"a": None}] expected = [{"a": "Not available"}] codeflash_output = replace_none_and_null_with_empty_str(data, [])

def test_edge_missing_all_required_fields(): # Test all required fields missing data = [{}] required_fields = ["a", "b"] expected = [{"a": "Not available", "b": "Not available"}] codeflash_output = replace_none_and_null_with_empty_str(data, required_fields)

def test_edge_required_field_already_not_available(): # Test required_fields where key is present but value is None data = [{"a": None}] required_fields = ["a"] expected = [{"a": "Not available"}] codeflash_output = replace_none_and_null_with_empty_str(data, required_fields)

def test_edge_nan_string_with_spaces(): # Test ' nan ' with spaces is replaced data = [{"a": " nan "}] expected = [{"a": "Not available"}] codeflash_output = replace_none_and_null_with_empty_str(data)

def test_edge_zero_and_false(): # Test that 0 and False are not replaced data = [{"a": 0, "b": False}] expected = [{"a": 0, "b": False}] codeflash_output = replace_none_and_null_with_empty_str(data)

def test_edge_multiple_types(): # Test a mix of types data = [{"a": None, "b": "null", "c": float('nan'), "d": 0, "e": "", "f": False}] expected = [{"a": "Not available", "b": "Not available", "c": "Not available", "d": 0, "e": "", "f": False}] codeflash_output = replace_none_and_null_with_empty_str(data)

def test_edge_dict_with_extra_keys(): # Test dict with extra keys not in required_fields data = [{"a": 1, "b": 2, "c": 3}] required_fields = ["a", "b"] expected = [{"a": 1, "b": 2, "c": 3}] codeflash_output = replace_none_and_null_with_empty_str(data, required_fields)

3. LARGE SCALE TEST CASES

def test_large_scale_many_dicts(): # Test with a large list of dicts data = [{"a": None if i % 2 == 0 else i, "b": "null" if i % 3 == 0 else str(i)} for i in range(500)] expected = [ { "a": "Not available" if i % 2 == 0 else i, "b": "Not available" if i % 3 == 0 else str(i) } for i in range(500) ] codeflash_output = replace_none_and_null_with_empty_str(data)

def test_large_scale_many_fields(): # Test with dicts with many fields fields = [f"f{i}" for i in range(100)] data = [ {field: None if i % 2 == 0 else "null" if i % 3 == 0 else i for i, field in enumerate(fields)} for _ in range(10) ] expected = [ {field: "Not available" if i % 2 == 0 or i % 3 == 0 else i for i, field in enumerate(fields)} for _ in range(10) ] codeflash_output = replace_none_and_null_with_empty_str(data)

def test_large_scale_required_fields(): # Test with required_fields on large data data = [{"a": None}, {"b": 2}] required_fields = [f"f{i}" for i in range(50)] + ["a", "b"] expected = [ dict({"a": "Not available"}, **{k: "Not available" for k in required_fields if k != "a"}), dict({"b": 2, "a": "Not available"}, **{k: "Not available" for k in required_fields if k not in ("a", "b")}), ] codeflash_output = replace_none_and_null_with_empty_str(data, required_fields); result = codeflash_output # Check all required fields present and correct values for res, exp in zip(result, expected): for k in required_fields: pass

def test_large_scale_non_dicts(): # Test with a large list of non-dict elements data = [i for i in range(200)] + ["null", None, float('nan')] expected = [i for i in range(200)] + ["null", None, float('nan')] codeflash_output = replace_none_and_null_with_empty_str(data)

def test_large_scale_mixed(): # Mix of dicts and non-dicts data = [{"a": None}] * 100 + [None] * 100 expected = [{"a": "Not available"}] * 100 + [None] * 100 codeflash_output = replace_none_and_null_with_empty_str(data)

codeflash_output is used to check that the output of the original code is the same as that of the optimized code.