def test_set_position_insert_before_first(self, m_pi): """Insert at first position if given number below 1.""" p1 = Index() p2 = Index() p3 = Index() p1.position = 1 p2.position = 2 p3.position = 3 m_pi.return_value = [p1, p2, p3] ptest = Index() ptest.position = None ptest.set_position(0) assert ptest.position == 1 assert p1.position == 2 assert p2.position == 3 assert p3.position == 4 m_pi.return_value = [p1, p2, p3, ptest] ptest2 = Index() ptest2.position = None ptest2.set_position(-1) assert ptest2.position == 1 assert ptest.position == 2 assert p1.position == 3 assert p2.position == 4 assert p3.position == 5
def test_last(self, m_pi): """Return the highest positioned instance.""" p1 = Index() p2 = Index() p3 = Index() p1.position = 1 p2.position = 2 p3.position = 3 m_pi.return_value = [p1, p2, p3] p = Index() assert p.last is p3
def test_clean_positions_removes_self(self, m_pi): """Remove self from positioning if specified.""" p1 = Index() p2 = Index() p3 = Index() p1.position = 1 p2.position = 5 p3.position = 9 m_pi.return_value = [p1, p2, p3] p1.clean_positions(remove_self=True) assert p2.position == 1 assert p3.position == 2
def test_auto_position_with_others(self, m_gai): p1 = Index() p1.position = 1 p2 = Index() p2.position = 2 p3 = Index() p3.position = 3 m_gai.return_value = [p1, p2, p3] p4 = Index() p4.position = None p4.auto_position() assert p4.position == 4
def test_clean_positions_removes_gaps(self, m_pi): """Remove gaps in position when cleaning.""" p1 = Index() p2 = Index() p3 = Index() p1.position = 1 p2.position = 5 p3.position = 9 m_pi.return_value = [p1, p2, p3] p1.clean_positions() assert p1.position == 1 assert p2.position == 2 assert p3.position == 3
def test__step_backward(self, db): """Get the previous instance in the sequence.""" idx1 = Index() idx2 = Index() idx3 = Index() idx1.position = 1 idx2.position = 3 idx3.position = 4 db.session.add_all([idx1, idx2, idx3]) db.session.commit() assert idx3._step(forward=False) is idx2 assert idx2._step(forward=False) is idx1 assert idx1._step(forward=False) is None
def test_clean_positions_sets_nulls(self, m_pi): """Set positions for objects with no position set.""" p1 = Index() p2 = Index() p3 = Index() p4 = Index() p1.position = 1 p2.position = None p3.position = 2 p4.position = None m_pi.return_value = [p1, p2, p3, p4] p1.clean_positions() assert p1.position == 1 assert p3.position == 2 assert p2.position == 3 assert p4.position == 4
def test_set_position_insert_after_last(self, m_pi): """Insert after last position if given arbitrarily larger position.""" p1 = Index() p2 = Index() p3 = Index() p1.position = 1 p2.position = 2 p3.position = 3 m_pi.return_value = [p1, p2, p3] ptest = Index() ptest.position = None ptest.set_position(42) assert p1.position == 1 assert p2.position == 2 assert p3.position == 3 assert ptest.position == 4
def test_set_position_insert_first(self, m_pi): """Bump others up when inserting to start position.""" p1 = Index() p2 = Index() p3 = Index() p1.position = 1 p2.position = 2 p3.position = 3 m_pi.return_value = [p1, p2, p3] ptest = Index() ptest.position = None ptest.set_position(1) assert ptest.position == 1 assert p1.position == 2 assert p2.position == 3 assert p3.position == 4
def test_set_position_inserts(self, m_pi): """Setting a position btwn first and last should insert there.""" p1 = Index() p2 = Index() p3 = Index() p1.position = 1 p2.position = 2 p3.position = 3 m_pi.return_value = [p1, p2, p3] ptest = Index() ptest.position = None ptest.set_position(2) assert p1.position == 1 assert ptest.position == 2 assert p2.position == 3 assert p3.position == 4
def test__eq__(self): """Return True if all columns are the same value.""" idx1 = Index() idx2 = Index() idx1.id = 42 assert idx1 != idx2 idx2.id = 42 assert idx1 == idx2 idx1.position = 3 idx1.name = 'Annual' idx1.slug = 'annual' idx1.description = 'Not built to last.' assert idx1 != idx2 idx2.position = 3 idx2.name = 'Annual' idx2.slug = 'annual' idx2.description = 'Not built to last.' assert idx1 == idx2
def test_auto_position_first(self, m_gai): """Set position to 1 if no other instances exist.""" m_gai.return_value = [] p1 = Index() # Since Index is not a declarative model, but a mixin that # adds a column to models, p1.position needs to be set to None before # using auto_position to prevent a TypeError from being raised due to # attempting to interpret an unbound column as a boolean. p1.position = None p1.auto_position() assert p1.position == 1
def test_dict__to_from_dict__(self, m_q): """An Index.dict_ fed to Index.from_dict_ creates identical Index.""" m_q.get.return_value = None idx1 = Index() idx1.id = 42 idx1.position = 3 idx1.name = 'Annual' idx1.slug = 'annual' idx1.description = 'Not built to last.' d = idx1.dict_ assert Index.from_dict_(d) == idx1
def test_from_dict__index_exists(self, m_q): """Do not allow from_dict_ to create an Index w/ id already in use.""" old_idx = Index() old_idx.id = 42 m_q.get.return_value = old_idx idx = Index() idx.id = 42 idx.position = 3 idx.name = 'Annual' idx.slug = 'annual' idx.description = 'Not built to last.' d = idx.dict_ with pytest.raises(ValueError): Index.from_dict_(d)