Example #1
0
    def test_environment_gets_most_specific(self):
        class ConcreteComponentSub(ConcreteComponent):
            pass

        with Environment(ConcreteComponent, ConcreteComponentSub):
            c = Environment.provide(AbstractComponent)
            self.assertIsInstance(c, ConcreteComponentSub)
Example #2
0
 def test_nested_environments(self):
     with Environment(ConcreteComponent):
         c = Environment.provide(AbstractComponent)
         self.assertIsInstance(c, ConcreteComponent)
         with Environment(AlternativeComponent):
             c = Environment.provide(AbstractComponent)
             self.assertIsInstance(c, AlternativeComponent)
Example #3
0
 def test_inject_provides_correct_implementation(self):
     d = Dependent()
     with Environment(ConcreteComponent):
         self.assertIsInstance(d.abstract_component, AbstractComponent)
         self.assertIsInstance(d.abstract_component, ConcreteComponent)
     with Environment(AlternativeComponent):
         self.assertIsInstance(d.abstract_component, AbstractComponent)
         self.assertIsInstance(d.abstract_component, AlternativeComponent)
 def test_more_than_one_instance_in_cache(self):
     with Environment():
         s1 = Environment.provide(SomeComponent, self)
         s2 = Environment.provide(SomeOtherComponent, self)
         s3 = Environment.provide(SomeComponent, self)
         s4 = Environment.provide(SomeOtherComponent, self)
         self.assertIs(s1, s3)
         self.assertIs(s2, s4)
Example #5
0
 def test_load_extension(self):
     sys.modules['ipython_environment'] = MockIpythonEnvironment
     load_ipython_extension(None)
     self.assertEqual(MockIpythonEnvironment.environment,
                      Environment.current_env())
     unload_ipython_extension(None)
     with self.assertRaises(NoEnvironment):
         Environment.current_env()
     sys.modules['ipython_environment'] = None
Example #6
0
 def test_environment_provides_correct_implementation(self):
     with Environment(ConcreteComponent):
         c = Environment.provide(AbstractComponent)
         self.assertIsInstance(c, AbstractComponent)
         self.assertIsInstance(c, ConcreteComponent)
     with Environment(AlternativeComponent):
         c = Environment.provide(AbstractComponent)
         self.assertIsInstance(c, AbstractComponent)
         self.assertIsInstance(c, AlternativeComponent)
Example #7
0
 def test_match_returns_correct_env(self):
     env1 = Environment()
     env2 = Environment()
     os.environ['TEST_ENV'] = 'ENV1'
     env = match(environment_variable='TEST_ENV', ENV1=env1, ENV2=env2)
     self.assertIs(env, env1)
     os.environ['TEST_ENV'] = 'ENV2'
     env = match(environment_variable='TEST_ENV', ENV1=env1, ENV2=env2)
     self.assertIs(env, env2)
Example #8
0
    def test_mocks_are_reset_after_context_exit(self):
        with Environment():
            some_component_mock = mock(SomeComponent)
            d = Depenedent()
            self.assertIs(some_component_mock, d.some_component)

        with Environment():
            d = Depenedent()
            self.assertIsNot(some_component_mock, d.some_component)
            self.assertIsInstance(d.some_component, SomeComponent)
    def test_subtype_is_singleton(self):
        class SomeComponentSingleton(SomeComponent, Singleton):
            pass

        with Environment(SomeComponentSingleton):
            s1 = Environment.provide(SomeComponent, object())
            s2 = Environment.provide(SomeComponent, object())
            self.assertIs(s1, s2)
            s3 = Environment.provide(SomeComponentSingleton, object())
            self.assertIs(s1, s3)
Example #10
0
    def test_new_environment_in_thread(self):
        def test():
            with Environment(AlternativeComponent):
                c1 = Environment.provide(AbstractComponent)
                self.assertIsInstance(c1, AlternativeComponent)

        with Environment(ConcreteComponent):
            threading.Thread(target=test).start()
            c2 = Environment.provide(AbstractComponent)
            self.assertIsInstance(c2, ConcreteComponent)
Example #11
0
    def test_same_environment_in_thread(self):
        def test():
            with self.assertRaises(NoEnvironment):
                Environment.provide(AbstractComponent)

        with Environment(ConcreteComponent):
            threading.Thread(target=test).start()
