def test_passing_language(graphql_client, post_factory): post_factory( title=LazyI18nString({"en": "this is a test", "it": "questa è una prova"}), slug=LazyI18nString({"en": "slug", "it": "lumaca"}), content=LazyI18nString({"en": "content", "it": "contenuto"}), excerpt=LazyI18nString({"en": "excerpt", "it": "sommario"}), published=timezone.now() - timedelta(days=1), image=None, ) resp = graphql_client.query( """query { blogPost(slug: "slug") { title(language: "it") slug(language: "it") content(language: "it") excerpt(language: "it") } } """ ) assert not resp.get("errors") assert resp["data"]["blogPost"] == { "title": "questa è una prova", "slug": "lumaca", "content": "contenuto", "excerpt": "sommario", }
def test_passing_language(graphql_client, page_factory): page_factory( title=LazyI18nString({ "en": "this is a test", "it": "questa è una prova" }), slug=LazyI18nString({ "en": "slug", "it": "lumaca" }), content=LazyI18nString({ "en": "content", "it": "contenuto" }), published=True, image=None, conference__code="pycon11", ) resp = graphql_client.query("""query { page(code: "pycon11", slug: "slug") { title(language: "it") slug(language: "it") } } """) assert not resp.get("errors") assert resp["data"]["page"] == { "title": "questa è una prova", "slug": "lumaca" }
def test_get_single_conference_keynote( conference_factory, keynote_factory, keynote_speaker_factory, graphql_client, topic_factory, ): conference = conference_factory() keynote = keynote_factory( slug=LazyI18nString({ "en": "title", "it": "titolo" }), title=LazyI18nString({ "en": "title", "it": "titolo" }), conference=conference, topic=topic_factory(), ) speaker = keynote_speaker_factory(keynote=keynote) resp = graphql_client.query( """ query($code: String!, $slug: String!) { conference(code: $code) { keynote(slug: $slug) { title(language: "en") topic { id name } speakers { name } } } } """, variables={ "code": conference.code, "slug": "title" }, ) assert "errors" not in resp keynote_data = resp["data"]["conference"]["keynote"] assert keynote_data["title"] == "title" assert keynote_data["topic"]["id"] == str(keynote.topic.id) assert len(keynote_data["speakers"]) == 1 assert {"name": speaker.name} in keynote_data["speakers"]
def test_get_conference_copy(conference_factory, generic_copy_factory, graphql_client): conference = conference_factory() conference_b = conference_factory() generic_copy_factory( conference=conference, key="intro", content=LazyI18nString({"en": "hello!"}) ) resp = graphql_client.query( """ query($code: String!, $key: String!) { conference(code: $code) { copy(key: $key) } } """, variables={"code": conference.code, "key": "intro"}, ) assert "errors" not in resp assert resp["data"]["conference"]["copy"] == "hello!" resp = graphql_client.query( """ query($code: String!, $key: String!) { conference(code: $code) { copy(key: $key) } } """, variables={"code": conference_b.code, "key": "intro"}, ) assert "errors" not in resp assert resp["data"]["conference"]["copy"] is None
def test_query_single_page(rf, graphql_client, user_factory, page_factory): request = rf.get("/") page = page_factory( slug=LazyI18nString({"en": "demo"}), published=True, image=None, conference__code="pycon11", ) resp = graphql_client.query("""query { page(code: "pycon11", slug: "demo") { id title slug content image } } """) assert not resp.get("errors") assert { "id": str(page.id), "title": str(page.title), "slug": str(page.slug), "content": str(page.content), "image": _get_image_url(request, page.image), } == resp["data"]["page"] resp = graphql_client.query("""query { page(slug: "demo", code: "pyconb") { id } } """) assert resp["data"]["page"] is None
def test_get_conference_faqs(conference_factory, faq_factory, graphql_client): conference = conference_factory() conference_b = conference_factory() faq_factory( conference=conference, question=LazyI18nString({"en": "Do you love this conference?"}), answer=LazyI18nString({"en": "Yes!"}), ) resp = graphql_client.query( """ query($code: String!) { conference(code: $code) { faqs { question answer } } } """, variables={"code": conference.code}, ) assert "errors" not in resp assert resp["data"]["conference"]["faqs"] == [{ "question": "Do you love this conference?", "answer": "Yes!" }] resp = graphql_client.query( """ query($code: String!) { conference(code: $code) { faqs { question answer } } } """, variables={"code": conference_b.code}, ) assert "errors" not in resp assert resp["data"]["conference"]["faqs"] == []
def test_filter_by_slug(page_factory): slug_en = "demo" slug_it = "prova" page_factory(slug=LazyI18nString({"en": slug_en, "it": slug_it}), published=True) page_factory(published=True) assert Page.published_pages.by_slug(slug_it).count() == 1 assert Page.published_pages.by_slug(slug_en).count() == 1
def test_query_events(graphql_client, conference_factory, event_factory): now = timezone.now() conference = conference_factory(start=now, end=now + timezone.timedelta(days=3)) event_factory( conference=conference, title=LazyI18nString({ "en": "hello world", "it": "ciao mondo" }), slug=LazyI18nString({ "en": "slug", "it": "lumaca" }), ) resp = graphql_client.query( """query($code: String!) { conference(code: $code) { events { title slug titleIt: title(language: "it") slugIt: slug(language: "it") } } }""", variables={"code": conference.code}, ) assert not resp.get("errors") assert len(resp["data"]["conference"]["events"]) == 1 event = resp["data"]["conference"]["events"][0] assert event["title"] == "hello world" assert event["titleIt"] == "ciao mondo" assert event["slug"] == "slug" assert event["slugIt"] == "lumaca"
def test_get_conference_keynotes_without_topic( conference_factory, keynote_factory, keynote_speaker_factory, graphql_client, rf, ): conference = conference_factory() keynote = keynote_factory(title=LazyI18nString({ "en": "title", "it": "titolo" }), conference=conference) speaker = keynote_speaker_factory(keynote=keynote) resp = graphql_client.query( """ query($code: String!) { conference(code: $code) { keynotes { title(language: "en") topic { id name } speakers { name photo } } } } """, variables={"code": conference.code}, ) assert "errors" not in resp assert len(resp["data"]["conference"]["keynotes"]) == 1 keynote_data = resp["data"]["conference"]["keynotes"][0] assert keynote_data["title"] == "title" assert keynote_data["topic"] is None assert len(keynote_data["speakers"]) == 1 req = rf.get("/") assert { "name": speaker.name, "photo": get_image_url_from_request(req, speaker.photo), } in keynote_data["speakers"]
def generate(self, extra_kwargs=None): kwargs = {} kwargs.update(self.provider_kwargs) kwargs.update(extra_kwargs or {}) locale_to_faker = {"en": "en_US", "it": "it_IT"} data = {} for lang, _ in settings.LANGUAGES: fake = self._get_faker(locale_to_faker[lang]) data[lang] = fake.format(self.provider, **kwargs) return LazyI18nString(data)
def test_passing_language(graphql_client, job_listing_factory): job_listing_factory( title=LazyI18nString({ "en": "this is a test", "it": "diventa una lumaca" }), slug=LazyI18nString({ "en": "slug", "it": "lumaca" }), ) resp = graphql_client.query("""query { jobListing(slug: "slug") { title(language: "it") slug(language: "it") } } """) assert not resp.get("errors") assert resp["data"]["jobListing"] == { "title": "diventa una lumaca", "slug": "lumaca", }
def test_defaults_on_browser_language(graphql_client, page_factory): page_factory( title=LazyI18nString({"en": "this is a test", "it": "questa è una prova"}), slug=LazyI18nString({"en": "slug", "it": "lumaca"}), content=LazyI18nString({"en": "content", "it": "contenuto"}), published=True, image=None, conference__code="pycon11", ) headers = {"HTTP_ACCEPT_LANGUAGE": "it;q=0.8,de;q=0.7,la;q=0.6"} resp = graphql_client.query( """query { page(code: "pycon11", slug: "slug") { title slug } } """, headers=headers, ) assert not resp.get("errors") assert resp["data"]["page"] == {"title": "questa è una prova", "slug": "lumaca"}
def test_query_single_post(rf, graphql_client, user_factory, post_factory): request = rf.get("/") post = post_factory( slug=LazyI18nString({ "en": "demo", "it": "esempio" }), published=timezone.now() - timedelta(days=1), image=None, ) resp = graphql_client.query("""query { blogPost(slug: "demo") { id title slug excerpt content published image author { id } } } """) assert { "id": str(post.id), "title": str(post.title), "slug": str(post.slug), "excerpt": str(post.excerpt), "content": str(post.content), "published": post.published.isoformat(), "image": get_image_url_from_request(request, post.image), "author": { "id": str(post.author_id) }, } == resp["data"]["blogPost"] resp = graphql_client.query("""query { blogPost(slug: "donut") { id } } """) assert resp["data"]["blogPost"] is None
def test_query_single_job_listing(rf, graphql_client, job_listing_factory): request = rf.get("/") listing = job_listing_factory( slug=LazyI18nString({ "en": "demo", "it": "esempio" }), company_logo=None, ) resp = graphql_client.query("""query { jobListing(slug: "demo") { id title slug description company companyLogo applyUrl } } """) assert { "id": str(listing.id), "title": str(listing.title), "slug": str(listing.slug), "description": str(listing.description), "company": str(listing.company), "companyLogo": None, "applyUrl": str(listing.apply_url), } == resp["data"]["jobListing"] resp = graphql_client.query("""query { jobListing(slug: "donut") { id } } """) assert resp["data"]["jobListing"] is None
def test_conference_to_str(conference_factory): assert "Ciao Mondo <ep1>" == str( conference_factory(name=LazyI18nString({"en": "Ciao Mondo"}), code="ep1"))
def test_init_json(): s = LazyI18nString('{"en": "Hello World!", "it": "Ciao Mondo!"}') assert s.data == {"en": "Hello World!", "it": "Ciao Mondo!"} assert bool(s) is True
def test_init_invalid_json(): s = LazyI18nString("Hello World!") assert s.data == "Hello World!" assert bool(s) is True
def test_empty_string(): s = LazyI18nString({}) assert s.data == {} assert bool(s) is False
def test_init_with_dictionary(): s = LazyI18nString({"en": "Hello World!", "it": "Ciao Mondo!"}) assert s.data == {"en": "Hello World!", "it": "Ciao Mondo!"} assert bool(s) is True
def test_not_equal(): s1 = LazyI18nString({"en": "Hello World!", "it": "Ciao Mondo!"}) s2 = LazyI18nString({"en": "Goodbye World!", "it": "Arrivederci Mondo!"}) assert not s1 == s2
def test_not_equal_none(): s1 = LazyI18nString({"en": "Hello World!", "it": "Ciao Mondo!"}) s2 = None assert not s1 == s2
def test_equal(): s1 = LazyI18nString({"en": "Hello World!", "it": "Ciao Mondo!"}) s2 = LazyI18nString({"en": "Hello World!", "it": "Ciao Mondo!"}) assert s1 == s2