def setUp(self):
     self.file = 'test_db.json'
     db = Database(self.file)
     db['key1'] = '1'
     db['key2'] = 'two'
     db['no_value_key'] = None
     db['key3'] = 0
     self.dp = DatabasePopulator(db)
class TestDatabasePopulator(unittest.TestCase):

    def setUp(self):
        self.file = 'test_db.json'
        db = Database(self.file)
        db['key1'] = '1'
        db['key2'] = 'two'
        db['no_value_key'] = None
        db['key3'] = 0
        self.dp = DatabasePopulator(db)

    def tearDown(self):
        os.remove(self.file)

    def test_get_replacement(self):

        self.assertEqual(
            '1',
            self.dp.get_replacement('key1')
        )
        self.assertEqual(
            'two',
            self.dp.get_replacement('key2')
        )

    def test_key_with_no_value(self):
        self.assertRaises(
            KeyError,
            self.dp.get_replacement,
            'no_value_key'
        )
        self.assertEqual(
            '3',
            self.dp.get_replacement('no_value_key', default_value=3)
        )

    def test_key_that_doesnt_exist(self):
        self.assertRaises(
            KeyError,
            self.dp.get_replacement,
            'not_a_key'
        )

    def test_post_op(self):
        for i in range(10):
            self.assertEqual(
                i,
                self.dp._db['key3']
            )
            self.dp.get_replacement('key3', modify_before_resaving_fn='increment')

        for i in range(10):
            self.assertEqual(
                10-i,
                self.dp._db['key3']
            )
            self.dp.get_replacement('key3', modify_before_resaving_fn='decrement')
    def setUp(self) -> None:

        db = Database(db_file)
        db["user_name"] = None
        db["question_idx"] = 0
        db["answers"] = None

        with open(variation_file, 'w', newline='') as f:
            json.dump(variation_file_contents, f)

        variety_populator_ = VarietyPopulator(variation_file)
        database_populator_ = DatabasePopulator(db_file)
        self._text_populator = TextPopulator(variety_populator_,
                                             database_populator_)
        self._db = db
    def test_bad_database_key(self):

        bad_variation_file = 'bad_variation.json'
        bad_variation_file_contents = {
            "greeting": ["Hi", "Hello {'db': 'not_a_key'}"]
        }
        db = Database(db_file)

        with open(bad_variation_file, 'w', newline='') as f:
            json.dump(bad_variation_file_contents, f)

        variety_populator_ = VarietyPopulator(bad_variation_file)
        database_populator_ = DatabasePopulator(db)
        self.assertRaises(
            KeyError,
            TextPopulator,
            variety_populator_,
            database_populator_,
        )
        os.remove(bad_variation_file)
    def test_partial_entry_in_variation_file(self):

        bad_variation_file = 'bad_variation.json'
        bad_variation_file_contents = {"greeting": ["Hi", "Hello{"]}
        db = Database(db_file)
        db["user_name"] = None
        db["question_idx"] = 0
        db["answers"] = None

        with open(bad_variation_file, 'w', newline='') as f:
            json.dump(bad_variation_file_contents, f)

        variety_populator_ = VarietyPopulator(bad_variation_file)
        database_populator_ = DatabasePopulator(db_file)
        self.assertRaises(
            ValueError,
            TextPopulator,
            variety_populator_,
            database_populator_,
        )
        os.remove(bad_variation_file)
    def test_replacement_with_multi_arg_entry(self):
        correct_variation_file = 'correct_variation.json'
        correct_variation_file_contents = {
            "greeting": [
                "Hi ready for number {'db': 'question_idx', 'post-op': 'increment'}",
                "Hello {'db': 'user_name'}"
            ]
        }

        db = Database(db_file)
        db["user_name"] = None
        db["question_idx"] = 0
        db["answers"] = None

        with open(correct_variation_file, 'w', newline='') as f:
            json.dump(correct_variation_file_contents, f)

        variety_populator_ = VarietyPopulator(correct_variation_file)
        database_populator_ = DatabasePopulator(db_file)
        TextPopulator(
            variety_populator_,
            database_populator_,
        )
        os.remove(correct_variation_file)
    """

    variation_file = 'variation.json'
    variation_dict = {
        "greeting": ["Hi", "Hello", "Hola"],
        "question": [
            "Do you like green?",
            "Do you like dogs?",
            "Do you like apples?",
            "Do you like me?"
        ],
        "foo": ["foo", "fake"],
        "foobar": "foo-bar",
        "fakebar": "fake-bar"
    }

    with open(variation_file, 'w', newline='') as f:
        json.dump(variation_dict, f)

    import atexit
    atexit.register(lambda: os.remove(db_file))
    atexit.register(lambda: os.remove(variation_file))

    variety_populator_ = VarietyPopulator(variation_file)
    database_populator_ = DatabasePopulator(db_file)

    text_populator = TextPopulator(variety_populator_, database_populator_)
    for _ in range(4):
        out = text_populator.run(my_str)
        print(out)