Example #12
0
    def test_circular_dependency(self):
        class AbstractA(Component):
            pass

        class AbstractB(Component):
            pass

        class A(AbstractA):
            b = inject(AbstractB)

            def __init__(self):
                self.b

        class B(AbstractB):
            a = inject(AbstractA)

            def __init__(self):
                self.a

        class Dependent:
            a = inject(AbstractA)

        with Environment(A, B):
            with self.assertRaises(CircularDependency):
                Dependent().a
Example #13
0
 def test_mock_always_replaces_component(self):
     with Environment():
         some_component_mock = mock(SomeComponent)
         some_component_mock.method.return_value = 'some other value'
         d = Depenedent()
         self.assertIs(d.some_component, some_component_mock)
         self.assertEqual(d.some_component.method(), 'some other value')
Example #14
0
def read_documents(label_path, page_folder, database_path):
    def get(value, convert=str):
        return convert(value) if not math.isnan(value) else None

    data = pandas.read_csv(label_path)
    documents = []
    for _, row in data.iterrows():
        filename = format_file_name(row.product_page_url)
        path = os.path.join(page_folder, filename)
        with open(path) as f:
            html = f.read()
            document = HTMLDocument(html=html,
                                    language=language(html),
                                    vendor=vendor(row.product_page_url),
                                    brand=get(row.brand),
                                    ean=get(row.ean, convert=int),
                                    asin=get(row.asin, convert=int),
                                    sku=get(row.sku),
                                    price=get(row.price, convert=float),
                                    currency=get(row.currency),
                                    gtin13=get(row.gtin13, convert=int))
            documents.append(document)
        exists = os.path.exists(database_path)
        if not exists:
            os.makedirs(path)
            with open(database_path) as f:
                pass
        config = Config(database_path=database_path)
        ArgumentProvider.config = config
        with Environment(ArgumentProvider):
            db = DocumentDatabase()
            db.save_documents(documents, overwrite=not exists)
Example #15
0
def production_environment() -> Environment:
    return Environment(
        SQLiteDatabase,
        SimpleLog,
        ConsoleArguments,
        ConsoleItemReader,
        ConsoleItemWriter
    )
Example #16
0
 def test_garbage_collection(self):
     with Environment() as e:
         d = Dependent()
         _ = d.some_component
         gc.collect()
         self.assertTrue(e.has_instance(SomeComponent, d))
         del d
         check_garbage(self, e)
Example #17
0
def dev_environment() -> Environment:
    return Environment(
        InMemoryDatabase,
        SimpleLog,
        ConsoleArguments,
        ConsoleItemReader,
        ConsoleItemWriter
    )
Example #18
0
def test_environment() -> Environment:
    return Environment(
        InMemoryDatabase,
        DummyLog,
        DummyArguments,
        DummyItemReader,
        DummyItemWriter,
    )
Example #19
0
    def test_decorater(self):
        test_environment = Environment(SomeComponent)

        @test_environment
        def test():
            component = Environment.provide(SomeComponent)
            self.assertIsInstance(component, SomeComponent)

        test()
Example #20
0
def run(database_path):
    config = Config(database_path=database_path)
    ArgumentProvider.config = config
    with Environment(ArgumentProvider):
        db = DocumentDatabase()
        documents = db.load_documents()
        cleaned_documents = remove_useless_tags(documents)
        tokenized_documents = html_tokenize(cleaned_documents)
        lowercase_documents = lowercase(tokenized_documents)
        db.save_documents(lowercase_documents)
Example #21
0
 def test_mock_is_specced(self):
     with Environment():
         some_component_mock = mock(SomeComponent)
         self.assertIsInstance(some_component_mock, SomeComponent)
         with self.assertRaises(AttributeError):
             some_component_mock.bad_method()
         with self.assertRaises(TypeError):
             some_component_mock()
         some_callable_component = mock(SomeCallableComponent)
         some_callable_component.return_value = 'mocked value'
         self.assertEqual(some_callable_component(), 'mocked value')
Example #22
0
    def test_scope(self):
        environment = Environment()
        d = Dependent()

        with environment:
            sc1 = d.some_component
            ss1 = d.some_singleton
        with environment:
            sc2 = d.some_component
            ss2 = d.some_singleton
        self.assertIsNot(sc1, sc2)
        self.assertIsNot(ss1, ss2)
