def test_add_index(self) -> None: t = Table('products') c1 = Column('id', 'integer') c2 = Column('name', 'varchar2') i1 = Index(['id']) i2 = Index(['name']) t.add_column(c1) t.add_column(c2) t.add_index(i1) t.add_index(i2) self.assertEqual(i1.table, t) self.assertEqual(i2.table, t) self.assertEqual(t.indexes, [i1, i2])
def parse_index(s, l, t): ''' (id, country) [pk] // composite primary key or "created_at" or booking_date [ name: 'name', unique ] ''' init_dict = {} if isinstance(t['subject'], str): subjects = [t['subject']] else: subjects = list(t['subject']) init_dict['subject_names'] = subjects settings = t.get('settings', {}) init_dict.update(settings) # comments after settings have priority if 'comment' in t: init_dict['comment'] = t['comment'][0] if 'comment' not in init_dict and 'comment_before' in t: comment = '\n'.join(c[0] for c in t['comment_before']) init_dict['comment'] = comment return Index(**init_dict)
def test_add_bad_index(self) -> None: t = Table('products') c = Column('id', 'integer') i = Index(['id', 'name']) t.add_column(c) with self.assertRaises(ColumnNotFoundError): t.add_index(i)
def test_basic_sql(self) -> None: t = Table('products') t.add_column(Column('id', 'integer')) r = Index(subject_names=['id'], table=t) t.add_index(r) expected = 'CREATE INDEX ON "products" ("id");' self.assertEqual(r.sql, expected)
def test_composite_with_expression(self) -> None: t = Table('products') t.add_column(Column('id', 'integer')) r = Index(subject_names=['id', '(id*3)'], table=t) t.add_index(r) self.assertEqual(r.subjects, [t['id'], '(id*3)']) expected = 'CREATE INDEX ON "products" ("id", (id*3));' self.assertEqual(r.sql, expected)
def test_pk(self) -> None: t = Table('products') t.add_column(Column('id', 'integer')) t.add_column(Column('name', 'varchar')) r = Index(subject_names=['id', 'name'], table=t, pk=True) t.add_index(r) expected = 'PRIMARY KEY ("id", "name")' self.assertEqual(r.sql, expected)
def test_note(self) -> None: t = Table('products') t.add_column(Column('id', 'integer')) n = Note('Index note') r = Index(subject_names=['id'], table=t, note=n) t.add_index(r) expected = 'CREATE INDEX ON "products" ("id"); -- Index note' self.assertEqual(r.sql, expected)
def test_unique_type_composite(self) -> None: t = Table('products') t.add_column(Column('id', 'integer')) t.add_column(Column('name', 'varchar')) r = Index(subject_names=['id', 'name'], table=t, type_='hash', unique=True) t.add_index(r) expected = 'CREATE UNIQUE INDEX ON "products" USING HASH ("id", "name");' self.assertEqual(r.sql, expected)
def test_index_inline(self) -> None: t = Table('products') c1 = Column('id', 'integer') c2 = Column('name', 'varchar2') t.add_column(c1) t.add_column(c2) i = Index(['id', 'name'], pk=True) t.add_index(i) expected = \ '''CREATE TABLE "products" ( "id" integer, "name" varchar2, PRIMARY KEY ("id", "name") ); ''' self.assertEqual(t.sql, expected)
def test_ref_index(self) -> None: t = Table('products') c1 = Column('id', 'integer') c2 = Column('name', 'varchar2') t.add_column(c1) t.add_column(c2) t2 = Table('names') c21 = Column('name_val', 'varchar2') t2.add_column(c21) r = TableReference(c2, t2, c21) t.add_ref(r) i = Index(['id', 'name']) t.add_index(i) expected = \ '''CREATE TABLE "products" ( "id" integer, "name" varchar2, FOREIGN KEY ("name") REFERENCES "names ("name_val") ); CREATE INDEX ON "products" ("id", "name"); ''' self.assertEqual(t.sql, expected)