def __init__(
        self,
        database,
    ):

        super().__init__(main_tags=[self.Tags.MAIN],
                         option_tags=[
                             self.Tags.POST_OP,
                             self.Tags.DEFAULT_VALUE,
                         ])

        if type(database) is str:
            self._db = Database(database)
        else:
            self._db = database
Ejemplo n.º 2
0
    def test_set_database(self):
        database = Database(self._database_file_name)
        database["key3"] = "value3"
        self.assertEqual("value3", database["key3"])

        database["key4"] = "value4"
        self.assertEqual("value4", database["key4"])
 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)
Ejemplo n.º 4
0
    def test_access_same_file_from_multiple_databases(self):
        test_database = {"key1": "value1", "key2": "value2"}

        database1 = Database(self._database_file_name)
        database2 = Database(self._database_file_name)

        self.assertEqual(test_database, database1.database)
        self.assertEqual(test_database, database2.database)

        database1["key3"] = "value3"

        expected_database = {
            "key1": "value1",
            "key2": "value2",
            "key3": "value3",
        }
        self.assertEqual(expected_database, database1.database)
        self.assertEqual(expected_database, database2.database)
Ejemplo n.º 5
0
 def test_load_from_database_file(self):
     expected_database = {
         "key1": "value1",
         "key2": "value2"
     }
     with open(self._database_file_name, "w") as f:
         json.dump(expected_database, f)
     database = Database(self._database_file_name)
     self.assertEqual(expected_database, database.database)
class DatabasePopulator(BasePopulator):
    class Tags:
        MAIN = 'db'
        POST_OP = 'post-op'
        DEFAULT_VALUE = 'default value'
        CONVERT_FN = 'convert fn'

    def __init__(
        self,
        database,
    ):

        super().__init__(main_tags=[self.Tags.MAIN],
                         option_tags=[
                             self.Tags.POST_OP,
                             self.Tags.DEFAULT_VALUE,
                         ])

        if type(database) is str:
            self._db = Database(database)
        else:
            self._db = database

    _common_fns = {
        'increment': lambda x: x + 1,
        'decrement': lambda x: x - 1,
    }

    def get_replacement(
        self,
        key,
        default_value=None,
        modify_before_resaving_fn=None,
    ):
        if self._db.is_set(key):
            value = self._db[key]
        elif default_value is not None:
            value = default_value
        else:
            raise KeyError(
                f"No value for key '{key}' found and no default value provided"
            )

        if modify_before_resaving_fn is not None:
            if modify_before_resaving_fn in DatabasePopulator._common_fns:
                modify_before_resaving_fn = DatabasePopulator._common_fns[
                    modify_before_resaving_fn]
            new_value = modify_before_resaving_fn(value)
            self._db[key] = new_value

        if type(value) is datetime.time:
            value = value.strftime("%-I:%M%p")

        return str(value)

    def __contains__(self, key):
        return key in self._db
Ejemplo n.º 7
0
    def test_create_new_database_file(self):
        database = Database(self._database_file_name)

        self.assertEqual(
            self._database_file_name,
            database.database_file,
            os.path.join(
                os.path.dirname(os.path.realpath(__file__)),
            )
        )
Ejemplo n.º 8
0
    def setUp(self) -> None:

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

        self._db = db
        self._io = TestIO()
        self._interface = ClientAndServerInterface(
            *[self._io.output_fn, self._io.input_fn] * 2, database=self._db)
Ejemplo n.º 9
0
    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
Ejemplo n.º 10
0
    def test_clear_database(self):
        expected_initial_database = {"key1": "value1", "key2": "value2"}
        expected_cleared_database = {"key1": "", "key2": ""}

        database = Database(self._database_file_name)
        self.assertEqual(expected_initial_database, database.database)

        database.clear_value("key1")
        database.clear_value("key2")
        self.assertEqual(expected_cleared_database, database.database)

        database.database = expected_initial_database
        self.assertEqual(expected_initial_database, database.database)

        database.clear_entire_database()
        self.assertEqual(expected_cleared_database, database.database)
Ejemplo n.º 11
0
    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)
Ejemplo n.º 12
0
    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)
Ejemplo n.º 13
0
    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)
Ejemplo n.º 14
0
 def test_load_from_empty_database_file(self):
     expected_database = {}
     with open(self._database_file_name, "w") as f:
         json.dump(expected_database, f)
     database = Database("test_database.json")
     self.assertEqual({}, database.database)
Ejemplo n.º 15
0
from interaction_engine.json_database import Database
from interaction_engine.messager import Message, Node, DirectedGraph
from interaction_engine.planner import MessagerPlanner
from interaction_engine.text_populator import TextPopulator, DatabasePopulator, VarietyPopulator
from interaction_engine.interfaces import TerminalClientAndServerInterface


"""
CREATE RESOURCES USED TO POPULATE TEXT
"""

import atexit

# Create the database
db_file = 'test_db.json'
db = Database(db_file)
db["user_name"] = None
db["question_idx"] = 0
db["answers"] = None

# Create a file with text used for variation
variation_file = 'variation.csv'
variation_file_contents = """
Code,Text
greeting,Hi
greeting,Hello
greeting,Hola
question,I am the life of the party
question,I am always prepared
question,I get stressed out easily
question,I have a rich vocabulary