コード例 #1
0
    def test_write_shopping_list(sefl):

        kitchen1 = Kitchen()
        assert len(kitchen1.pantry) == 0

        # add food to pantry
        food1 = Food('food1', 'category1', 'unit1', quantity_needed_in_stock=2)
        food2 = Food('food2', 'category2', 'unit2', quantity_needed_in_stock=4)
        food3 = Food('food3',
                     'category3',
                     'unit3',
                     quantity_needed_in_stock=10)

        kitchen1.add_to_pantry(food1, quantity=1)  # in list
        kitchen1.add_to_pantry(food2, quantity=7)  # not in list
        kitchen1.add_to_pantry(food3, quantity=4)  # in list

        assert len(kitchen1.pantry) == 3

        kitchen1.make_shopping_list()
        assert len(kitchen1.shopping_list) == 2

        data = Data()
        data.write_shopping_list(kitchen1.shopping_list,
                                 'test/test_write_shopping_list.json')

        with open('test/test_write_shopping_list.json', 'r') as list_reader:
            written_list = json.loads(list_reader.read())
            assert written_list['food1'] == 1
            assert written_list['food3'] == 6
        list_reader.close()
コード例 #2
0
    def test_write_pantry(self):

        # initialize kitchen with food and recipies
        kitchen = Kitchen()

        food1 = Food(name='food1',
                     unit='unit1',
                     category='category1',
                     quantity_needed_in_stock=10)
        food2 = Food('food2',
                     'category2',
                     'unit2',
                     quantity_needed_in_stock=20)

        kitchen.add_to_pantry(food1, 10)
        kitchen.add_to_pantry(food2, 20)

        data = Data()
        data.write_pantry(kitchen.pantry, 'test/test_write_pantry.json')

        # check file for valid json objects
        with open('test/test_write_pantry.json') as file:
            json_data = json.loads(file.read())

            assert type(json_data[0]) == dict
            assert json_data[0]['_name'] == 'food1'
            assert json_data[0]['_unit'] == 'unit1'
            assert json_data[0]['_category'] == 'category1'
            assert json_data[0]['_quantity_needed_in_stock'] == 10
            assert json_data[0]['_quantity_in_stock'] == 10

            assert type(json_data[1]) == dict
            assert json_data[1]['_name'] == 'food2'
            assert json_data[1]['_unit'] == 'unit2'
            assert json_data[1]['_category'] == 'category2'
            assert json_data[1]['_quantity_needed_in_stock'] == 20
            assert json_data[1]['_quantity_in_stock'] == 20

        file.close()