Ejemplo n.º 1
0
    def test_uri_property(self):
        uri = "/uri/1/"
        assert Dummy.objects.filter(uri=uri).count() == 0
        instance = Dummy()
        assert instance.uri is None
        assert instance.computed["uri"] is None
        assert not instance.computed_uri_hash
        with ForceChanger(instance):
            instance.uri = uri
        assert instance.uri == uri
        assert instance.computed["uri"] == uri
        assert instance.computed_uri_hash
        instance.save()
        assert Dummy.objects.filter(uri=uri).count() == 1
        with ForceChanger(instance):
            instance.uri = None
        assert instance.uri is None
        assert instance.computed_uri_hash is None

        uname = "*****@*****.**"
        instance.uname = uname
        assert instance.uname == uname
        assert instance.uri == "/uri/base/dummy/{}/".format(uname)
        assert instance.computed_uri_hash is not None
        instance.save()
        tran = TransactionManager.get_transaction()
        instance._syncdb_update(tran)
        query = str(Dummy.objects.filter(uname=uname).query)
        assert query.find("computed_uri_hash") != -1
        assert Dummy.objects.filter(uname=uname).count() == 1
        assert Dummy.objects.filter(uname="asdf").count() == 0
        with self.assertRaises(AssertionError):
            instance.uri = "asdf"

        other_uname = "*****@*****.**"
        instance.uname = other_uname
        assert instance.uname == other_uname
        instance.uname = uname
        assert instance.uname == uname
        instance2 = Dummy()
        instance2.uname = other_uname
        assert instance2.uname == other_uname
        assert instance2.status == Status.NEW
        with self.assertRaises(DuplicateUriException):
            instance2.uname = uname
        assert instance2.status == Status.INVALID
        with self.assertRaises(AssertionError):
            instance2.uname = other_uname
        with self.assertRaises(AssertionError):
            instance2.save()
        instance.save()
Ejemplo n.º 2
0
    def test_model_version(self):
        with Transaction():
            instance = Dummy()
            assert instance.version == 0
            with ForceChanger(instance):
                instance.uri = "/uri/patent/usa/granted/1/"
            assert instance.uri == "/uri/patent/usa/granted/1/"
            assert instance.version == 0
            instance.save()
        assert instance.version == 1

        with Transaction():
            instance2 = Model.objects.get(id=instance.id)
            assert instance2.version == 1
            with ForceChanger(instance2):
                instance2.uri = "/uri/patent/usa/granted/2/"
            assert instance2.version == 1
            instance2.save()
        assert instance2.version == 2

        with Transaction() as tran:
            instance3 = Model.objects.get(type__in=Dummy.types)
            assert instance3.version == 2
            tran.remove(instance3)
            instance_db = Model.objects.get(id=instance3.id)
            assert id(instance_db) != id(instance3)
            assert instance_db.version == 2
            instance_db.delete()
            assert instance_db.version == 2
        assert instance_db.version == 3
        assert instance3.version == 2
Ejemplo n.º 3
0
    def test_optimistic_lock(self):
        with Transaction():
            Dummy().save()

        with Transaction() as tran1:
            instance1 = Model.objects.get(type__in=Dummy.types)
        with Transaction() as tran2:
            instance2 = Model.objects.get(type__in=Dummy.types)

        assert instance1 == instance2
        assert id(instance1) != id(instance2)
        assert instance1.version == instance2.version == 1

        with tran1:
            with ForceChanger(instance1):
                instance1.uri = "/uri/patent/usa/granted/1/"
            assert instance1.version == 1
            instance1.save()
            assert instance1.version == 1
        assert instance1.version == 2

        with self.assertRaises(OptimisticLockException):
            with tran2:
                with ForceChanger(instance2):
                    instance2.uri = "/uri/patent/usa/granted/2/"
                assert instance2.version == 1
                instance2.save()
