def test_simple_filter(self): obj = ContentType.objects.get(pk=1) obj2 = ContentType.objects.get(pk=2) set_obj_attrs(obj, {'width': 200}) set_obj_attrs(obj2, {'width': 400}) r = filter_by_attrs(ContentType.objects.all(), width=200)[0] self.assertEqual(r.pk, obj.pk)
def test_filter__in(self): obj = ContentType.objects.get(pk=1) obj2 = ContentType.objects.get(pk=2) set_obj_attrs(obj, {'width': 200}) set_obj_attrs(obj2, {'width': 400}) r = filter_by_attrs(ContentType.objects.all(), width__in=[200, 400]) self.assertEqual(r.count(), 2)
def test_filter__contains(self): obj = ContentType.objects.get(pk=1) obj2 = ContentType.objects.get(pk=2) set_obj_attrs(obj, {'tags': 'milk, tee'}) set_obj_attrs(obj2, {'tags': 'milk'}) r = filter_by_attrs(ContentType.objects.all(), tags__contains='milk') self.assertEqual(r.count(), 2) r = filter_by_attrs(ContentType.objects.all(), tags__contains='tee') self.assertEqual(r.count(), 1)
def test_prefetch_related(self): obj = ContentType.objects.get(pk=1) obj2 = ContentType.objects.get(pk=2) set_obj_attrs(obj, {'tags': 'milk, tee', 'width': 1, 'is_ok': True}) set_obj_attrs(obj2, {'tags': 'milk', 'is_ok': False}) with self.assertNumQueries(2): qs = [] for x in prefetch_related_attrs(ContentType.objects.filter(id__in=[1, 2])): qs.append(get_obj_attrs(x)) # self.assertEqual(len(qs), 2) self.assertEqual(qs, [{'tags': 'milk, tee', 'width': 1, 'is_ok': True}, {'tags': 'milk', 'is_ok': False}])
def test_filter__gt__lt(self): obj = ContentType.objects.get(pk=1) obj2 = ContentType.objects.get(pk=2) set_obj_attrs(obj, {'width': 200}) set_obj_attrs(obj2, {'width': 400}) r = filter_by_attrs(ContentType.objects.all(), width__gt=100).count() self.assertEqual(r, 2) r = filter_by_attrs(ContentType.objects.all(), width__lt=1000).count() self.assertEqual(r, 2) r = filter_by_attrs(ContentType.objects.all(), width__gt=300).count() self.assertEqual(r, 1) r = filter_by_attrs(ContentType.objects.all(), width__lt=300).count() self.assertEqual(r, 1)
def create_step_10(request): query = request.session.get(STEP_6_QUERY) if not query: return redirect(REDIRECT_IF_NO_QUERY) map_id = request.session[STEP_3_MAP] dataset_id = request.session[STEP_3_DATASET] type = request.session[STEP_4_TYPE] map = Map.objects.get(id=map_id) dataset = Dataset.objects.get(id=dataset_id) result, cols = request.db.execute(query) selected_table_columns = [x[0] for x in cols] # objs = load_map_objs(map) for row in result: lat, lon, points = _create_10_set_coordinates(row, type, request) data = _create_10_set_data(row, type, request, selected_table_columns) m = MObject.objects.create(map=map, dataset=dataset, type=type, lon=lon, lat=lat) for point in points: m.points.add(Point.objects.create(lat=point[0], lon=point[1])) set_obj_attrs(m, data) print(m.id) # dump_map_objs(map, objs) # del request.session[STEP_1_FILEPATH] # del request.session[STEP_2_DATABASE_INIT_DATA] # del request.session[STEP_8_ATTRS] # del request.session[STEP_3_DATASET] # del request.session[STEP_3_MAP] # del request.session[STEP_4_TYPE] return render(request, 'create-10.html', {'map': map})
def _set_data(self, data): set_obj_attrs(self, data)
def test_unicode_name_filter(self): obj = ContentType.objects.get(pk=1) set_obj_attrs(obj, {u'тест': 200}) r = filter_by_attrs(ContentType.objects.all(), **{u'тест': 200})[0] self.assertEqual(r.id, 1)
def test_set_and_get_attrs(self): obj = ContentType.objects.get(pk=1) set_obj_attrs(obj, {'Color': 'Black', 'Width': 200}) res = get_obj_attrs(obj) self.assertEqual(res, {'Color': 'Black', 'Width': 200})