Skip to content

Commit

Permalink
lets see
Browse files Browse the repository at this point in the history
  • Loading branch information
baseplate-admin committed Feb 23, 2024
1 parent 1fc95f0 commit 7633fac
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 111 deletions.
4 changes: 1 addition & 3 deletions tests/taxonomy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@


class Taxonomy(TreeModel):
label_size = 2

name = models.TextField()

def __str__(self):
return '{}: {}'.format(self.path, self.name)
return f"{self.path}: {self.name}"
261 changes: 157 additions & 104 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,119 +4,141 @@


TEST_DATA = [
{'name': 'Bacteria'},
{'name': 'Plantae'},
{"name": "Bacteria"},
{"name": "Plantae"},
{
'name': 'Animalia',
'sub': [
"name": "Animalia",
"sub": [
{
'name': 'Chordata',
'sub': [
"name": "Chordata",
"sub": [
{
'name': 'Mammalia',
'sub': [
"name": "Mammalia",
"sub": [
{
'name': 'Carnivora',
'sub': [
"name": "Carnivora",
"sub": [
{
'name': 'Canidae',
'sub': [
"name": "Canidae",
"sub": [
{
'name': 'Canis',
'sub': [{'name': 'Canis lupus'}, {'name': 'Canis rufus'}]
"name": "Canis",
"sub": [
{"name": "Canis lupus"},
{"name": "Canis rufus"},
],
},
{
'name': 'Urocyon',
'sub': [{'name': 'Urocyon cinereoargenteus'}]
}
]
"name": "Urocyon",
"sub": [
{"name": "Urocyon cinereoargenteus"}
],
},
],
},
{
'name': 'Feliformia',
'sub': [
"name": "Feliformia",
"sub": [
{
'name': 'Felidae',
'sub': [
"name": "Felidae",
"sub": [
{
'name': 'Felinae',
'sub': [
"name": "Felinae",
"sub": [
{
'name': 'Lynx',
'sub': [{'name': 'Lynx lynx'}, {'name': 'Lynx rufus'}]
"name": "Lynx",
"sub": [
{
"name": "Lynx lynx"
},
{
"name": "Lynx rufus"
},
],
},
{
'name': 'Puma',
'sub': [{'name': 'Puma concolor'}]
}
]
"name": "Puma",
"sub": [
{
"name": "Puma concolor"
}
],
},
],
}
]
],
}
]
}
]
],
},
],
},
{
'name': 'Pilosa',
'sub': [
"name": "Pilosa",
"sub": [
{
'name': 'Folivora',
'sub': [
"name": "Folivora",
"sub": [
{
'name': 'Bradypodidae',
'sub': [
"name": "Bradypodidae",
"sub": [
{
'name': 'Bradypus',
'sub': [{'name': 'Bradypus tridactylus'}]
"name": "Bradypus",
"sub": [
{
"name": "Bradypus tridactylus"
}
],
}
]
],
}
]
],
}
]
}
]
],
},
],
},
{
'name': 'Reptilia',
'sub': [
"name": "Reptilia",
"sub": [
{
'name': 'Squamata',
'sub': [
"name": "Squamata",
"sub": [
{
'name': 'Iguania',
'sub': [
"name": "Iguania",
"sub": [
{
'name': 'Agamidae',
'sub': [
"name": "Agamidae",
"sub": [
{
'name': 'Pogona',
'sub': [
{'name': 'Pogona barbata'},
{'name': 'Pogona minor'},
{'name': 'Pogona vitticeps'}
]
"name": "Pogona",
"sub": [
{"name": "Pogona barbata"},
{"name": "Pogona minor"},
{
"name": "Pogona vitticeps"
},
],
}
]
],
}
]
],
}
]
],
}
]
}
]
],
},
],
}
]
}
],
},
]


def create_objects(objects, parent):
for obj in objects:
created = Taxonomy.objects.create_child(parent, name=obj['name'])
if 'sub' in obj:
create_objects(obj['sub'], created)
created = Taxonomy.objects.create_child(parent, name=obj["name"])
if "sub" in obj:
create_objects(obj["sub"], created)


def create_test_data():
Expand All @@ -130,21 +152,22 @@ def test_create(db):

def test_roots(db):
create_test_data()
roots = Taxonomy.objects.roots().values_list('name', flat=True)
assert set(roots) == set(['Bacteria', 'Plantae', 'Animalia'])
roots = Taxonomy.objects.roots().values_list("name", flat=True)
assert set(roots) == set(["Bacteria", "Plantae", "Animalia"])


