def test_accounts(setup_pct):

    # test basic Account creation
    pct = setup_pct
    test_account = pct.Account(name="Test Account")
    assert test_account.name == "Test Account"
    assert test_account.get_sales_rep() is None
    assert test_account.get_market_segments() == []

    del test_account

    # test creating an account with a SalesRep
    test_account = pct.Account(name="Test Account", sales_rep="Daffy Duck")
    assert test_account.get_sales_rep() == "Daffy Duck"

    del test_account

    # in all of the below test scenarios, we do a two way valiation to make
    # sure that both Account and MarketSegment properly track each other

    # test creating an account with a SalesRep and MarketSegments
    test_ms = pct.MarketSegment(name="Test Market Segment")
    test_account = pct.Account(name="Test Account",
                               sales_rep="Daffy Duck",
                               market_segments=[test_ms])
    assert test_ms in test_account.get_market_segments()
    assert test_account in test_ms.get_accounts()

    # test adding a MarketSegment
    test_ms_2 = pct.MarketSegment(name="Test Market Segment 2")
    test_account.add_to_market_segment(test_ms_2)
    assert test_ms_2 in test_account.get_market_segments()
    assert test_account in test_ms_2.get_accounts()

    # test removing a MarketSegment
    test_account.remove_from_market_segment(test_ms)
    assert test_ms not in test_account.get_market_segments()
    assert test_account not in test_ms.get_accounts()
    assert test_ms_2 in test_account.get_market_segments()
    assert test_account in test_ms_2.get_accounts()

    del test_account
    del test_ms
    del test_ms_2

    # test setting the MarketSegment list all together
    test_ms_1 = pct.MarketSegment(name="Test Market Segment 1")
    test_ms_2 = pct.MarketSegment(name="Test Market Segment 2")
    test_ms_3 = pct.MarketSegment(name="Test Market Segment 3")

    test_account = pct.Account(name="Test Account",
                               market_segments=[test_ms_1, test_ms_2])
    test_account.set_market_segments([test_ms_2, test_ms_3])
    assert test_ms_1 not in test_account.get_market_segments()
    assert test_account not in test_ms_1.get_accounts()
    assert test_ms_2 in test_account.get_market_segments()
    assert test_account in test_ms_2.get_accounts()
    assert test_ms_3 in test_account.get_market_segments()
    assert test_account in test_ms_3.get_accounts()
def test_q1_1_a(setup_pct):
    pct = setup_pct
    test_ms_1 = pct.MarketSegment(name="ms 1")
    test_ms_2 = pct.MarketSegment(name="ms 2")
    test_account = pct.Account(name="test account",
                               market_segments=[test_ms_1, test_ms_2])
    assert test_account is not None
def test_print_tree(setup_pct, capsys):
    pct = setup_pct
    manufacturing_ms = pct.MarketSegment(name='Manufacturing')
    rd_ms = pct.MarketSegment(name='R&D')
    aero_ms = pct.MarketSegment(name='Aerospace')
    defense_ms = pct.MarketSegment(name='Defense')
    cons_goods_ms = pct.MarketSegment(name='Consumer Goods')
    account = pct.Account(name="GE",
                          sales_rep="Daniel Testperson",
                          market_segments=[manufacturing_ms, rd_ms])
    child1 = pct.ChildAccount(name='Jet Engines', parent=account)
    child1.add_to_market_segment(aero_ms)
    child2 = pct.ChildAccount(name='DoD Contracts',
                              parent=child1,
                              sales_rep="William Testperson")
    manufacturing_ms.remove_account(child2)
    child2.add_to_market_segment(defense_ms)
    child3 = pct.ChildAccount(
        name='Appliances',
        parent=account,
        sales_rep="Janet Testperson",
        market_segments=[manufacturing_ms, cons_goods_ms])
    child4 = pct.ChildAccount(name="Washing Machines", parent=child3)
    child4.remove_from_market_segment(manufacturing_ms)
    pct.print_tree(account)
    out, _ = capsys.readouterr()

    assert """> GE (Manufacturing, R&D): Daniel Testperson
--> Jet Engines (Manufacturing, R&D, Aerospace): Daniel Testperson
----> DoD Contracts (R&D, Aerospace, Defense): William Testperson
--> Appliances (Manufacturing, Consumer Goods): Janet Testperson
----> Washing Machines (Consumer Goods): Janet Testperson""" in out
def test_q1_1_d(setup_pct):
    pct = setup_pct
    test_ms_1 = pct.MarketSegment(name="ms 1")
    test_ms_2 = pct.MarketSegment(name="ms 2")
    test_account = pct.Account(name='test account',
                               market_segments=[test_ms_1, test_ms_2])
    test_ms_1.remove_account(test_account)

    assert test_ms_1.name not in test_account.get_market_segments()
