Beispiel #1
0
    def test_tag_management(self):
        # Adding a new tag to an object (& saving) should also update the Tags db
        tags = [
            F'star wars {random.randint(0,9999)}',
            F'trapped {random.randint(0,9999)}'
        ]
        comment = Comment(name="Luke Skywalker",
                          comment="There's something alive in here!")
        comment.save()

        comment2 = Comment(id=comment.id)
        comment2.load()
        self.assertEqual(len(comment2.tags), 0)

        for tag in tags:
            # Tag does not exist in Tag DB
            self.assertFalse(Tag.exists(tag), tag)
            # Add to object
            comment2.tag(tag)

        comment2.save()
        self.assertEqual(len(comment2.tags), len(tags))

        for tag in tags:
            # Tag should now exist in Tag db
            self.assertTrue(Tag.exists(tag), tag)
Beispiel #2
0
    def test_tagging(self):
        # Basic instance
        secret = Secret(
            name="LEGO",
            system="www.lego.com",
            sub_system="UI",
            type=Secret.TYPE_USER_PASS,
            data={'username': '******', 'password': '******'},
        )
        self.assertIsNotNone(secret.tags)
        self.assertIsInstance(secret.tags, set)
        self.assertEqual(len(secret.tags), 0)

        # Create with Tags
        secret.tag("lego")
        secret.tag("brick-by-brick")
        self.assertEqual(len(secret.tags), 2)
        self.assertIsInstance(list(secret.tags)[0], Tag)
        secret.save()

        # Retrieve has Tags
        secret2 = Secret(id=secret.id)
        secret2.load()
        self.assertIsNotNone(secret2.tags)
        self.assertIsInstance(secret2.tags, set)
        self.assertEqual(len(secret2.tags), 2)
        self.assertIsInstance(list(secret2.tags)[0], Tag)
        self.assertTrue(Tag(name="lego") in secret2.tags)

        # Update tags
        secret2.tag("studs-r-us")
        self.assertEqual(len(secret2.tags), 3)
        self.assertIsInstance(list(secret2.tags)[2], Tag)

        secret2.save()

        secret3 = Secret(id=secret2.id)
        secret3.load()
        self.assertIsNotNone(secret3.tags)
        self.assertIsInstance(secret3.tags, set)
        self.assertEqual(len(secret3.tags), 3)

        self.assertTrue(Tag(name="lego") in secret3.tags)
        self.assertTrue(Tag(name="brick-by-brick") in secret3.tags)
        self.assertTrue(Tag(name="studs-r-us") in secret3.tags)
Beispiel #3
0
    def test_tagging_basics(self):
        comment = Comment(name="Carlos Phisher",
                          message="The world is borked!")

        #  No tags yet
        self.assertIsInstance(comment.tags, set)
        self.assertEqual(len(comment.tags), 0)

        # Add a tag
        comment.tag("Urgent")
        self.assertIsInstance(comment.tags, set)
        self.assertEqual(len(comment.tags), 1)
        self.assertIsInstance(list(comment.tags)[0], Tag)

        # Add the same tag == no change
        comment.tag("Urgent")
        self.assertIsInstance(comment.tags, set)
        self.assertEqual(len(comment.tags), 1)
        self.assertIsInstance(list(comment.tags)[0], Tag)

        # Add a few more tags
        comment.tag("Phone")  # Add by string
        comment.tag("CommErr")  # Add by string
        comment.tag(Tag(name="Slang"))  # Add by Tag class
        self.assertEqual(len(comment.tags), 4)
        self.assertTrue(Tag(name="CommErr") in comment.tags)

        # Remove one - by string
        comment.remove_tag("Slang")
        self.assertEqual(len(comment.tags), 3)
        self.assertFalse(Tag(name="Slang") in comment.tags)

        # Remove another - by Tag
        comment.remove_tag(Tag(name="Phone"))
        self.assertEqual(len(comment.tags), 2)
        self.assertFalse(Tag(name="Phone") in comment.tags)

        # Add tags at instance creation
        comment2 = Comment(name="John Doe",
                           message="Mice eat cheese!?",
                           tags=['mice', 'food', 'dairy'])

        self.assertIsInstance(comment2.tags, set)
        self.assertEqual(len(comment2.tags), 3)
        self.assertIsInstance(list(comment.tags)[0], Tag)
Beispiel #4
0
def find():
    resp = None
    status = 200

    try:
        query_string = request.args.copy()

        # page  = int(query_string.pop('page', 1))

        tags = None
        if not query_string:
            tags = Tag.fetch()
        else:
            tags = Tag.find(**query_string)

        resp = {'tags': tags}
    except Exception as e:
        status = 500
        resp = {"error": str(e)}

    return jsonify(resp), status
