Browse Source

move the database tests to the respective file...

main
John-Mark Gurney 4 years ago
parent
commit
55620d1f26
3 changed files with 44 additions and 38 deletions
  1. +5
    -36
      bitelab/__init__.py
  2. +37
    -1
      bitelab/data.py
  3. +2
    -1
      bitelab/testing.py

+ 5
- 36
bitelab/__init__.py View File

@@ -336,8 +336,10 @@ async def release_board(board_id, user: str = Depends(lookup_user),

return brd

@router.post('/board/{board_id_or_class}/attrs', response_model=Union[Board, Error])
async def set_board_attrs():
@router.post('/board/{board_id}/attrs', response_model=Union[Board, Error])
async def set_board_attrs(board_id,
attrs: Dict[str, Any],
brdmgr: BoardManager = Depends(get_boardmanager)):
pass

@router.get('/board/',response_model=Dict[str, Board])
@@ -392,10 +394,6 @@ class TestUnhashLRU(unittest.TestCase):
# does not return the same object as the first cache
self.assertIsNot(cachefun(lsta), cachefun2(lsta))

async def _setup_data(data):
await data.APIKey.objects.create(user='foo', key='thisisanapikey')
await data.APIKey.objects.create(user='bar', key='anotherlongapikey')

# Per RFC 5737 (https://tools.ietf.org/html/rfc5737):
# The blocks 192.0.2.0/24 (TEST-NET-1), 198.51.100.0/24 (TEST-NET-2),
# and 203.0.113.0/24 (TEST-NET-3) are provided for use in
@@ -421,7 +419,7 @@ class TestBiteLab(unittest.IsolatedAsyncioTestCase):
self.dbtempfile.name)
self.data = make_orm(self.database)

await _setup_data(self.data)
await data._setup_data(self.data)

# setup settings
self.settings = config.Settings(db_file=self.dbtempfile.name,
@@ -635,32 +633,3 @@ class TestBiteLab(unittest.IsolatedAsyncioTestCase):
'attrs': { 'power': True },
}
self.assertEqual(res.json(), info)

class TestDatabase(unittest.IsolatedAsyncioTestCase):
def setUp(self):
# setup temporary directory
self.dbtempfile = tempfile.NamedTemporaryFile()

self.database = data.databases.Database('sqlite:///' +
self.dbtempfile.name)
self.data = make_orm(self.database)

def tearDown(self):
self.data = None
self.database = None
self.dbtempfile = None

async def test_apikey(self):
data = self.data

# that the test database starts empty
self.assertEqual(await data.APIKey.objects.all(), [])

# that when it is populated with test data
await _setup_data(data)

# the data can be accessed
self.assertEqual((await data.APIKey.objects.get(
key='thisisanapikey')).user, 'foo')
self.assertEqual((await data.APIKey.objects.get(
key='anotherlongapikey')).user, 'bar')

+ 37
- 1
bitelab/data.py View File

@@ -27,11 +27,14 @@
#

from typing import Optional, Union, Dict, Any
import databases
from pydantic import BaseModel, Field
from datetime import datetime

import databases
import orm
import sqlalchemy
import tempfile
import unittest

__all__ = [ 'make_orm', 'BoardClassInfo', 'Board', 'Error' ]

@@ -91,3 +94,36 @@ def make_orm(database):
setattr(r, i, lcls[i])

return r

async def _setup_data(data):
await data.APIKey.objects.create(user='foo', key='thisisanapikey')
await data.APIKey.objects.create(user='bar', key='anotherlongapikey')

class TestDatabase(unittest.IsolatedAsyncioTestCase):
def setUp(self):
# setup temporary directory
self.dbtempfile = tempfile.NamedTemporaryFile()

self.database = databases.Database('sqlite:///' +
self.dbtempfile.name)
self.data = make_orm(self.database)

def tearDown(self):
self.data = None
self.database = None
self.dbtempfile = None

async def test_apikey(self):
data = self.data

# that the test database starts empty
self.assertEqual(await data.APIKey.objects.all(), [])

# that when it is populated with test data
await _setup_data(data)

# the data can be accessed
self.assertEqual((await data.APIKey.objects.get(
key='thisisanapikey')).user, 'foo')
self.assertEqual((await data.APIKey.objects.get(
key='anotherlongapikey')).user, 'bar')

+ 2
- 1
bitelab/testing.py View File

@@ -29,5 +29,6 @@
# Module that includes all the test cases.

from .snmp import TestSNMPPower, TestSNMPWrapper
from . import TestDatabase, TestBiteLab, TestUnhashLRU
from .data import TestDatabase
from . import TestBiteLab, TestUnhashLRU
from .__main__ import TestClient

Loading…
Cancel
Save