Beispiel #1
0
def main():
    f = open("food_diary.json", 'r')
    try:
        data = json.load(f)
    except:
        data = {}

    c = open('calories.json', 'r')
    try:
        calories = json.load(c)
    except:
        calories = {}

    f.close()
    c.close()

    cal = Calories(calories)
    fd = FoodDiary(data, cal)
    cli = CLI(fd)

    cli.start()

    f = open("food_diary.json", 'w')
    json.dump(data, f, indent=4)
    f.close()

    c = open("calories.json", 'w')
    json.dump(calories, c, indent=4)
    c.close()
Beispiel #2
0
class TestCLI(unittest.TestCase):
    """ Basic CLI functionality test cases. """

    def setUp(self):
        """ Prepare mock stdin and stdout, and a CLI instance to test. """
        # Create stdin and stdout files for testing.
        self.test_stdinout = MockStdInOut()

        # Create queues to pass messages to and get messages from the CLI.
        self.to_cli_q = queue.Queue()
        self.from_cli_q = queue.Queue()

        # Create a test CLI with the test stdin and stdout.
        self.test_cli = CLI(self.to_cli_q, self.from_cli_q,
                            stdin=self.test_stdinout,
                            stdout=self.test_stdinout)
        self.test_cli.start()

    def tearDown(self):
        """ Just stop the test CLI instance. """
        self.test_cli.stop()

    def test_input_and_output(self):
        """ Test basic input and output of the CLI. """
        # Test that the CLI input works first.
        test_input = "This is some test input"
        self.test_stdinout.to_send_data.append(test_input + '\n')
        self.assertEqual(self.from_cli_q.get(timeout=0.5), test_input)

        # Now check the output. Pass a message to be written out.
        test_output = 'This is some test output'
        self.to_cli_q.put(test_output)
        # Wait for the output to be written.
        time.sleep(0.1)
        self.assertEqual(self.test_stdinout.received_data.pop(0),
                         test_output + '\n')

        # Check the CLI is still running happily.
        self.assertTrue(self.test_cli.running)

        # Finally, check there is no unread data in the files.
        self.assertEqual(self.test_stdinout.received_data, [])
        self.assertEqual(self.test_stdinout.to_send_data, [])

    def test_ending_session(self):
        """ Test the user can end the CLI instance cleanly. """
        # Simulate sending an EOF.
        self.test_stdinout.to_send_data.append('')
        self.assertEqual(self.from_cli_q.get(timeout=0.5), None)

        # This stops the reading thread, but nt the whole CLI - stop the rest.
        self.test_cli.stop()

        # Check the CLI is now stopped.
        self.assertFalse(self.test_cli.running)

        # Finally, check there is no unread data in the files.
        self.assertEqual(self.test_stdinout.received_data, [])
        self.assertEqual(self.test_stdinout.to_send_data, [])
Beispiel #3
0
def main():
    f = open("food_diary.json", 'r')
    try:
        data = json.load(f)
    except:
        data = {}

    f.close()

    fd = FoodDiary(data)
    cli = CLI(fd)

    cli.start()

    f = open("food_diary.json", 'w')
    json.dump(data, f, indent=4)
    f.close()
Beispiel #4
0
def manual_test():
    """ Manual test for the CLI if needed. """
    to_cli_q = queue.Queue()
    from_cli_q = queue.Queue()
    cli = CLI(to_cli_q, from_cli_q)
    cli.start()
    log.info('CLI running state: %r', cli.running)

    time.sleep(10)

    log.info('CLI running state: %r', cli.running)
    cli.stop()

    try:
        while True:
            log.info('Got CLI input: %r', from_cli_q.get(timeout=0.1))
    except queue.Empty:
        pass
Beispiel #5
0
def main():
    dbase = DBinit()
    console = CLI(dbase)
    console.start()
Beispiel #6
0
def main():
    sql_manager.create_database()
    cli = CLI()
    cli.start()
Beispiel #7
0
def main():
    c = CLI()
    c.start()
Beispiel #8
0
def main():
    dbase = DBinit()
    console = CLI(dbase)
    console.start()
Beispiel #9
0
def main():
    c = CLI()
    c.start()
Beispiel #10
0
def main():
    sql_manager.create_database()
    cli = CLI()
    cli.start()