Beispiel #5
0
    def test_normalize(self):
        tests = [
            ('HELLO',                       'hello'),
            ('Hello.World',                 'hello.world'),
            ('HELLO_wOrLd',                 'hello_world'),
            ('Hello, World!',               'hello-world'),
            ('  hello,world!!',             'hello-world'),
            ('helloWorld',                  'helloworld'),
            ('   hello,,,,,   world!!!   ', 'hello-world'),
            ('###  ! Hello World @ Large ! ###', 'hello-world-large')
        ]

        for tset in tests:
            self.assertEqual(Tag.normalize(tset[0]), tset[1], tset[0])
Beispiel #6
0
class TagTest(unittest.TestCase):

    def setUp(self):
        self.tag = Tag(name="Urgent")

    def test_basics(self):
        # Tag name should be normalized when constructed
        tag = Tag(name="Hack-A-Thon 2020")
        self.assertEqual(tag.name, "hack-a-thon-2020")

    def test_serialize(self):
        data = self.tag.serialize()

        self.assertEqual(self.tag.name, data['name'])

    # def test_immutable(self):
    #     self.assertEqual(self.tag.name, "urgent")

    #     with self.assertRaisesRegex(AttributeError, "can't set attribute"):
    #         self.tag.name = "Not So Urgent"

    #     self.assertEqual(self.tag.name, "urgent")

    def test_str__repr(self):
        self.assertEqual(str(self.tag), self.tag.name)
        self.assertEqual(repr(self.tag), self.tag.name)

    def test_eq(self):
        tag2 = Tag(name=self.tag.name)

        self.assertEqual(self.tag, tag2)

    def test_normalize(self):
        tests = [
            ('HELLO',                       'hello'),
            ('Hello.World',                 'hello.world'),
            ('HELLO_wOrLd',                 'hello_world'),
            ('Hello, World!',               'hello-world'),
            ('  hello,world!!',             'hello-world'),
            ('helloWorld',                  'helloworld'),
            ('   hello,,,,,   world!!!   ', 'hello-world'),
            ('###  ! Hello World @ Large ! ###', 'hello-world-large')
        ]

        for tset in tests:
            self.assertEqual(Tag.normalize(tset[0]), tset[1], tset[0])
Beispiel #7
0
    def test_search_by_tag(self):
        for i in range(0, 10):
            comment = Comment(name=F"User {i}",
                              message=F"Foo Bar {i}",
                              tags=[F"Anonymous", F"Tag{i}", F"Tag{i*i}"])
            comment.save()

        comment = Comment(name="John Titor",
                          message="I am from the future. I can prove it.",
                          tags=['Steins;Gate', 'WW III', 'Future'])
        comment.save()

        results = Comment.find(tags="anonymous")
        self.assertEqual(len(results), 10)

        results = Comment.find(tags="future")
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0].id, comment.id)
        self.assertEqual(results[0].name, "John Titor")
        self.assertTrue(Tag(name="Steins;Gate") in results[0].tags)
Beispiel #8
0
    def test_tagging(self):
        self.assertIsNotNone(self.todo.tags)
        self.assertIsInstance(self.todo.tags, set)
        self.assertEqual(len(self.todo.tags), 0)

        self.todo.tag("maintenance")
        self.todo.tag("deckc")
        self.assertEqual(len(self.todo.tags), 2)
        self.assertIsInstance(list(self.todo.tags)[0], Tag)

        self.todo.save()

        note2 = Todo(id=self.todo.id)
        note2.load()
        self.assertIsNotNone(note2.tags)
        self.assertIsInstance(note2.tags, set)
        self.assertEqual(len(note2.tags), 2)
        self.assertIsInstance(list(note2.tags)[0], Tag)

        self.assertTrue(Tag(name="maintenance") in note2.tags)
Beispiel #9
0
    def test_tagging(self):
        self.assertIsNotNone(self.note.tags)
        self.assertIsInstance(self.note.tags, set)
        self.assertEqual(len(self.note.tags), 0)

        self.note.tag("Hello")
        self.note.tag("World")
        self.assertEqual(len(self.note.tags), 2)
        self.assertIsInstance(list(self.note.tags)[0], Tag)

        self.note.save()

        note2 = Note(id=self.note.id)
        note2.load()
        self.assertIsNotNone(note2.tags)
        self.assertIsInstance(note2.tags, set)
        self.assertEqual(len(note2.tags), 2)
        self.assertIsInstance(list(note2.tags)[0], Tag)

        self.assertTrue(Tag(name="Hello") in note2.tags)
