switch to equals in most of the code, fix dependencies, clean tests, make all not relation fields work with type hints

This commit is contained in:
collerek
2020-10-31 18:11:48 +01:00
parent 8fba94efa1
commit 7d5e291a19
47 changed files with 558 additions and 438 deletions

View File

@ -1,4 +1,4 @@
from typing import List
from typing import List, Optional
import databases
import sqlalchemy
@ -32,8 +32,8 @@ class Category(ormar.Model):
metadata = metadata
database = database
id: ormar.Integer(primary_key=True)
name: ormar.String(max_length=100)
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
class Item(ormar.Model):
@ -42,9 +42,9 @@ class Item(ormar.Model):
metadata = metadata
database = database
id: ormar.Integer(primary_key=True)
name: ormar.String(max_length=100)
category: ormar.ForeignKey(Category, nullable=True)
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
category= ormar.ForeignKey(Category, nullable=True)
@app.get("/items/", response_model=List[Item])

View File

@ -1,3 +1,5 @@
from typing import Optional
import databases
import sqlalchemy
@ -12,8 +14,8 @@ class Department(ormar.Model):
database = database
metadata = metadata
id: ormar.Integer(primary_key=True)
name: ormar.String(max_length=100)
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
class Course(ormar.Model):
@ -21,10 +23,10 @@ class Course(ormar.Model):
database = database
metadata = metadata
id: ormar.Integer(primary_key=True)
name: ormar.String(max_length=100)
completed: ormar.Boolean(default=False)
department: ormar.ForeignKey(Department)
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
completed= ormar.Boolean(default=False)
department= ormar.ForeignKey(Department)
department = Department(name='Science')

View File

@ -1,3 +1,5 @@
from typing import Optional
import databases
import sqlalchemy
@ -12,8 +14,8 @@ class Department(ormar.Model):
database = database
metadata = metadata
id: ormar.Integer(primary_key=True)
name: ormar.String(max_length=100)
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
class Course(ormar.Model):
@ -21,10 +23,10 @@ class Course(ormar.Model):
database = database
metadata = metadata
id: ormar.Integer(primary_key=True)
name: ormar.String(max_length=100)
completed: ormar.Boolean(default=False)
department: ormar.ForeignKey(Department, related_name="my_courses")
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
completed= ormar.Boolean(default=False)
department= ormar.ForeignKey(Department, related_name="my_courses")
department = Department(name='Science')

View File

@ -1,3 +1,5 @@
from typing import Optional
import databases
import sqlalchemy
@ -12,8 +14,8 @@ class Department(ormar.Model):
database = database
metadata = metadata
id: ormar.Integer(primary_key=True)
name: ormar.String(max_length=100)
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
class Course(ormar.Model):
@ -21,7 +23,7 @@ class Course(ormar.Model):
database = database
metadata = metadata
id: ormar.Integer(primary_key=True)
name: ormar.String(max_length=100)
completed: ormar.Boolean(default=False)
department: ormar.ForeignKey(Department)
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
completed= ormar.Boolean(default=False)
department= ormar.ForeignKey(Department)

View File

@ -1,3 +1,5 @@
from datetime import datetime
import databases
import sqlalchemy
from sqlalchemy import func, text
@ -14,8 +16,8 @@ class Product(ormar.Model):
metadata = metadata
database = database
id: ormar.Integer(primary_key=True)
name: ormar.String(max_length=100)
company: ormar.String(max_length=200, server_default='Acme')
sort_order: ormar.Integer(server_default=text("10"))
created: ormar.DateTime(server_default=func.now())
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
company = ormar.String(max_length=200, server_default='Acme')
sort_order = ormar.Integer(server_default=text("10"))
created= ormar.DateTime(server_default=func.now())

View File

@ -12,6 +12,6 @@ class Course(ormar.Model):
database = database
metadata = metadata
id: ormar.Integer(primary_key=True)
name: ormar.String(max_length=100)
completed: ormar.Boolean(default=False)
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
completed= ormar.Boolean(default=False)

View File

@ -15,6 +15,6 @@ class Course(ormar.Model):
database = database
metadata = metadata
id: ormar.Integer(primary_key=True)
name: ormar.String(max_length=100)
completed: ormar.Boolean(default=False)
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
completed= ormar.Boolean(default=False)

View File

@ -12,9 +12,9 @@ class Course(ormar.Model):
database = database
metadata = metadata
id: ormar.Integer(primary_key=True)
name: ormar.String(max_length=100)
completed: ormar.Boolean(default=False)
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
completed= ormar.Boolean(default=False)
print(Course.__fields__)

View File

@ -12,9 +12,9 @@ class Course(ormar.Model):
database = database
metadata = metadata
id: ormar.Integer(primary_key=True)
name: ormar.String(max_length=100)
completed: ormar.Boolean(default=False)
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
completed= ormar.Boolean(default=False)
print(Course.Meta.table.columns)