@pytest.mark.parametrize(
'name, expected', [
('Animalia', ['Chordata']),
('Mammalia', ['Carnivora', 'Pilosa']),
('Reptilia', ['Squamata']),
('Pogona', ['Pogona barbata', 'Pogona minor', 'Pogona vitticeps'])
]
"name, expected",
[
("Animalia", ["Chordata"]),
("Mammalia", ["Carnivora", "Pilosa"]),
("Reptilia", ["Squamata"]),
("Pogona", ["Pogona barbata", "Pogona minor", "Pogona vitticeps"]),
],
)
def test_children(db, name, expected):
create_test_data()
children = Taxonomy.objects.get(name=name).children().values_list('name', flat=True)
children = Taxonomy.objects.get(name=name).children().values_list("name", flat=True)
assert set(children) == set(expected)


Expand All @@ -157,57 +180,87 @@ def test_label(db):


@pytest.mark.parametrize(
'name, expected', [
('Canis lupus', ['Animalia', 'Chordata', 'Mammalia', 'Carnivora', 'Canidae', 'Canis', 'Canis lupus']),
('Bacteria', ['Bacteria']),
('Chordata', ['Animalia', 'Chordata'])
]
"name, expected",
[
(
"Canis lupus",
[
"Animalia",
"Chordata",
"Mammalia",
"Carnivora",
"Canidae",
"Canis",
"Canis lupus",
],
),
("Bacteria", ["Bacteria"]),
("Chordata", ["Animalia", "Chordata"]),
],
)
def test_ancestors(db, name, expected):
create_test_data()
ancestors = Taxonomy.objects.get(name=name).ancestors().values_list('name', flat=True)
ancestors = (
Taxonomy.objects.get(name=name).ancestors().values_list("name", flat=True)
)
assert list(ancestors) == expected


@pytest.mark.parametrize(
'name, expected', [
('Canidae', ['Canidae', 'Canis', 'Canis lupus', 'Canis rufus', 'Urocyon', 'Urocyon cinereoargenteus']),
('Bradypus tridactylus', ['Bradypus tridactylus']),
('Pogona', ['Pogona', 'Pogona barbata', 'Pogona minor', 'Pogona vitticeps'])
]
"name, expected",
[
(
"Canidae",
[
"Canidae",
"Canis",
"Canis lupus",
"Canis rufus",
"Urocyon",
"Urocyon cinereoargenteus",
],
),
("Bradypus tridactylus", ["Bradypus tridactylus"]),
("Pogona", ["Pogona", "Pogona barbata", "Pogona minor", "Pogona vitticeps"]),
],
)
def test_descendants(db, name, expected):
create_test_data()
descendants = Taxonomy.objects.get(name=name).descendants().values_list('name', flat=True)
descendants = (
Taxonomy.objects.get(name=name).descendants().values_list("name", flat=True)
)
assert set(descendants) == set(expected)


@pytest.mark.parametrize(
'name, expected', [
('Feliformia', 'Carnivora'),
('Plantae', None),
('Pogona minor', 'Pogona')
]
"name, expected",
[("Feliformia", "Carnivora"), ("Plantae", None), ("Pogona minor", "Pogona")],
)
def test_parent(db, name, expected):
create_test_data()
parent = Taxonomy.objects.get(name=name).parent()
assert getattr(parent, 'name', None) == expected
assert getattr(parent, "name", None) == expected


@pytest.mark.parametrize(
'name, expected', [
('Carnivora', ['Pilosa']),
('Pogona vitticeps', ['Pogona minor', 'Pogona barbata'])
]
"name, expected",
[
("Carnivora", ["Pilosa"]),
("Pogona vitticeps", ["Pogona minor", "Pogona barbata"]),
],
)
def test_siblings(db, name, expected):
create_test_data()
siblings = Taxonomy.objects.get(name=name).siblings().values_list('name', flat=True)
siblings = Taxonomy.objects.get(name=name).siblings().values_list("name", flat=True)
assert set(siblings) == set(expected)


def test_slicing(db):
create_test_data()
qs = Taxonomy.objects.all()
assert qs[:3].count() == 3


def test_automatic_name_creation():
for i in range(0, 2_538_557_185_841_324_496):
Taxonomy.objects.create_child(name=i)
4 changes: 0 additions & 4 deletions tests/test_path_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,3 @@ def generator():
yield "beer"

assert str(PathValue(generator())) == "100.bottles.of.beer"


def test_automatic_generation_of_label():
pass

0 comments on commit 7633fac

Please sign in to comment.