def test_q1_1_c(setup_pct):
    pct = setup_pct
    test_account = pct.Account(name="test account")
    print(dir(pct))
    test_account.add_to_market_segment(pct.MarketSegment(name="new segment"))
    print(dir(pct))

    # new_segment_ms

    assert pct.new_segment_ms and pct.new_segment_ms\
        in test_account.get_market_segments()
def test_q1_1_b(setup_pct):
    pct = setup_pct
    test_ms_1 = pct.MarketSegment(name="ms 1")
    test_ms_2 = pct.MarketSegment(name="ms 2")
    test_account = pct.Account(name="test account",
                               market_segments=[test_ms_1, test_ms_2])

    market_segments = test_account.get_market_segments()

    assert isinstance(market_segments, list)
    assert market_segments == [test_ms_1, test_ms_2]
def test_q2_1(setup_pct):
    pct = setup_pct
    test_ms = pct.MarketSegment(name="test ms")
    test_acc = pct.Account(name="test acc")
    test_ms.add_account(test_acc)
    with pytest.raises(ValueError) as val_err:
        test_ms.add_account(test_acc)
    assert "test acc already associated to test ms" in str(val_err.value)

    with pytest.raises(ValueError) as val_err:
        test_acc.add_to_market_segment(test_ms)
    assert "test acc already part of test ms" in str(val_err.value)
def test_market_segments(setup_pct):

    # test basic creation of MarketSegment
    pct = setup_pct
    test_ms = pct.MarketSegment(name="Test Market Segment")
    assert test_ms.name == "Test Market Segment"
    assert test_ms.get_accounts() == []

    del test_ms

    # in all of the below test scenarios, we do a two way valiation to make
    # sure that both Account and MarketSegment properly track each other

    # Test creating a Market Segment with a list of Accounts
    test_account = pct.Account(name="Test Account")
    test_ms = pct.MarketSegment(name="Test Market Segment",
                                accounts=[test_account])
    assert test_account in test_ms.get_accounts()
    assert test_ms in test_account.get_market_segments()

    # Test adding an account via the .add_account method
    test_account_2 = pct.Account(name="Test Account 2")
    test_ms.add_account(test_account_2)
    assert test_account_2 in test_ms.get_accounts()
    assert test_ms in test_account.get_market_segments()

    # Test removing the account via the .remove_account method
    test_ms.remove_account(test_account)
    assert test_account not in test_ms.get_accounts()
    assert test_ms not in test_account.get_market_segments()
    assert test_account_2 in test_ms.get_accounts()
    assert test_ms in test_account_2.get_market_segments()

    # make sure to clean up (should define better fixtures for setup/teardown
    del test_ms
    del test_account
    del test_account_2
def test_q1_1_e(setup_pct):
    pct = setup_pct
    test_ms_1 = pct.MarketSegment(name="ms 1")
    test_ms_2 = pct.MarketSegment(name="ms 2")
    test_account_1 = pct.Account(name="acc 1")
    test_account_2 = pct.Account(name="acc 2")

    test_ms_1.add_account(test_account_1)
    test_ms_1.add_account(test_account_2)
    test_ms_2.add_account(test_account_1)
    test_ms_2.add_account(test_account_2)

    assert test_ms_1 in test_account_1.get_market_segments()
    assert test_ms_2 in test_account_1.get_market_segments()
    assert test_account_1.name in\
        [x.name for x in test_ms_1.__dict__['_accounts']]
    assert test_account_2.name in\
        [x.name for x in test_ms_1.__dict__['_accounts']]
    assert test_ms_1 in test_account_2.get_market_segments()
    assert test_ms_2 in test_account_2.get_market_segments()
    assert test_account_1.name in\
        [x.name for x in test_ms_2.__dict__['_accounts']]
    assert test_account_2.name in\
        [x.name for x in test_ms_2.__dict__['_accounts']]