Ejemplo n.º 4
0
 def test_compute_patent_summary(self):
     instance = Dummy()
     Dummy.compute_patent_summary(instance)
     assert instance.patent_summary["len"] is None
     assert instance.patent_summary["summary"] is None
     instance.patent["raw"] = "1234"
     Dummy.compute_patent_summary(instance)
     assert instance.patent_summary["len"] == 4
     assert instance.patent_summary["summary"] == "1"
     instance.patent["raw"] = None
     Dummy.compute_patent_summary(instance)
     assert instance.patent_summary["len"] is None
     assert instance.patent_summary["summary"] is None
     instance.patent["raw"] = "None"
     Dummy.compute_patent_summary(instance)
     assert instance.patent_summary["len"] == 4
     assert instance.patent_summary["summary"] == "N"
     instance.patent["raw"] = "None None None None"
     Dummy.compute_patent_summary(instance)
     assert instance.patent_summary["len"] == 19
     assert instance.patent_summary["summary"] == "N"
     instance.patent["raw"] = " N"
     Dummy.compute_patent_summary(instance)
     assert instance.patent_summary["len"] == 2
     assert instance.patent_summary["summary"] == " "
     instance.patent["raw"] = "0"
     Dummy.compute_patent_summary(instance)
     assert instance.patent_summary["len"] == 1
     assert instance.patent_summary["summary"] == "0"
Ejemplo n.º 5
0
    def test_data_setter(self):
        instance = Dummy()
        old_computed = deepcopy(instance.computed)
        old_data = deepcopy(instance.data)
        with ForceChanger(instance):
            instance.uri = "/uri/1/"
        assert instance.computed["uri"] == "/uri/1/"
        old_computed["uri"] = "/uri/1/"
        old_computed["uri_hash"] = json_encode(instance.uri_hash)
        assert instance.computed == old_computed

        instance.data = {"temp": 1}
        assert instance.data["temp"] == 1
        old_data["temp"] = 1
        assert instance.data == old_data

        instance.data = {"temp": 0}
        assert instance.data["temp"] == 0
        old_data["temp"] = 0
        assert instance.data == old_data

        instance.monitors.append(0)
        assert instance.data["monitors"] == [0]
        old_data["monitors"] = [0]
        assert instance.data == old_data
Ejemplo n.º 6
0
    def test_model_setter(self):
        uri = "/uri/patent/usa/granted/1/"
        instance = Dummy()

        with ForceChanger(instance):
            instance.uri = uri
        self.assertDictContainsSubset({"uri": uri}, instance.computed)
        instance.temp = 1
        self.assertDictContainsSubset({"temp": 1}, instance.data)

        with self.assertRaises(AttributeError):
            del instance.temp
        with self.assertRaises(TypeError):
            del instance["temp"]

        instance.patent.update({"raw": "ttt", "plain": "ttt"})
        self.assertDictContainsSubset(
            {"patent": {
                "raw": "ttt",
                "plain": "ttt",
                "html": None
            }}, instance.data)
        instance.patent.update({"raw": "u", "html": "u"})
        self.assertDictContainsSubset(
            {"patent": {
                "raw": "u",
                "plain": "ttt",
                "html": "u"
            }}, instance.data)

        instance.data["monitors"] = []
        instance.monitors.append(1)
        self.assertDictContainsSubset({"monitors": [1]}, instance.data)
        instance.monitors.append(2)
        self.assertDictContainsSubset({"monitors": [1, 2]}, instance.data)
Ejemplo n.º 7
0
    def test_status(self):
        with Transaction() as tran:
            instance = Dummy(raw={"patent": "aaa"})
            assert instance.status is Status.NEW
            with ForceChanger(instance):
                instance.uri = "/uri/patent/usa/granted/1/"
            assert instance.uri == "/uri/patent/usa/granted/1/"
            assert instance.status is Status.NEW
            instance.save()
        with tran:
            assert instance.status is Status.NORMAL
            assert instance.uri == "/uri/patent/usa/granted/1/"

            assert instance.status is Status.NORMAL
            assert instance.uri == "/uri/patent/usa/granted/1/"
            with ForceChanger(instance):
                instance.uri = "/uri/patent/usa/granted/2/"
            assert instance.uri == "/uri/patent/usa/granted/2/"
            assert instance.status is Status.DIRTY
            instance.save()
            assert instance.status is Status.DIRTY
            assert instance.uri == "/uri/patent/usa/granted/2/"
            instance.delete()
        with tran:
            assert instance.status is Status.DELETED
            with self.assertRaises(AssertionError):
                with ForceChanger(instance):
                    instance.uri = "/uri/patent/usa/granted/3/"
            assert instance.uri == "/uri/patent/usa/granted/2/"
