Пример #1
0
    def testSlice(self):
        schema = tc.table.Schema([
            tc.Column("0", tc.U64),
            tc.Column("1", tc.U64),
            tc.Column("2", tc.U64),
            tc.Column("3", tc.U64),
        ], [
            tc.Column("value", tc.Number),
        ])

        for i in range(4):
            schema.create_index(str(i), [str(i)])

        data = [
            ([0, 0, 1, 0], 1),
            ([0, 1, 2, 0], 2),
            ([1, 0, 0, 0], 3),
            ([1, 0, 1, 0], 3),
        ]

        cxt = tc.Context()
        cxt.table = tc.table.Table(schema)
        cxt.inserts = [cxt.table.insert(coord, [value]) for (coord, value) in data]
        cxt.result = tc.After(cxt.inserts, cxt.table.where({
            "0": slice(2),
            "1": slice(3),
            "2": slice(4),
            "3": slice(1)
        }))

        expect = expected(schema, [coord + [value] for coord, value in data])
        actual = self.host.post(ENDPOINT, cxt)
        self.assertEqual(actual, expect)
Пример #2
0
    def setUpClass(cls):
        users = tc.table.Schema([tc.Column("user_id", tc.U32)], [
            tc.Column("email", tc.String, 320),
            tc.Column("display_name", tc.String, 100)
        ])

        products = tc.table.Schema(
            [tc.Column("sku", tc.U32)],
            [tc.Column("name", tc.String, 256),
             tc.Column("price", tc.U32)])

        orders = tc.table.Schema([tc.Column("order_id", tc.U32)], [
            tc.Column("user_id", tc.U32),
            tc.Column("sku", tc.U32),
            tc.Column("quantity", tc.U32)
        ]).create_index("user", ["user_id"]).create_index("product", ["sku"])

        schema = (tc.graph.Schema().create_table("user", users).create_table(
            "product", products).create_table("order", orders).create_edge(
                "friend", tc.graph.edge.Schema(
                    "user.user_id", "user.user_id")).create_edge(
                        "order_product",
                        tc.graph.edge.Schema("product.sku",
                                             "order.sku")).create_edge(
                                                 "user_order",
                                                 tc.graph.edge.Schema(
                                                     "user.user_id",
                                                     "order.user_id")))

        cls.host = start_host("test_graph", [TestGraph(schema=schema)])
Пример #3
0
class Product(tc.app.Model):
    __uri__ = URI.append("Product")

    price = tc.Column("price", tc.I32)
    name = tc.Column("name", tc.String, 100)

    def __init__(self, product, name, price):
        self.product = product
        self.price = price
        self.name = name
Пример #4
0
class User(tc.app.Model):
    __uri__ = URI.append("User")

    first_name = tc.Column("first_name", tc.String, 100)
    last_name = tc.Column("last_name", tc.String, 100)

    def __init__(self, user_id, first_name, last_name):
        self.user_id = user_id
        self.first_name = first_name
        self.last_name = last_name
Пример #5
0
 def test_key(self):
     """Parameterized unit test for the `key` function."""
     cases = [
         (User, [tc.Column("user_id", tc.U32)]),
         (Order, [tc.Column("order_id", tc.U32)]),
         (Product, [tc.Column("product_id", tc.U32)]),
     ]
     for c, e in cases:
         with self.subTest(c=c, e=e):
             self.assertEqual(c.key(), e)
Пример #6
0
class User(tc.app.Model):
    __uri__ = URI.append("Foo")

    first_name = tc.Column(tc.String, 100)
    last_name = tc.Column(tc.String, 100)
    email = tc.Column(tc.EmailAddress, 100)

    def __init__(self, first_name, last_name, email):
        self.first_name = first_name
        self.last_name = last_name
        self.email = email
Пример #7
0
 def test_createSchema_simple(self):
     """Test that creating a schema works using a basic Model."""
     schema = tc.table.create_schema(User)
     expected = tc.table.Schema(
         [tc.Column("user_id", tc.U32)],
         [
             tc.Column("first_name", tc.String, 100),
             tc.Column("last_name", tc.String, 100),
         ],
     )
     self.assertIsInstance(schema, tc.table.Schema)
     self.assertEqual(sorted(schema.columns(), key=str),
                      sorted(expected.columns(), key=str))
Пример #8
0
 def test_createSchema_complex(self):
     """Test that creating a schema works using a complex Model."""
     schema = tc.table.create_schema(Order)
     expected = (tc.table.Schema(
         [tc.Column("order_id", tc.U32)],
         [
             tc.Column("product_id", tc.U32),
             tc.Column("user_id", tc.U32),
             tc.Column("quantity", tc.I32),
         ],
     ).create_index("user",
                    ["user_id"]).create_index("product", ["product_id"]))
     self.assertIsInstance(schema, tc.table.Schema)
     self.assertEqual(sorted(schema.indices), sorted(expected.indices))
     self.assertEqual(sorted(schema.columns(), key=str),
                      sorted(expected.columns(), key=str))
