Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factories cleanup #808

Merged
merged 1 commit into from
Feb 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
[#799](https://github.com/opendatateam/udata/pull/799)
- Some minor tuning on generic references
[#801](https://github.com/opendatateam/udata/pull/801)
- Cleanup factories
[#808](https://github.com/opendatateam/udata/pull/808)

## 1.0.3 (2017-02-21)

Expand Down
11 changes: 5 additions & 6 deletions udata/core/badges/factories.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
# -*- coding: utf-8 -*-

from __future__ import unicode_literals

from factory.mongoengine import MongoEngineFactory
import factory

from factory.fuzzy import FuzzyChoice

from udata import models
from udata.utils import faker
from .models import Badge


def badge_factory(model):
class BadgeFactory(MongoEngineFactory):
class BadgeFactory(factory.mongoengine.MongoEngineFactory):
class Meta:
model = models.Badge
model = Badge

kind = FuzzyChoice(model.__badges__.keys())

Expand Down
43 changes: 21 additions & 22 deletions udata/core/dataset/factories.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from hashlib import sha1

import factory

from factory.mongoengine import MongoEngineFactory

from udata import models
from udata.utils import faker

from .models import Dataset, Resource, Checksum, CommunityResource, License


class DatasetFactory(MongoEngineFactory):
class DatasetFactory(factory.mongoengine.MongoEngineFactory):
class Meta:
model = models.Dataset
model = Dataset

title = factory.LazyAttribute(lambda o: faker.sentence())
description = factory.LazyAttribute(lambda o: faker.text())
title = factory.Faker('sentence')
description = factory.Faker('text')
frequency = 'unknown'


Expand All @@ -24,37 +23,37 @@ def resources(self):
return [ResourceFactory()]


class ChecksumFactory(MongoEngineFactory):
class ChecksumFactory(factory.mongoengine.MongoEngineFactory):
class Meta:
model = models.Checksum
model = Checksum

type = 'sha1'
value = factory.LazyAttribute(lambda o: sha1(faker.word()).hexdigest())
value = factory.Faker('sha1')


class BaseResourceFactory(MongoEngineFactory):
title = factory.LazyAttribute(lambda o: faker.sentence())
description = factory.LazyAttribute(lambda o: faker.text())
class BaseResourceFactory(factory.mongoengine.MongoEngineFactory):
title = factory.Faker('sentence')
description = factory.Faker('text')
filetype = 'file'
url = factory.LazyAttribute(lambda o: faker.url())
url = factory.Faker('url')
checksum = factory.SubFactory(ChecksumFactory)
mime = factory.LazyAttribute(lambda o: faker.mime_type('text'))
filesize = factory.LazyAttribute(lambda o: faker.pyint())
mime = factory.Faker('mime_type', category='text')
filesize = factory.Faker('pyint')


class CommunityResourceFactory(BaseResourceFactory):
class Meta:
model = models.CommunityResource
model = CommunityResource


class ResourceFactory(BaseResourceFactory):
class Meta:
model = models.Resource
model = Resource


class LicenseFactory(MongoEngineFactory):
class LicenseFactory(factory.mongoengine.MongoEngineFactory):
class Meta:
model = models.License
model = License

id = factory.Sequence(lambda n: '{0}-{1}'.format(faker.word(), n))
title = factory.LazyAttribute(lambda o: faker.sentence())
title = factory.Faker('sentence')
12 changes: 5 additions & 7 deletions udata/core/discussions/factories.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
# -*- coding: utf-8 -*-

from __future__ import unicode_literals

import factory
from factory.mongoengine import MongoEngineFactory

from udata import models
from .models import Discussion, Message


class DiscussionFactory(MongoEngineFactory):
class DiscussionFactory(factory.mongoengine.MongoEngineFactory):
class Meta:
model = models.Discussion
model = Discussion

title = factory.Faker('sentence')


class MessageDiscussionFactory(MongoEngineFactory):
class MessageDiscussionFactory(factory.mongoengine.MongoEngineFactory):
class Meta:
model = models.Message
model = Message

content = factory.Faker('sentence')
4 changes: 1 addition & 3 deletions udata/core/issues/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

import factory

from factory.mongoengine import MongoEngineFactory

from .models import Issue


class IssueFactory(MongoEngineFactory):
class IssueFactory(factory.mongoengine.MongoEngineFactory):
class Meta:
model = Issue

Expand Down
19 changes: 8 additions & 11 deletions udata/core/organization/factories.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
# -*- coding: utf-8 -*-

from __future__ import unicode_literals

import factory
from factory.mongoengine import MongoEngineFactory

from udata import models
from udata.utils import faker
from .models import Organization, Team


class OrganizationFactory(MongoEngineFactory):
class OrganizationFactory(factory.mongoengine.MongoEngineFactory):
class Meta:
model = models.Organization
model = Organization

name = factory.LazyAttribute(lambda o: faker.sentence())
description = factory.LazyAttribute(lambda o: faker.text())
name = factory.Faker('sentence')
description = factory.Faker('text')


class TeamFactory(MongoEngineFactory):
class TeamFactory(factory.mongoengine.MongoEngineFactory):
class Meta:
model = models.Team
model = Team

name = factory.LazyAttribute(lambda o: faker.sentence())
name = factory.Faker('sentence')
18 changes: 8 additions & 10 deletions udata/core/post/factories.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
# -*- coding: utf-8 -*-

from __future__ import unicode_literals

import factory
from factory.mongoengine import MongoEngineFactory

from udata import models
from udata.core.dataset.factories import DatasetFactory
from udata.core.reuse.factories import ReuseFactory
from udata.utils import faker

from .models import Post


class PostFactory(MongoEngineFactory):
class PostFactory(factory.mongoengine.MongoEngineFactory):
class Meta:
model = models.Post
model = Post

name = factory.LazyAttribute(lambda o: faker.sentence())
headline = factory.LazyAttribute(lambda o: faker.sentence())
content = factory.LazyAttribute(lambda o: faker.text())
private = factory.LazyAttribute(lambda o: False)
name = factory.Faker('sentence')
headline = factory.Faker('sentence')
content = factory.Faker('text')
private = False

@factory.lazy_attribute
def datasets(self):
Expand Down
18 changes: 9 additions & 9 deletions udata/core/reuse/factories.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
# -*- coding: utf-8 -*-

from __future__ import unicode_literals

import factory
from factory.mongoengine import MongoEngineFactory

from factory.fuzzy import FuzzyChoice

from udata import models, utils
from udata.core.dataset.factories import DatasetFactory
from udata.utils import faker

from .models import Reuse, REUSE_TYPES


class ReuseFactory(MongoEngineFactory):
class ReuseFactory(factory.mongoengine.MongoEngineFactory):
class Meta:
model = models.Reuse
model = Reuse

title = factory.LazyAttribute(lambda o: faker.sentence())
description = factory.LazyAttribute(lambda o: faker.text())
title = factory.Faker('sentence')
description = factory.Faker('text')
url = factory.LazyAttribute(
lambda o: '/'.join([faker.url(), utils.unique_string()]))
type = FuzzyChoice(models.REUSE_TYPES.keys())
lambda o: '/'.join([faker.url(), faker.unique_string()]))
type = FuzzyChoice(REUSE_TYPES.keys())


class VisibleReuseFactory(ReuseFactory):
Expand Down
17 changes: 7 additions & 10 deletions udata/core/site/factories.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
# -*- coding: utf-8 -*-

from __future__ import unicode_literals

import factory
from factory.mongoengine import MongoEngineFactory

from udata import models
from udata.utils import faker
from .models import Site, SiteSettings


class SiteSettingsFactory(MongoEngineFactory):
class SiteSettingsFactory(factory.mongoengine.MongoEngineFactory):
class Meta:
model = models.SiteSettings
model = SiteSettings


class SiteFactory(MongoEngineFactory):
class SiteFactory(factory.mongoengine.MongoEngineFactory):
class Meta:
model = models.Site
model = Site

id = factory.LazyAttribute(lambda o: faker.word())
title = factory.LazyAttribute(lambda o: faker.name())
id = factory.Faker('word')
title = factory.Faker('name')
settings = factory.SubFactory(SiteSettingsFactory)
41 changes: 21 additions & 20 deletions udata/core/spatial/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,11 @@

from geojson.utils import generate_random

from factory.mongoengine import MongoEngineFactory

from udata.utils import unique_string, faker
from udata.utils import add_faker_provider

from .models import GeoLevel, GeoZone, SpatialCoverage, spatial_granularities


def random_spatial_granularity(*args, **kwargs):
return faker.random_element(
[row[0] for row in spatial_granularities])


class GeoJsonProvider(BaseProvider):
'''A Fake GeoJSON provider'''
def random_range(self, min=2, max=5):
Expand Down Expand Up @@ -97,31 +90,39 @@ def feature_collection(self):
}


faker.add_provider(GeoJsonProvider)
class SpatialProvider(BaseProvider):
def spatial_granularity(self):
return self.generator.random_element([
row[0] for row in spatial_granularities
])


add_faker_provider(GeoJsonProvider)
add_faker_provider(SpatialProvider)


class SpatialCoverageFactory(MongoEngineFactory):
class SpatialCoverageFactory(factory.mongoengine.MongoEngineFactory):
class Meta:
model = SpatialCoverage

geom = factory.LazyAttribute(lambda o: faker.multipolygon())
granularity = factory.LazyAttribute(random_spatial_granularity)
geom = factory.Faker('multipolygon')
granularity = factory.Faker('spatial_granularity')


class GeoZoneFactory(MongoEngineFactory):
class GeoZoneFactory(factory.mongoengine.MongoEngineFactory):
class Meta:
model = GeoZone

id = factory.LazyAttribute(lambda o: '/'.join((o.level, o.code)))
level = factory.LazyAttribute(lambda o: unique_string())
name = factory.LazyAttribute(lambda o: faker.city())
code = factory.LazyAttribute(lambda o: faker.zipcode())
geom = factory.LazyAttribute(lambda o: faker.multipolygon())
level = factory.Faker('unique_string')
name = factory.Faker('city')
code = factory.Faker('zipcode')
geom = factory.Faker('multipolygon')


class GeoLevelFactory(MongoEngineFactory):
class GeoLevelFactory(factory.mongoengine.MongoEngineFactory):
class Meta:
model = GeoLevel

id = factory.LazyAttribute(lambda o: unique_string())
name = factory.LazyAttribute(lambda o: faker.name())
id = factory.Faker('unique_string')
name = factory.Faker('name')