def test_set_entry_show_locked_entry(): """Given an Encyclopaedia with 10 unlocked entries and 5 locked entries, And show_locked_entry is true, When I set the 10th unlocked entry to be the active entry, Then the entry should be marked as viewed, And the entry should be the active entry, And the Encyclopaedia's current_position should be 14 """ enc = Encyclopaedia(show_locked_buttons=False, show_locked_entry=True) for x in range(0, 5): EncEntry(parent=enc, name="Test Name {}".format(x + 1), text=["Test Text"], locked=False) for x in range(5, 10): EncEntry(parent=enc, name="Test Name {}".format(x + 1), text=["Test Text"], locked=True) for x in range(10, 15): e = EncEntry(parent=enc, name="Test Name {}".format(x + 1), text=["Test Text"], locked=False) # Use the last unlocked Entry created for the test. enc.SetEntry(e)() assert e == enc.active assert e.viewed assert 14 == enc.current_position
def test_next_page(): enc = Encyclopaedia() e = EncEntry( parent=enc, name="Test Name", text=["Test Text"], ) ee = EncEntry( parent=e, name="Sub1", text=["Test Text"], ) eee = EncEntry( parent=e, name="Sub2", text=["Test Text"], ) enc.SetEntry(e)() assert e == enc.active assert enc.sub_current_position == 1 enc.NextPage()() assert enc.sub_current_position == 2 assert e.current_page == ee
def test_filtering(): enc = Encyclopaedia() apple = EncEntry( parent=enc, name="Apple", text=["Test Text"], subject="Fruits" ) banana = EncEntry( parent=enc, name="Banana", text=["Test Text"], subject="Fruits" ) cantaloupe = EncEntry( parent=enc, name="Cantaloupe", text=["Test Text"], subject="Fruits" ) cucumber = EncEntry( parent=enc, name="Cucumber", text=["Test Text"], subject="Vegetables" ) enc.FilterBySubject("Fruits")() assert "Fruits" == enc.filtering assert [apple, banana, cantaloupe] == enc.current_entries
def test_set_entry(): """Test Actions through their implementation in Encyclopaedia.""" enc = Encyclopaedia() for x in range(5): e = EncEntry( parent=enc, name="Test Name", text=["Test Text"], locked=False ) for x in range(5): EncEntry( parent=enc, name="Test Name", text=["Test Text"], locked=True ) # Use the last unlocked Entry created for the test. enc.SetEntry(e)() assert e == enc.active assert 4 == enc.current_position
def test_filter_by_subject(): """Test Actions through their implementation in Encyclopaedia.""" enc = Encyclopaedia() expected_entries = [] for x in range(5): e = EncEntry( parent=enc, name="Test Name", text=["Test Text"], subject="Robots" ) expected_entries.append(e) for x in range(5): EncEntry( parent=enc, name="Test Name", text=["Test Text"], subject="Humans" ) enc.FilterBySubject("Robots")() assert "Robots" == enc.filtering assert expected_entries == enc.filtered_entries
def test_toggle_show_locked_buttons_reverse_sorting(): """Ensure reverse sorting isn't broken when toggling show_locked_buttons.""" enc = Encyclopaedia(sorting_mode=Encyclopaedia.SORT_REVERSE_ALPHABETICAL) entry_names = ["Apple", "Carrot", "Deer", "Eel", "Fajita"] for x in range(5): EncEntry( parent=enc, name=entry_names[x], text=["Test Text"], locked=False ) locked_entry = EncEntry( parent=enc, name="Banana", text=["Test Text"], locked=True ) # Locked entry should be the first entry, due to reverse sorting assert str(enc.all_entries[0]) == str(locked_entry) # Locked entry should not be visible on screen assert locked_entry not in enc.current_entries # Start showing locked buttons enc.ToggleShowLockedButtons()() assert str(enc.current_entries[0]) == str(locked_entry)
def test_viewed_callback_multiple(): enc = Encyclopaedia() e = EncEntry(parent=enc, name="Test Name", text=["Test Text"]) global i i = 0 global j j = 50 @e.on("viewed") def cb(entry): global i i += 1 @e.on("viewed") def cb2(entry): global j j += 10 assert 0 == i assert 50 == j enc.SetEntry(e)() assert 1 == i assert 60 == j
def test_viewed_callback_change_entry(): enc = Encyclopaedia() e = EncEntry( parent=enc, name="Test Name", text=["Test Text"] ) e2 = EncEntry( parent=enc, name="Test Name", text=["Test Text"] ) global i i = 0 def cb(*args): global i i += 1 e2.viewed_callback = (cb,) assert 0 == i enc.SetEntry(e)() assert 0 == i enc.NextEntry()() assert 1 == i
def test_entry_current_page(): enc = Encyclopaedia() e = EncEntry( parent=enc, name="Test Name", text=["Test Text"], locked=False ) enc.SetEntry(e)() assert "Page 1 / 1" == enc.labels.entry_current_page
def test_toggle_show_locked_entry(): """Test Actions through their implementation in Encyclopaedia.""" enc = Encyclopaedia() for x in range(5): EncEntry(parent=enc, name="Test Name", text=["Test Text"], locked=False) enc.ToggleShowLockedEntry()() assert True is enc.show_locked_entry
def test_sort_encyclopaeda(): """Test Actions through their implementation in Encyclopaedia.""" enc = Encyclopaedia() for x in range(5): EncEntry(parent=enc, name="Test Name", text=["Test Text"], locked=False) enc.Sort(sorting_mode=Encyclopaedia.SORT_SUBJECT)() assert Encyclopaedia.SORT_SUBJECT == enc.sorting_mode
def test_duplicate_entry_numbers(): """When trying to assign a number to an EncEntry that's already taken, an Exception should be thrown. """ enc = Encyclopaedia() EncEntry( parent=enc, name="Test Name", text=["Test Text"], locked=False ) EncEntry( parent=enc, name="Test Name", text=["Test Text"], locked=False ) with pytest.raises(ValueError) as e: EncEntry( parent=enc, number=1, name="Test Name", text=["Test Text"], locked=False ) message = '1 is already taken.' assert message == str(e.value)
def test_current_entries_show_all(): """With filtering off and show_locked_buttons set to True, Encyclopaedia.current_entries property should return Encyclopaedia.all_entries. """ enc = Encyclopaedia(show_locked_buttons=True) expected_list = [] for x in range(5): e = EncEntry( parent=enc, name="Test Name", text=["Test Text"], locked=False ) expected_list.append(e) for x in range(5): e = EncEntry( parent=enc, name="Test Name", text=["Test Text"], locked=True ) expected_list.append(e) assert expected_list == enc.current_entries
def test_encentry_template_invalid_args(): """Arguments that aren't valid for an EncEntry aren't valid here.""" enc = Encyclopaedia() GreekGodsEntry = EncEntryTemplate(food="Pizza") with pytest.raises(TypeError): GreekGodsEntry(parent=enc, name="Zeus")
def test_encentry_template(): enc = Encyclopaedia() GreekGodsEntry = EncEntryTemplate(subject="Greek Gods") about_zeus = GreekGodsEntry(parent=enc, name="Zeus") assert isinstance(about_zeus, EncEntry) assert "Greek Gods" == about_zeus.subject
def test_viewed_callback_set_entry(): enc = Encyclopaedia() e = EncEntry(parent=enc, name="Test Name", text=["Test Text"]) global i i = 0 @e.on("viewed") def cb(entry): global i i += 1 assert 0 == i enc.SetEntry(e)() assert 1 == i
def test_next_entry(): """Test Actions through their implementation in Encyclopaedia.""" enc = Encyclopaedia() for x in range(0, 5): EncEntry(parent=enc, name="Test Name {}".format(x + 1), text=["Test Text"], locked=False) assert 0 == enc.current_position enc.NextEntry()() assert enc.current_entries[1] == enc.active assert enc.current_entries[1].viewed assert 1 == enc.current_position
def test_image(): enc = Encyclopaedia() e = EncEntry(parent=enc, name="Test Name", text=["Test Text"], image="placeholder", locked_image="placeholder") assert "placeholder" == e.image assert e.has_image
def test_name(): enc = Encyclopaedia() e = EncEntry( parent=enc, name="Test Name", text=["Test Text"], locked=False, ) assert "Test Name" == e.name
def test_name_locked(): enc = Encyclopaedia() e = EncEntry( parent=enc, name="Test Name", text=["Test Text"], locked=True, ) assert "???" == e.name
def test_percentage_unlocked_empty(): """When an encyclopaedia is empty, Then accessing percentage_unlocked raises an Exception. And provide a readable error message. """ enc = Encyclopaedia() with pytest.raises(ZeroDivisionError) as e: enc.percentage_unlocked message = 'Cannot calculate percentage unlocked of empty Encyclopaedia' assert message == str(e.value)
def test_add_subpage(): enc = Encyclopaedia() e = EncEntry(parent=enc, name="Test Name", text=["Test Text"], locked=False) ee = EncEntry(parent=e, name="A Sub-Page", text=["Test Text"], locked=False) assert [[1, e], [2, ee]] == e.sub_entry_list
def test_unlock_callback(): enc = Encyclopaedia() global baz baz = 0 def foobar(): global baz baz += 1 e = EncEntry( parent=enc, name="Test Name", text=["Test Text"], locked=True, ) enc.unlock_callback = foobar # Unlock the first entry e.locked = False assert e.locked is False assert 1 == baz
def test_entry_current_page_closed_page(): enc = Encyclopaedia() EncEntry( parent=enc, name="Test Name", text=["Test Text"], locked=False ) with pytest.raises(AttributeError) as e: enc.labels.entry_current_page message = "Cannot display Entry's current page when no entry is open." assert message == str(e.value)
def test_unlock_subpage(): enc = Encyclopaedia() e = EncEntry( parent=enc, name="Test Name", text=["Test Text"], locked=False, ) ee = EncEntry(parent=e, name="A Sub-Page", text=["Test Text"], locked=True) # Unlock the sub-page ee.locked = False assert ee.locked is False
def test_viewed_persistent_get(): """When viewed status is controlled by a persistent variable Then the EncEntry's viewed attribute should be linked to a persistent variable. """ enc = Encyclopaedia() about_zeus = EncEntry( parent=enc, name="Zeus", text=["Test Text"], viewed_persistent=True, ) assert about_zeus.viewed == persistent.Zeus_viewed
def test_text_modify(): enc = Encyclopaedia() e = EncEntry( parent=enc, name="Test Name", text=["Test Text"], ) # Pretend we viewed the entry e.viewed = True # Modify text e.text = ["New Text"] assert e.viewed is False
def test_percentage_unlocked(): """Base unit test for the Encyclopaedia.percentage_unlocked property. """ enc = Encyclopaedia() for x in range(5): EncEntry(parent=enc, name="Test Name", text=["Test Text"], locked=False) for x in range(5): EncEntry(parent=enc, name="Test Name", text=["Test Text"], locked=True) assert 50.00 == enc.percentage_unlocked
def test_unlock_entry(): enc = Encyclopaedia() e = EncEntry( parent=enc, name="Test Name", text=["Test Text"], locked=True, ) assert e in enc.all_entries assert e not in enc.unlocked_entries # Unlock the first entry e.locked = False assert e.locked is False assert e in enc.all_entries assert e in enc.unlocked_entries
def test_locked_persistent_set(): """When locked status is controlled by a persistent variable Then the EncEntry's locked attribute should be linked to a persistent variable. """ enc = Encyclopaedia() about_zeus = EncEntry( parent=enc, name="Zeus", text=["Test Text"], locked=True, locked_persistent=True, ) about_zeus.locked = False assert persistent.Zeus_locked is False assert about_zeus.locked == persistent.Zeus_locked