Пример #9
0
 def test_createSchema_withArbitraryValues(self):
     """Test that creating a schema ignores arbitrary attributes. Only
     values of Column or Model are recognised.
     """
     schema = tc.table.create_schema(Arbitrary)
     expected = tc.table.Schema([tc.Column("arbitrary_id", tc.U32)], [])
     self.assertIsInstance(schema, tc.table.Schema)
     self.assertEqual(sorted(schema.columns(), key=str),
                      sorted(expected.columns(), key=str))
Пример #10
0
class Order(tc.app.Model):
    __uri__ = URI.append("Product")

    quantity = tc.Column("quantity", tc.I32)
    product_id = Product
    user_id = User

    def __init__(self, order_id, quantity, user_id, product_id):
        self.quantity = quantity
        self.user_id = user_id
        self.product_id = product_id
Пример #11
0
    def testSelect(self):
        count = 5
        values = [[v] for v in range(count)]
        keys = [[num2words(i)] for i in range(count)]

        cxt = tc.Context()
        cxt.table = tc.table.Table(SCHEMA)
        cxt.inserts = [cxt.table.insert(k, v) for k, v in zip(keys, values)]
        cxt.result = tc.After(cxt.inserts, cxt.table.select(["name"]))

        expected = {
            str(tc.URI(tc.table.Table)): [
                tc.to_json(tc.table.Schema([tc.Column("name", tc.String, 512)])),
                list(sorted(keys))
            ]
        }

        actual = self.host.post(ENDPOINT, cxt)

        self.assertEqual(actual, expected)
Пример #12
0
import random
import tinychain as tc
import unittest

from num2words import num2words
from .base import HostTest

ENDPOINT = "/transact/hypothetical"
SCHEMA = tc.table.Schema(
    [tc.Column("name", tc.String, 512)], [tc.Column("views", tc.UInt)]).create_index("views", ["views"])


class TableTests(HostTest):
    def testCreate(self):
        cxt = tc.Context()
        cxt.table = tc.table.Table(SCHEMA)
        cxt.result = tc.After(cxt.table.insert(("name",), (0,)), cxt.table.count())

        count = self.host.post(ENDPOINT, cxt)
        self.assertEqual(count, 1)

    def testDelete(self):
        count = 2
        values = [(v,) for v in range(count)]
        keys = [(num2words(i),) for i in range(count)]

        cxt = tc.Context()
        cxt.table = tc.table.Table(SCHEMA)
        cxt.inserts = [cxt.table.insert(k, v) for k, v in zip(keys, values)]
        cxt.delete = tc.After(cxt.inserts, cxt.table.delete())
        cxt.result = tc.After(cxt.delete, cxt.table)
Пример #13
0
import random
import tinychain as tc
import unittest

from num2words import num2words
from .base import PersistenceTest
from ..process import DEFAULT_PORT, start_host

ENDPOINT = "/transact/hypothetical"
SCHEMA = tc.table.Schema([tc.Column("name", tc.String, 512)],
                         [tc.Column("views", tc.UInt)]).create_index(
                             "views", ["views"])


class TableChainTests(PersistenceTest, unittest.TestCase):
    NAME = "table"
    NUM_HOSTS = 4

    def app(self, chain_type):
        class Persistent(tc.app.App):
            __uri__ = tc.URI(f"http://127.0.0.1:{DEFAULT_PORT}/test/table")

            def __init__(self):
                self.table = chain_type(tc.table.Table(SCHEMA))
                tc.app.App.__init__(self)

            @tc.delete
            def truncate(self):
                return self.table.delete()

        return Persistent()
Пример #14
0
import tinychain as tc
import unittest

from num2words import num2words
from .base import PersistenceTest
from ..process import DEFAULT_PORT

ENDPOINT = "/transact/hypothetical"
SCHEMA = tc.btree.Schema((tc.Column("number", tc.Int), tc.Column("word", tc.String, 100)))


class BTreeChainTests(PersistenceTest, unittest.TestCase):
    NAME = "btree"

    def app(self, chain_type):
        class Persistent(tc.app.App):
            __uri__ = tc.URI(f"http://127.0.0.1:{DEFAULT_PORT}/test/btree")

            def __init__(self):
                self.tree = chain_type(tc.btree.BTree(SCHEMA))
                tc.app.App.__init__(self)

        return Persistent()

    def execute(self, hosts):
        row1 = [1, "one"]
        row2 = [2, "two"]

        hosts[0].put("/test/btree/tree", None, row1)
        for host in hosts:
            actual = host.get("/test/btree/tree", (1,))