def test_inheritance_w_new_data(sources): base_factory = Factory( data={ "students": { "table": """ | id | first_name | | - | - | | s1 | Buffy | | s2 | Willow | """ } }, sources=sources, ) modified_table = """ | id | first_name | last_name | | - | - | - | | s1 | Buffy | Summers | | s2 | Xander | Harris | """ composite_factory = Factory( data={"students": { "table": deepcopy(modified_table) }}, inherit_from=[base_factory], sources=sources, ) expected = markdown_to_df(modified_table) actual = composite_factory.data["students"]["dataframe"] assert_frame_equal(actual, expected)
def test_factories_stack_a_source(identifiers, sources): factory = Factory( data={ "students": { "table": """ | id | first_name | | - | - | | s1 | Buffy | | s2 | Willow | """ } }, sources=sources, ) factory.generate("TestCase") expected = markdown_to_df(""" | id | first_name | | - | - | | {s1} | Buffy | | {s2} | Willow | """.format( s1=identifiers["student"].generate(case="TestCase", named_id="s1")["id"], s2=identifiers["student"].generate(case="TestCase", named_id="s2")["id"], )) actual = sources["students"].data[expected.columns] assert_frame_equal(actual, expected)
def test_factories_stack_sources(identifiers, sources): factory = Factory( data={ "students": { "table": """ | id | organization_id | first_name | | - | - | - | | s1 | o1 | Buffy | | s2 | o1 | Willow | """ }, "organizations": { "table": """ | id | name | | - | - | | o1 | Sunnydale High | """ }, }, sources=sources, ) factory.generate("TestCase") expected_students = markdown_to_df(""" | id | organization_id | first_name | | - | - | - | | {s1} | {o1} | Buffy | | {s2} | {o1} | Willow | """.format( s1=identifiers["student"].generate(case="TestCase", named_id="s1")["id"], s2=identifiers["student"].generate(case="TestCase", named_id="s2")["id"], o1=identifiers["organization"].generate(case="TestCase", named_id="o1")["id"], )) actual_students = sources["students"].data.drop(columns=["external_id"]) expected_organizations = markdown_to_df(""" | id | name | | - | - | | {o1} | Sunnydale High | """.format(o1=identifiers["organization"].generate( case="TestCase", named_id="o1")["id"])) actual_organizations = sources["organizations"].data.drop(columns=["uuid"]) assert_frame_equal(actual_students, expected_students) assert_frame_equal(actual_organizations, expected_organizations)
def test_inheritance_w_multiple_composite_sources(sources): base_factory = Factory( data={ "students": { "table": """ | id | first_name | | - | - | | s1 | Buffy | | s2 | Willow | """ } }, sources=sources, ) modified_students_table = """ | id | first_name | last_name | | - | - | - | | s1 | Buffy | Summers | | s2 | Xander | Harris | """ new_organizations_table = """ | id | name | | - | - | | o1 | Sunnydale High | """ composite_factory = Factory( data={ "students": { "table": deepcopy(modified_students_table) }, "organizations": { "table": deepcopy(new_organizations_table) }, }, inherit_from=[base_factory], sources=sources, ) expected = markdown_to_df(modified_students_table) actual = composite_factory.data["students"]["dataframe"] assert_frame_equal(actual, expected) expected = markdown_to_df(new_organizations_table) actual = composite_factory.data["organizations"]["dataframe"] assert_frame_equal(actual, expected)
def test_inheritance_defaults_are_overridden(identifiers, sources): base_factory = Factory( data={ "students": { "table": """ | id | | - | | s1 | """, "values": { "first_name": "Bob" }, } }, sources=sources, ) composite_factory = Factory( data={ "students": { "table": """ | id | | - | | s1 | """, "values": { "last_name": "Loblaw" }, } }, inherit_from=[base_factory], sources=sources, ) composite_factory.generate("TestCase") expected = markdown_to_df(""" | id | first_name | last_name | | - | - | - | | {s1} | Bob | Loblaw | """.format(s1=identifiers["student"].generate(case="TestCase", named_id="s1")["id"])) actual = sources["students"].data.drop( columns=["external_id", "organization_id"]) assert_frame_equal(actual, expected)
def test_scenario_case_factories_can_override(identifiers, sources, student_factory, organization_factory): scenario = Scenario( cases={ "StudentOrg": Case(factory=Factory( sources=sources, inherit_from=[student_factory, organization_factory], data={ "students": { "table": """ | id | organization_id | first_name | | - | - | - | | s1 | o1 | Bill | | s2 | o1 | Ted | """ } }, )) }) scenario.generate() expected = markdown_to_df(""" | id | organization_id | first_name | | - | - | - | | {s1} | {o1} | Bill | | {s2} | {o1} | Ted | """.format( s1=identifiers["student"].generate(case=scenario.cases["StudentOrg"], named_id="s1")["id"], s2=identifiers["student"].generate(case=scenario.cases["StudentOrg"], named_id="s2")["id"], o1=identifiers["organization"].generate( case=scenario.cases["StudentOrg"], named_id="o1")["id"], )) actual = sources["students"].data assert_frame_equal(actual, expected) expected = markdown_to_df(""" | id | name | | - | - | | {o1} | San Dimas High | | {o2} | Alaska Military Academy | """.format( o1=identifiers["organization"].generate( case=scenario.cases["StudentOrg"], named_id="o1")["id"], o2=identifiers["organization"].generate( case=scenario.cases["StudentOrg"], named_id="o2")["id"], )) actual = sources["organizations"].data assert_frame_equal(actual, expected)
def test_scenarios_stack_case_data(identifiers, sources, student_factory): scenario = Scenario( cases={ "SimpleStudent": Case(factory=Factory(sources=sources, inherit_from=[student_factory])), "AltStudent": Case(factory=Factory( sources=sources, inherit_from=[student_factory], data={ "students": { "table": """ | id | first_name | | - | - | | s1 | Napoleon | """ } }, )), }) scenario.generate() expected = markdown_to_df(""" | id | first_name | | - | - | | {s1} | Bill | | {s2} | Ted | | {as1} | Napoleon | """.format( s1=identifiers["student"].generate( case=scenario.cases["SimpleStudent"], named_id="s1")["id"], s2=identifiers["student"].generate( case=scenario.cases["SimpleStudent"], named_id="s2")["id"], as1=identifiers["student"].generate(case=scenario.cases["AltStudent"], named_id="s1")["id"], )) actual = sources["students"].data.drop(columns="organization_id") assert_frame_equal(actual, expected)
def test_scenarios_generate_case_data_over_multiple_cases( identifiers, sources, student_factory, organization_factory): scenario = Scenario( cases={ "SimpleStudent": Case(factory=Factory(sources=sources, inherit_from=[student_factory])), "SimpleOrganization": Case(factory=Factory(sources=sources, inherit_from=[organization_factory])), }) scenario.generate() expected = markdown_to_df(""" | id | first_name | | - | - | | {s1} | Bill | | {s2} | Ted | """.format( s1=identifiers["student"].generate( case=scenario.cases["SimpleStudent"], named_id="s1")["id"], s2=identifiers["student"].generate( case=scenario.cases["SimpleStudent"], named_id="s2")["id"], )) actual = sources["students"].data.drop(columns="organization_id") assert_frame_equal(actual, expected) expected = markdown_to_df(""" | id | name | | - | - | | {o1} | San Dimas High | | {o2} | Alaska Military Academy | """.format( o1=identifiers["organization"].generate( case=scenario.cases["SimpleOrganization"], named_id="o1")["id"], o2=identifiers["organization"].generate( case=scenario.cases["SimpleOrganization"], named_id="o2")["id"], )) actual = sources["organizations"].data assert_frame_equal(actual, expected)
def test_inheritance_wo_new_data(sources): base_factory = Factory( data={ "students": { "table": """ | id | first_name | | - | - | | s1 | Buffy | | s2 | Willow | """ } }, sources=sources, ) composite_factory = Factory(inherit_from=[base_factory], sources=sources) expected = base_factory.data["students"]["dataframe"] actual = composite_factory.data["students"]["dataframe"] assert_frame_equal(actual, expected)
def test_multiple_inheritance(sources): base1_factory = Factory( data={ "students": { "table": """ | id | first_name | | - | - | | s1 | Buffy | | s2 | Willow | """ } }, sources=sources, ) base2_factory = Factory( data={ "organizations": { "table": """ | id | name | | - | - | | o1 | Sunnydale High | """ } }, sources=sources, ) composite_factory = Factory(inherit_from=[base1_factory, base2_factory], sources=sources) expected = base1_factory.data["students"]["dataframe"] actual = composite_factory.data["students"]["dataframe"] assert_frame_equal(actual, expected) expected = base2_factory.data["organizations"]["dataframe"] actual = composite_factory.data["organizations"]["dataframe"] assert_frame_equal(actual, expected)
def organization_factory(sources): return Factory( data={ "organizations": { "table": """ | id | name | | - | - | | o1 | San Dimas High | | o2 | Alaska Military Academy | """ } }, sources=sources, )
def student_factory(sources): return Factory( data={ "students": { "table": """ | id | first_name | | - | - | | s1 | Bill | | s2 | Ted | """ } }, sources=sources, )
def test_raises_when_markdown_is_missing_header_sep(sources): with pytest.raises(BadMarkdownTableError): Factory( data={ "students": { "table": """ | id | first_name | | s1 | Buffy | | s2 | Willow | """ } }, sources=sources, )
def test_raises_when_markdown_causes_pandas_failures(sources): with pytest.raises(BadMarkdownTableError): Factory( data={ "students": { "table": """ | id first_name | | - | - | | s1 | | s2 | Willow | """ } }, sources=sources, )
def test_merge_data(): data1 = { "students": { "table": "a", "dataframe": "a", "values": { "value_a1": "a", "X": "a" }, } } data2 = { "students": { "table": "b", "dataframe": "b", "values": { "value_b1": "b", "X": "b" }, }, "organizations": { "table": "b", "dataframe": "b" }, } actual = Factory.merge_data(data1, data2) expected = { "students": { "table": "b", "dataframe": "b", "values": { "value_a1": "a", "X": "b", "value_b1": "b" }, }, "organizations": { "table": "b", "dataframe": "b" }, } assert actual == expected
def test_raises_when_markdown_is_missing(sources): # Common mistake: use "data" instead of "table", resulting in None being in "table" with pytest.raises(BadMarkdownTableError): Factory(data={"students": {"table": None}}, sources=sources)