def test_safe_get_with_objects(self) -> None:
     app = Fixtures.next_application()
     app2 = Fixtures.next_application()
     fake_result = {key: [value] for key, value in app.__dict__.items()}
     fake_result2 = {key: [value] for key, value in app2.__dict__.items()}
     transform = self.get_proxy()._convert_to_application
     self.assertEqual(
         _safe_get([{
             'a': [fake_result]
         }], 'a', transform=transform), app)
     self.assertCountEqual(
         _safe_get_list([{
             'a': [fake_result, fake_result2]
         }],
                        'a',
                        transform=transform), [app, app2])
    def test_rt_table_with_non_existent_app(self) -> None:
        application = Fixtures.next_application()
        # purposefully don't insert application
        expected_table = Fixtures.next_table(application=application)

        self.get_proxy().put_table(table=expected_table)
        actual_table: Table = self.get_proxy().get_table(
            table_uri=checkNotNone(expected_table.key))

        self.assertEqual(actual_table.table_writer, None)
        self.assertEqual(actual_table.owners, [])
    def test_rt_table_with_owner(self) -> None:
        user = Fixtures.next_user(is_active=True)
        self.get_proxy().put_user(data=user)
        application = Fixtures.next_application(application_id=user.user_id)
        expected = Fixtures.next_table(application=application)
        self.get_proxy().put_table(table=expected)

        actual: Table = self.get_proxy().get_table(
            table_uri=checkNotNone(expected.key))

        self.assertEqual(user.user_id, actual.owners[0].user_id)
    def test_get_latest_updated_ts(self) -> None:
        application = Fixtures.next_application()
        self.get_proxy().put_app(data=application)
        table = Fixtures.next_table(application=application)
        table_uri: str = checkNotNone(table.key)
        self.get_proxy().put_table(table=table)
        res = self.get_proxy().get_latest_updated_ts()
        self.assertEqual(type(res), int)
        actual: Table = self.get_proxy().get_table(table_uri=table_uri)
        self.assertEqual(actual.last_updated_timestamp, res)

        # try posting the same table again and make sure the timestamp updates
        time.sleep(1)
        self.get_proxy().put_table(table=table)
        res2 = self.get_proxy().get_latest_updated_ts()
        self.assertNotEqual(res, res2)
        actual = self.get_proxy().get_table(table_uri=table_uri)
        self.assertEqual(actual.last_updated_timestamp, res2)
    def test_get_popular_tables(self) -> None:
        application = Fixtures.next_application()
        self.get_proxy().put_app(data=application)
        # Add 10 tables
        tables: List[Table] = [
            Fixtures.next_table(application=application) for _ in range(10)
        ]
        self.get_proxy().post_tables(tables=tables)

        user = Fixtures.next_user()
        self.get_proxy().put_user(data=user)

        # add reads to 6 of them, expecting that only the top five will be "popular"
        expected_popular_tables = []
        reads = 0
        for i in range(6):
            table_name: str = checkNotNone(tables[i].name)
            table_uri: str = checkNotNone(tables[i].key)
            self.get_proxy().add_read_count(table_uri=table_uri,
                                            user_id=f'{user.user_id}',
                                            read_count=reads)
            if reads > 0:
                expected_popular_tables.append(table_name)
            reads += 1000

        # ensure popular tables returns those 5 we added
        actual_popular_tables = self.get_proxy().get_popular_tables(
            num_entries=5)
        self.assertEqual(len(actual_popular_tables), 5)

        popular_tables = []
        for table in tables:
            if table.name in expected_popular_tables:
                popular_tables.append(
                    PopularTable(database=table.database,
                                 cluster=table.cluster,
                                 schema=table.schema,
                                 name=table.name,
                                 description=table.description))
        self.assertEqual(sorted(actual_popular_tables), sorted(popular_tables))
 def test_owner_rt(self) -> None:
     application = Fixtures.next_application()
     self.get_proxy().put_app(data=application)
     table = Fixtures.next_table(application=application)
     self.get_proxy().put_table(table=table)
     user = Fixtures.next_user()
     self.get_proxy().put_user(data=user)
     user_id: str = user.user_id or 'test'
     self.get_proxy().add_owner(table_uri=checkNotNone(table.key),
                                owner=user_id)
     table = self.get_proxy().get_table(table_uri=checkNotNone(table.key))
     self.assertEqual([user_id], [u.user_id for u in table.owners])
     self.get_proxy().delete_owner(table_uri=checkNotNone(table.key),
                                   owner=user_id)
     no_owner_table: Table = self.get_proxy().get_table(
         table_uri=checkNotNone(table.key))
     self.assertEqual([], no_owner_table.owners)
     relations = self.get_relationship(node_type1='User',
                                       node_key1=user_id,
                                       node_type2='Table',
                                       node_key2=checkNotNone(table.key))
     self.assertEqual(0, len(relations))