def test_persists_tags_and_returns_them(self):
        self.sut = self.create()

        tags = [Tag("id-1", "first tag"), Tag("id-2", "other tag")]

        self.assertEqual(tags, self.sut.persist_tags(tags))

        rows = self.connection_provider.execute_query("SELECT name, tag_id " \
            "FROM {}".format(self.tags_table_name))

        expected_rows = [("first tag", "id-1"), ("other tag", "id-2")]

        self.assertEqual(rows, expected_rows)
    def test_does_not_persist_tags_that_already_exist(self):
        self.sut = self.create()

        tags = [Tag("id-1", "first tag"), Tag("id-2", "other tag")]

        self.connection_provider.execute_query("INSERT INTO {} (tag_id, name)" \
            " VALUES ('id-X', 'first tag')".format(self.tags_table_name))

        result = self.sut.persist_tags(tags)

        expected_tags = [Tag("id-X", "first tag"), tags[-1]]

        self.assertListEqual(expected_tags, result)
    def test_persists_expense_tags_and_relations(self):
        self.sut = self.create()

        category = Category("category-id-1", "Some Category")
        tags = [Tag("id-1", "first tag"), Tag("id-2", "other tag")]
        expense = Expense("expense-id-1", "TEST", 1, 1566172800, category,
                          tags)

        retriever = SqliteExpensesRetriever(self.database_tables,
                                            self.connection_provider)

        self.sut.persist_expense_tags(expense)

        self.assertListEqual(tags, retriever.retrieve_tags())
Example #4
0
    def from_json(cls, json):
        category = Category.from_json(json["category"])
        tags = [Tag.from_json(tag) for tag in json["tags"]]

        return Expense(uuid.uuid4(), json["name"], json["cost"],
                       convert_date_string_to_timestamp(json["date"]),
                       category, tags)
Example #5
0
    def test_retrieves_tags(self):
        self.sut = self.create()

        tag_data = [
            ("uuid4-2", "other tag"),
            ("uuid4-1", "some tag")
        ]

        expected_tags = [
            Tag(tag_data[0][0], tag_data[0][1]),
            Tag(tag_data[1][0], tag_data[1][1])
        ]

        query = "INSERT INTO {} (tag_id, name) VALUES" \
            "('uuid4-1', 'some tag'), " \
            "('uuid4-2', 'other tag')".format(
                self.tags_table_name
            )

        self.connection_provider.execute_query(query)

        self.assertEqual(expected_tags, self.sut.retrieve_tags())
Example #6
0
    def test_retrieves_expense_tags(self, _mock_print):
        self.sut = self.create()

        persister = SqliteExpensesPersister(
            self.database_tables, self.connection_provider)

        tags = [
            Tag("tag-1", "First Tag"),
            Tag("tag-2", "Second Tag"),
            Tag("tag-3", "Third Tag")
        ]

        category = Category("category-1", "Some Category")

        expenses = [
            Expense("ex-1", "First Expense", 11, 1566172800, category, tags[:2]),
            Expense("ex-2", "Other Expense", 22, 1566172800, category, tags[1:])
        ]

        for expense in expenses:
            persister.add_expense(expense)

        self.assertListEqual(tags[:2], self.sut.retrieve_expense_tags(expenses[0]))
Example #7
0
def add_tags():
    if request.method == "POST":
        tags_payload = request.get_json(force=True)

        if not tags_payload:
            return jsonify([])

        tags = [Tag(None, tag) for tag in tags_payload]
        expenses_persister = get_expenses_persister()

        added_tags = expenses_persister.persist_tags(tags)

        return jsonify(convert_models_to_json(added_tags))

    return None
 def __convert_table_row_to_tag(self, table_row, additional_rows=[]):
     return Tag(table_row[1], html.unescape(table_row[0]))