def some_devices(sample_series, sample_color): return [ Device.create(name="iPhone 6S Plus", colors=[sample_color], series=sample_series), Device.create(name="iPhone 6S +", colors=[sample_color], series=sample_series), Device.create(name="iPhone 9", colors=[sample_color], series=sample_series), Device.create(name="iPhone 7", colors=[sample_color], series=sample_series), Device.create(name="iPhone SE", colors=[sample_color], series=sample_series), Device.create(name="iPhone XS Max", colors=[sample_color], series=sample_series), Device.create(name="iPhone XS", colors=[sample_color], series=sample_series), Device.create(name="iPhone X", colors=[sample_color], series=sample_series), Device.create(name="iPhone 11", colors=[sample_color], series=sample_series), Device.create(name="iPhone Pro", colors=[sample_color], series=sample_series), ]
def test_normalize(self, sample_series, sample_color): letters = string.ascii_lowercase devices = [ Device.create(name=''.join(random.choice(letters) for x in range(10)), colors=[sample_color], series=sample_series) for i in range(10) ] for d in devices: d.order_index = 0 d.save() Device.normalize() for i, device in enumerate(devices): assert device.order_index == i
def test_name(self, sample_repair, sample_device, some_devices): merger = Device.merge([ sample_device.id, ] + list(map(lambda d: d.id, some_devices))) assert merger.name == sample_device.name try: merger = Device.merge([ sample_device.id, ]) assert False except IndexError: assert True
def search_api(device_name): """ Test endpoint for development """ found_devices_normal = Device.search(device_name).all() found_devices_similar = Device.search_order_by_similarity(device_name).all() found_devices_array = Device.search_by_array(device_name).all() found_devices_levenshtein = Device.search_levenshtein(device_name).all() return jsonify(results={ 'normal': [device.name for device in found_devices_normal], 'similar': [device.name for device in found_devices_similar], 'array': [device.name for device in found_devices_array], 'levenshtein': [device.name for device in found_devices_levenshtein], })
def test_order(self, sample_series, sample_color): letters = string.ascii_lowercase devices = [ Device.create(name=''.join(random.choice(letters) for x in range(10)), colors=[sample_color], series=sample_series) for i in range(10) ] assert len(devices) == 10 for i, device in enumerate(devices): assert device.order_index == i
def test_repairs(self, sample_device, sample_repair, another_repair, another_device): reps = [sample_repair, another_repair] merger = Device.merge([sample_device.id, another_device.id]) assert list( map(lambda x: x.name, sorted(merger.repairs, key=lambda r: r.id))) == list( map(lambda x: x.name, sorted(reps, key=lambda r: r.id)))
def action_approve(self, ids): try: merger = Device.merge(ids) except (IntegrityError, FlushError) as e: flash("Zusammenführen fehlgeschlagen", "danger") current_app.logger.error(e) return except IndexError: flash("Bitte wähle mindestens 1 Gerät aus.") return flash( f"Die Geräte wurden erfolgreich zu {merger.name} zusammengeführt. Bitte passe den Namen an und prüfe die Bilder." ) return redirect(url_for('device.edit_view', id=merger.id))
def test_move_down(self, sample_series, sample_color): letters = string.ascii_lowercase devices = [ Device.create(name=''.join(random.choice(letters) for x in range(10)), colors=[sample_color], series=sample_series) for i in range(10) ] first = devices[0] first_1 = devices[1] assert first.order_index < first_1.order_index first.move_down() assert first.order_index > first_1.order_index for i in range(100): first.move_down() assert first.order_index == 9
def test_move_up(self, sample_series, sample_color): letters = string.ascii_lowercase devices = [ Device.create(name=''.join(random.choice(letters) for x in range(10)), colors=[sample_color], series=sample_series) for i in range(10) ] last = devices[-1] last_1 = devices[-2] assert last.order_index > last_1.order_index last.move_up() assert last.order_index < last_1.order_index for i in range(100): last.move_up() assert last.order_index == 0
def test_create(self, db, sample_device, some_devices): assert "search" in dir(Device) # Normal search q = Device.query.filter(Device.name.like('iphone')) assert len(q.all()) == 0 assert 'LIKE' in str(q) # Fuzzy search q = Device.query.filter(Device.name.op('%%')('iphone ')) assert len(q.all()) > 0 assert 'LIKE' not in str(q) # Similarity result = Device.search_order_by_similarity('iPhone X') print(result, result.all()) result = result.all() assert result[0].name == 'iPhone X'
def sample_device(sample_series, sample_color): """ Create a sample device """ return Device.create(name="iPhone 6S", colors=[sample_color], series=sample_series)
def test_delete(self, sample_device, some_devices): Device.merge([ sample_device.id, ] + list(map(lambda d: d.id, some_devices))) assert Device.query.count() == 1
def test_colors(self, sample_device, another_device, sample_color): colors = list(sample_device.colors) + list(another_device.colors) merger = Device.merge([sample_device.id, another_device.id]) assert set(sorted(merger.colors, key=lambda r: r.id)) == set( sorted(colors, key=lambda r: r.id))
def test_is_tablet(self, sample_device, some_devices): merger = Device.merge([ sample_device.id, ] + list(map(lambda d: d.id, some_devices))) assert merger.is_tablet == sample_device.is_tablet
def device_create_or_get(series: DeviceSeries, device_name: str) -> Device: device = Device.query.filter(Device.name == device_name).first() if not device: device = Device.create(series=series, name=device_name) return device