Example #1
0
 def test_punch_in_first(self) -> None:
     """Test the method "punch_in" with no clock punches."""
     if self._SCRATCH.exists():
         self._SCRATCH.unlink()
     with pc.PunchClock(self._SCRATCH) as clock:
         clock.punch_in()
         self.assertEqual(pc.State.IN, clock.state)
     self._assert_recent_integral(self._SCRATCH, pc.State.IN)
Example #2
0
 def test_init_blank(self) -> None:
     """Test the method "__init__" with a blank log."""
     if self._SCRATCH.exists():
         self._SCRATCH.unlink()
     self._SCRATCH.touch()
     with pc.PunchClock(self._SCRATCH) as clock:
         self.assertEqual(pc.State.OUT, clock.state)
     modified = pd.read_csv(self._SCRATCH)
     pd.util.testing.assert_index_equal(self._INDEX, modified.columns)
Example #3
0
 def test_punch_out_in(self) -> None:
     """Test the method "punch_out" when clocked in."""
     shutil.copy(str(self._IN), str(self._SCRATCH))
     original = pd.read_csv(self._SCRATCH)
     with pc.PunchClock(self._SCRATCH) as clock:
         clock.punch_out()
         self.assertEqual(pc.State.OUT, clock.state)
     reopened = self._assert_recent_integral(self._SCRATCH, pc.State.OUT)
     reopened.loc[0, pc.State.OUT.value] = None
     pd.util.testing.assert_frame_equal(original, reopened)
Example #4
0
    def _test_reset(self, log_path: pl.Path, state: pc.State) -> None:
        """Test the method "reset".

        Args:
            log_path: The path to a clock punch log.
            state: The expected initial state of the punch clock.
        """
        shutil.copy(str(log_path), str(self._SCRATCH))
        with pc.PunchClock(self._SCRATCH) as clock:
            self.assertEqual(state, clock.state)
            clock.reset()
            self.assertEqual(pc.State.OUT, clock.state)
        has_header = pd.read_csv(self._HAS_HEADER)
        modified = pd.read_csv(self._SCRATCH)
        pd.util.testing.assert_frame_equal(has_header, modified)
Example #5
0
 def test_punch_out_many_in(self) -> None:
     """Test the method "punch_out" with many punches when clocked in."""
     shutil.copy(str(self._MANY_IN), str(self._SCRATCH))
     original = pd.read_csv(self._SCRATCH)
     with pc.PunchClock(self._SCRATCH) as clock:
         self.assertEqual(pc.State.IN, clock.state)
         clock.punch_out()
         self.assertEqual(pc.State.OUT, clock.state)
     reopened = pd.read_csv(self._SCRATCH)
     original.loc[self._LAST_IDX, pc.State.OUT.value] = time.time()
     pd.util.testing.assert_almost_equal(
         original,
         reopened,
         check_dtype=False
     )
Example #6
0
    def _assert_nothing_changes(
            self,
            log_path: pl.Path,
            state: pc.State,
            method_name: str = None,
            expected_return_value: dt.timedelta = None
        ) -> ty.Tuple[dt.timedelta, pd.DataFrame]:
        """Assert that nothing changes.

        Instantiation of a punch clock should not alter the punch log.
        Any named method also should not alter the punch log nor the
        punch clock's state.

        Args:
            log_path: The path to a clock punch log.
            state: The expected state of the punch clock.
            method_name: The name of a punch-clock method.
            expected_return_value: The value expected to be returned
                from the method.

        Returns:
            A tuple. The first element is the value returned from the
            named method, and the second element is clock punch log in
            its original state.
        """
        original = pd.read_csv(log_path)
        with pc.PunchClock(log_path) as clock:
            self.assertEqual(state, clock.state)
            if method_name:
                method = getattr(clock, method_name)
                return_value = method()
                if expected_return_value:
                    self.assertEqual(expected_return_value, return_value)
            else:
                return_value = None
            self.assertEqual(state, clock.state)
        reopened = pd.read_csv(log_path)
        pd.util.testing.assert_frame_equal(original, reopened)
        return return_value, original