def test_query_image(db: SQLAlchemy, graphene_client: Client): image = Image(name="myImage") image.set_keywords("keyword1", "keyword2") db.session.add(image) db.session.commit() result = graphene_client.execute(''' query { image(imageId: 1) { imageId name public keywords } } ''') assert_that(json.dumps(result)).is_equal_to_ignoring_whitespace(''' { "data": { "image": { "imageId": 1, "name": "myImage", "public": false, "keywords": [ "keyword1", "keyword2" ] } } } ''')
def create_image(): # noinspection PyShadowingNames image = ImageModel(created=created, last_modified=last_modified, name=name, description=description) image.set_creation_date(creation_date) image.set_keywords(*([] if keywords is None else keywords)) return image
def create_image(): # noinspection PyShadowingNames image = ImageModel( created=created, last_modified=last_modified, name=name, description=description ) image.set_creation_date(creation_date) image.set_keywords(*([] if keywords is None else keywords)) return image
def test_last_modified_trigger(db: SQLAlchemy): image = Image(name="myImage") db.session.add(image) db.session.commit() assert_that(image.last_modified).is_equal_to(image.created) time.sleep(1) image.description = "myDescription" db.session.commit() assert_that((image.last_modified - image.created).seconds).is_greater_than_or_equal_to(1)
def test_last_modified(db: SQLAlchemy): """ Verifies that we can set a last_modified value when the object is created, but the trigger will still update it """ last_modified = datetime.fromtimestamp(0) image = Image(last_modified=last_modified) db.session.add(image) db.session.commit() assert_that(image.last_modified).is_equal_to(last_modified) image.description = "myDescription" db.session.commit() assert_that(image.last_modified).is_not_equal_to(last_modified)
def test_image_galleries(db): image = Image(name="myImage") gallery1 = Gallery(name="gallery1") gallery1.images.append(GalleryImage(image=image, position=0)) db.session.add(gallery1) db.session.commit() assert_that([i.gallery for i in image.galleries]).contains_only(gallery1) gallery2 = Gallery(name="gallery2") gallery2.images.append(GalleryImage(image=image, position=0)) db.session.add(gallery2) db.session.commit() assert_that([i.gallery for i in image.galleries]).contains_only(gallery1, gallery2) gallery1.images.remove(gallery1.images[0]) db.session.commit() assert_that([i.gallery for i in image.galleries]).contains_only(gallery2) assert_that(gallery1.images).is_empty() assert_that([i.image for i in gallery2.images]).contains_only(image) db.session.delete(image) db.session.commit() assert_that(db.session.query(Gallery).count()).is_equal_to(2) assert_that(gallery2.images).is_empty()
def test_delete_image_by_image_id(db: SQLAlchemy, graphene_client: Client): image = Image(name="myImage") db.session.add(image) db.session.commit() assert_that(Image.query.get(1)).is_not_none() result = graphene_client.execute(''' mutation { deleteImage(imageId: 1) { image { imageId, name } } } ''') assert_that(json.dumps(result)).is_equal_to_ignoring_whitespace(''' { "data": { "deleteImage": { "image": { "imageId": 1, "name": "myImage" } } } }''') assert_that(Image.query.get(1)).is_none()
def test_add_sets_defaults(db: SQLAlchemy): image = Image(name="myImage") assert_that(image.created).is_none() assert_that(image.last_modified).is_none() db.session.add(image) db.session.commit() assert_that(image.created).is_not_none() assert_that(image.last_modified).is_equal_to(image.created)
def test_gallery_images(db): gallery = Gallery(name="myGallery") image1 = Image(name="image1") image2 = Image(name="image2") image3 = Image(name="image3") gallery.images.append(GalleryImage(image=image3, position=2)) gallery.images.append(GalleryImage(image=image2, position=1)) gallery.images.append(GalleryImage(image=image1, position=0)) db.session.add(gallery) db.session.commit() assert_that([i.image for i in gallery.images ]).contains_sequence(image1, image2, image3) gallery.images[1].position = 2 gallery.images[2].position = 1 db.session.commit() assert_that([i.image for i in gallery.images ]).contains_sequence(image1, image3, image2) db.session.delete(gallery) db.session.commit() assert_that(db.session.query(Image).count()).is_equal_to(3)
def test_keywords(db: SQLAlchemy): image = Image() db.session.add(image) db.session.commit() assert_that(image.keywords).is_empty() image.keywords.append(Keyword(image=image, position=0, keyword="foo")) image.keywords.append(Keyword(image=image, position=2, keyword="baz")) image.keywords.append(Keyword(image=image, position=1, keyword="bar")) db.session.commit() assert_that([i.keyword for i in image.keywords]).contains_sequence("foo", "bar", "baz") image.keywords.remove(image.keywords[0]) db.session.commit() assert_that([i.keyword for i in image.keywords]).contains_sequence("bar", "baz")
def test_public(db: SQLAlchemy): image = Image() db.session.add(image) db.session.commit() assert_that(image.public).is_false() gallery = Gallery(public=False) gallery.images.append(GalleryImage(image=image, position=0)) db.session.add(gallery) db.session.commit() assert_that(image.public).is_false() gallery.public = True db.session.commit() assert_that(image.public).is_true()
def test_query_image_by_id(db: SQLAlchemy, graphene_client: Client): image = Image() db.session.add(image) db.session.commit() result = graphene_client.execute(f''' query {{ image(id: "{to_global_id("Image", 1)}") {{ imageId }} }} ''') assert_that(json.dumps(result)).is_equal_to_ignoring_whitespace(''' { "data": { "image": { "imageId": 1 } } } ''')
def test_query_all_with_images(db: SQLAlchemy): image = Image(name="myImage") db.session.add(image) db.session.commit() assert_that(Image.query.all()).contains_only(image)
def test_repr(): assert_that(repr(Image(id=31, name="my image"))).is_equal_to("<Image (id=31, name='my image')>")
def test_set_keywords(db: SQLAlchemy): image = Image() db.session.add(image) db.session.commit() assert_that(image.keywords).is_empty() image.set_keywords(*"foo bar baz".split()) db.session.commit() assert_that([i.keyword for i in image.keywords]).contains_sequence("foo", "bar", "baz") image.set_keywords(*"foo baz bar".split()) db.session.commit() assert_that([i.keyword for i in image.keywords]).contains_sequence("foo", "baz", "bar") image.set_keywords(*"a b".split()) db.session.commit() assert_that([i.keyword for i in image.keywords]).contains_sequence("a", "b") image.set_keywords("one") db.session.commit() assert_that([i.keyword for i in image.keywords]).contains_sequence("one") image.set_keywords() db.session.commit() assert_that(image.keywords).is_empty()