Ejemplo n.º 8
0
 def test_id(self):
     instance = Dummy()
     self.assertIsNotNone(instance.id)
     old_id = instance.id
     instance.save()
     self.assertIsNotNone(instance.id)
     new_id = instance.id
     self.assertEqual(old_id, new_id)
Ejemplo n.º 9
0
 def test_id2(self):
     instance = Dummy()
     assert instance.id is not None
     old_id = instance.id
     instance.save()
     assert instance.id is not None
     new_id = instance.id
     assert old_id == new_id
Ejemplo n.º 10
0
 def test_create_db(self):
     with Transaction():
         self.assertEqual(Model.objects.count(), 0)
         instance = Dummy()
         self.assertEqual(Model.objects.count(), 0)
         instance.save()
         self.assertEqual(Model.objects.count(), 1)
         assert instance.version == 1
     self.assertEqual(Model.objects.count(), 1)
     assert instance.version == 1
Ejemplo n.º 11
0
 def test_delete_with_new_status(self):
     with Transaction() as tran:
         instance = Dummy()
         assert instance.status == Status.NEW
         instance.delete()
         assert instance.status == Status.DELETED
         assert instance.id is not None
     with tran:
         instance._destroy()
         assert instance.id is None
Ejemplo n.º 12
0
 def test_set_computed_uri_hash(self):
     instance = Dummy()
     with self.assertRaises(AssertionError):
         instance.computed_uri_hash = "/uri/0/"
     assert instance.computed_uri_hash is None
     with ForceChanger(instance):
         instance.uri = "/uri/1/"
     with self.assertRaises(AssertionError):
         instance.computed_uri_hash = None
     assert instance.computed_uri_hash is not None
Ejemplo n.º 13
0
    def test_no_transaction(self):
        with Transaction():
            Dummy().save()
            Dummy().save()
            Dummy().save()

        assert Model.objects.count() == 3
        assert len(Model.objects.filter(type__in=Dummy.types).all()) == 3
        assert not Model.objects.filter(type__in=Dummy.types).query.order_by
        assert Model.objects.order_by("-id").query.order_by
        assert Model.objects.filter(type__in=Dummy.types).order_by("-id").query.order_by
        instance1 = Model.objects.filter(type__in=Dummy.types).order_by("-id").first()
        assert instance1
        instance2 = Model.objects.get(id=instance1.id)
        assert instance1 == instance2
        assert id(instance1) != instance2

        with self.assertRaises(AssertionError):
            with ForceChanger(instance2):
                instance2.uri = "/uri/2/"
Ejemplo n.º 14
0
 def test_get_delete(self):
     assert Dummy.objects.count() == 0
     instance = Dummy()
     instance.data["d"] = {"a": 12}
     id = instance.id
     instance.save()
     assert Dummy.objects.count() == 1
     instance.delete()
     assert Dummy.objects.count() == 0
     assert Dummy.objects.get_deleted(id=id)
     assert Dummy.objects.get(id=id).status == Status.DELETED
Ejemplo n.º 15
0
    def test_model_getter(self):
        instance = Dummy()

        uri = "/uri/patent/usa/granted/1/"
        with ForceChanger(instance):
            instance.uri = uri
        assert instance.uri == uri

        instance.d = {"a": 1, "b": 2, "c": None}
        assert instance.d == {"a": 1, "b": 2, "c": None}
        assert instance.d["a"] == 1
Ejemplo n.º 16
0
 def test_create_db2(self):
     with Transaction():
         assert Model.objects.count() == 0
         instance = Dummy()
         assert Model.objects.count() == 0
         assert instance.version == 0
         instance.save()
         assert Model.objects.count() == 1
         assert instance.version == 1
     assert Model.objects.count() == 1
     assert instance.version == 1
Ejemplo n.º 17
0
 def test(self):
     with Transaction():
         instance = Dummy()
         del instance.data["temp"]
         instance.save()
         assert "temp" not in instance.data
     assert Dummy.objects.filter(temp=None).count() == 0
     with Transaction():
         instance = Dummy.objects.order_by("-id").first()
         assert "temp" in instance.data
         instance.save()
     assert Dummy.objects.filter(temp=None).count() == 1