Example #23
0
    def test_mock_replaces_named_value(self):
        class Dependency:
            def method(self):
                pass

        with Environment(key=Dependency()):
            mock_dependency = mock('key')
            mock_dependency.method.return_value = 'value'
            with self.assertRaises(AttributeError):
                mock_dependency.no_such_method()
            injected = inject('key')
            self.assertEqual(injected, mock_dependency)
            self.assertEqual(mock_dependency.method(), 'value')
Example #24
0
 def test_nested_environments(self):
     key = Key()
     with Environment(ConcreteComponent):
         c1 = Environment.provide(AbstractComponent, self)
         self.assertIsInstance(c1, ConcreteComponent)
         with Environment(AlternativeComponent):
             c2 = Environment.provide(AbstractComponent, self)
             c3 = Environment.provide(AbstractComponent, key)
             self.assertIs(c1, c2)
             self.assertIsInstance(c3, AlternativeComponent)
         c4 = Environment.provide(AbstractComponent, key)
         c5 = Environment.provide(AbstractComponent, self)
         self.assertIsNot(c3, c4)
         self.assertIs(c1, c5)
Example #25
0
    def test_scope_multi_threaded(self):
        environment = Environment()
        d = Dependent()
        q = Queue()
        c = threading.Condition()

        with environment:
            some_component1 = d.some_component
            singleton1 = d.some_singleton

        def t1():
            with c:
                c.wait()
            some_component2 = q.get()
            singleton2 = q.get()
            with environment:
                q.put(d.some_component)
                q.put(d.some_singleton)
                with c:
                    c.notify()
                self.assertIsNot(d.some_component, some_component2)
                self.assertIsNot(d.some_singleton, singleton2)
                self.assertIsNot(d.some_component, some_component1)
                self.assertIsNot(d.some_singleton, singleton1)

        def t2():
            with environment:
                q.put(d.some_component)
                q.put(d.some_singleton)
                with c:
                    c.notify()
                    c.wait()
                some_component3 = q.get()
                singleton3 = q.get()
                self.assertIsNot(d.some_component, some_component3)
                self.assertIsNot(d.some_singleton, singleton3)
                self.assertIsNot(d.some_component, some_component1)
                self.assertIsNot(d.some_singleton, singleton1)

        threading.Thread(target=t1).start()
        threading.Thread(target=t2).start()
Example #26
0
from serum import Environment
from vico.database import DocumentDatabase
import pickle
import re

from vico.html_document import HTMLDocument

with Environment():
    database = DocumentDatabase()
    docs = database.load_documents()
    with open('data/indices.pkl', 'rb') as f:
        indices = pickle.load(f)
    indices2tokens = {v: k for k, v in indices.items()}
    ean_docs = []
    for doc in docs:
        if doc.ean is None:
            ean_docs.append(doc)
            continue
        formatted_ean = re.sub(r'\.[0-9]+', '', doc.ean)
        ean_bio_labels = []
        for window in doc.windows:
            tokens = [indices2tokens[i] if i != 0 else 'PAD' for i in window]
            middle_token = tokens[len(tokens) // 2]
            if middle_token == formatted_ean:
                ean_bio_labels.append(1)
            else:
                ean_bio_labels.append(0)
        assert len(ean_bio_labels) == len(doc.windows)
        doc_dict = doc._asdict()
        doc_dict['ean_bio_labels'] = ean_bio_labels
        ean_docs.append(HTMLDocument(**doc_dict))
Example #27
0
 def test():
     with self.assertRaises(NoEnvironment):
         Environment.provide(AbstractComponent)
Example #28
0
 def test():
     with Environment(AlternativeComponent):
         c1 = Environment.provide(AbstractComponent)
         self.assertIsInstance(c1, AlternativeComponent)
Example #29
0
 def test():
     component = Environment.provide(SomeComponent)
     self.assertIsInstance(component, SomeComponent)
Example #30
0
 def test_intersection(self):
     e1 = Environment(SomeComponent)
     e2 = Environment(ConcreteComponent)
     e3 = e1 | e2
     self.assertIn(SomeComponent, e3)
     self.assertIn(ConcreteComponent, e3)