Beispiel #10
0
    def convert(self):
        print(
            F"--- Converting Metiisto/{self.m_cfg['name']} to Cartaro/{self.c_cfg['name']} ---"
        )
        obj_cursor = self.metiisto.cursor()

        # Fetch Metiisto data
        fld_str = ",".join(self.m_cfg['fields'])
        obj_sql = F"select id,{fld_str} from {self.m_cfg['name']} order by id"
        if self.opts.get('limit', None):
            obj_sql += F" limit {self.opts['limit']}"
        print(F"     => {obj_sql}")

        tags = {}
        if self.opts.get("has_tags", False):
            tag_class = self.opts['tag_class']
            tag_sql = F"select tagged_object.obj_id, tags.name from tags, tagged_object where obj_class='{tag_class}' and tags.id = tagged_object.tag_id order by obj_id"
            print(F"     => {tag_sql}")

            tag_cursor = self.metiisto.cursor()
            tag_cursor.execute(tag_sql)
            for row in tag_cursor:
                note_id = row[0]
                if note_id not in tags:
                    tags[note_id] = []

                tags[note_id].append(Tag.normalize(row[1]))

        count = 0
        obj_cursor.execute(obj_sql)
        for row in obj_cursor:
            record = {}
            obj_id = row[0]

            if self.opts.get('interactive', False):
                print(
                    "-------------------------------------------------------")
                pprint.pprint(row, indent=2)
                print(
                    "-------------------------------------------------------")

            # row[0] == DB `id`, skip for mapping
            mapping = zip(self.c_cfg['fields'], row[1:])
            for (name, raw_value) in mapping:
                value = raw_value
                if raw_value and name.endswith('_at'):
                    date = arrow.get(raw_value, Base.TIMEZONE)
                    value = date.timestamp
                elif name.startswith('is_'):
                    value = True if raw_value else False

                transformer = self.opts.get(F"{name}_transformer", None)
                if transformer:
                    value = transformer(
                        value,
                        record=record,
                        interactive=self.opts.get('interactive'))

                # print(F"{name} == {value} - {type(value)}")
                record[name] = value

            if self.opts.get("has_tags", False):
                record['tags'] = tags.get(obj_id, [])

            for tag in self.opts.get("additional_tags", []):
                if not 'tags' in record:
                    record['tags'] = []
                record['tags'].append(Tag.normalize(tag))

            # Metiisto side does not have TS, Added `created_at` to Cartaro data
            if not self.opts.get('has_datestamps', True):
                record['created_at'] = record[
                    'created_at'] if 'created_at' in record else arrow.now(
                        Base.TIMEZONE).timestamp
                record['updated_at'] = None
                record['deleted_at'] = None

            self.cartaro.insert(record)
            count += 1
            print(F"{self.m_cfg['name']} - {count}", end="\r")

        print(F"     => Converted {count} records.")
Beispiel #11
0
         'preserve_data': True,
         'additional_tags': ['sticky note']
     }
 },
 "tags": {
     'metiisto': {
         'name': 'tags',
         'fields': ['name']
     },
     'cartaro': {
         'name': 'Tags',
         'fields': ['name']
     },
     'options': {
         'has_datestamps': False,
         'name_transformer': lambda name, **kwargs: Tag.normalize(name)
     }
 },
 "work_days": {
     'metiisto': {
         'name':
         'work_days',
         'fields': [
             'work_date', 'time_in', 'time_out', 'note',
             'is_vacation*100+is_holiday*10+is_sick_day as type'
         ],
     },
     'cartaro': {
         'name': 'WorkDays',
         'fields': ['date', 'time_in', 'time_out', 'note', 'type'],
     },
Beispiel #12
0
 def __gen_tags(self, count, prefix='', suffix=''):
     for i in range(0, count):
         tag = Tag(
             name=F"{prefix} {self.FAKER.word().capitalize()} {suffix}")
         tag.save()
Beispiel #13
0
 def setUp(self):
     self.tag = Tag(name="Urgent")
Beispiel #14
0
    def test_eq(self):
        tag2 = Tag(name=self.tag.name)

        self.assertEqual(self.tag, tag2)
Beispiel #15
0
 def setUp(self):
     # Setup Flask Testing
     cartaro.flask_app.config['TESTING'] = True
     self.client = cartaro.flask_app.test_client()
     Tag.purge()
Beispiel #16
0
 def test_basics(self):
     # Tag name should be normalized when constructed
     tag = Tag(name="Hack-A-Thon 2020")
     self.assertEqual(tag.name, "hack-a-thon-2020")
Beispiel #17
0
    work_days = WorkDay.fetch()

    unused_tags = []
    for tag in tags:
        used = False
        for items in (entries, notes, secrets, todos, work_days):
            used = used_by(tag, items)
            if used:
                break
        if not used:
            unused_tags.append(tag)

    for tag in unused_tags:
        print(F"Pruning {tag.id:04d} - [{tag.name}].")
        tag.delete()


################################################################################
if __name__ == "__main__":
    env = os.getenv('CARTARO_ENV', 'dev')
    doc_path = os.getenv('CARTARO_DOC_PATH')
    if not doc_path:
        raise Exception("CARTARO_DOC_PATH **must** be set.")

    print(F"Analyzing Tags for {doc_path} in {env}...")

    tags = Tag.fetch()

    purge_empty(tags)
    prune_unused(tags)