Ejemplo n.º 18
0
 def test(self):
     instance = Dummy()
     instance.d = {"a": 1, "b": 2, "c": now()}
     actor = Actor()
     with ForceChanger(instance):
         assert instance.data["owner"] is None
         instance.owner = actor
         assert instance.data["owner"] == actor.id
         instance.owner = None
         assert instance.data["owner"] is None
         instance.owner = actor
         assert instance.data["owner"] == actor.id
Ejemplo n.º 19
0
 def test_create_db3(self):
     with Transaction():
         assert Model.objects.count() == 0
         instance = Dummy()
         assert Model.objects.count() == 0
         instance.save()
         assert Model.objects.count() == 1
         assert instance.version == 1
         with ForceChanger(instance):
             instance.uri = "/uri/2/"
         instance.save()
         assert instance.version == 1
     assert instance.version == 2
Ejemplo n.º 20
0
 def test_manager_get(self):
     with Transaction() as tran:
         self.assertEqual(Model.objects.count(), 0)
         instance = Dummy()
         self.assertEqual(Model.objects.count(), 0)
         instance.save()
     with tran:
         self.assertEqual(Model.objects.count(), 1)
         instance2 = Model.objects.get(id=instance.id)
         assert id(instance) == id(instance2)
         TransactionManager._clear()
         instance3 = Model.objects.get(id=instance.id)
         assert instance == instance3
         assert id(instance) != id(instance3)
Ejemplo n.º 21
0
    def test(self):
        with Transaction():
            Dummy.objects.create()
            Dummy.objects.create()
        assert Dummy.objects.count() == 2

        with ReadonlyTransaction():
            instance1 = Dummy.objects.first("-id")
            instance2 = Dummy.objects.first("-id")
            assert id(instance1) == id(instance2)
        assert Dummy.objects.count() == 2

        with ReadonlyTransaction():
            dummy = Dummy()
            assert dummy.status == Status.NO_SYNC
            dummy.temp = 1
            assert dummy.status == Status.NO_SYNC
        assert dummy.status == Status.NO_SYNC
        assert Dummy.objects.count() == 2

        with ReadonlyTransaction():
            dummy = Dummy()
            with self.assertRaises(AssertionError):
                dummy.save()
Ejemplo n.º 22
0
    def test(self):
        with Transaction():
            instance = Dummy()
            # 테스트를 위한 어거지 코드이므로 일반적인 상황에서는 절대 이렇게 하면 안됨
            del instance.data["temp"]
            del instance.computed["monitors_count"]
            instance.save()
            assert "temp" not in instance.data
            assert "monitors_count" not in instance.computed
        assert "temp" not in instance.data
        assert "monitors_count" not in instance.computed

        instance = Dummy.objects.get(id=instance.id)
        assert instance.data["temp"] is None
        assert instance.computed["monitors_count"] == 0
Ejemplo n.º 23
0
 def test_computed_framework(self):
     instance = Dummy()
     with self.assertRaises(AssertionError):
         instance.computed = {}
     assert instance.computed_uri_hash is None
     with ForceChanger(instance):
         instance.uri = "/uri/1/"
     uri_hash_1 = instance.computed_uri_hash
     assert uri_hash_1
     assert instance.computed["uri_hash"] == str(uri_hash_1)
     with ForceChanger(instance):
         instance.uri = "/uri/2/"
     uri_hash_2 = instance.computed_uri_hash
     assert uri_hash_2
     assert instance.computed["uri_hash"] == str(uri_hash_2)
     assert uri_hash_1 != uri_hash_2
Ejemplo n.º 24
0
    def test(self):
        uri = "/uri/1/"
        with Transaction():
            instance = Dummy()
            with ForceChanger(instance):
                instance.uri = uri
            assert instance.uri_hash == compute_hash_uuid(uri)
            instance.save()
            assert instance.uri_hash == compute_hash_uuid(uri)
        assert instance.uri_hash == compute_hash_uuid(uri)
        mjson1 = instance.mjson

        with ReadonlyTransaction():
            instance = Dummy.objects.get(id=instance.id)
            assert instance.mjson == mjson1
            assert instance.uri == uri
            assert instance.uri_hash == compute_hash_uuid(uri)
