def test_prefetched_queryset_level_1_relation(self): create_samples(continent_names=['europe', 'asia'], country_names=['germany', 'south korea'], continent_fields=['name', 'denonym'], country_fields=['name', 'denonym'], langs=['de', 'tr']) lvl_1 = ('countries', ) continents = Continent.objects.prefetch_related(*lvl_1) europe = [x for x in continents if x.code == 'EU'][0] germany = europe.countries.all()[0] asia = [x for x in continents if x.code == 'AS'][0] south_korea = asia.countries.all()[0] hierarchy = _get_relations_hierarchy(*lvl_1) ct_continent = ContentType.objects.get_for_model(Continent) ct_country = ContentType.objects.get_for_model(Country) mapping, query = _get_purview(continents, hierarchy) self.assertDictEqual( mapping, { ct_continent.id: { str(europe.pk): europe, str(asia.pk): asia }, ct_country.id: { str(germany.pk): germany, str(south_korea.pk): south_korea }, })
def test_prefetched_instance_level_2_relation(self): create_samples(continent_names=['europe'], country_names=['germany'], city_names=['cologne'], continent_fields=['name', 'denonym'], country_fields=['name', 'denonym'], city_fields=['name', 'denonym'], langs=['de', 'tr']) lvl_2 = ('countries__cities', ) europe = Continent.objects.prefetch_related(*lvl_2).get(code='EU') germany = europe.countries.all()[0] cologne = germany.cities.all()[0] hierarchy = _get_relations_hierarchy(*lvl_2) ct_continent = ContentType.objects.get_for_model(Continent) ct_city = ContentType.objects.get_for_model(City) mapping, query = _get_purview(europe, hierarchy) self.assertDictEqual( mapping, { ct_continent.id: { str(europe.pk): europe }, ct_city.id: { str(cologne.id): cologne } })
def test_queryset_level_1_2_relation_with_lang(self): create_samples(continent_names=['europe', 'asia'], country_names=['germany', 'south korea'], city_names=['cologne', 'seoul'], continent_fields=['name', 'denonym'], country_fields=['name', 'denonym'], city_fields=['name', 'denonym'], langs=['de', 'tr']) lvl_1_2 = ( 'countries', 'countries__cities', ) continents = Continent.objects.all() hierarchy = _get_relations_hierarchy(*lvl_1_2) mapping, query = _get_purview(continents, hierarchy) self.assertQuerysetEqual( _get_translations(query, 'de').order_by('id'), [ '<Translation: Europe: Europa>', '<Translation: European: Europäisch>', '<Translation: Germany: Deutschland>', '<Translation: German: Deutsche>', '<Translation: Cologne: Köln>', '<Translation: Cologner: Kölner>', '<Translation: Asia: Asien>', '<Translation: Asian: Asiatisch>', '<Translation: South Korea: Südkorea>', '<Translation: South Korean: Südkoreanisch>', '<Translation: Seoul: Seül>', '<Translation: Seouler: Seüler>', ])
def test_instance_level_1_relation(self): create_samples(continent_names=['europe'], country_names=['germany'], continent_fields=['name', 'denonym'], country_fields=['name', 'denonym'], langs=['de', 'tr']) lvl_1 = ('countries', ) europe = Continent.objects.get(code='EU') germany = europe.countries.all()[0] hierarchy = _get_relations_hierarchy(*lvl_1) ct_continent = ContentType.objects.get_for_model(Continent) ct_country = ContentType.objects.get_for_model(Country) mapping, query = _get_purview(europe, hierarchy) self.assertDictEqual( mapping, { ct_continent.id: { str(europe.pk): europe }, ct_country.id: { str(germany.pk): germany } })
def test_instance_level_1_2_relation_with_lang(self): create_samples(continent_names=['europe'], country_names=['germany'], city_names=['cologne'], continent_fields=['name', 'denonym'], country_fields=['name', 'denonym'], city_fields=['name', 'denonym'], langs=['de', 'tr']) lvl_1_2 = ( 'countries', 'countries__cities', ) europe = Continent.objects.get(code='EU') hierarchy = _get_relations_hierarchy(*lvl_1_2) mapping, query = _get_purview(europe, hierarchy) self.assertQuerysetEqual( _get_translations(query, 'de').order_by('id'), [ '<Translation: Europe: Europa>', '<Translation: European: Europäisch>', '<Translation: Germany: Deutschland>', '<Translation: German: Deutsche>', '<Translation: Cologne: Köln>', '<Translation: Cologner: Kölner>', ])
def test_one_translations_rel(self): create_samples(continent_names=['europe'], continent_fields=['name', 'denonym'], langs=['de']) europe = Continent.objects.get(code='EU') self.assertQuerysetEqual(europe.translations.order_by('id'), [ '<Translation: Europe: Europa>', '<Translation: European: Europäisch>', ])
def test_prefetched_instance_level_0_relation(self): create_samples(continent_names=['europe'], continent_fields=['name', 'denonym'], langs=['de', 'tr']) europe = Continent.objects.get(code='EU') hierarchy = _get_relations_hierarchy() ct_continent = ContentType.objects.get_for_model(Continent) mapping, query = _get_purview(europe, hierarchy) self.assertDictEqual(mapping, {ct_continent.id: { str(europe.pk): europe }})
def test_accent_not_exists(self): create_samples(continent_names=['europe', 'asia'], country_names=['germany', 'south korea'], city_names=['cologne', 'seoul'], continent_fields=['name', 'denonym'], country_fields=['name', 'denonym'], city_fields=['name', 'denonym'], langs=['de', 'tr']) europe = Continent.objects.get(code='EU') germany = europe.countries.all()[0] cologne = germany.cities.all()[0] response = self.client.get(reverse('sample:continent_detail', args=(europe.id, )), HTTP_ACCEPT_LANGUAGE='de-at') self.assertEqual(response.status_code, 200) self.assertDictEqual( json.loads(response.content.decode('utf-8')), { 'id': europe.id, 'code': 'EU', 'name': 'Europa', 'denonym': 'Europäisch', 'countries': [{ 'id': germany.id, 'code': 'DE', 'name': 'Deutschland', 'denonym': 'Deutsche', 'cities': [{ 'id': cologne.id, 'name': 'Köln', 'denonym': 'Kölner' }] }] })
def test_queryset_level_0_relation_with_lang(self): create_samples(continent_names=['europe', 'asia'], country_names=['germany', 'south korea'], city_names=['cologne', 'seoul'], continent_fields=['name', 'denonym'], country_fields=['name', 'denonym'], city_fields=['name', 'denonym'], langs=['de', 'tr']) continents = Continent.objects.all() hierarchy = _get_relations_hierarchy() mapping, query = _get_purview(continents, hierarchy) self.assertQuerysetEqual( _get_translations(query, 'de').order_by('id'), [ '<Translation: Europe: Europa>', '<Translation: European: Europäisch>', '<Translation: Asia: Asien>', '<Translation: Asian: Asiatisch>', ])
def test_queryset_level_2_relation(self): create_samples(continent_names=['europe', 'asia'], country_names=['germany', 'south korea'], city_names=['cologne', 'seoul'], continent_fields=['name', 'denonym'], country_fields=['name', 'denonym'], city_fields=['name', 'denonym'], langs=['de', 'tr']) lvl_2 = ('countries__cities', ) continents = Continent.objects.all() europe = [x for x in continents if x.code == 'EU'][0] germany = europe.countries.all()[0] cologne = germany.cities.all()[0] asia = [x for x in continents if x.code == 'AS'][0] south_korea = asia.countries.all()[0] seoul = south_korea.cities.all()[0] hierarchy = _get_relations_hierarchy(*lvl_2) ct_continent = ContentType.objects.get_for_model(Continent) ct_city = ContentType.objects.get_for_model(City) mapping, query = _get_purview(continents, hierarchy) self.assertDictEqual( mapping, { ct_continent.id: { str(europe.pk): europe, str(asia.pk): asia }, ct_city.id: { str(cologne.id): cologne, str(seoul.id): seoul } })
def test_prefetched_queryset_level_0_relation(self): create_samples(continent_names=['europe', 'asia'], continent_fields=['name', 'denonym'], langs=['de', 'tr']) continents = Continent.objects.all() europe = [x for x in continents if x.code == 'EU'][0] asia = [x for x in continents if x.code == 'AS'][0] hierarchy = _get_relations_hierarchy() ct_continent = ContentType.objects.get_for_model(Continent) mapping, query = _get_purview(continents, hierarchy) self.assertDictEqual(mapping, { ct_continent.id: { str(europe.pk): europe, str(asia.pk): asia }, })
def test_language(self): create_samples(continent_names=['europe', 'asia'], country_names=['germany', 'south korea'], city_names=['cologne', 'seoul'], continent_fields=['name', 'denonym'], country_fields=['name', 'denonym'], city_fields=['name', 'denonym'], langs=['de', 'tr']) continents = Continent.objects.all() europe = [x for x in continents if x.code == 'EU'][0] germany = europe.countries.all()[0] cologne = germany.cities.all()[0] asia = [x for x in continents if x.code == 'AS'][0] south_korea = asia.countries.all()[0] seoul = south_korea.cities.all()[0] response = self.client.get(reverse('sample:continent_list'), HTTP_ACCEPT_LANGUAGE='de') self.assertEqual(response.status_code, 200) self.assertListEqual(json.loads(response.content.decode('utf-8')), [{ 'id': europe.id, 'code': 'EU', 'name': 'Europa', 'denonym': 'Europäisch', 'countries': [{ 'id': germany.id, 'code': 'DE', 'name': 'Deutschland', 'denonym': 'Deutsche', 'cities': [{ 'id': cologne.id, 'name': 'Köln', 'denonym': 'Kölner' }] }] }, { 'id': asia.id, 'code': 'AS', 'name': 'Asien', 'denonym': 'Asiatisch', 'countries': [{ 'id': south_korea.id, 'code': 'KR', 'name': 'Südkorea', 'denonym': 'Südkoreanisch', 'cities': [{ 'id': seoul.id, 'name': 'Seül', 'denonym': 'Seüler' }] }] }])
def test_instance(self): create_samples(continent_names=['europe']) europe = Continent.objects.get(code='EU') self.assertEqual(_get_entity_details(europe), (False, Continent))
def test_queryset(self): create_samples(continent_names=['europe', 'asia']) continents = Continent.objects.all() self.assertEqual(_get_entity_details(continents), (True, Continent))
def test_iterable(self): create_samples(continent_names=['europe', 'asia']) continents = list(Continent.objects.all()) self.assertEqual(_get_entity_details(continents), (True, Continent))