Пример #1
0
def test():
    from db import DemoDB

    def _test(db: DemoDB, save_inc: int, update_inc: int):
        # init
        db.int_val = 1

        # save
        db.int_val += save_inc
        db.save()

        assert_load_same(db, 1 + save_inc)

        def _cb(val: DemoDB) -> Any:
            val.int_val += update_inc
            return val.int_val

        db.update(_cb)

        assert_load_same(db, 1 + save_inc + update_inc)

    def assert_load_same(db: DemoDB, expect: int):
        load_db = DemoDB().with_context(db.context).load()
        assert load_db.int_val == expect

    # 测试全局
    _test(DemoDB(), 1, 10)

    # 测试设置context
    _test(DemoDB().with_context("test"), 2, 20)
Пример #2
0
def test_filepath_db():
    from db import DemoDB

    # 测试指定路径
    filepath_db = DemoDB().with_context("test_filepath").with_db_filepath("card_secrets/_local_test.txt")
    filepath_db.load()
    print(filepath_db.int_val)
    filepath_db.int_val = 666
    filepath_db.save()
    print(filepath_db.prepare_env_and_get_db_filepath())
Пример #3
0
    def test_table_keys_per_column(self):
        short_db = DemoDB(keys_per_column=1)
        self.assertEqual("""+----------------------------------------------------------------------------------------+
|                                         Track                                          |
+--------------+---------------+-----------------------+---------------------------------+
| Column       | Type          | Foreign Keys          | Reference Keys                  |
+--------------+---------------+-----------------------+---------------------------------+
| TrackId      | INTEGER       |                       | InvoiceLine.TrackId, (+ 1 more) |
| Name         | NVARCHAR(200) |                       |                                 |
| AlbumId      | INTEGER       | Album.AlbumId         |                                 |
| MediaTypeId  | INTEGER       | MediaType.MediaTypeId |                                 |
| GenreId      | INTEGER       | Genre.GenreId         |                                 |
| Composer     | NVARCHAR(220) |                       |                                 |
| Milliseconds | INTEGER       |                       |                                 |
| Bytes        | INTEGER       |                       |                                 |
| UnitPrice    | NUMERIC(10,2) |                       |                                 |
+--------------+---------------+-----------------------+---------------------------------+""".strip(),
                         '{0}'.format(short_db.tables.Track.__repr__()).strip())
Пример #4
0
# -*- coding: utf-8 -*-
"""
Created on Tue May 14 18:29:23 2019

@author: Aluno 10
"""

#pip install -U scikit-learn
#pip install yellowbrick

import pandas as pd
from db import DemoDB

database = DemoDB()
print(database.tables)

#Pegando a tabela de album e artista
album = database.tables.Album.all()
print(album.head())

#PEgando a tabela de artista
artist = database.tables.Artist.all()
print(artist.head())

#Juntando as tabelas
album_artista = pd.merge(artist, album)
print(album_artista.head())

#declarando as ligações
album_artista = pd.merge(artist, album, on="ArtistId")
Пример #5
0
 def setUp(self):
     self.db = DemoDB()
Пример #6
0
 def assert_load_same(db: DemoDB, expect: int):
     load_db = DemoDB().with_context(db.context).load()
     assert load_db.int_val == expect
Пример #7
0
import pandas as pd
from db import DemoDB, DB, list_profiles, remove_profile
import unittest


db = DemoDB()

class PandaSQLTest(unittest.TestCase):

    def setUp(self):
        pass

    def test_query_rowsum(self):
        df = db.query("select * from Artist;")
        self.assertEqual(len(df), 275)
    
    def test_query_groupby(self):
        q = "select AlbumId, sum(1) from Track group by 1"
        df = db.query(q)
        self.assertEqual(len(df), 347)

    def test_query_from_file_rowsum(self):
        with open("/tmp/testscript.sql", "w") as f:
            f.write("select * from Artist;")
        df = db.query_from_file("/tmp/testscript.sql")
        self.assertEqual(len(df), 275)

    def test_add_profile(self):
        profiles = list_profiles()
        db.save_credentials(profile="test_profile")
        self.assertEqual(len(profiles)+1, len(list_profiles()))