Ejemplo n.º 25
0
    def test_standalone(self):
        instance = Dummy()

        uri = "/uri/patent/usa/granted/1/"
        with ForceChanger(instance):
            instance.uri = uri
        assert instance.uri == uri

        instance.d = {"a": 1, "b": 2, "c": None}
        assert instance.d == {"a": 1, "b": 2, "c": None}
        assert instance.d["a"] == 1

        instance.d = {"a": 11}
        assert instance.d == {"a": 11, "b": 2, "c": None}
        instance.d = {"b": 22}
        assert instance.d == {"a": 11, "b": 22, "c": None}
        instance.d = {}
        assert instance.d == {"a": 11, "b": 22, "c": None}
Ejemplo n.º 26
0
    def test(self):
        with Transaction():
            instance = Dummy()
            instance.monitors = [1, 2]
            # 테스트를 위한 어거지 코드이므로 일반적인 상황에서는 절대 이렇게 하면 안됨
            try:
                del instance.computed["monitors_count"]
                del instance.data["temp"]
            except KeyError:
                pass
            instance.save()
            assert "temp" not in instance.data
            assert "monitors_count" not in instance.computed
        assert "temp" not in instance.data
        assert "monitors_count" not in instance.computed

        instance = Dummy.objects.get(id=instance.id)
        assert instance.temp is None
        assert instance.monitors_count == 2
Ejemplo n.º 27
0
    def test_model_setter2(self):
        instance = Dummy(raw={"patent": "aaa"})
        with ForceChanger(instance):
            instance.uri = "/uri/patent/usa/granted/1/"
        assert instance.computed["uri"] == "/uri/patent/usa/granted/1/"
        instance.save()
        assert instance.computed["uri"] == "/uri/patent/usa/granted/1/"

        assert instance.computed["uri"] == "/uri/patent/usa/granted/1/"
        with ForceChanger(instance):
            instance.uri = "/uri/patent/usa/granted/2/"
        assert instance.computed["uri"] == "/uri/patent/usa/granted/2/"
        instance.save()
        assert instance.computed["uri"] == "/uri/patent/usa/granted/2/"

        instance.delete()
        with self.assertRaises(AssertionError):
            with ForceChanger(instance):
                instance.uri = "/uri/patent/usa/granted/3/"
        assert instance.computed["uri"] == "/uri/patent/usa/granted/2/"
Ejemplo n.º 28
0
    def test_take_exclusive_lock(self):
        with Transaction():
            Dummy().save()

        with Transaction() as tran1:
            instance1 = Model.objects.filter(
                type__in=Dummy.types).order_by("-id").first()

        tran2 = Transaction()
        other_connection = ConnectionHandler()[DEFAULT_DB_ALIAS]
        assert tran1.connection != other_connection
        tran2.connection = other_connection
        with tran2:
            instance2 = Model.objects.filter(
                type__in=Dummy.types).order_by("-id").first()

        assert instance1 == instance2
        assert id(instance1) != id(instance2)

        with tran1:
            instance1.take_exclusive_lock()
Ejemplo n.º 29
0
    def test_transaction(self):
        with Transaction():
            instance = Dummy()
            instance.save()
        assert instance.last_transaction is not None
        old_last_transaction = instance.last_transaction

        with Transaction() as tran1:
            tran2 = TransactionManager.get_transaction()
            assert id(tran1) == id(tran2)
            instance1 = Model.objects.get(id=instance.id)

        # 중간에 다른 트랜잭션에서 치고 들어온 것을 simulation
        with Transaction() as tran3:
            assert tran3 != tran1
            tran4 = TransactionManager.get_transaction()
            assert id(tran3) == id(tran4)
            instance2 = Model.objects.get(id=instance1.id)
            assert instance2 == instance1
            assert id(instance1) != id(instance2)
            with ForceChanger(instance2):
                instance2.uri = "/uri/1/"
            instance2.save()
        assert instance2.last_transaction > old_last_transaction
        old_last_transaction = instance2.last_transaction

        with tran1:
            tran6 = TransactionManager.get_transaction()
            assert id(tran6) == id(tran1)
            assert instance1.computed["uri"] is None

        with Transaction():
            instance3 = Model.objects.get(id=instance1.id)
            assert instance3.computed["uri"] == "/uri/1/"
            instance3.delete()
        assert instance3.last_transaction > old_last_transaction
Ejemplo n.º 30
0
 def test(self):
     instance = Dummy()
     instance.uname = "/mnt/raw/"
     assert instance.uri == "/uri/base/dummy/mnt/raw/"