View File

@ -12,9 +12,9 @@ class Course(ormar.Model):
database = database
metadata = metadata
id: ormar.Integer(primary_key=True)
name: ormar.String(max_length=100)
completed: ormar.Boolean(default=False)
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
completed= ormar.Boolean(default=False)
print({x:v.__dict__ for x,v in Course.Meta.model_fields.items()})
"""

View File

@ -13,7 +13,7 @@ class Child(ormar.Model):
metadata = metadata
database = database
id: ormar.Integer(name='child_id', primary_key=True)
first_name: ormar.String(name='fname', max_length=100)
last_name: ormar.String(name='lname', max_length=100)
born_year: ormar.Integer(name='year_born', nullable=True)
id = ormar.Integer(name='child_id', primary_key=True)
first_name = ormar.String(name='fname', max_length=100)
last_name = ormar.String(name='lname', max_length=100)
born_year = ormar.Integer(name='year_born', nullable=True)

View File

@ -4,6 +4,6 @@ class Album(ormar.Model):
metadata = metadata
database = database
id: ormar.Integer(name='album_id', primary_key=True)
name: ormar.String(name='album_name', max_length=100)
artist: ormar.ForeignKey(Artist, name='artist_id')
id = ormar.Integer(name='album_id', primary_key=True)
name = ormar.String(name='album_name', max_length=100)
artist= ormar.ForeignKey(Artist, name='artist_id')

View File

@ -11,8 +11,8 @@ class Artist(ormar.Model):
metadata = metadata
database = database
id: ormar.Integer(name='artist_id', primary_key=True)
first_name: ormar.String(name='fname', max_length=100)
last_name: ormar.String(name='lname', max_length=100)
born_year: ormar.Integer(name='year')
children: ormar.ManyToMany(Child, through=ArtistChildren)
id = ormar.Integer(name='artist_id', primary_key=True)
first_name = ormar.String(name='fname', max_length=100)
last_name = ormar.String(name='lname', max_length=100)
born_year = ormar.Integer(name='year')
children: Optional[Union[Child, List[Child]]] = ormar.ManyToMany(Child, through=ArtistChildren)

View File

@ -1,3 +1,5 @@
from typing import Optional
import databases
import ormar
import sqlalchemy
@ -12,8 +14,8 @@ class Album(ormar.Model):
metadata = metadata
database = database
id: ormar.Integer(primary_key=True)
name: ormar.String(max_length=100)
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
class Track(ormar.Model):
@ -22,7 +24,7 @@ class Track(ormar.Model):
metadata = metadata
database = database
id: ormar.Integer(primary_key=True)
album: ormar.ForeignKey(Album)
title: ormar.String(max_length=100)
position: ormar.Integer()
id = ormar.Integer(primary_key=True)
album= ormar.ForeignKey(Album)
title = ormar.String(max_length=100)
position = ormar.Integer()

View File

@ -1,3 +1,5 @@
from typing import Optional
import databases
import sqlalchemy
@ -12,8 +14,8 @@ class Department(ormar.Model):
database = database
metadata = metadata
id: ormar.Integer(primary_key=True)
name: ormar.String(max_length=100)
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
class Course(ormar.Model):
@ -21,10 +23,10 @@ class Course(ormar.Model):
database = database
metadata = metadata
id: ormar.Integer(primary_key=True)
name: ormar.String(max_length=100)
completed: ormar.Boolean(default=False)
department: ormar.ForeignKey(Department)
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
completed= ormar.Boolean(default=False)
department= ormar.ForeignKey(Department)
department = Department(name='Science')

View File

@ -1,3 +1,5 @@
from typing import Optional, Union, List
import databases
import ormar
import sqlalchemy
@ -12,9 +14,9 @@ class Author(ormar.Model):
database = database
metadata = metadata
id: ormar.Integer(primary_key=True)
first_name: ormar.String(max_length=80)
last_name: ormar.String(max_length=80)
id = ormar.Integer(primary_key=True)
first_name = ormar.String(max_length=80)
last_name = ormar.String(max_length=80)
class Category(ormar.Model):
@ -23,8 +25,8 @@ class Category(ormar.Model):
database = database
metadata = metadata
id: ormar.Integer(primary_key=True)
name: ormar.String(max_length=40)
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=40)
class PostCategory(ormar.Model):
@ -42,7 +44,7 @@ class Post(ormar.Model):
database = database
metadata = metadata
id: ormar.Integer(primary_key=True)
title: ormar.String(max_length=200)
categories: ormar.ManyToMany(Category, through=PostCategory)
author: ormar.ForeignKey(Author)
id = ormar.Integer(primary_key=True)
title = ormar.String(max_length=200)
categories: Optional[Union[Category, List[Category]]] = ormar.ManyToMany(Category, through=PostCategory)
author= ormar.ForeignKey(Author)